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