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