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