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