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