Added $Id$ keywords, cleaned up comment tags
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_arraybrowser.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 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
81 /**
82 * Make browsable tree
83 * 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()
84 *
85 * @param array The array to display
86 * @param string Key-position id. Build up during recursive calls - [key1].[key2].[key3] - an so on.
87 * @param string Depth-data - basically a prefix for the icons. For calling this function from outside, let it stay blank.
88 * @return string HTML for the tree
89 * @see SC_mod_tools_config_index::main()
90 */
91 function tree($arr, $depth_in, $depthData) {
92 $HTML='';
93 $a=0;
94
95 if ($depth_in) {$depth_in = $depth_in.'.';}
96
97 $c=count($arr);
98 reset($arr);
99 while (list($key,)=each($arr)) {
100 $a++;
101 $depth=$depth_in.$key;
102 $goto = substr(md5($depth),0,6);
103
104 $deeper = (is_array($arr[$key]) && $this->depthKeys[$depth]) ? 1 : 0;
105 $PM = 'join';
106 $LN = ($a==$c)?'blank':'line';
107 $BTM = ($a==$c)?'bottom':'';
108 $PM = is_array($arr[$key]) ? ($deeper ? 'minus':'plus') : 'join';
109
110
111 $HTML.=$depthData;
112 $theIcon='<img src="'.$GLOBALS['BACK_PATH'].'t3lib/gfx/ol/'.$PM.$BTM.'.gif" width="18" height="16" align="top" border="0" alt="" />';
113 if ($PM=='join') {
114 $HTML.=$theIcon;
115 } else {
116 $HTML.='<a name="'.$goto.'" href="'.htmlspecialchars('index.php?node['.$depth.']='.($deeper?0:1).'#'.$goto).'">'.$theIcon.'</a>';
117 }
118
119 $label = $key;
120 $HTML.='['.$label.']';
121
122 if (!is_array($arr[$key])) {
123 $theValue = $arr[$key];
124 if ($this->fixedLgd) {
125 $imgBlocks = ceil(1+strlen($depthData)/77);
126 // debug($imgBlocks);
127 $lgdChars = 68-ceil(strlen('['.$key.']')*0.8)-$imgBlocks*3;
128 $theValue = $this->fixed_lgd($theValue,$lgdChars);
129 }
130 if ($this->searchKeys[$depth]) {
131 $HTML.='=<b><span style="color:red;">'.htmlspecialchars($theValue).'</font></b>';
132 } else {
133 $HTML.='=<b>'.htmlspecialchars($theValue).'</b>';
134 }
135 }
136 $HTML.='<br />';
137
138 if ($deeper) {
139 $HTML.=$this->tree($arr[$key], $depth, $depthData.'<img src="'.$GLOBALS['BACK_PATH'].'t3lib/gfx/ol/'.$LN.'.gif" width="18" height="16" align="top" alt="" />');
140 }
141 }
142 return $HTML;
143 }
144
145 /**
146 * Creates an array with "depthKeys" which will expand the array to show the search results
147 *
148 * @param array The array to search for the value
149 * @param string Depth string - blank for first call (will build up during recursive calling creating an id of the position: [key1].[key2].[key3]
150 * @param string The string to search for
151 * @param array Key array, for first call pass empty array
152 * @return array
153 */
154 function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray) {
155 reset($keyArr);
156 $c=count($keyArr);
157 if ($depth_in) {$depth_in = $depth_in.'.';}
158 while (list($key,)=each($keyArr)) {
159 $depth=$depth_in.$key;
160 $deeper = is_array($keyArr[$key]);
161
162 if ($this->regexMode) {
163 if (ereg($searchString,$keyArr[$key])) { $this->searchKeys[$depth]=1; }
164 } else {
165 if (stristr($keyArr[$key],$searchString)) { $this->searchKeys[$depth]=1; }
166 }
167
168 if ($deeper) {
169 $cS = count($this->searchKeys);
170 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
171 if ($cS != count($this->searchKeys)) {
172 $keyArray[$depth]=1;
173 }
174 }
175 }
176 return $keyArray;
177 }
178
179 /**
180 * Fixed length function
181 *
182 * @param string String to process
183 * @param integer Max number of chars
184 * @return string Processed string
185 */
186 function fixed_lgd($string,$chars) {
187 if ($chars >= 4) {
188 if(strlen($string)>$chars) {
189 return substr($string, 0, $chars-3).'...';
190 }
191 }
192 return $string;
193 }
194
195 /**
196 * Function modifying the depthKey array
197 *
198 * @param array Array with instructions to open/close nodes.
199 * @param array Input depth_key array
200 * @return array Output depth_key array with entries added/removed based on $arr
201 * @see SC_mod_tools_config_index::main()
202 */
203 function depthKeys($arr,$settings) {
204 $tsbrArray=array();
205 reset($arr);
206 while(list($theK,$theV)=each($arr)) {
207 $theKeyParts = explode('.',$theK);
208 $depth='';
209 $c=count($theKeyParts);
210 $a=0;
211 while(list(,$p)=each($theKeyParts)) {
212 $a++;
213 $depth.=($depth?'.':'').$p;
214 $tsbrArray[$depth]= ($c==$a) ? $theV : 1;
215 }
216 }
217 // Modify settings
218 reset($tsbrArray);
219 while(list($theK,$theV)=each($tsbrArray)) {
220 if ($theV) {
221 $settings[$theK] = 1;
222 } else {
223 unset($settings[$theK]);
224 }
225 }
226 return $settings;
227 }
228 }
229
230 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php']) {
231 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php']);
232 }
233 ?>