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