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