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