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