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