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