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