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