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