[TASK] Make alt_doc.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\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 FormEngine
596 *
597 * @return string The URL to FormEngine with parameters.
598 */
599 public function editUrl() {
600 $parameters = array();
601 // All records
602 $elements = $this->elFromTable('');
603 foreach ($elements as $tP => $value) {
604 list($table, $uid) = explode('|', $tP);
605 $parameters['edit[' . $table . '][' . $uid . ']'] = 'edit';
606 }
607 return BackendUtility::getModuleUrl('record_edit', $parameters);
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 ($this->getBackendUser()->jsConfirmation(JsConfirmation::COPY_MOVE_PASTE)) {
634 $labelKey = 'LLL:EXT:lang/locallang_core.xlf:mess.' . ($this->currentMode() == 'copy' ? 'copy' : 'move') . ($this->current == 'normal' ? '' : 'cb') . '_' . $type;
635 $msg = $this->getLanguageService()->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 = $this->getLanguageService()->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($this->getLanguageService()->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 $this->getBackendUser()->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|string $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 return array();
844 }
845
846 /**
847 * Reports if the current pad has elements (does not check file/DB type OR if file/DBrecord exists or not. Only counting array)
848 *
849 * @return bool TRUE if elements exist.
850 */
851 public function isElements() {
852 return is_array($this->clipData[$this->current]['el']) && count($this->clipData[$this->current]['el']);
853 }
854
855 /*****************************************
856 *
857 * FOR USE IN tce_db.php:
858 *
859 ****************************************/
860 /**
861 * Applies the proper paste configuration in the $cmd array send to tce_db.php.
862 * $ref is the target, see description below.
863 * The current pad is pasted
864 *
865 * $ref: [tablename]:[paste-uid].
866 * Tablename is the name of the table from which elements *on the current clipboard* is pasted with the 'pid' paste-uid.
867 * No tablename means that all items on the clipboard (non-files) are pasted. This requires paste-uid to be positive though.
868 * so 'tt_content:-3' means 'paste tt_content elements on the clipboard to AFTER tt_content:3 record
869 * 'tt_content:30' means 'paste tt_content elements on the clipboard into page with id 30
870 * ':30' means 'paste ALL database elements on the clipboard into page with id 30
871 * ':-30' not valid.
872 *
873 * @param string $ref [tablename]:[paste-uid], see description
874 * @param array $CMD Command-array
875 * @param NULL|array If additional values should get set in the copied/moved record this will be an array containing key=>value pairs
876 * @return array Modified Command-array
877 */
878 public function makePasteCmdArray($ref, $CMD, array $update = NULL) {
879 list($pTable, $pUid) = explode('|', $ref);
880 $pUid = (int)$pUid;
881 // pUid must be set and if pTable is not set (that means paste ALL elements)
882 // the uid MUST be positive/zero (pointing to page id)
883 if ($pTable || $pUid >= 0) {
884 $elements = $this->elFromTable($pTable);
885 // So the order is preserved.
886 $elements = array_reverse($elements);
887 $mode = $this->currentMode() == 'copy' ? 'copy' : 'move';
888 // Traverse elements and make CMD array
889 foreach ($elements as $tP => $value) {
890 list($table, $uid) = explode('|', $tP);
891 if (!is_array($CMD[$table])) {
892 $CMD[$table] = array();
893 }
894 if (is_array($update)) {
895 $CMD[$table][$uid][$mode] = array(
896 'action' => 'paste',
897 'target' => $pUid,
898 'update' => $update,
899 );
900 } else {
901 $CMD[$table][$uid][$mode] = $pUid;
902 }
903 if ($mode == 'move') {
904 $this->removeElement($tP);
905 }
906 }
907 $this->endClipboard();
908 }
909 return $CMD;
910 }
911
912 /**
913 * Delete record entries in CMD array
914 *
915 * @param array $CMD Command-array
916 * @return array Modified Command-array
917 */
918 public function makeDeleteCmdArray($CMD) {
919 // all records
920 $elements = $this->elFromTable('');
921 foreach ($elements as $tP => $value) {
922 list($table, $uid) = explode('|', $tP);
923 if (!is_array($CMD[$table])) {
924 $CMD[$table] = array();
925 }
926 $CMD[$table][$uid]['delete'] = 1;
927 $this->removeElement($tP);
928 }
929 $this->endClipboard();
930 return $CMD;
931 }
932
933 /*****************************************
934 *
935 * FOR USE IN tce_file.php:
936 *
937 ****************************************/
938 /**
939 * Applies the proper paste configuration in the $file array send to tce_file.php.
940 * The current pad is pasted
941 *
942 * @param string $ref Reference to element (splitted by "|")
943 * @param array $FILE Command-array
944 * @return array Modified Command-array
945 */
946 public function makePasteCmdArray_file($ref, $FILE) {
947 list($pTable, $pUid) = explode('|', $ref);
948 $elements = $this->elFromTable('_FILE');
949 $mode = $this->currentMode() == 'copy' ? 'copy' : 'move';
950 // Traverse elements and make CMD array
951 foreach ($elements as $tP => $path) {
952 $FILE[$mode][] = array('data' => $path, 'target' => $pUid);
953 if ($mode == 'move') {
954 $this->removeElement($tP);
955 }
956 }
957 $this->endClipboard();
958 return $FILE;
959 }
960
961 /**
962 * Delete files in CMD array
963 *
964 * @param array $FILE Command-array
965 * @return array Modified Command-array
966 */
967 public function makeDeleteCmdArray_file($FILE) {
968 $elements = $this->elFromTable('_FILE');
969 // Traverse elements and make CMD array
970 foreach ($elements as $tP => $path) {
971 $FILE['delete'][] = array('data' => $path);
972 $this->removeElement($tP);
973 }
974 $this->endClipboard();
975 return $FILE;
976 }
977
978 /**
979 * Returns LanguageService
980 *
981 * @return \TYPO3\CMS\Lang\LanguageService
982 */
983 protected function getLanguageService() {
984 return $GLOBALS['LANG'];
985 }
986
987 /**
988 * Returns the current BE user.
989 *
990 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
991 */
992 protected function getBackendUser() {
993 return $GLOBALS['BE_USER'];
994 }
995
996 /**
997 * Return DatabaseConnection
998 *
999 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
1000 */
1001 protected function getDatabaseConnection() {
1002 return $GLOBALS['TYPO3_DB'];
1003 }
1004
1005 }