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