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