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