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