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