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