[TASK] Merge submodule workspaces into core
[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 textfile 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 * @return string
553 * @todo Define visibility
554 */
555 public function pasteUrl($table, $uid, $setRedirect = 1) {
556 $rU = $this->backPath . ($table == '_FILE' ? 'tce_file.php' : 'tce_db.php') . '?' . ($setRedirect ? 'redirect=' . rawurlencode(GeneralUtility::linkThisScript(array('CB' => ''))) : '') . '&vC=' . $GLOBALS['BE_USER']->veriCode() . '&prErr=1&uPT=1' . '&CB[paste]=' . rawurlencode(($table . '|' . $uid)) . '&CB[pad]=' . $this->current . BackendUtility::getUrlToken('tceAction');
557 return $rU;
558 }
559
560 /**
561 * deleteUrl for current pad
562 *
563 * @param boolean $setRedirect If set, then the redirect URL will point back to the current script, but with CB reset.
564 * @param boolean $file If set, then the URL will link to the tce_file.php script in the typo3/ dir.
565 * @return string
566 * @todo Define visibility
567 */
568 public function deleteUrl($setRedirect = 1, $file = 0) {
569 $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');
570 return $rU;
571 }
572
573 /**
574 * editUrl of all current elements
575 * ONLY database
576 * Links to alt_doc.php
577 *
578 * @return string The URL to alt_doc.php with parameters.
579 * @todo Define visibility
580 */
581 public function editUrl() {
582 // All records
583 $elements = $this->elFromTable('');
584 $editCMDArray = array();
585 foreach ($elements as $tP => $value) {
586 list($table, $uid) = explode('|', $tP);
587 $editCMDArray[] = '&edit[' . $table . '][' . $uid . ']=edit';
588 }
589 $rU = $this->backPath . 'alt_doc.php?' . implode('', $editCMDArray);
590 return $rU;
591 }
592
593 /**
594 * Returns the remove-url (file and db)
595 * for file $table='_FILE' and $uid = shortmd5 hash of path
596 *
597 * @param string $table Tablename
598 * @param string $uid Uid integer/shortmd5 hash
599 * @return string URL
600 * @todo Define visibility
601 */
602 public function removeUrl($table, $uid) {
603 return GeneralUtility::linkThisScript(array('CB' => array('remove' => $table . '|' . $uid)));
604 }
605
606 /**
607 * Returns confirm JavaScript message
608 *
609 * @param string $table Table name
610 * @param mixed $rec For records its an array, for files its a string (path)
611 * @param string $type Type-code
612 * @param array $clElements Array of selected elements
613 * @return string JavaScript "confirm" message
614 * @todo Define visibility
615 */
616 public function confirmMsg($table, $rec, $type, $clElements) {
617 if ($GLOBALS['BE_USER']->jsConfirmation(2)) {
618 $labelKey = 'LLL:EXT:lang/locallang_core.xlf:mess.' . ($this->currentMode() == 'copy' ? 'copy' : 'move') . ($this->current == 'normal' ? '' : 'cb') . '_' . $type;
619 $msg = $GLOBALS['LANG']->sL($labelKey);
620 if ($table == '_FILE') {
621 $thisRecTitle = basename($rec);
622 if ($this->current == 'normal') {
623 $selItem = reset($clElements);
624 $selRecTitle = basename($selItem);
625 } else {
626 $selRecTitle = count($clElements);
627 }
628 } else {
629 $thisRecTitle = $table == 'pages' && !is_array($rec) ? $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] : BackendUtility::getRecordTitle($table, $rec);
630 if ($this->current == 'normal') {
631 $selItem = $this->getSelectedRecord();
632 $selRecTitle = $selItem['_RECORD_TITLE'];
633 } else {
634 $selRecTitle = count($clElements);
635 }
636 }
637 // Message
638 $conf = 'confirm(' . $GLOBALS['LANG']->JScharCode(sprintf($msg, GeneralUtility::fixed_lgd_cs($selRecTitle, 30), GeneralUtility::fixed_lgd_cs($thisRecTitle, 30))) . ')';
639 } else {
640 $conf = '';
641 }
642 return $conf;
643 }
644
645 /**
646 * Clipboard label - getting from "EXT:lang/locallang_core.xlf:"
647 *
648 * @param string $key Label Key
649 * @param string $Akey Alternative key to "labels
650 * @return string
651 * @todo Define visibility
652 */
653 public function clLabel($key, $Akey = 'labels') {
654 return htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:' . $Akey . '.' . $key));
655 }
656
657 /**
658 * Creates GET parameters for linking to the export module.
659 *
660 * @return string GET parameters for current clipboard content to be exported.
661 * @todo Define visibility
662 */
663 public function exportClipElementParameters() {
664 // Init
665 $pad = $this->current;
666 $params = array();
667 $params[] = 'tx_impexp[action]=export';
668 // Traverse items:
669 if (is_array($this->clipData[$pad]['el'])) {
670 foreach ($this->clipData[$pad]['el'] as $k => $v) {
671 if ($v) {
672 list($table, $uid) = explode('|', $k);
673 // Rendering files/directories on the clipboard
674 if ($table == '_FILE') {
675 if (file_exists($v) && GeneralUtility::isAllowedAbsPath($v)) {
676 $params[] = 'tx_impexp[' . (is_dir($v) ? 'dir' : 'file') . '][]=' . rawurlencode($v);
677 }
678 } else {
679 // Rendering records:
680 $rec = BackendUtility::getRecord($table, $uid);
681 if (is_array($rec)) {
682 $params[] = 'tx_impexp[record][]=' . rawurlencode(($table . ':' . $uid));
683 }
684 }
685 }
686 }
687 }
688 return '?' . implode('&', $params);
689 }
690
691 /*****************************************
692 *
693 * Helper functions
694 *
695 ****************************************/
696 /**
697 * Removes element on clipboard
698 *
699 * @param string $el Key of element in ->clipData array
700 * @return void
701 * @todo Define visibility
702 */
703 public function removeElement($el) {
704 unset($this->clipData[$this->current]['el'][$el]);
705 $this->changed = 1;
706 }
707
708 /**
709 * Saves the clipboard, no questions asked.
710 * Use ->endClipboard normally (as it checks if changes has been done so saving is necessary)
711 *
712 * @access private
713 * @return void
714 * @todo Define visibility
715 */
716 public function saveClipboard() {
717 $GLOBALS['BE_USER']->pushModuleData('clipboard', $this->clipData);
718 }
719
720 /**
721 * Returns the current mode, 'copy' or 'cut'
722 *
723 * @return string "copy" or "cut
724 * @todo Define visibility
725 */
726 public function currentMode() {
727 return $this->clipData[$this->current]['mode'] == 'copy' ? 'copy' : 'cut';
728 }
729
730 /**
731 * This traverses the elements on the current clipboard pane
732 * and unsets elements which does not exist anymore or are disabled.
733 *
734 * @return void
735 * @todo Define visibility
736 */
737 public function cleanCurrent() {
738 if (is_array($this->clipData[$this->current]['el'])) {
739 foreach ($this->clipData[$this->current]['el'] as $k => $v) {
740 list($table, $uid) = explode('|', $k);
741 if ($table != '_FILE') {
742 if (!$v || !is_array(BackendUtility::getRecord($table, $uid, 'uid'))) {
743 unset($this->clipData[$this->current]['el'][$k]);
744 $this->changed = 1;
745 }
746 } else {
747 if (!$v) {
748 unset($this->clipData[$this->current]['el'][$k]);
749 $this->changed = 1;
750 } else {
751 try {
752 ResourceFactory::getInstance()->retrieveFileOrFolderObject($v);
753 } catch (\TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException $e) {
754 // The file has been deleted in the meantime, so just remove it silently
755 unset($this->clipData[$this->current]['el'][$k]);
756 }
757 }
758 }
759 }
760 }
761 }
762
763 /**
764 * Counts the number of elements from the table $matchTable. If $matchTable is blank, all tables (except '_FILE' of course) is counted.
765 *
766 * @param string $matchTable Table to match/count for.
767 * @param string $pad Can optionally be used to set another pad than the current.
768 * @return array Array with keys from the CB.
769 * @todo Define visibility
770 */
771 public function elFromTable($matchTable = '', $pad = '') {
772 $pad = $pad ? $pad : $this->current;
773 $list = array();
774 if (is_array($this->clipData[$pad]['el'])) {
775 foreach ($this->clipData[$pad]['el'] as $k => $v) {
776 if ($v) {
777 list($table, $uid) = explode('|', $k);
778 if ($table != '_FILE') {
779 if ((!$matchTable || (string) $table == (string) $matchTable) && $GLOBALS['TCA'][$table]) {
780 $list[$k] = $pad == 'normal' ? $v : $uid;
781 }
782 } else {
783 if ((string) $table == (string) $matchTable) {
784 $list[$k] = $v;
785 }
786 }
787 }
788 }
789 }
790 return $list;
791 }
792
793 /**
794 * Verifies if the item $table/$uid is on the current pad.
795 * 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...
796 *
797 * @param string $table Table name, (_FILE for files...)
798 * @param integer $uid Element uid (path for files)
799 * @return string
800 * @todo Define visibility
801 */
802 public function isSelected($table, $uid) {
803 $k = $table . '|' . $uid;
804 return $this->clipData[$this->current]['el'][$k] ? ($this->current == 'normal' ? $this->currentMode() : 1) : '';
805 }
806
807 /**
808 * Returns item record $table,$uid if selected on current clipboard
809 * If table and uid is blank, the first element is returned.
810 * Makes sense only for DB records - not files!
811 *
812 * @param string $table Table name
813 * @param integer $uid Element uid
814 * @return array Element record with extra field _RECORD_TITLE set to the title of the record
815 * @todo Define visibility
816 */
817 public function getSelectedRecord($table = '', $uid = '') {
818 if (!$table && !$uid) {
819 $elArr = $this->elFromTable('');
820 reset($elArr);
821 list($table, $uid) = explode('|', key($elArr));
822 }
823 if ($this->isSelected($table, $uid)) {
824 $selRec = BackendUtility::getRecordWSOL($table, $uid);
825 $selRec['_RECORD_TITLE'] = BackendUtility::getRecordTitle($table, $selRec);
826 return $selRec;
827 }
828 }
829
830 /**
831 * Reports if the current pad has elements (does not check file/DB type OR if file/DBrecord exists or not. Only counting array)
832 *
833 * @return boolean TRUE if elements exist.
834 * @todo Define visibility
835 */
836 public function isElements() {
837 return is_array($this->clipData[$this->current]['el']) && count($this->clipData[$this->current]['el']);
838 }
839
840 /*****************************************
841 *
842 * FOR USE IN tce_db.php:
843 *
844 ****************************************/
845 /**
846 * Applies the proper paste configuration in the $cmd array send to tce_db.php.
847 * $ref is the target, see description below.
848 * The current pad is pasted
849 *
850 * $ref: [tablename]:[paste-uid].
851 * Tablename is the name of the table from which elements *on the current clipboard* is pasted with the 'pid' paste-uid.
852 * No tablename means that all items on the clipboard (non-files) are pasted. This requires paste-uid to be positive though.
853 * so 'tt_content:-3' means 'paste tt_content elements on the clipboard to AFTER tt_content:3 record
854 * 'tt_content:30' means 'paste tt_content elements on the clipboard into page with id 30
855 * ':30' means 'paste ALL database elements on the clipboard into page with id 30
856 * ':-30' not valid.
857 *
858 * @param string $ref [tablename]:[paste-uid], see description
859 * @param array $CMD Command-array
860 * @return array Modified Command-array
861 * @todo Define visibility
862 */
863 public function makePasteCmdArray($ref, $CMD) {
864 list($pTable, $pUid) = explode('|', $ref);
865 $pUid = intval($pUid);
866 // pUid must be set and if pTable is not set (that means paste ALL elements)
867 // the uid MUST be positive/zero (pointing to page id)
868 if ($pTable || $pUid >= 0) {
869 $elements = $this->elFromTable($pTable);
870 // So the order is preserved.
871 $elements = array_reverse($elements);
872 $mode = $this->currentMode() == 'copy' ? 'copy' : 'move';
873 // Traverse elements and make CMD array
874 foreach ($elements as $tP => $value) {
875 list($table, $uid) = explode('|', $tP);
876 if (!is_array($CMD[$table])) {
877 $CMD[$table] = array();
878 }
879 $CMD[$table][$uid][$mode] = $pUid;
880 if ($mode == 'move') {
881 $this->removeElement($tP);
882 }
883 }
884 $this->endClipboard();
885 }
886 return $CMD;
887 }
888
889 /**
890 * Delete record entries in CMD array
891 *
892 * @param array $CMD Command-array
893 * @return array Modified Command-array
894 * @todo Define visibility
895 */
896 public function makeDeleteCmdArray($CMD) {
897 // all records
898 $elements = $this->elFromTable('');
899 foreach ($elements as $tP => $value) {
900 list($table, $uid) = explode('|', $tP);
901 if (!is_array($CMD[$table])) {
902 $CMD[$table] = array();
903 }
904 $CMD[$table][$uid]['delete'] = 1;
905 $this->removeElement($tP);
906 }
907 $this->endClipboard();
908 return $CMD;
909 }
910
911 /*****************************************
912 *
913 * FOR USE IN tce_file.php:
914 *
915 ****************************************/
916 /**
917 * Applies the proper paste configuration in the $file array send to tce_file.php.
918 * The current pad is pasted
919 *
920 * @param string $ref Reference to element (splitted by "|")
921 * @param array $FILE Command-array
922 * @return array Modified Command-array
923 * @todo Define visibility
924 */
925 public function makePasteCmdArray_file($ref, $FILE) {
926 list($pTable, $pUid) = explode('|', $ref);
927 $elements = $this->elFromTable('_FILE');
928 $mode = $this->currentMode() == 'copy' ? 'copy' : 'move';
929 // Traverse elements and make CMD array
930 foreach ($elements as $tP => $path) {
931 $FILE[$mode][] = array('data' => $path, 'target' => $pUid);
932 if ($mode == 'move') {
933 $this->removeElement($tP);
934 }
935 }
936 $this->endClipboard();
937 return $FILE;
938 }
939
940 /**
941 * Delete files in CMD array
942 *
943 * @param array $FILE Command-array
944 * @return array Modified Command-array
945 * @todo Define visibility
946 */
947 public function makeDeleteCmdArray_file($FILE) {
948 $elements = $this->elFromTable('_FILE');
949 // Traverse elements and make CMD array
950 foreach ($elements as $tP => $path) {
951 $FILE['delete'][] = array('data' => $path);
952 $this->removeElement($tP);
953 }
954 $this->endClipboard();
955 return $FILE;
956 }
957
958 }
959
960 ?>