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