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