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