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