[TASK] Use "int" in @param notation
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Clipboard / Clipboard.php
1 <?php
2 namespace TYPO3\CMS\Backend\Clipboard;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Backend\Utility\IconUtility;
19 use TYPO3\CMS\Core\Resource\ResourceFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
22 use TYPO3\CMS\Core\Utility\MathUtility;
23
24 /**
25 * TYPO3 clipboard for records and files
26 *
27 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
28 */
29 class Clipboard {
30
31 /**
32 * @var int
33 */
34 public $numberTabs = 3;
35
36 /**
37 * Clipboard data kept here
38 *
39 * Keys:
40 * 'normal'
41 * 'tab_[x]' where x is >=1 and denotes the pad-number
42 * 'mode' : 'copy' means copy-mode, default = moving ('cut')
43 * 'el' : Array of elements:
44 * DB: keys = '[tablename]|[uid]' eg. 'tt_content:123'
45 * DB: values = 1 (basically insignificant)
46 * FILE: keys = '_FILE|[shortmd5 of path]' eg. '_FILE|9ebc7e5c74'
47 * FILE: values = The full filepath, eg. '/www/htdocs/typo3/32/dummy/fileadmin/sem1_3_examples/alternative_index.php'
48 * or 'C:/www/htdocs/typo3/32/dummy/fileadmin/sem1_3_examples/alternative_index.php'
49 *
50 * 'current' pointer to current tab (among the above...)
51 * '_setThumb' boolean: If set, file thumbnails are shown.
52 *
53 * The virtual tablename '_FILE' will always indicate files/folders. When checking for elements from eg. 'all tables'
54 * (by using an empty string) '_FILE' entries are excluded (so in effect only DB elements are counted)
55 *
56 * @var array
57 */
58 public $clipData = array();
59
60 /**
61 * @var int
62 */
63 public $changed = 0;
64
65 /**
66 * @var string
67 */
68 public $current = '';
69
70 /**
71 * @var string
72 */
73 public $backPath = '';
74
75 /**
76 * @var int
77 */
78 public $lockToNormal = 0;
79
80 // If set, clipboard is displaying files.
81 /**
82 * @var int
83 */
84 public $fileMode = 0;
85
86 /*****************************************
87 *
88 * Initialize
89 *
90 ****************************************/
91 /**
92 * Initialize the clipboard from the be_user session
93 *
94 * @return void
95 */
96 public function initializeClipboard() {
97 $this->backPath = $GLOBALS['BACK_PATH'];
98 // Get data
99 $clipData = $GLOBALS['BE_USER']->getModuleData('clipboard', $GLOBALS['BE_USER']->getTSConfigVal('options.saveClipboard') ? '' : 'ses');
100 // NumberTabs
101 $clNP = $GLOBALS['BE_USER']->getTSConfigVal('options.clipboardNumberPads');
102 if (MathUtility::canBeInterpretedAsInteger($clNP) && $clNP >= 0) {
103 $this->numberTabs = MathUtility::forceIntegerInRange($clNP, 0, 20);
104 }
105 // Resets/reinstates the clipboard pads
106 $this->clipData['normal'] = is_array($clipData['normal']) ? $clipData['normal'] : array();
107 for ($a = 1; $a <= $this->numberTabs; $a++) {
108 $this->clipData['tab_' . $a] = is_array($clipData['tab_' . $a]) ? $clipData['tab_' . $a] : array();
109 }
110 // Setting the current pad pointer ($this->current) and _setThumb (which determines whether or not do show file thumbnails)
111 $this->clipData['current'] = ($this->current = isset($this->clipData[$clipData['current']]) ? $clipData['current'] : 'normal');
112 $this->clipData['_setThumb'] = $clipData['_setThumb'];
113 }
114
115 /**
116 * Call this method after initialization if you want to lock the clipboard to operate on the normal pad only.
117 * Trying to switch pad through ->setCmd will not work.
118 * This is used by the clickmenu since it only allows operation on single elements at a time (that is the "normal" pad)
119 *
120 * @return void
121 */
122 public function lockToNormal() {
123 $this->lockToNormal = 1;
124 $this->current = 'normal';
125 }
126
127 /**
128 * The array $cmd may hold various keys which notes some action to take.
129 * Normally perform only one action at a time.
130 * In scripts like db_list.php / filelist/mod1/index.php the GET-var CB is used to control the clipboard.
131 *
132 * Selecting / Deselecting elements
133 * Array $cmd['el'] has keys = element-ident, value = element value (see description of clipData array in header)
134 * Selecting elements for 'copy' should be done by simultaneously setting setCopyMode.
135 *
136 * @param array $cmd Array of actions, see function description
137 * @return void
138 */
139 public function setCmd($cmd) {
140 if (is_array($cmd['el'])) {
141 foreach ($cmd['el'] as $k => $v) {
142 if ($this->current == 'normal') {
143 unset($this->clipData['normal']);
144 }
145 if ($v) {
146 $this->clipData[$this->current]['el'][$k] = $v;
147 } else {
148 $this->removeElement($k);
149 }
150 $this->changed = 1;
151 }
152 }
153 // Change clipboard pad (if not locked to normal)
154 if ($cmd['setP']) {
155 $this->setCurrentPad($cmd['setP']);
156 }
157 // Remove element (value = item ident: DB; '[tablename]|[uid]' FILE: '_FILE|[shortmd5 hash of path]'
158 if ($cmd['remove']) {
159 $this->removeElement($cmd['remove']);
160 $this->changed = 1;
161 }
162 // Remove all on current pad (value = pad-ident)
163 if ($cmd['removeAll']) {
164 $this->clipData[$cmd['removeAll']] = array();
165 $this->changed = 1;
166 }
167 // Set copy mode of the tab
168 if (isset($cmd['setCopyMode'])) {
169 $this->clipData[$this->current]['mode'] = $this->isElements() ? ($cmd['setCopyMode'] ? 'copy' : '') : '';
170 $this->changed = 1;
171 }
172 // Toggle thumbnail display for files on/off
173 if (isset($cmd['setThumb'])) {
174 $this->clipData['_setThumb'] = $cmd['setThumb'];
175 $this->changed = 1;
176 }
177 }
178
179 /**
180 * Setting the current pad on clipboard
181 *
182 * @param string $padIdent Key in the array $this->clipData
183 * @return void
184 */
185 public function setCurrentPad($padIdent) {
186 // Change clipboard pad (if not locked to normal)
187 if (!$this->lockToNormal && $this->current != $padIdent) {
188 if (isset($this->clipData[$padIdent])) {
189 $this->clipData['current'] = ($this->current = $padIdent);
190 }
191 if ($this->current != 'normal' || !$this->isElements()) {
192 $this->clipData[$this->current]['mode'] = '';
193 }
194 // Setting mode to default (move) if no items on it or if not 'normal'
195 $this->changed = 1;
196 }
197 }
198
199 /**
200 * Call this after initialization and setCmd in order to save the clipboard to the user session.
201 * The function will check if the internal flag ->changed has been set and if so, save the clipboard. Else not.
202 *
203 * @return void
204 */
205 public function endClipboard() {
206 if ($this->changed) {
207 $this->saveClipboard();
208 }
209 $this->changed = 0;
210 }
211
212 /**
213 * Cleans up an incoming element array $CBarr (Array selecting/deselecting elements)
214 *
215 * @param array $CBarr Element array from outside ("key" => "selected/deselected")
216 * @param string $table The 'table which is allowed'. Must be set.
217 * @param bool $removeDeselected Can be set in order to remove entries which are marked for deselection.
218 * @return array Processed input $CBarr
219 */
220 public function cleanUpCBC($CBarr, $table, $removeDeselected = 0) {
221 if (is_array($CBarr)) {
222 foreach ($CBarr as $k => $v) {
223 $p = explode('|', $k);
224 if ((string) $p[0] != (string) $table || $removeDeselected && !$v) {
225 unset($CBarr[$k]);
226 }
227 }
228 }
229 return $CBarr;
230 }
231
232 /*****************************************
233 *
234 * Clipboard HTML renderings
235 *
236 ****************************************/
237 /**
238 * Prints the clipboard
239 *
240 * @return string HTML output
241 */
242 public function printClipboard() {
243 $out = array();
244 $elCount = count($this->elFromTable($this->fileMode ? '_FILE' : ''));
245 // Upper header
246 $out[] = '
247 <tr class="t3-row-header">
248 <td colspan="3">' . BackendUtility::wrapInHelp('xMOD_csh_corebe', 'list_clipboard', $this->clLabel('clipboard', 'buttons')) . '</td>
249 </tr>';
250 // Button/menu header:
251 $thumb_url = GeneralUtility::linkThisScript(array('CB' => array('setThumb' => $this->clipData['_setThumb'] ? 0 : 1)));
252 $rmall_url = GeneralUtility::linkThisScript(array('CB' => array('removeAll' => $this->current)));
253 // Copymode Selector menu
254 $copymode_url = GeneralUtility::linkThisScript();
255 $moveLabel = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_misc.xlf:moveElements'));
256 $copyLabel = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_misc.xlf:copyElements'));
257 $opt = array();
258 $opt[] = '<option style="padding-left: 20px; background-image: url(\'' . IconUtility::skinImg($this->backPath, 'gfx/clip_cut.gif', '', 1) . '\'); background-repeat: no-repeat;" value="" ' . ($this->currentMode() == 'copy' ? '' : 'selected="selected"') . '>' . $moveLabel . '</option>';
259 $opt[] = '<option style="padding-left: 20px; background-image: url(\'' . IconUtility::skinImg($this->backPath, 'gfx/clip_copy.gif', '', 1) . '\'); background-repeat: no-repeat;" value="1" ' . ($this->currentMode() == 'copy' ? 'selected="selected"' : '') . '>' . $copyLabel . '</option>';
260 $copymode_selector = ' <select name="CB[setCopyMode]" onchange="this.form.method=\'POST\'; this.form.action=\'' . htmlspecialchars(($copymode_url . '&CB[setCopyMode]=')) . '\'+(this.options[this.selectedIndex].value); this.form.submit(); return true;" >' . implode('', $opt) . '</select>';
261 // Selector menu + clear button
262 $opt = array();
263 $opt[] = '<option value="" selected="selected">' . $this->clLabel('menu', 'rm') . '</option>';
264 // Import / Export link:
265 if ($elCount && ExtensionManagementUtility::isLoaded('impexp')) {
266 $url = BackendUtility::getModuleUrl('xMOD_tximpexp', $this->exportClipElementParameters());
267 $opt[] = '<option value="' . htmlspecialchars(('window.location.href=' . GeneralUtility::quoteJSvalue($url) . ';')) . '">' . $this->clLabel('export', 'rm') . '</option>';
268 }
269 // Edit:
270 if (!$this->fileMode && $elCount) {
271 $opt[] = '<option value="' . htmlspecialchars(('window.location.href=\'' . $this->editUrl() . '&returnUrl=\'+top.rawurlencode(window.location.href);')) . '">' . $this->clLabel('edit', 'rm') . '</option>';
272 }
273 // Delete:
274 if ($elCount) {
275 if ($GLOBALS['BE_USER']->jsConfirmation(4)) {
276 $js = '
277 if (confirm(' . GeneralUtility::quoteJSvalue(sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:mess.deleteClip'), $elCount)) . ')){
278 window.location.href=\'' . $this->deleteUrl(0, ($this->fileMode ? 1 : 0)) . '&redirect=\'+top.rawurlencode(window.location.href);
279 }
280 ';
281 } else {
282 $js = ' window.location.href=\'' . $this->deleteUrl(0, ($this->fileMode ? 1 : 0)) . '&redirect=\'+top.rawurlencode(window.location.href); ';
283 }
284 $opt[] = '<option value="' . htmlspecialchars($js) . '">' . $this->clLabel('delete', 'rm') . '</option>';
285 }
286 $selector_menu = '<select name="_clipMenu" onchange="eval(this.options[this.selectedIndex].value);this.selectedIndex=0;">' . implode('', $opt) . '</select>';
287 $out[] = '
288 <tr class="typo3-clipboard-head">
289 <td nowrap="nowrap">' . '<a href="' . htmlspecialchars($thumb_url) . '#clip_head">' . '<img' . IconUtility::skinImg($this->backPath, ('gfx/thumb_' . ($this->clipData['_setThumb'] ? 's' : 'n') . '.gif'), 'width="21" height="16"') . ' vspace="2" border="0" title="' . $this->clLabel('thumbmode_clip') . '" alt="" />' . '</a>' . '</td>
290 <td width="95%" nowrap="nowrap">' . $copymode_selector . ' ' . $selector_menu . '</td>
291 <td>' . '<a href="' . htmlspecialchars($rmall_url) . '#clip_head">' . IconUtility::getSpriteIcon('actions-document-close', array('title' => $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:buttons.clear', TRUE))) . '</a></td>
292 </tr>';
293 // Print header and content for the NORMAL tab:
294 $out[] = '
295 <tr class="bgColor5">
296 <td colspan="3"><a href="' . htmlspecialchars(GeneralUtility::linkThisScript(array('CB' => array('setP' => 'normal')))) . '#clip_head" title="' . $this->clLabel('normal-description') . '">'
297 . IconUtility::getSpriteIcon(('actions-view-table-' . ($this->current == 'normal' ? 'collapse' : 'expand')))
298 . $this->padTitleWrap($this->clLabel('normal'), 'normal')
299 . '</a></td>
300 </tr>';
301 if ($this->current == 'normal') {
302 $out = array_merge($out, $this->printContentFromTab('normal'));
303 }
304 // Print header and content for the NUMERIC tabs:
305 for ($a = 1; $a <= $this->numberTabs; $a++) {
306 $out[] = '
307 <tr class="bgColor5">
308 <td colspan="3"><a href="' . htmlspecialchars(GeneralUtility::linkThisScript(array('CB' => array('setP' => ('tab_' . $a))))) . '#clip_head" title="' . $this->clLabel('cliptabs-description') . '">'
309 . IconUtility::getSpriteIcon(('actions-view-table-' . ($this->current == 'tab_' . $a ? 'collapse' : 'expand')))
310 . $this->padTitleWrap(sprintf($this->clLabel('cliptabs-name'), $a), ('tab_' . $a))
311 . '</a></td>
312 </tr>';
313 if ($this->current == 'tab_' . $a) {
314 $out = array_merge($out, $this->printContentFromTab('tab_' . $a));
315 }
316 }
317 // Wrap accumulated rows in a table:
318 $output = '<a name="clip_head"></a>
319
320 <!--
321 TYPO3 Clipboard:
322 -->
323 <table cellpadding="0" cellspacing="1" border="0" width="290" id="typo3-clipboard">
324 ' . implode('', $out) . '
325 </table>';
326 // Wrap in form tag:
327 $output = '<form action="">' . $output . '</form>';
328 // Return the accumulated content:
329 return $output;
330 }
331
332 /**
333 * Print the content on a pad. Called from ->printClipboard()
334 *
335 * @access private
336 * @param string $pad Pad reference
337 * @return array Array with table rows for the clipboard.
338 */
339 public function printContentFromTab($pad) {
340 $lines = array();
341 if (is_array($this->clipData[$pad]['el'])) {
342 foreach ($this->clipData[$pad]['el'] as $k => $v) {
343 if ($v) {
344 list($table, $uid) = explode('|', $k);
345 $bgColClass = $table == '_FILE' && $this->fileMode || $table != '_FILE' && !$this->fileMode ? 'bgColor4-20' : 'bgColor4';
346 // Rendering files/directories on the clipboard
347 if ($table == '_FILE') {
348 $fileObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($v);
349 if ($fileObject) {
350 $thumb = '';
351 $folder = $fileObject instanceof \TYPO3\CMS\Core\Resource\Folder;
352 $size = $folder ? '' : '(' . GeneralUtility::formatSize($fileObject->getSize()) . 'bytes)';
353 $icon = IconUtility::getSpriteIconForResource($fileObject, array('style' => 'margin: 0 20px;', 'title' => $fileObject->getName() . ' ' . $size));
354 if (!$folder && $this->clipData['_setThumb'] && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileObject->getExtension())) {
355 $processedFile = $fileObject->process(\TYPO3\CMS\Core\Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW, array());
356 if ($processedFile) {
357 $thumbUrl = $processedFile->getPublicUrl(TRUE);
358 $thumb .= '<br /><img src="' . htmlspecialchars($thumbUrl) . '" title="' . htmlspecialchars($fileObject->getName()) . '" alt="" />';
359 }
360 }
361 $lines[] = '
362 <tr>
363 <td class="' . $bgColClass . '">' . $icon . '</td>
364 <td class="' . $bgColClass . '" nowrap="nowrap" width="95%">&nbsp;' . $this->linkItemText(htmlspecialchars(GeneralUtility::fixed_lgd_cs($fileObject->getName(), $GLOBALS['BE_USER']->uc['titleLen'])), $fileObject->getName()) . ($pad == 'normal' ? ' <strong>(' . ($this->clipData['normal']['mode'] == 'copy' ? $this->clLabel('copy', 'cm') : $this->clLabel('cut', 'cm')) . ')</strong>' : '') . '&nbsp;' . $thumb . '</td>
365 <td class="' . $bgColClass . '" align="center" nowrap="nowrap">' . '<a href="#" onclick="' . htmlspecialchars(('top.launchView(\'' . $table . '\', \'' . $v . '\'); return false;')) . '">' . IconUtility::getSpriteIcon('actions-document-info', array('title' => $this->clLabel('info', 'cm'))) . '</a>' . '<a href="' . htmlspecialchars($this->removeUrl('_FILE', GeneralUtility::shortmd5($v))) . '#clip_head">' . IconUtility::getSpriteIcon('actions-selection-delete', array('title' => $this->clLabel('removeItem'))) . '</a>' . '</td>
366 </tr>';
367 } else {
368 // If the file did not exist (or is illegal) then it is removed from the clipboard immediately:
369 unset($this->clipData[$pad]['el'][$k]);
370 $this->changed = 1;
371 }
372 } else {
373 // Rendering records:
374 $rec = BackendUtility::getRecordWSOL($table, $uid);
375 if (is_array($rec)) {
376 $lines[] = '
377 <tr>
378 <td class="' . $bgColClass . '">' . $this->linkItemText(IconUtility::getSpriteIconForRecord($table, $rec, array('style' => 'margin: 0 20px;', 'title' => htmlspecialchars(BackendUtility::getRecordIconAltText($rec, $table)))), $rec, $table) . '</td>
379 <td class="' . $bgColClass . '" nowrap="nowrap" width="95%">&nbsp;' . $this->linkItemText(htmlspecialchars(GeneralUtility::fixed_lgd_cs(BackendUtility::getRecordTitle($table, $rec), $GLOBALS['BE_USER']->uc['titleLen'])), $rec, $table) . ($pad == 'normal' ? ' <strong>(' . ($this->clipData['normal']['mode'] == 'copy' ? $this->clLabel('copy', 'cm') : $this->clLabel('cut', 'cm')) . ')</strong>' : '') . '&nbsp;</td>
380 <td class="' . $bgColClass . '" align="center" nowrap="nowrap">' . '<a href="#" onclick="' . htmlspecialchars(('top.launchView(\'' . $table . '\', \'' . (int)$uid . '\'); return false;')) . '">' . IconUtility::getSpriteIcon('actions-document-info', array('title' => $this->clLabel('info', 'cm'))) . '</a>' . '<a href="' . htmlspecialchars($this->removeUrl($table, $uid)) . '#clip_head">' . IconUtility::getSpriteIcon('actions-selection-delete', array('title' => $this->clLabel('removeItem'))) . '</a>' . '</td>
381 </tr>';
382 $localizationData = $this->getLocalizations($table, $rec, $bgColClass, $pad);
383 if ($localizationData) {
384 $lines[] = $localizationData;
385 }
386 } else {
387 unset($this->clipData[$pad]['el'][$k]);
388 $this->changed = 1;
389 }
390 }
391 }
392 }
393 }
394 if (!count($lines)) {
395 $lines[] = '
396 <tr>
397 <td class="bgColor4"><img src="clear.gif" width="56" height="1" alt="" /></td>
398 <td colspan="2" class="bgColor4" nowrap="nowrap" width="95%">&nbsp;<em>(' . $this->clLabel('clipNoEl') . ')</em>&nbsp;</td>
399 </tr>';
400 }
401 $this->endClipboard();
402 return $lines;
403 }
404
405 /**
406 * Returns true if the clipboard contains elements
407 *
408 * @return boolean
409 */
410 public function hasElements() {
411 foreach ($this->clipData as $data) {
412 if (isset($data['el']) && is_array($data['el']) && !empty($data['el'])) {
413 return TRUE;
414 }
415 }
416
417 return FALSE;
418 }
419
420 /**
421 * Gets all localizations of the current record.
422 *
423 * @param string $table The table
424 * @param array $parentRec The current record
425 * @param string $bgColClass Class for the background color of a column
426 * @param string $pad Pad reference
427 * @return string HTML table rows
428 */
429 public function getLocalizations($table, $parentRec, $bgColClass, $pad) {
430 $lines = array();
431 $tcaCtrl = $GLOBALS['TCA'][$table]['ctrl'];
432 if ($table != 'pages' && BackendUtility::isTableLocalizable($table) && !$tcaCtrl['transOrigPointerTable']) {
433 $where = array();
434 $where[] = $tcaCtrl['transOrigPointerField'] . '=' . (int)$parentRec['uid'];
435 $where[] = $tcaCtrl['languageField'] . '<>0';
436 if (isset($tcaCtrl['delete']) && $tcaCtrl['delete']) {
437 $where[] = $tcaCtrl['delete'] . '=0';
438 }
439 if (isset($tcaCtrl['versioningWS']) && $tcaCtrl['versioningWS']) {
440 $where[] = 't3ver_wsid=' . $parentRec['t3ver_wsid'];
441 }
442 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', $table, implode(' AND ', $where));
443 if (is_array($rows)) {
444 $modeData = '';
445 if ($pad == 'normal') {
446 $mode = $this->clipData['normal']['mode'] == 'copy' ? 'copy' : 'cut';
447 $modeData = ' <strong>(' . $this->clLabel($mode, 'cm') . ')</strong>';
448 }
449 foreach ($rows as $rec) {
450 $lines[] = '
451 <tr>
452 <td class="' . $bgColClass . '">' . IconUtility::getSpriteIconForRecord($table, $rec, array('style' => 'margin-left: 38px;')) . '</td>
453 <td class="' . $bgColClass . '" nowrap="nowrap" width="95%">&nbsp;' . htmlspecialchars(GeneralUtility::fixed_lgd_cs(BackendUtility::getRecordTitle($table, $rec), $GLOBALS['BE_USER']->uc['titleLen'])) . $modeData . '&nbsp;</td>
454 <td class="' . $bgColClass . '" align="center" nowrap="nowrap">&nbsp;</td>
455 </tr>';
456 }
457 }
458 }
459 return implode('', $lines);
460 }
461
462 /**
463 * Wraps title of pad in bold-tags and maybe the number of elements if any.
464 *
465 * @param string $str String (already htmlspecialchars()'ed)
466 * @param string $pad Pad reference
467 * @return string HTML output (htmlspecialchar'ed content inside of tags.)
468 */
469 public function padTitleWrap($str, $pad) {
470 $el = count($this->elFromTable($this->fileMode ? '_FILE' : '', $pad));
471 if ($el) {
472 return '<strong>' . $str . '</strong> (' . ($pad == 'normal' ? ($this->clipData['normal']['mode'] == 'copy' ? $this->clLabel('copy', 'cm') : $this->clLabel('cut', 'cm')) : htmlspecialchars($el)) . ')';
473 } else {
474 return $GLOBALS['TBE_TEMPLATE']->dfw($str);
475 }
476 }
477
478 /**
479 * Wraps the title of the items listed in link-tags. The items will link to the page/folder where they originate from
480 *
481 * @param string $str Title of element - must be htmlspecialchar'ed on beforehand.
482 * @param mixed $rec If array, a record is expected. If string, its a path
483 * @param string $table Table name
484 * @return string
485 */
486 public function linkItemText($str, $rec, $table = '') {
487 if (is_array($rec) && $table) {
488 if ($this->fileMode) {
489 $str = $GLOBALS['TBE_TEMPLATE']->dfw($str);
490 } else {
491 $str = '<a href="' . htmlspecialchars(BackendUtility::getModuleUrl('web_list', array('id' => $rec['pid']), $this->backPath)) . '">' . $str . '</a>';
492 }
493 } elseif (file_exists($rec)) {
494 if (!$this->fileMode) {
495 $str = $GLOBALS['TBE_TEMPLATE']->dfw($str);
496 } else {
497 if (ExtensionManagementUtility::isLoaded('filelist')) {
498 $str = '<a href="' . htmlspecialchars(($this->backPath . BackendUtility::getModuleUrl('file_list') . '&id=' . dirname($rec))) . '">' . $str . '</a>';
499 }
500 }
501 }
502 return $str;
503 }
504
505 /**
506 * Returns the select-url for database elements
507 *
508 * @param string $table Table name
509 * @param int $uid Uid of record
510 * @param bool $copy If set, copymode will be enabled
511 * @param bool $deselect If set, the link will deselect, otherwise select.
512 * @param array $baseArray The base array of GET vars to be sent in addition. Notice that current GET vars WILL automatically be included.
513 * @return string URL linking to the current script but with the CB array set to select the element with table/uid
514 */
515 public function selUrlDB($table, $uid, $copy = 0, $deselect = 0, $baseArray = array()) {
516 $CB = array('el' => array(rawurlencode($table . '|' . $uid) => $deselect ? 0 : 1));
517 if ($copy) {
518 $CB['setCopyMode'] = 1;
519 }
520 $baseArray['CB'] = $CB;
521 return GeneralUtility::linkThisScript($baseArray);
522 }
523
524 /**
525 * Returns the select-url for files
526 *
527 * @param string $path Filepath
528 * @param bool $copy If set, copymode will be enabled
529 * @param bool $deselect If set, the link will deselect, otherwise select.
530 * @param array $baseArray The base array of GET vars to be sent in addition. Notice that current GET vars WILL automatically be included.
531 * @return string URL linking to the current script but with the CB array set to select the path
532 */
533 public function selUrlFile($path, $copy = 0, $deselect = 0, $baseArray = array()) {
534 $CB = array('el' => array(rawurlencode('_FILE|' . GeneralUtility::shortmd5($path)) => $deselect ? '' : $path));
535 if ($copy) {
536 $CB['setCopyMode'] = 1;
537 }
538 $baseArray['CB'] = $CB;
539 return GeneralUtility::linkThisScript($baseArray);
540 }
541
542 /**
543 * pasteUrl of the element (database and file)
544 * For the meaning of $table and $uid, please read from ->makePasteCmdArray!!!
545 * The URL will point to tce_file or tce_db depending in $table
546 *
547 * @param string $table Tablename (_FILE for files)
548 * @param mixed $uid "destination": can be positive or negative indicating how the paste is done (paste into / paste after)
549 * @param bool $setRedirect If set, then the redirect URL will point back to the current script, but with CB reset.
550 * @param array|NULL $update Additional key/value pairs which should get set in the moved/copied record (via DataHandler)
551 * @return string
552 */
553 public function pasteUrl($table, $uid, $setRedirect = TRUE, array $update = NULL) {
554 $rU = $this->backPath . ($table == '_FILE' ? 'tce_file.php' : 'tce_db.php') . '?' .
555 ($setRedirect ? 'redirect=' . rawurlencode(GeneralUtility::linkThisScript(array('CB' => ''))) : '') .
556 '&vC=' . $GLOBALS['BE_USER']->veriCode() .
557 '&prErr=1&uPT=1' .
558 '&CB[paste]=' . rawurlencode($table . '|' . $uid) .
559 '&CB[pad]=' . $this->current .
560 (is_array($update) ? GeneralUtility::implodeArrayForUrl('CB[update]', $update) : '') .
561 BackendUtility::getUrlToken('tceAction');
562 return $rU;
563 }
564
565 /**
566 * deleteUrl for current pad
567 *
568 * @param bool $setRedirect If set, then the redirect URL will point back to the current script, but with CB reset.
569 * @param bool $file If set, then the URL will link to the tce_file.php script in the typo3/ dir.
570 * @return string
571 */
572 public function deleteUrl($setRedirect = 1, $file = 0) {
573 $rU = $this->backPath . ($file ? 'tce_file.php' : 'tce_db.php') . '?' . ($setRedirect ? 'redirect=' . rawurlencode(GeneralUtility::linkThisScript(array('CB' => ''))) : '') . '&vC=' . $GLOBALS['BE_USER']->veriCode() . '&prErr=1&uPT=1' . '&CB[delete]=1' . '&CB[pad]=' . $this->current . BackendUtility::getUrlToken('tceAction');
574 return $rU;
575 }
576
577 /**
578 * editUrl of all current elements
579 * ONLY database
580 * Links to alt_doc.php
581 *
582 * @return string The URL to alt_doc.php with parameters.
583 */
584 public function editUrl() {
585 // All records
586 $elements = $this->elFromTable('');
587 $editCMDArray = array();
588 foreach ($elements as $tP => $value) {
589 list($table, $uid) = explode('|', $tP);
590 $editCMDArray[] = '&edit[' . $table . '][' . $uid . ']=edit';
591 }
592 $rU = $this->backPath . 'alt_doc.php?' . implode('', $editCMDArray);
593 return $rU;
594 }
595
596 /**
597 * Returns the remove-url (file and db)
598 * for file $table='_FILE' and $uid = shortmd5 hash of path
599 *
600 * @param string $table Tablename
601 * @param string $uid Uid integer/shortmd5 hash
602 * @return string URL
603 */
604 public function removeUrl($table, $uid) {
605 return GeneralUtility::linkThisScript(array('CB' => array('remove' => $table . '|' . $uid)));
606 }
607
608 /**
609 * Returns confirm JavaScript message
610 *
611 * @param string $table Table name
612 * @param mixed $rec For records its an array, for files its a string (path)
613 * @param string $type Type-code
614 * @param array $clElements Array of selected elements
615 * @param string $columnLabel Name of the content column
616 * @return string JavaScript "confirm" message
617 */
618 public function confirmMsg($table, $rec, $type, $clElements, $columnLabel = '') {
619 if ($GLOBALS['BE_USER']->jsConfirmation(2)) {
620 $labelKey = 'LLL:EXT:lang/locallang_core.xlf:mess.' . ($this->currentMode() == 'copy' ? 'copy' : 'move') . ($this->current == 'normal' ? '' : 'cb') . '_' . $type;
621 $msg = $GLOBALS['LANG']->sL($labelKey . ($columnLabel ? '_colPos': ''));
622 if ($table == '_FILE') {
623 $thisRecTitle = basename($rec);
624 if ($this->current == 'normal') {
625 $selItem = reset($clElements);
626 $selRecTitle = basename($selItem);
627 } else {
628 $selRecTitle = count($clElements);
629 }
630 } else {
631 $thisRecTitle = $table == 'pages' && !is_array($rec) ? $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] : BackendUtility::getRecordTitle($table, $rec);
632 if ($this->current == 'normal') {
633 $selItem = $this->getSelectedRecord();
634 $selRecTitle = $selItem['_RECORD_TITLE'];
635 } else {
636 $selRecTitle = count($clElements);
637 }
638 }
639 // @TODO
640 // This can get removed as soon as the "_colPos" label is translated
641 // into all available locallang languages.
642 if (!$msg && $columnLabel) {
643 $thisRecTitle .= ' | ' . $columnLabel;
644 $msg = $GLOBALS['LANG']->sL($labelKey);
645 }
646
647 // Message
648 $conf = 'confirm(' . GeneralUtility::quoteJSvalue(sprintf($msg, GeneralUtility::fixed_lgd_cs($selRecTitle, 30), GeneralUtility::fixed_lgd_cs($thisRecTitle, 30), GeneralUtility::fixed_lgd_cs($columnLabel, 30))) . ')';
649 } else {
650 $conf = '';
651 }
652 return $conf;
653 }
654
655 /**
656 * Clipboard label - getting from "EXT:lang/locallang_core.xlf:"
657 *
658 * @param string $key Label Key
659 * @param string $Akey Alternative key to "labels
660 * @return string
661 */
662 public function clLabel($key, $Akey = 'labels') {
663 return htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:' . $Akey . '.' . $key));
664 }
665
666 /**
667 * Creates GET parameters for linking to the export module.
668 *
669 * @return array GET parameters for current clipboard content to be exported
670 */
671 protected function exportClipElementParameters() {
672 // Init
673 $pad = $this->current;
674 $params = array();
675 $params['tx_impexp[action]'] = 'export';
676 // Traverse items:
677 if (is_array($this->clipData[$pad]['el'])) {
678 foreach ($this->clipData[$pad]['el'] as $k => $v) {
679 if ($v) {
680 list($table, $uid) = explode('|', $k);
681 // Rendering files/directories on the clipboard
682 if ($table == '_FILE') {
683 if (file_exists($v) && GeneralUtility::isAllowedAbsPath($v)) {
684 $params['tx_impexp[' . (is_dir($v) ? 'dir' : 'file') . '][]'] = $v;
685 }
686 } else {
687 // Rendering records:
688 $rec = BackendUtility::getRecord($table, $uid);
689 if (is_array($rec)) {
690 $params['tx_impexp[record][]'] = $table . ':' . $uid;
691 }
692 }
693 }
694 }
695 }
696 return $params;
697 }
698
699 /*****************************************
700 *
701 * Helper functions
702 *
703 ****************************************/
704 /**
705 * Removes element on clipboard
706 *
707 * @param string $el Key of element in ->clipData array
708 * @return void
709 */
710 public function removeElement($el) {
711 unset($this->clipData[$this->current]['el'][$el]);
712 $this->changed = 1;
713 }
714
715 /**
716 * Saves the clipboard, no questions asked.
717 * Use ->endClipboard normally (as it checks if changes has been done so saving is necessary)
718 *
719 * @access private
720 * @return void
721 */
722 public function saveClipboard() {
723 $GLOBALS['BE_USER']->pushModuleData('clipboard', $this->clipData);
724 }
725
726 /**
727 * Returns the current mode, 'copy' or 'cut'
728 *
729 * @return string "copy" or "cut
730 */
731 public function currentMode() {
732 return $this->clipData[$this->current]['mode'] == 'copy' ? 'copy' : 'cut';
733 }
734
735 /**
736 * This traverses the elements on the current clipboard pane
737 * and unsets elements which does not exist anymore or are disabled.
738 *
739 * @return void
740 */
741 public function cleanCurrent() {
742 if (is_array($this->clipData[$this->current]['el'])) {
743 foreach ($this->clipData[$this->current]['el'] as $k => $v) {
744 list($table, $uid) = explode('|', $k);
745 if ($table != '_FILE') {
746 if (!$v || !is_array(BackendUtility::getRecord($table, $uid, 'uid'))) {
747 unset($this->clipData[$this->current]['el'][$k]);
748 $this->changed = 1;
749 }
750 } else {
751 if (!$v) {
752 unset($this->clipData[$this->current]['el'][$k]);
753 $this->changed = 1;
754 } else {
755 try {
756 ResourceFactory::getInstance()->retrieveFileOrFolderObject($v);
757 } catch (\TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException $e) {
758 // The file has been deleted in the meantime, so just remove it silently
759 unset($this->clipData[$this->current]['el'][$k]);
760 }
761 }
762 }
763 }
764 }
765 }
766
767 /**
768 * Counts the number of elements from the table $matchTable. If $matchTable is blank, all tables (except '_FILE' of course) is counted.
769 *
770 * @param string $matchTable Table to match/count for.
771 * @param string $pad Can optionally be used to set another pad than the current.
772 * @return array Array with keys from the CB.
773 */
774 public function elFromTable($matchTable = '', $pad = '') {
775 $pad = $pad ? $pad : $this->current;
776 $list = array();
777 if (is_array($this->clipData[$pad]['el'])) {
778 foreach ($this->clipData[$pad]['el'] as $k => $v) {
779 if ($v) {
780 list($table, $uid) = explode('|', $k);
781 if ($table != '_FILE') {
782 if ((!$matchTable || (string) $table == (string) $matchTable) && $GLOBALS['TCA'][$table]) {
783 $list[$k] = $pad == 'normal' ? $v : $uid;
784 }
785 } else {
786 if ((string) $table == (string) $matchTable) {
787 $list[$k] = $v;
788 }
789 }
790 }
791 }
792 }
793 return $list;
794 }
795
796 /**
797 * Verifies if the item $table/$uid is on the current pad.
798 * If the pad is "normal", the mode value is returned if the element existed. Thus you'll know if the item was copy or cut moded...
799 *
800 * @param string $table Table name, (_FILE for files...)
801 * @param int $uid Element uid (path for files)
802 * @return string
803 */
804 public function isSelected($table, $uid) {
805 $k = $table . '|' . $uid;
806 return $this->clipData[$this->current]['el'][$k] ? ($this->current == 'normal' ? $this->currentMode() : 1) : '';
807 }
808
809 /**
810 * Returns item record $table,$uid if selected on current clipboard
811 * If table and uid is blank, the first element is returned.
812 * Makes sense only for DB records - not files!
813 *
814 * @param string $table Table name
815 * @param int $uid Element uid
816 * @return array Element record with extra field _RECORD_TITLE set to the title of the record
817 */
818 public function getSelectedRecord($table = '', $uid = '') {
819 if (!$table && !$uid) {
820 $elArr = $this->elFromTable('');
821 reset($elArr);
822 list($table, $uid) = explode('|', key($elArr));
823 }
824 if ($this->isSelected($table, $uid)) {
825 $selRec = BackendUtility::getRecordWSOL($table, $uid);
826 $selRec['_RECORD_TITLE'] = BackendUtility::getRecordTitle($table, $selRec);
827 return $selRec;
828 }
829 }
830
831 /**
832 * Reports if the current pad has elements (does not check file/DB type OR if file/DBrecord exists or not. Only counting array)
833 *
834 * @return boolean TRUE if elements exist.
835 */
836 public function isElements() {
837 return is_array($this->clipData[$this->current]['el']) && count($this->clipData[$this->current]['el']);
838 }
839
840 /*****************************************
841 *
842 * FOR USE IN tce_db.php:
843 *
844 ****************************************/
845 /**
846 * Applies the proper paste configuration in the $cmd array send to tce_db.php.
847 * $ref is the target, see description below.
848 * The current pad is pasted
849 *
850 * $ref: [tablename]:[paste-uid].
851 * Tablename is the name of the table from which elements *on the current clipboard* is pasted with the 'pid' paste-uid.
852 * No tablename means that all items on the clipboard (non-files) are pasted. This requires paste-uid to be positive though.
853 * so 'tt_content:-3' means 'paste tt_content elements on the clipboard to AFTER tt_content:3 record
854 * 'tt_content:30' means 'paste tt_content elements on the clipboard into page with id 30
855 * ':30' means 'paste ALL database elements on the clipboard into page with id 30
856 * ':-30' not valid.
857 *
858 * @param string $ref [tablename]:[paste-uid], see description
859 * @param array $CMD Command-array
860 * @param NULL|array If additional values should get set in the copied/moved record this will be an array containing key=>value pairs
861 * @return array Modified Command-array
862 */
863 public function makePasteCmdArray($ref, $CMD, array $update = NULL) {
864 list($pTable, $pUid) = explode('|', $ref);
865 $pUid = (int)$pUid;
866 // pUid must be set and if pTable is not set (that means paste ALL elements)
867 // the uid MUST be positive/zero (pointing to page id)
868 if ($pTable || $pUid >= 0) {
869 $elements = $this->elFromTable($pTable);
870 // So the order is preserved.
871 $elements = array_reverse($elements);
872 $mode = $this->currentMode() == 'copy' ? 'copy' : 'move';
873 // Traverse elements and make CMD array
874 foreach ($elements as $tP => $value) {
875 list($table, $uid) = explode('|', $tP);
876 if (!is_array($CMD[$table])) {
877 $CMD[$table] = array();
878 }
879 if (is_array($update)) {
880 $CMD[$table][$uid][$mode] = array(
881 'action' => 'paste',
882 'target' => $pUid,
883 'update' => $update,
884 );
885 } else {
886 $CMD[$table][$uid][$mode] = $pUid;
887 }
888 if ($mode == 'move') {
889 $this->removeElement($tP);
890 }
891 }
892 $this->endClipboard();
893 }
894 return $CMD;
895 }
896
897 /**
898 * Delete record entries in CMD array
899 *
900 * @param array $CMD Command-array
901 * @return array Modified Command-array
902 */
903 public function makeDeleteCmdArray($CMD) {
904 // all records
905 $elements = $this->elFromTable('');
906 foreach ($elements as $tP => $value) {
907 list($table, $uid) = explode('|', $tP);
908 if (!is_array($CMD[$table])) {
909 $CMD[$table] = array();
910 }
911 $CMD[$table][$uid]['delete'] = 1;
912 $this->removeElement($tP);
913 }
914 $this->endClipboard();
915 return $CMD;
916 }
917
918 /*****************************************
919 *
920 * FOR USE IN tce_file.php:
921 *
922 ****************************************/
923 /**
924 * Applies the proper paste configuration in the $file array send to tce_file.php.
925 * The current pad is pasted
926 *
927 * @param string $ref Reference to element (splitted by "|")
928 * @param array $FILE Command-array
929 * @return array Modified Command-array
930 */
931 public function makePasteCmdArray_file($ref, $FILE) {
932 list($pTable, $pUid) = explode('|', $ref);
933 $elements = $this->elFromTable('_FILE');
934 $mode = $this->currentMode() == 'copy' ? 'copy' : 'move';
935 // Traverse elements and make CMD array
936 foreach ($elements as $tP => $path) {
937 $FILE[$mode][] = array('data' => $path, 'target' => $pUid);
938 if ($mode == 'move') {
939 $this->removeElement($tP);
940 }
941 }
942 $this->endClipboard();
943 return $FILE;
944 }
945
946 /**
947 * Delete files in CMD array
948 *
949 * @param array $FILE Command-array
950 * @return array Modified Command-array
951 */
952 public function makeDeleteCmdArray_file($FILE) {
953 $elements = $this->elFromTable('_FILE');
954 // Traverse elements and make CMD array
955 foreach ($elements as $tP => $path) {
956 $FILE['delete'][] = array('data' => $path);
957 $this->removeElement($tP);
958 }
959 $this->endClipboard();
960 return $FILE;
961 }
962
963 }