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