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