[FEATURE] Add wincache as a cache backend
[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 $HTML .= $depthData;
90 $theIcon = '<img' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/ol/' . $PM . $BTM . '.gif', 'width="18" height="16"') .
91 ' align="top" border="0" alt="" />';
92 if ($PM == 'join') {
93 $HTML .= $theIcon;
94 } else {
95 $HTML .=
96 ($this->expAll ? '' : '<a id="' . $goto . '" href="' . htmlspecialchars(t3lib_BEfunc::getModuleUrl(t3lib_div::_GP('M')) . '&node[' .
97 $depth . ']=' . ($deeper ? 0 : 1) . '#' . $goto) . '">') .
98 $theIcon .
99 ($this->expAll ? '' : '</a>');
100 }
101
102 $label = $key;
103 $HTML .= $this->wrapArrayKey($label, $depth, !$isArray ? $arr[$key] : '');
104
105 if (!$isArray) {
106 $theValue = $arr[$key];
107 if ($this->fixedLgd) {
108 $imgBlocks = ceil(1 + strlen($depthData) / 77);
109 $lgdChars = 68 - ceil(strlen('[' . $key . ']') * 0.8) - $imgBlocks * 3;
110 $theValue = $this->fixed_lgd($theValue, $lgdChars);
111 }
112 if ($this->searchKeys[$depth]) {
113 $HTML .= '=<span style="color:red;">' . $this->wrapValue($theValue, $depth) . '</span>';
114 } else {
115 $HTML .= '=' . $this->wrapValue($theValue, $depth);
116 }
117 }
118 $HTML .= '<br />';
119
120 if ($deeper) {
121 $HTML .= $this->tree($arr[$key], $depth, $depthData . '<img' .
122 t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/ol/' . $LN . '.gif', 'width="18" height="16"') .
123 ' align="top" alt="" />');
124 }
125 }
126 return $HTML;
127 }
128
129 /**
130 * Wrapping the value in bold tags etc.
131 *
132 * @param string $theValue The title string
133 * @param string $depth Depth path
134 * @return string Title string, htmlspecialchars()'ed
135 */
136 function wrapValue($theValue, $depth) {
137 $wrappedValue = '';
138 if (strlen($theValue) > 0) {
139 $wrappedValue = '<strong>' . htmlspecialchars($theValue) . '</strong>';
140 }
141 return $wrappedValue;
142 }
143
144 /**
145 * Wrapping the value in bold tags etc.
146 *
147 * @param string $label The title string
148 * @param string $depth Depth path
149 * @param string $theValue The value for the array entry.
150 * @return string Title string, htmlspecialchars()'ed
151 */
152 function wrapArrayKey($label, $depth, $theValue) {
153
154 // Protect label:
155 $label = htmlspecialchars($label);
156
157 // If varname is set:
158 if ($this->varName && !$this->dontLinkVar) {
159 $variableName = $this->varName . '[\'' . str_replace('.', '\'][\'', $depth) . '\'] = ' .
160 (!t3lib_utility_Math::canBeInterpretedAsInteger($theValue) ? '\'' . addslashes($theValue) . '\'' : $theValue) . '; ';
161 $label = '<a href="' . htmlspecialchars(t3lib_BEfunc::getModuleUrl(t3lib_div::_GP('M')) . '&varname=' . urlencode($variableName)) . '#varname">' . $label . '</a>';
162 }
163
164 // Return:
165 return '[' . $label . ']';
166 }
167
168 /**
169 * Creates an array with "depthKeys" which will expand the array to show the search results
170 *
171 * @param array $keyArr The array to search for the value
172 * @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]
173 * @param string $searchString The string to search for
174 * @param array $keyArray Key array, for first call pass empty array
175 * @return array
176 */
177 function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray) {
178 $c = count($keyArr);
179 if ($depth_in) {
180 $depth_in = $depth_in . '.';
181 }
182
183 foreach ($keyArr as $key => $value) {
184 $depth = $depth_in . $key;
185 $deeper = is_array($keyArr[$key]);
186
187 if ($this->regexMode) {
188 if (preg_match('/' . $searchString . '/', $keyArr[$key]) || ($this->searchKeysToo && preg_match('/' . $searchString . '/', $key))) {
189 $this->searchKeys[$depth] = 1;
190 }
191 } else {
192 if ((!$deeper && stristr($keyArr[$key], $searchString)) || ($this->searchKeysToo && stristr($key, $searchString))) {
193 $this->searchKeys[$depth] = 1;
194 }
195 }
196
197 if ($deeper) {
198 $cS = count($this->searchKeys);
199 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
200 if ($cS != count($this->searchKeys)) {
201 $keyArray[$depth] = 1;
202 }
203 }
204 }
205 return $keyArray;
206 }
207
208 /**
209 * Fixed length function
210 *
211 * @param string $string String to process
212 * @param integer $chars Max number of chars
213 * @return string Processed string
214 */
215 function fixed_lgd($string, $chars) {
216 if ($chars >= 4) {
217 if (strlen($string) > $chars) {
218 return substr($string, 0, $chars - 3) . '...';
219 }
220 }
221 return $string;
222 }
223
224 /**
225 * Function modifying the depthKey array
226 *
227 * @param array $arr Array with instructions to open/close nodes.
228 * @param array $settings Input depth_key array
229 * @return array Output depth_key array with entries added/removed based on $arr
230 * @see SC_mod_tools_config_index::main()
231 */
232 function depthKeys($arr, $settings) {
233 $tsbrArray = array();
234 foreach ($arr as $theK => $theV) {
235 $theKeyParts = explode('.', $theK);
236 $depth = '';
237 $c = count($theKeyParts);
238 $a = 0;
239 foreach ($theKeyParts as $p) {
240 $a++;
241 $depth .= ($depth ? '.' : '') . $p;
242 $tsbrArray[$depth] = ($c == $a) ? $theV : 1;
243 }
244 }
245 // Modify settings
246 foreach ($tsbrArray as $theK => $theV) {
247 if ($theV) {
248 $settings[$theK] = 1;
249 } else {
250 unset($settings[$theK]);
251 }
252 }
253 return $settings;
254 }
255 }
256
257 ?>