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