[TASK] Merge submodule extbase into core
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / Classes / Utility / ArrayBrowser.php
1 <?php
2 namespace TYPO3\CMS\Lowlevel\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Class for displaying an array as a tree
31 *
32 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
33 * XHTML compliant
34 *
35 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
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 * @see SC_mod_tools_config_index::main()
43 */
44 class ArrayBrowser {
45
46 /**
47 * @todo Define visibility
48 */
49 public $expAll = FALSE;
50
51 // If set, will expand all (depthKeys is obsolete then) (and no links are applied)
52 /**
53 * @todo Define visibility
54 */
55 public $dontLinkVar = FALSE;
56
57 // If set, the variable keys are not linked.
58 /**
59 * @todo Define visibility
60 */
61 public $depthKeys = array();
62
63 // Array defining which keys to expand. Typically set from outside from some session variable - otherwise the array will collapse.
64 /**
65 * @todo Define visibility
66 */
67 public $searchKeys = array();
68
69 // After calling the getSearchKeys function this array is populated with the key-positions in the array which contains values matching the search.
70 /**
71 * @todo Define visibility
72 */
73 public $fixedLgd = 1;
74
75 // If set, the values are truncated with "..." appended if longer than a certain length.
76 /**
77 * @todo Define visibility
78 */
79 public $regexMode = 0;
80
81 // If set, search for string with regex, otherwise stristr()
82 /**
83 * @todo Define visibility
84 */
85 public $searchKeysToo = FALSE;
86
87 // If set, array keys are subject to the search too.
88 /**
89 * @todo Define visibility
90 */
91 public $varName = '';
92
93 // Set var name here if you want links to the variable name.
94 /**
95 * Make browsable tree
96 * Before calling this function you may want to set some of the internal vars like depthKeys, regexMode and fixedLgd.
97 * For examples see SC_mod_tools_config_index::main()
98 *
99 * @param array $arr The array to display
100 * @param string $depth_in Key-position id. Build up during recursive calls - [key1].[key2].[key3] - an so on.
101 * @param string $depthData Depth-data - basically a prefix for the icons. For calling this function from outside, let it stay blank.
102 * @return string HTML for the tree
103 * @see SC_mod_tools_config_index::main()
104 * @todo Define visibility
105 */
106 public function tree($arr, $depth_in, $depthData) {
107 $HTML = '';
108 $a = 0;
109 if ($depth_in) {
110 $depth_in = $depth_in . '.';
111 }
112 $c = count($arr);
113 foreach ($arr as $key => $value) {
114 $a++;
115 $depth = $depth_in . $key;
116 $goto = 'a' . substr(md5($depth), 0, 6);
117 if (is_object($arr[$key])) {
118 $arr[$key] = (array) $arr[$key];
119 }
120 $isArray = is_array($arr[$key]);
121 $deeper = $isArray && ($this->depthKeys[$depth] || $this->expAll);
122 $PM = 'join';
123 $LN = $a == $c ? 'blank' : 'line';
124 $BTM = $a == $c ? 'bottom' : '';
125 $PM = $isArray ? ($deeper ? 'minus' : 'plus') : 'join';
126 $HTML .= $depthData;
127 $theIcon = '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], ('gfx/ol/' . $PM . $BTM . '.gif'), 'width="18" height="16"') . ' align="top" border="0" alt="" />';
128 if ($PM == 'join') {
129 $HTML .= $theIcon;
130 } else {
131 $HTML .= ($this->expAll ? '' : '<a id="' . $goto . '" href="' . htmlspecialchars((\TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('M')) . '&node[' . $depth . ']=' . ($deeper ? 0 : 1) . '#' . $goto)) . '">') . $theIcon . ($this->expAll ? '' : '</a>');
132 }
133 $label = $key;
134 $HTML .= $this->wrapArrayKey($label, $depth, !$isArray ? $arr[$key] : '');
135 if (!$isArray) {
136 $theValue = $arr[$key];
137 if ($this->fixedLgd) {
138 $imgBlocks = ceil(1 + strlen($depthData) / 77);
139 $lgdChars = 68 - ceil(strlen(('[' . $key . ']')) * 0.8) - $imgBlocks * 3;
140 $theValue = $this->fixed_lgd($theValue, $lgdChars);
141 }
142 if ($this->searchKeys[$depth]) {
143 $HTML .= '=<span style="color:red;">' . $this->wrapValue($theValue, $depth) . '</span>';
144 } else {
145 $HTML .= '=' . $this->wrapValue($theValue, $depth);
146 }
147 }
148 $HTML .= '<br />';
149 if ($deeper) {
150 $HTML .= $this->tree($arr[$key], $depth, $depthData . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], ('gfx/ol/' . $LN . '.gif'), 'width="18" height="16"') . ' align="top" alt="" />');
151 }
152 }
153 return $HTML;
154 }
155
156 /**
157 * Wrapping the value in bold tags etc.
158 *
159 * @param string $theValue The title string
160 * @param string $depth Depth path
161 * @return string Title string, htmlspecialchars()'ed
162 * @todo Define visibility
163 */
164 public function wrapValue($theValue, $depth) {
165 $wrappedValue = '';
166 if (strlen($theValue) > 0) {
167 $wrappedValue = '<strong>' . htmlspecialchars($theValue) . '</strong>';
168 }
169 return $wrappedValue;
170 }
171
172 /**
173 * Wrapping the value in bold tags etc.
174 *
175 * @param string $label The title string
176 * @param string $depth Depth path
177 * @param string $theValue The value for the array entry.
178 * @return string Title string, htmlspecialchars()'ed
179 * @todo Define visibility
180 */
181 public function wrapArrayKey($label, $depth, $theValue) {
182 // Protect label:
183 $label = htmlspecialchars($label);
184 // If varname is set:
185 if ($this->varName && !$this->dontLinkVar) {
186 $variableName = $this->varName . '[\'' . str_replace('.', '\'][\'', $depth) . '\'] = ' . (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($theValue) ? '\'' . addslashes($theValue) . '\'' : $theValue) . '; ';
187 $label = '<a href="' . htmlspecialchars((\TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('M')) . '&varname=' . urlencode($variableName))) . '#varname">' . $label . '</a>';
188 }
189 // Return:
190 return '[' . $label . ']';
191 }
192
193 /**
194 * Creates an array with "depthKeys" which will expand the array to show the search results
195 *
196 * @param array $keyArr The array to search for the value
197 * @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]
198 * @param string $searchString The string to search for
199 * @param array $keyArray Key array, for first call pass empty array
200 * @return array
201 * @todo Define visibility
202 */
203 public function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray) {
204 $c = count($keyArr);
205 if ($depth_in) {
206 $depth_in = $depth_in . '.';
207 }
208 foreach ($keyArr as $key => $value) {
209 $depth = $depth_in . $key;
210 $deeper = is_array($keyArr[$key]);
211 if ($this->regexMode) {
212 if (preg_match('/' . $searchString . '/', $keyArr[$key]) || $this->searchKeysToo && preg_match('/' . $searchString . '/', $key)) {
213 $this->searchKeys[$depth] = 1;
214 }
215 } else {
216 if (!$deeper && stristr($keyArr[$key], $searchString) || $this->searchKeysToo && stristr($key, $searchString)) {
217 $this->searchKeys[$depth] = 1;
218 }
219 }
220 if ($deeper) {
221 $cS = count($this->searchKeys);
222 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
223 if ($cS != count($this->searchKeys)) {
224 $keyArray[$depth] = 1;
225 }
226 }
227 }
228 return $keyArray;
229 }
230
231 /**
232 * Fixed length function
233 *
234 * @param string $string String to process
235 * @param integer $chars Max number of chars
236 * @return string Processed string
237 * @todo Define visibility
238 */
239 public function fixed_lgd($string, $chars) {
240 if ($chars >= 4) {
241 if (strlen($string) > $chars) {
242 return substr($string, 0, ($chars - 3)) . '...';
243 }
244 }
245 return $string;
246 }
247
248 /**
249 * Function modifying the depthKey array
250 *
251 * @param array $arr Array with instructions to open/close nodes.
252 * @param array $settings Input depth_key array
253 * @return array Output depth_key array with entries added/removed based on $arr
254 * @see SC_mod_tools_config_index::main()
255 * @todo Define visibility
256 */
257 public function depthKeys($arr, $settings) {
258 $tsbrArray = array();
259 foreach ($arr as $theK => $theV) {
260 $theKeyParts = explode('.', $theK);
261 $depth = '';
262 $c = count($theKeyParts);
263 $a = 0;
264 foreach ($theKeyParts as $p) {
265 $a++;
266 $depth .= ($depth ? '.' : '') . $p;
267 $tsbrArray[$depth] = $c == $a ? $theV : 1;
268 }
269 }
270 // Modify settings
271 foreach ($tsbrArray as $theK => $theV) {
272 if ($theV) {
273 $settings[$theK] = 1;
274 } else {
275 unset($settings[$theK]);
276 }
277 }
278 return $settings;
279 }
280
281 }
282
283
284 ?>