[BUGFIX] Make icon color white for colored buttons
[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\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
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 $out = array();
253 $elementCount = count($this->elFromTable($this->fileMode ? '_FILE' : ''));
254 // Button/menu header:
255 $removeAllUrl = GeneralUtility::linkThisScript(array('CB' => array('removeAll' => $this->current)));
256 // Copymode Selector menu
257 $copymodeUrl = GeneralUtility::linkThisScript();
258 $moveLabel = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_misc.xlf:moveElements'));
259 $copyLabel = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_misc.xlf:copyElements'));
260
261 $copymodeSelector = '
262 <div class="btn-group">
263 <button class="btn btn-default dropdown-toggle" type="button" id="copymodeSelector" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
264 ' . ($this->currentMode() === 'copy' ? $copyLabel : $moveLabel) . '
265 <span class="caret"></span>
266 </button>
267 <ul class="dropdown-menu" aria-labelledby="copymodeSelector">
268 <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>
269 <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>
270 </ul>
271 </div>
272 ';
273
274 // Selector menu + clear button
275 $optionArray = array();
276 // Import / Export link:
277 if ($elementCount && ExtensionManagementUtility::isLoaded('impexp')) {
278 $url = BackendUtility::getModuleUrl('xMOD_tximpexp', $this->exportClipElementParameters());
279 $optionArray[] = '<li><a href="#" onclick="' . htmlspecialchars(('window.location.href=' . GeneralUtility::quoteJSvalue($url) . ';')) . '">' . $this->clLabel('export', 'rm') . '</a></li>';
280 }
281 // Edit:
282 if (!$this->fileMode && $elementCount) {
283 $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>';
284 }
285
286 $deleteLink = '';
287 $menuSelector = '';
288 if ($elementCount) {
289 // Delete:
290 $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(SvgIconProvider::MARKUP_IDENTIFIER_INLINE) . '</a>';
291 if ($this->getBackendUser()->jsConfirmation(JsConfirmation::DELETE)) {
292 $js = '
293 if (confirm(' . GeneralUtility::quoteJSvalue(sprintf($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:mess.deleteClip'), $elementCount)) . ')){
294 window.location.href=' . GeneralUtility::quoteJSvalue($this->deleteUrl(0, ($this->fileMode ? 1 : 0)) . '&redirect=') . '+top.rawurlencode(window.location.href);
295 }
296 ';
297 } else {
298 $js = ' window.location.href=' . GeneralUtility::quoteJSvalue($this->deleteUrl(0, ($this->fileMode ? 1 : 0)) . '&redirect=') . '+top.rawurlencode(window.location.href); ';
299 }
300 $optionArray[] = '<li><a href="#" onclick="' . htmlspecialchars($js) . '">' . $this->clLabel('delete', 'rm') . '</a></li>';
301
302 // menuSelector
303 $menuSelector = '
304 <div class="btn-group">
305 <button class="btn btn-default dropdown-toggle" type="button" id="menuSelector" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
306 ' . $this->clLabel('menu', 'rm') . '
307 <span class="caret"></span>
308 </button>
309 <ul class="dropdown-menu" aria-labelledby="menuSelector">
310 ' . implode('', $optionArray) . '
311 </ul>
312 </div>
313 ';
314 }
315
316 $out[] = '
317 <tr>
318 <td colspan="2" nowrap="nowrap" width="95%">' . $copymodeSelector . ' ' . $menuSelector . '</td>
319 <td nowrap="nowrap" class="col-control">' . $deleteLink . '</td>
320 </tr>';
321
322 // Print header and content for the NORMAL tab:
323 // check for current item so it can be wrapped in strong tag
324 $current = ($this->current == 'normal');
325 $out[] = '
326 <tr>
327 <td colspan="3"><a href="' . htmlspecialchars(GeneralUtility::linkThisScript(array('CB' => array('setP' => 'normal')))) . '#clip_head" title="' . $this->clLabel('normal-description') . '">'
328 . '<span class="t3-icon fa ' . ($current ? 'fa-check-circle' : 'fa-circle-o') . '"></span>'
329 . $this->padTitleWrap($this->clLabel('normal'), 'normal', $current)
330 . '</a></td>
331 </tr>';
332 if ($this->current == 'normal') {
333 $out = array_merge($out, $this->printContentFromTab('normal'));
334 }
335 // Print header and content for the NUMERIC tabs:
336 for ($a = 1; $a <= $this->numberTabs; $a++) {
337 // check for current item so it can be wrapped in strong tag
338 $current = ($this->current == 'tab_' . $a);
339 $out[] = '
340 <tr>
341 <td colspan="3"><a href="' . htmlspecialchars(GeneralUtility::linkThisScript(array('CB' => array('setP' => ('tab_' . $a))))) . '#clip_head" title="' . $this->clLabel('cliptabs-description') . '">'
342 . '<span class="t3-icon fa ' . ($current ? 'fa-check-circle' : 'fa-circle-o') . '"></span>'
343 . $this->padTitleWrap(sprintf($this->clLabel('cliptabs-name'), $a), ('tab_' . $a), $current)
344 . '</a></td>
345 </tr>';
346 if ($this->current == 'tab_' . $a) {
347 $out = array_merge($out, $this->printContentFromTab('tab_' . $a));
348 }
349 }
350 // Wrap accumulated rows in a table:
351 $output = '<a name="clip_head"></a>
352
353 <!--
354 TYPO3 Clipboard:
355 -->
356 <div class="row">
357 <div class="col-sm-12">
358 <div class="panel panel-default">
359 <div class="panel-heading">' . BackendUtility::wrapInHelp('xMOD_csh_corebe', 'list_clipboard', $this->clLabel('clipboard', 'buttons')) . '</div>
360 <table class="table">
361 ' . implode('', $out) . '
362 </table>
363 </div>
364 </div>
365 </div>
366 ';
367 // Wrap in form tag:
368 $output = '<form action="" id="clipboard_form">' . $output . '</form>';
369 // Return the accumulated content:
370 return $output;
371 }
372
373 /**
374 * Print the content on a pad. Called from ->printClipboard()
375 *
376 * @access private
377 * @param string $pad Pad reference
378 * @return array Array with table rows for the clipboard.
379 */
380 public function printContentFromTab($pad)
381 {
382 $lines = array();
383 if (is_array($this->clipData[$pad]['el'])) {
384 foreach ($this->clipData[$pad]['el'] as $k => $v) {
385 if ($v) {
386 list($table, $uid) = explode('|', $k);
387 $bgColClass = $table == '_FILE' && $this->fileMode || $table != '_FILE' && !$this->fileMode ? 'bgColor4-20' : 'bgColor4';
388 // Rendering files/directories on the clipboard
389 if ($table == '_FILE') {
390 $fileObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($v);
391 if ($fileObject) {
392 $thumb = '';
393 $folder = $fileObject instanceof \TYPO3\CMS\Core\Resource\Folder;
394 $size = $folder ? '' : '(' . GeneralUtility::formatSize($fileObject->getSize()) . 'bytes)';
395 $icon = '<span title="' . htmlspecialchars($fileObject->getName() . ' ' . $size) . '">' . $this->iconFactory->getIconForResource($fileObject, Icon::SIZE_SMALL)->render() . '</span>';
396 if (!$folder && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileObject->getExtension())) {
397 $processedFile = $fileObject->process(\TYPO3\CMS\Core\Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW, array());
398 if ($processedFile) {
399 $thumbUrl = $processedFile->getPublicUrl(true);
400 $thumb = '<br /><img src="' . htmlspecialchars($thumbUrl) . '" ' .
401 'width="' . $processedFile->getProperty('width') . '" ' .
402 'height="' . $processedFile->getProperty('height') . '" ' .
403 'title="' . htmlspecialchars($fileObject->getName()) . '" alt="" />';
404 }
405 }
406 $lines[] = '
407 <tr>
408 <td nowrap="nowrap" class="col-icon">' . $icon . '</td>
409 <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>
410 <td nowrap="nowrap" class="col-control">
411 <div class="btn-group">
412 <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>
413 </div>
414 </td>
415 </tr>';
416 } else {
417 // If the file did not exist (or is illegal) then it is removed from the clipboard immediately:
418 unset($this->clipData[$pad]['el'][$k]);
419 $this->changed = 1;
420 }
421 } else {
422 // Rendering records:
423 $rec = BackendUtility::getRecordWSOL($table, $uid);
424 if (is_array($rec)) {
425 $lines[] = '
426 <tr>
427 <td nowrap="nowrap" class="col-icon">' . $this->linkItemText($this->iconFactory->getIconForRecord($table, $rec, Icon::SIZE_SMALL)->render(), $rec, $table) . '</td>
428 <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>
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) . ', \'' . (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>
432 </div>
433 </td>
434 </tr>';
435 $localizationData = $this->getLocalizations($table, $rec, $bgColClass, $pad);
436 if ($localizationData) {
437 $lines[] = $localizationData;
438 }
439 } else {
440 unset($this->clipData[$pad]['el'][$k]);
441 $this->changed = 1;
442 }
443 }
444 }
445 }
446 }
447 $this->endClipboard();
448 return $lines;
449 }
450
451 /**
452 * Returns true if the clipboard contains elements
453 *
454 * @return bool
455 */
456 public function hasElements()
457 {
458 foreach ($this->clipData as $data) {
459 if (isset($data['el']) && is_array($data['el']) && !empty($data['el'])) {
460 return true;
461 }
462 }
463
464 return false;
465 }
466
467 /**
468 * Gets all localizations of the current record.
469 *
470 * @param string $table The table
471 * @param array $parentRec The current record
472 * @param string $bgColClass Class for the background color of a column
473 * @param string $pad Pad reference
474 * @return string HTML table rows
475 */
476 public function getLocalizations($table, $parentRec, $bgColClass, $pad)
477 {
478 $lines = array();
479 $tcaCtrl = $GLOBALS['TCA'][$table]['ctrl'];
480 if ($table != 'pages' && BackendUtility::isTableLocalizable($table) && !$tcaCtrl['transOrigPointerTable']) {
481 $where = array();
482 $where[] = $tcaCtrl['transOrigPointerField'] . '=' . (int)$parentRec['uid'];
483 $where[] = $tcaCtrl['languageField'] . '<>0';
484 if (isset($tcaCtrl['delete']) && $tcaCtrl['delete']) {
485 $where[] = $tcaCtrl['delete'] . '=0';
486 }
487 if (isset($tcaCtrl['versioningWS']) && $tcaCtrl['versioningWS']) {
488 $where[] = 't3ver_wsid=' . $parentRec['t3ver_wsid'];
489 }
490 $rows = $this->getDatabaseConnection()->exec_SELECTgetRows('*', $table, implode(' AND ', $where));
491 if (is_array($rows)) {
492 $modeData = '';
493 if ($pad == 'normal') {
494 $mode = $this->clipData['normal']['mode'] == 'copy' ? 'copy' : 'cut';
495 $modeData = ' <strong>(' . $this->clLabel($mode, 'cm') . ')</strong>';
496 }
497 foreach ($rows as $rec) {
498 $lines[] = '
499 <tr>
500 <td nowrap="nowrap" class="col-icon">' . $this->iconFactory->getIconForRecord($table, $rec, Icon::SIZE_SMALL)->render() . '</td>
501 <td nowrap="nowrap" width="95%">' . htmlspecialchars(GeneralUtility::fixed_lgd_cs(BackendUtility::getRecordTitle($table, $rec), $this->getBackendUser()->uc['titleLen'])) . $modeData . '</td>
502 <td nowrap="nowrap" class="col-control"></td>
503 </tr>';
504 }
505 }
506 }
507 return implode('', $lines);
508 }
509
510 /**
511 * Wraps title of pad in bold-tag and maybe the number of elements if any.
512 * Only applies bold-tag if the item is active
513 *
514 * @param string $str String (already htmlspecialchars()'ed)
515 * @param string $pad Pad reference
516 * @param bool $active is currently active
517 * @return string HTML output (htmlspecialchar'ed content inside of tags.)
518 */
519 public function padTitleWrap($str, $pad, $active)
520 {
521 $el = count($this->elFromTable($this->fileMode ? '_FILE' : '', $pad));
522 if ($el) {
523 $str .= ' (' . ($pad == 'normal' ? ($this->clipData['normal']['mode'] == 'copy' ? $this->clLabel('copy', 'cm') : $this->clLabel('cut', 'cm')) : htmlspecialchars($el)) . ')';
524 }
525 if ($active === true) {
526 return '<strong>' . $str . '</strong>';
527 } else {
528 return '<span class="text-muted">' . $str . '</span>';
529 }
530 }
531
532 /**
533 * Wraps the title of the items listed in link-tags. The items will link to the page/folder where they originate from
534 *
535 * @param string $str Title of element - must be htmlspecialchar'ed on beforehand.
536 * @param mixed $rec If array, a record is expected. If string, its a path
537 * @param string $table Table name
538 * @return string
539 */
540 public function linkItemText($str, $rec, $table = '')
541 {
542 if (is_array($rec) && $table) {
543 if ($this->fileMode) {
544 $str = '<span class="text-muted">' . $str . '</span>';
545 } else {
546 $str = '<a href="' . htmlspecialchars(BackendUtility::getModuleUrl('web_list', array('id' => $rec['pid']))) . '">' . $str . '</a>';
547 }
548 } elseif (file_exists($rec)) {
549 if (!$this->fileMode) {
550 $str = '<span class="text-muted">' . $str . '</span>';
551 } else {
552 if (ExtensionManagementUtility::isLoaded('filelist')) {
553 $str = '<a href="' . htmlspecialchars(BackendUtility::getModuleUrl('file_list', array('id' => dirname($rec)))) . '">' . $str . '</a>';
554 }
555 }
556 }
557 return $str;
558 }
559
560 /**
561 * Returns the select-url for database elements
562 *
563 * @param string $table Table name
564 * @param int $uid Uid of record
565 * @param bool|int $copy If set, copymode will be enabled
566 * @param bool|int $deselect If set, the link will deselect, otherwise select.
567 * @param array $baseArray The base array of GET vars to be sent in addition. Notice that current GET vars WILL automatically be included.
568 * @return string URL linking to the current script but with the CB array set to select the element with table/uid
569 */
570 public function selUrlDB($table, $uid, $copy = 0, $deselect = 0, $baseArray = array())
571 {
572 $CB = array('el' => array(rawurlencode($table . '|' . $uid) => $deselect ? 0 : 1));
573 if ($copy) {
574 $CB['setCopyMode'] = 1;
575 }
576 $baseArray['CB'] = $CB;
577 return GeneralUtility::linkThisScript($baseArray);
578 }
579
580 /**
581 * Returns the select-url for files
582 *
583 * @param string $path Filepath
584 * @param bool|int $copy If set, copymode will be enabled
585 * @param bool|int $deselect If set, the link will deselect, otherwise select.
586 * @param array $baseArray The base array of GET vars to be sent in addition. Notice that current GET vars WILL automatically be included.
587 * @return string URL linking to the current script but with the CB array set to select the path
588 */
589 public function selUrlFile($path, $copy = 0, $deselect = 0, $baseArray = array())
590 {
591 $CB = array('el' => array(rawurlencode('_FILE|' . GeneralUtility::shortmd5($path)) => $deselect ? '' : $path));
592 if ($copy) {
593 $CB['setCopyMode'] = 1;
594 }
595 $baseArray['CB'] = $CB;
596 return GeneralUtility::linkThisScript($baseArray);
597 }
598
599 /**
600 * pasteUrl of the element (database and file)
601 * For the meaning of $table and $uid, please read from ->makePasteCmdArray!!!
602 * The URL will point to tce_file or tce_db depending in $table
603 *
604 * @param string $table Tablename (_FILE for files)
605 * @param mixed $uid "destination": can be positive or negative indicating how the paste is done (paste into / paste after)
606 * @param bool $setRedirect If set, then the redirect URL will point back to the current script, but with CB reset.
607 * @param array|NULL $update Additional key/value pairs which should get set in the moved/copied record (via DataHandler)
608 * @return string
609 */
610 public function pasteUrl($table, $uid, $setRedirect = true, array $update = null)
611 {
612 $urlParameters = [
613 'vC' => $this->getBackendUser()->veriCode(),
614 'prErr' => 1,
615 'uPT' => 1,
616 'CB[paste]' => $table . '|' . $uid,
617 'CB[pad]' => $this->current
618 ];
619 if ($setRedirect) {
620 $urlParameters['redirect'] = GeneralUtility::linkThisScript(array('CB' => ''));
621 }
622 if (is_array($update)) {
623 $urlParameters['CB[update]'] = $update;
624 }
625 return BackendUtility::getModuleUrl($table === '_FILE' ? 'tce_file' : 'tce_db', $urlParameters);
626 }
627
628 /**
629 * deleteUrl for current pad
630 *
631 * @param bool|int $setRedirect If set, then the redirect URL will point back to the current script, but with CB reset.
632 * @param bool|int $file If set, then the URL will link to the tce_file.php script in the typo3/ dir.
633 * @return string
634 */
635 public function deleteUrl($setRedirect = 1, $file = 0)
636 {
637 $urlParameters = [
638 'vC' => $this->getBackendUser()->veriCode(),
639 'prErr' => 1,
640 'uPT' => 1,
641 'CB[delete]' => 1,
642 'CB[pad]' => $this->current
643 ];
644 if ($setRedirect) {
645 $urlParameters['redirect'] = GeneralUtility::linkThisScript(array('CB' => ''));
646 }
647 return BackendUtility::getModuleUrl($file ? 'tce_file' : 'tce_db', $urlParameters);
648 }
649
650 /**
651 * editUrl of all current elements
652 * ONLY database
653 * Links to FormEngine
654 *
655 * @return string The URL to FormEngine with parameters.
656 */
657 public function editUrl()
658 {
659 $parameters = array();
660 // All records
661 $elements = $this->elFromTable('');
662 foreach ($elements as $tP => $value) {
663 list($table, $uid) = explode('|', $tP);
664 $parameters['edit[' . $table . '][' . $uid . ']'] = 'edit';
665 }
666 return BackendUtility::getModuleUrl('record_edit', $parameters);
667 }
668
669 /**
670 * Returns the remove-url (file and db)
671 * for file $table='_FILE' and $uid = shortmd5 hash of path
672 *
673 * @param string $table Tablename
674 * @param string $uid Uid integer/shortmd5 hash
675 * @return string URL
676 */
677 public function removeUrl($table, $uid)
678 {
679 return GeneralUtility::linkThisScript(array('CB' => array('remove' => $table . '|' . $uid)));
680 }
681
682 /**
683 * Returns confirm JavaScript message
684 *
685 * @param string $table Table name
686 * @param mixed $rec For records its an array, for files its a string (path)
687 * @param string $type Type-code
688 * @param array $clElements Array of selected elements
689 * @param string $columnLabel Name of the content column
690 * @return string JavaScript "confirm" message
691 */
692 public function confirmMsg($table, $rec, $type, $clElements, $columnLabel = '')
693 {
694 if ($this->getBackendUser()->jsConfirmation(JsConfirmation::COPY_MOVE_PASTE)) {
695 $labelKey = 'LLL:EXT:lang/locallang_core.xlf:mess.' . ($this->currentMode() == 'copy' ? 'copy' : 'move') . ($this->current == 'normal' ? '' : 'cb') . '_' . $type;
696 $msg = $this->getLanguageService()->sL($labelKey . ($columnLabel ? '_colPos': ''));
697 if ($table == '_FILE') {
698 $thisRecTitle = basename($rec);
699 if ($this->current == 'normal') {
700 $selItem = reset($clElements);
701 $selRecTitle = basename($selItem);
702 } else {
703 $selRecTitle = count($clElements);
704 }
705 } else {
706 $thisRecTitle = $table == 'pages' && !is_array($rec) ? $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] : BackendUtility::getRecordTitle($table, $rec);
707 if ($this->current == 'normal') {
708 $selItem = $this->getSelectedRecord();
709 $selRecTitle = $selItem['_RECORD_TITLE'];
710 } else {
711 $selRecTitle = count($clElements);
712 }
713 }
714 // @TODO
715 // This can get removed as soon as the "_colPos" label is translated
716 // into all available locallang languages.
717 if (!$msg && $columnLabel) {
718 $thisRecTitle .= ' | ' . $columnLabel;
719 $msg = $this->getLanguageService()->sL($labelKey);
720 }
721
722 // Message
723 $conf = 'confirm(' . GeneralUtility::quoteJSvalue(sprintf($msg, GeneralUtility::fixed_lgd_cs($selRecTitle, 30), GeneralUtility::fixed_lgd_cs($thisRecTitle, 30), GeneralUtility::fixed_lgd_cs($columnLabel, 30))) . ')';
724 } else {
725 $conf = '';
726 }
727 return $conf;
728 }
729
730 /**
731 * Clipboard label - getting from "EXT:lang/locallang_core.xlf:"
732 *
733 * @param string $key Label Key
734 * @param string $Akey Alternative key to "labels
735 * @return string
736 */
737 public function clLabel($key, $Akey = 'labels')
738 {
739 return htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:' . $Akey . '.' . $key));
740 }
741
742 /**
743 * Creates GET parameters for linking to the export module.
744 *
745 * @return array GET parameters for current clipboard content to be exported
746 */
747 protected function exportClipElementParameters()
748 {
749 // Init
750 $pad = $this->current;
751 $params = array();
752 $params['tx_impexp']['action'] = 'export';
753 // Traverse items:
754 if (is_array($this->clipData[$pad]['el'])) {
755 foreach ($this->clipData[$pad]['el'] as $k => $v) {
756 if ($v) {
757 list($table, $uid) = explode('|', $k);
758 // Rendering files/directories on the clipboard
759 if ($table == '_FILE') {
760 if (file_exists($v) && GeneralUtility::isAllowedAbsPath($v)) {
761 $params['tx_impexp'][is_dir($v) ? 'dir' : 'file'][] = $v;
762 }
763 } else {
764 // Rendering records:
765 $rec = BackendUtility::getRecord($table, $uid);
766 if (is_array($rec)) {
767 $params['tx_impexp']['record'][] = $table . ':' . $uid;
768 }
769 }
770 }
771 }
772 }
773 return $params;
774 }
775
776 /*****************************************
777 *
778 * Helper functions
779 *
780 ****************************************/
781 /**
782 * Removes element on clipboard
783 *
784 * @param string $el Key of element in ->clipData array
785 * @return void
786 */
787 public function removeElement($el)
788 {
789 unset($this->clipData[$this->current]['el'][$el]);
790 $this->changed = 1;
791 }
792
793 /**
794 * Saves the clipboard, no questions asked.
795 * Use ->endClipboard normally (as it checks if changes has been done so saving is necessary)
796 *
797 * @access private
798 * @return void
799 */
800 public function saveClipboard()
801 {
802 $this->getBackendUser()->pushModuleData('clipboard', $this->clipData);
803 }
804
805 /**
806 * Returns the current mode, 'copy' or 'cut'
807 *
808 * @return string "copy" or "cut
809 */
810 public function currentMode()
811 {
812 return $this->clipData[$this->current]['mode'] == 'copy' ? 'copy' : 'cut';
813 }
814
815 /**
816 * This traverses the elements on the current clipboard pane
817 * and unsets elements which does not exist anymore or are disabled.
818 *
819 * @return void
820 */
821 public function cleanCurrent()
822 {
823 if (is_array($this->clipData[$this->current]['el'])) {
824 foreach ($this->clipData[$this->current]['el'] as $k => $v) {
825 list($table, $uid) = explode('|', $k);
826 if ($table != '_FILE') {
827 if (!$v || !is_array(BackendUtility::getRecord($table, $uid, 'uid'))) {
828 unset($this->clipData[$this->current]['el'][$k]);
829 $this->changed = 1;
830 }
831 } else {
832 if (!$v) {
833 unset($this->clipData[$this->current]['el'][$k]);
834 $this->changed = 1;
835 } else {
836 try {
837 ResourceFactory::getInstance()->retrieveFileOrFolderObject($v);
838 } catch (\TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException $e) {
839 // The file has been deleted in the meantime, so just remove it silently
840 unset($this->clipData[$this->current]['el'][$k]);
841 }
842 }
843 }
844 }
845 }
846 }
847
848 /**
849 * Counts the number of elements from the table $matchTable. If $matchTable is blank, all tables (except '_FILE' of course) is counted.
850 *
851 * @param string $matchTable Table to match/count for.
852 * @param string $pad Can optionally be used to set another pad than the current.
853 * @return array Array with keys from the CB.
854 */
855 public function elFromTable($matchTable = '', $pad = '')
856 {
857 $pad = $pad ? $pad : $this->current;
858 $list = array();
859 if (is_array($this->clipData[$pad]['el'])) {
860 foreach ($this->clipData[$pad]['el'] as $k => $v) {
861 if ($v) {
862 list($table, $uid) = explode('|', $k);
863 if ($table != '_FILE') {
864 if ((!$matchTable || (string)$table == (string)$matchTable) && $GLOBALS['TCA'][$table]) {
865 $list[$k] = $pad == 'normal' ? $v : $uid;
866 }
867 } else {
868 if ((string)$table == (string)$matchTable) {
869 $list[$k] = $v;
870 }
871 }
872 }
873 }
874 }
875 return $list;
876 }
877
878 /**
879 * Verifies if the item $table/$uid is on the current pad.
880 * 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...
881 *
882 * @param string $table Table name, (_FILE for files...)
883 * @param int $uid Element uid (path for files)
884 * @return string
885 */
886 public function isSelected($table, $uid)
887 {
888 $k = $table . '|' . $uid;
889 return $this->clipData[$this->current]['el'][$k] ? ($this->current == 'normal' ? $this->currentMode() : 1) : '';
890 }
891
892 /**
893 * Returns item record $table,$uid if selected on current clipboard
894 * If table and uid is blank, the first element is returned.
895 * Makes sense only for DB records - not files!
896 *
897 * @param string $table Table name
898 * @param int|string $uid Element uid
899 * @return array Element record with extra field _RECORD_TITLE set to the title of the record
900 */
901 public function getSelectedRecord($table = '', $uid = '')
902 {
903 if (!$table && !$uid) {
904 $elArr = $this->elFromTable('');
905 reset($elArr);
906 list($table, $uid) = explode('|', key($elArr));
907 }
908 if ($this->isSelected($table, $uid)) {
909 $selRec = BackendUtility::getRecordWSOL($table, $uid);
910 $selRec['_RECORD_TITLE'] = BackendUtility::getRecordTitle($table, $selRec);
911 return $selRec;
912 }
913 return array();
914 }
915
916 /**
917 * Reports if the current pad has elements (does not check file/DB type OR if file/DBrecord exists or not. Only counting array)
918 *
919 * @return bool TRUE if elements exist.
920 */
921 public function isElements()
922 {
923 return is_array($this->clipData[$this->current]['el']) && !empty($this->clipData[$this->current]['el']);
924 }
925
926 /*****************************************
927 *
928 * FOR USE IN tce_db.php:
929 *
930 ****************************************/
931 /**
932 * Applies the proper paste configuration in the $cmd array send to tce_db.php.
933 * $ref is the target, see description below.
934 * The current pad is pasted
935 *
936 * $ref: [tablename]:[paste-uid].
937 * Tablename is the name of the table from which elements *on the current clipboard* is pasted with the 'pid' paste-uid.
938 * No tablename means that all items on the clipboard (non-files) are pasted. This requires paste-uid to be positive though.
939 * so 'tt_content:-3' means 'paste tt_content elements on the clipboard to AFTER tt_content:3 record
940 * 'tt_content:30' means 'paste tt_content elements on the clipboard into page with id 30
941 * ':30' means 'paste ALL database elements on the clipboard into page with id 30
942 * ':-30' not valid.
943 *
944 * @param string $ref [tablename]:[paste-uid], see description
945 * @param array $CMD Command-array
946 * @param NULL|array If additional values should get set in the copied/moved record this will be an array containing key=>value pairs
947 * @return array Modified Command-array
948 */
949 public function makePasteCmdArray($ref, $CMD, array $update = null)
950 {
951 list($pTable, $pUid) = explode('|', $ref);
952 $pUid = (int)$pUid;
953 // pUid must be set and if pTable is not set (that means paste ALL elements)
954 // the uid MUST be positive/zero (pointing to page id)
955 if ($pTable || $pUid >= 0) {
956 $elements = $this->elFromTable($pTable);
957 // So the order is preserved.
958 $elements = array_reverse($elements);
959 $mode = $this->currentMode() == 'copy' ? 'copy' : 'move';
960 // Traverse elements and make CMD array
961 foreach ($elements as $tP => $value) {
962 list($table, $uid) = explode('|', $tP);
963 if (!is_array($CMD[$table])) {
964 $CMD[$table] = array();
965 }
966 if (is_array($update)) {
967 $CMD[$table][$uid][$mode] = array(
968 'action' => 'paste',
969 'target' => $pUid,
970 'update' => $update,
971 );
972 } else {
973 $CMD[$table][$uid][$mode] = $pUid;
974 }
975 if ($mode == 'move') {
976 $this->removeElement($tP);
977 }
978 }
979 $this->endClipboard();
980 }
981 return $CMD;
982 }
983
984 /**
985 * Delete record entries in CMD array
986 *
987 * @param array $CMD Command-array
988 * @return array Modified Command-array
989 */
990 public function makeDeleteCmdArray($CMD)
991 {
992 // all records
993 $elements = $this->elFromTable('');
994 foreach ($elements as $tP => $value) {
995 list($table, $uid) = explode('|', $tP);
996 if (!is_array($CMD[$table])) {
997 $CMD[$table] = array();
998 }
999 $CMD[$table][$uid]['delete'] = 1;
1000 $this->removeElement($tP);
1001 }
1002 $this->endClipboard();
1003 return $CMD;
1004 }
1005
1006 /*****************************************
1007 *
1008 * FOR USE IN tce_file.php:
1009 *
1010 ****************************************/
1011 /**
1012 * Applies the proper paste configuration in the $file array send to tce_file.php.
1013 * The current pad is pasted
1014 *
1015 * @param string $ref Reference to element (splitted by "|")
1016 * @param array $FILE Command-array
1017 * @return array Modified Command-array
1018 */
1019 public function makePasteCmdArray_file($ref, $FILE)
1020 {
1021 list($pTable, $pUid) = explode('|', $ref);
1022 $elements = $this->elFromTable('_FILE');
1023 $mode = $this->currentMode() == 'copy' ? 'copy' : 'move';
1024 // Traverse elements and make CMD array
1025 foreach ($elements as $tP => $path) {
1026 $FILE[$mode][] = array('data' => $path, 'target' => $pUid);
1027 if ($mode == 'move') {
1028 $this->removeElement($tP);
1029 }
1030 }
1031 $this->endClipboard();
1032 return $FILE;
1033 }
1034
1035 /**
1036 * Delete files in CMD array
1037 *
1038 * @param array $FILE Command-array
1039 * @return array Modified Command-array
1040 */
1041 public function makeDeleteCmdArray_file($FILE)
1042 {
1043 $elements = $this->elFromTable('_FILE');
1044 // Traverse elements and make CMD array
1045 foreach ($elements as $tP => $path) {
1046 $FILE['delete'][] = array('data' => $path);
1047 $this->removeElement($tP);
1048 }
1049 $this->endClipboard();
1050 return $FILE;
1051 }
1052
1053 /**
1054 * Returns LanguageService
1055 *
1056 * @return \TYPO3\CMS\Lang\LanguageService
1057 */
1058 protected function getLanguageService()
1059 {
1060 return $GLOBALS['LANG'];
1061 }
1062
1063 /**
1064 * Returns the current BE user.
1065 *
1066 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
1067 */
1068 protected function getBackendUser()
1069 {
1070 return $GLOBALS['BE_USER'];
1071 }
1072
1073 /**
1074 * Return DatabaseConnection
1075 *
1076 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
1077 */
1078 protected function getDatabaseConnection()
1079 {
1080 return $GLOBALS['TYPO3_DB'];
1081 }
1082 }