See changelog (updated later)
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_arraybrowser.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2004 Kasper Skaarhoj (kasper@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 * Class for displaying an array as a tree
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
32 * XHTML compliant
33 *
34 * @author Kasper Skaarhoj <kasper@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 75: class t3lib_arrayBrowser
42 * 91: function tree($arr, $depth_in, $depthData)
43 * 154: function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
44 * 186: function fixed_lgd($string,$chars)
45 * 203: function depthKeys($arr,$settings)
46 *
47 * TOTAL FUNCTIONS: 4
48 * (This index is automatically created/updated by the extension "extdeveval")
49 *
50 */
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 /**
67 * Class for displaying an array as a tree
68 * See the extension 'lowlevel' /config (Backend module 'Tools > Configuration')
69 *
70 * @author Kasper Skaarhoj <kasper@typo3.com>
71 * @package TYPO3
72 * @subpackage t3lib
73 * @see SC_mod_tools_config_index::main()
74 */
75 class t3lib_arrayBrowser {
76 var $depthKeys = array(); // Array defining which keys to expand. Typically set from outside from some session variable - otherwise the array will collapse.
77 var $searchKeys = array(); // After calling the getSearchKeys function this array is populated with the key-positions in the array which contains values matching the search.
78 var $fixedLgd=1; // If set, the values are truncated with "..." appended if longer than a certain length.
79 var $regexMode=0; // If set, search for string with regex, otherwise stristr()
80 var $varName=''; // Set var name here if you want links to the variable name.
81
82 /**
83 * Make browsable tree
84 * Before calling this function you may want to set some of the internal vars like depthKeys, regexMode and fixedLgd. For examples see SC_mod_tools_config_index::main()
85 *
86 * @param array The array to display
87 * @param string Key-position id. Build up during recursive calls - [key1].[key2].[key3] - an so on.
88 * @param string Depth-data - basically a prefix for the icons. For calling this function from outside, let it stay blank.
89 * @return string HTML for the tree
90 * @see SC_mod_tools_config_index::main()
91 */
92 function tree($arr, $depth_in, $depthData) {
93 $HTML='';
94 $a=0;
95
96 if ($depth_in) {$depth_in = $depth_in.'.';}
97
98 $c=count($arr);
99 reset($arr);
100 while (list($key,)=each($arr)) {
101 $a++;
102 $depth = $depth_in.$key;
103 $goto = substr(md5($depth),0,6);
104
105 $deeper = (is_array($arr[$key]) && $this->depthKeys[$depth]) ? 1 : 0;
106 $PM = 'join';
107 $LN = ($a==$c)?'blank':'line';
108 $BTM = ($a==$c)?'bottom':'';
109 $PM = is_array($arr[$key]) ? ($deeper ? 'minus':'plus') : 'join';
110
111
112 $HTML.=$depthData;
113 $theIcon='<img'.t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'],'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' align="top" border="0" alt="" />';
114 if ($PM=='join') {
115 $HTML.=$theIcon;
116 } else {
117 $HTML.='<a name="'.$goto.'" href="'.htmlspecialchars('index.php?node['.$depth.']='.($deeper?0:1).'#'.$goto).'">'.$theIcon.'</a>';
118 }
119
120 $label = $key;
121 $HTML.= $this->wrapArrayKey($label,$depth,!is_array($arr[$key]) ? $arr[$key] : '');
122
123 if (!is_array($arr[$key])) {
124 $theValue = $arr[$key];
125 if ($this->fixedLgd) {
126 $imgBlocks = ceil(1+strlen($depthData)/77);
127 // debug($imgBlocks);
128 $lgdChars = 68-ceil(strlen('['.$key.']')*0.8)-$imgBlocks*3;
129 $theValue = $this->fixed_lgd($theValue,$lgdChars);
130 }
131 if ($this->searchKeys[$depth]) {
132 $HTML.='=<span style="color:red;">'.$this->wrapValue($theValue,$depth).'</font>';
133 } else {
134 $HTML.='='.$this->wrapValue($theValue,$depth);
135 }
136 }
137 $HTML.='<br />';
138
139 if ($deeper) {
140 $HTML.=$this->tree($arr[$key], $depth, $depthData.'<img'.t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'],'gfx/ol/'.$LN.'.gif','width="18" height="16"').' align="top" alt="" />');
141 }
142 }
143 return $HTML;
144 }
145
146 /**
147 * Wrapping the value in bold tags etc.
148 *
149 * @param string The title string
150 * @param string Depth path
151 * @return string Title string, htmlspecialchars()'ed
152 */
153 function wrapValue($theValue,$depth) {
154 return '<b>'.htmlspecialchars($theValue).'</b>';
155 }
156
157 /**
158 * Wrapping the value in bold tags etc.
159 *
160 * @param string The title string
161 * @param string Depth path
162 * @param string The value for the array entry.
163 * @return string Title string, htmlspecialchars()'ed
164 */
165 function wrapArrayKey($label,$depth,$theValue) {
166
167 // Protect label:
168 $label = htmlspecialchars($label);
169
170 // If varname is set:
171 if ($this->varName) {
172 $variableName = $this->varName.'[\''.str_replace('.','\'][\'',$depth).'\'] = '.(!t3lib_div::testInt($theValue) ? '\''.addslashes($theValue).'\'' : $theValue).'; ';
173 $label = '<a href="'.htmlspecialchars('index.php?varname='.$variableName.'#varname').'">'.$label.'</a>';
174 }
175
176 // Return:
177 return '['.$label.']';
178 }
179
180 /**
181 * Creates an array with "depthKeys" which will expand the array to show the search results
182 *
183 * @param array The array to search for the value
184 * @param string Depth string - blank for first call (will build up during recursive calling creating an id of the position: [key1].[key2].[key3]
185 * @param string The string to search for
186 * @param array Key array, for first call pass empty array
187 * @return array
188 */
189 function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray) {
190 reset($keyArr);
191 $c=count($keyArr);
192 if ($depth_in) {$depth_in = $depth_in.'.';}
193 while (list($key,)=each($keyArr)) {
194 $depth=$depth_in.$key;
195 $deeper = is_array($keyArr[$key]);
196
197 if ($this->regexMode) {
198 if (ereg($searchString,$keyArr[$key])) { $this->searchKeys[$depth]=1; }
199 } else {
200 if (stristr($keyArr[$key],$searchString)) { $this->searchKeys[$depth]=1; }
201 }
202
203 if ($deeper) {
204 $cS = count($this->searchKeys);
205 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
206 if ($cS != count($this->searchKeys)) {
207 $keyArray[$depth]=1;
208 }
209 }
210 }
211 return $keyArray;
212 }
213
214 /**
215 * Fixed length function
216 *
217 * @param string String to process
218 * @param integer Max number of chars
219 * @return string Processed string
220 */
221 function fixed_lgd($string,$chars) {
222 if ($chars >= 4) {
223 if(strlen($string)>$chars) {
224 return substr($string, 0, $chars-3).'...';
225 }
226 }
227 return $string;
228 }
229
230 /**
231 * Function modifying the depthKey array
232 *
233 * @param array Array with instructions to open/close nodes.
234 * @param array Input depth_key array
235 * @return array Output depth_key array with entries added/removed based on $arr
236 * @see SC_mod_tools_config_index::main()
237 */
238 function depthKeys($arr,$settings) {
239 $tsbrArray=array();
240 reset($arr);
241 while(list($theK,$theV)=each($arr)) {
242 $theKeyParts = explode('.',$theK);
243 $depth='';
244 $c=count($theKeyParts);
245 $a=0;
246 while(list(,$p)=each($theKeyParts)) {
247 $a++;
248 $depth.=($depth?'.':'').$p;
249 $tsbrArray[$depth]= ($c==$a) ? $theV : 1;
250 }
251 }
252 // Modify settings
253 reset($tsbrArray);
254 while(list($theK,$theV)=each($tsbrArray)) {
255 if ($theV) {
256 $settings[$theK] = 1;
257 } else {
258 unset($settings[$theK]);
259 }
260 }
261 return $settings;
262 }
263 }
264
265 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php']) {
266 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php']);
267 }
268 ?>