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