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