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