Fixed bug #17150: Workspaces non-admin user cannot send to next stage (Thanks to...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_foldertree.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (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 * Generate a folder tree
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 November/2003 by Kasper Skårhøj
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 * @coauthor René Fritz <r.fritz@colorcube.de>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 82: class t3lib_folderTree extends t3lib_treeView
42 * 89: function t3lib_folderTree()
43 * 107: function wrapIcon($icon,$row)
44 * 130: function wrapTitle($title,$row,$bank=0)
45 * 145: function getId($v)
46 * 155: function getJumpToParam($v)
47 * 167: function getTitleStr($row,$titleLen=30)
48 * 177: function getBrowsableTree()
49 * 240: function getFolderTree($files_path, $depth=999, $depthData='')
50 * 320: function getCount($files_path)
51 * 336: function initializePositionSaving()
52 *
53 * TOTAL FUNCTIONS: 10
54 * (This index is automatically created/updated by the extension "extdeveval")
55 *
56 */
57
58
59 /**
60 * Extension class for the t3lib_treeView class, specially made for browsing folders in the File module
61 *
62 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
63 * @coauthor René Fritz <r.fritz@colorcube.de>
64 * @package TYPO3
65 * @subpackage t3lib
66 * @see class t3lib_treeView
67 */
68 class t3lib_folderTree extends t3lib_treeView {
69
70 /**
71 * Constructor function of the class
72 *
73 * @return void
74 */
75 function t3lib_folderTree() {
76 parent::init();
77
78 $this->MOUNTS = $GLOBALS['FILEMOUNTS'];
79
80 $this->treeName = 'folder';
81 $this->titleAttrib = ''; //don't apply any title
82 $this->domIdPrefix = 'folder';
83 }
84
85 /**
86 * Wrapping the folder icon
87 *
88 * @param string The image tag for the icon
89 * @param array The row for the current element
90 * @return string The processed icon input value.
91 * @access private
92 */
93 function wrapIcon($icon, $row) {
94 // Add title attribute to input icon tag
95 $theFolderIcon = $this->addTagAttributes($icon, ($this->titleAttrib ? $this->titleAttrib . '="' . $this->getTitleAttrib($row) . '"' : ''));
96
97 // Wrap icon in click-menu link.
98 if (!$this->ext_IconMode) {
99 $theFolderIcon = $GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon($theFolderIcon, $row['path'], '', 0);
100 } elseif (!strcmp($this->ext_IconMode, 'titlelink')) {
101 $aOnClick = 'return jumpTo(\'' . $this->getJumpToParam($row) . '\',this,\'' . $this->domIdPrefix . $this->getId($row) . '\',' . $this->bank . ');';
102 $theFolderIcon = '<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">' . $theFolderIcon . '</a>';
103 }
104 return $theFolderIcon;
105 }
106
107 /**
108 * Wrapping $title in a-tags.
109 *
110 * @param string Title string
111 * @param string Item record
112 * @param integer Bank pointer (which mount point number)
113 * @return string
114 * @access private
115 */
116 function wrapTitle($title, $row, $bank = 0) {
117 $aOnClick = 'return jumpTo(\'' . $this->getJumpToParam($row) . '\',this,\'' . $this->domIdPrefix . $this->getId($row) . '\',' . $bank . ');';
118 $CSM = '';
119 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['useOnContextMenuHandler']) {
120 $CSM = ' oncontextmenu="' . htmlspecialchars($GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon('', $row['path'], '', 0, '', '', TRUE)) . '"';
121 }
122 return '<a href="#" title="' . htmlspecialchars($row['title']) . '" onclick="' . htmlspecialchars($aOnClick) . '"' . $CSM . '>' . $title . '</a>';
123 }
124
125 /**
126 * Returns the id from the record - for folders, this is an md5 hash.
127 *
128 * @param array Record array
129 * @return integer The "uid" field value.
130 */
131 function getId($v) {
132 return t3lib_div::md5Int($v['path']);
133 }
134
135 /**
136 * Returns jump-url parameter value.
137 *
138 * @param array The record array.
139 * @return string The jump-url parameter.
140 */
141 function getJumpToParam($v) {
142 return rawurlencode($v['path']);
143 }
144
145 /**
146 * Returns the title for the input record. If blank, a "no title" labele (localized) will be returned.
147 * '_title' is used for setting an alternative title for folders.
148 *
149 * @param array The input row array (where the key "_title" is used for the title)
150 * @param integer Title length (30)
151 * @return string The title.
152 */
153 function getTitleStr($row, $titleLen = 30) {
154 return $row['_title'] ? $row['_title'] : parent::getTitleStr($row, $titleLen);
155 }
156
157 /**
158 * Will create and return the HTML code for a browsable tree of folders.
159 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
160 *
161 * @return string HTML code for the browsable tree
162 */
163 function getBrowsableTree() {
164
165 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
166 $this->initializePositionSaving();
167
168 // Init done:
169 $titleLen = intval($this->BE_USER->uc['titleLen']);
170 $treeArr = array();
171
172 // Traverse mounts:
173 foreach ($this->MOUNTS as $key => $val) {
174 $md5_uid = md5($val['path']);
175 $specUID = hexdec(substr($md5_uid, 0, 6));
176 $this->specUIDmap[$specUID] = $val['path'];
177
178 // Set first:
179 $this->bank = $val['nkey'];
180 $isOpen = $this->stored[$val['nkey']][$specUID] || $this->expandFirst;
181 $this->reset();
182
183 // Set PM icon:
184 $cmd = $this->bank . '_' . ($isOpen ? '0_' : '1_') . $specUID . '_' . $this->treeName;
185 $icon = '<img' . t3lib_iconWorks::skinImg($this->backPath, 'gfx/ol/' . ($isOpen ? 'minus' : 'plus') . 'only.gif', 'width="18" height="16"') . ' alt="" />';
186 $firstHtml = $this->PM_ATagWrap($icon, $cmd);
187
188 switch ($val['type']) {
189 case 'user':
190 $icon = 'gfx/i/_icon_ftp_user.gif';
191 break;
192 case 'group':
193 $icon = 'gfx/i/_icon_ftp_group.gif';
194 break;
195 case 'readonly':
196 $icon = 'gfx/i/_icon_ftp_readonly.gif';
197 break;
198 default:
199 $icon = 'gfx/i/_icon_ftp.gif';
200 break;
201 }
202
203 // Preparing rootRec for the mount
204 $firstHtml .= $this->wrapIcon('<img' . t3lib_iconWorks::skinImg($this->backPath, $icon, 'width="18" height="16"') . ' alt="" />', $val);
205 $row = array();
206 $row['path'] = $val['path'];
207 $row['uid'] = $specUID;
208 $row['title'] = $val['name'];
209
210 // Add the root of the mount to ->tree
211 $this->tree[] = array('HTML' => $firstHtml, 'row' => $row, 'bank' => $this->bank);
212
213 // If the mount is expanded, go down:
214 if ($isOpen) {
215 // Set depth:
216 $depthD = '<img' . t3lib_iconWorks::skinImg($this->backPath, 'gfx/ol/blank.gif', 'width="18" height="16"') . ' alt="" />';
217 $this->getFolderTree($val['path'], 999, $depthD, $val['type']);
218 }
219
220 // Add tree:
221 $treeArr = array_merge($treeArr, $this->tree);
222 }
223 return $this->printTree($treeArr);
224 }
225
226 /**
227 * Fetches the data for the tree
228 *
229 * @param string Abs file path
230 * @param integer Max depth (recursivity limit)
231 * @param string HTML-code prefix for recursive calls.
232 * @return integer The count of items on the level
233 * @see getBrowsableTree()
234 */
235 function getFolderTree($files_path, $depth = 999, $depthData = '', $type = '') {
236
237 // This generates the directory tree
238 $dirs = t3lib_div::get_dirs($files_path);
239
240 $c = 0;
241 if (is_array($dirs)) {
242 $depth = intval($depth);
243 $HTML = '';
244 $a = 0;
245 $c = count($dirs);
246 sort($dirs);
247
248 foreach ($dirs as $key => $val) {
249 $a++;
250 $this->tree[] = array(); // Reserve space.
251 end($this->tree);
252 $treeKey = key($this->tree); // Get the key for this space
253 $LN = ($a == $c) ? 'blank' : 'line';
254
255 $val = preg_replace('/^\.\//', '', $val);
256 $title = $val;
257 $path = $files_path . $val . '/';
258 $webpath = t3lib_BEfunc::getPathType_web_nonweb($path);
259
260 $md5_uid = md5($path);
261 $specUID = hexdec(substr($md5_uid, 0, 6));
262 $this->specUIDmap[$specUID] = $path;
263 $row = array();
264 $row['path'] = $path;
265 $row['uid'] = $specUID;
266 $row['title'] = $title;
267
268 if ($depth > 1 && $this->expandNext($specUID)) {
269 $nextCount = $this->getFolderTree(
270 $path,
271 $depth - 1,
272 $this->makeHTML ? $depthData . '<img' . t3lib_iconWorks::skinImg($this->backPath, 'gfx/ol/' . $LN . '.gif', 'width="18" height="16"') . ' alt="" />' : '',
273 $type
274 );
275 $exp = 1; // Set "did expand" flag
276 } else {
277 $nextCount = $this->getCount($path);
278 $exp = 0; // Clear "did expand" flag
279 }
280
281 // Set HTML-icons, if any:
282 if ($this->makeHTML) {
283 $HTML = $depthData . $this->PMicon($row, $a, $c, $nextCount, $exp);
284
285 $icon = 'gfx/i/_icon_' . $webpath . 'folders' . ($type == 'readonly' ? '_ro' : '') . '.gif';
286 if ($val == '_temp_') {
287 $icon = 'gfx/i/sysf.gif';
288 $row['title'] = $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:temp', TRUE);
289 $row['_title'] = '<strong>' . $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:temp', TRUE) . '</strong>';
290 }
291 if ($val == '_recycler_') {
292 $icon = 'gfx/i/recycler.gif';
293 $row['title'] = $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:recycler', TRUE);
294 $row['_title'] = '<strong>' . $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:recycler', TRUE) . '</strong>';
295 }
296 $HTML .= $this->wrapIcon('<img' . t3lib_iconWorks::skinImg($this->backPath, $icon, 'width="18" height="16"') . ' alt="" />', $row);
297 }
298
299 // Finally, add the row/HTML content to the ->tree array in the reserved key.
300 $this->tree[$treeKey] = Array(
301 'row' => $row,
302 'HTML' => $HTML,
303 'bank' => $this->bank
304 );
305 }
306 }
307 return $c;
308 }
309
310 /**
311 * Counts the number of directories in a file path.
312 *
313 * @param string File path.
314 * @return integer
315 */
316 function getCount($files_path) {
317 // This generates the directory tree
318 $dirs = t3lib_div::get_dirs($files_path);
319 $c = 0;
320 if (is_array($dirs)) {
321 $c = count($dirs);
322 }
323 return $c;
324 }
325
326 /**
327 * Get stored tree structure AND updating it if needed according to incoming PM GET var.
328 *
329 * @return void
330 * @access private
331 */
332 function initializePositionSaving() {
333 // Get stored tree structure:
334 $this->stored = unserialize($this->BE_USER->uc['browseTrees'][$this->treeName]);
335
336 // Mapping md5-hash to shorter number:
337 $hashMap = array();
338 foreach ($this->MOUNTS as $key => $val) {
339 $nkey = hexdec(substr($key, 0, 4));
340 $hashMap[$nkey] = $key;
341 $this->MOUNTS[$key]['nkey'] = $nkey;
342 }
343
344 // PM action:
345 // (If an plus/minus icon has been clicked, the PM GET var is sent and we must update the stored positions in the tree):
346 $PM = explode('_', t3lib_div::_GP('PM')); // 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
347 if (count($PM) == 4 && $PM[3] == $this->treeName) {
348 if (isset($this->MOUNTS[$hashMap[$PM[0]]])) {
349 if ($PM[1]) { // set
350 $this->stored[$PM[0]][$PM[2]] = 1;
351 $this->savePosition($this->treeName);
352 } else { // clear
353 unset($this->stored[$PM[0]][$PM[2]]);
354 $this->savePosition($this->treeName);
355 }
356 }
357 }
358 }
359 }
360
361 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php'])) {
362 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php']);
363 }
364
365 ?>