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