[CLEANUP] Replace strlen() with === for zero length check
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / Classes / Utility / ArrayBrowser.php
1 <?php
2 namespace TYPO3\CMS\Lowlevel\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Class for displaying an array as a tree
19 * See the extension 'lowlevel' /config (Backend module 'Tools > Configuration')
20 *
21 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
22 * @see \TYPO3\CMS\Lowlevel\Utility\ArrayBrowser::main()
23 */
24 class ArrayBrowser {
25
26 /**
27 * @var bool
28 */
29 public $expAll = FALSE;
30
31 // If set, will expand all (depthKeys is obsolete then) (and no links are applied)
32 /**
33 * @var bool
34 */
35 public $dontLinkVar = FALSE;
36
37 // If set, the variable keys are not linked.
38 /**
39 * @var array
40 */
41 public $depthKeys = array();
42
43 // Array defining which keys to expand. Typically set from outside from some session variable - otherwise the array will collapse.
44 /**
45 * @var array
46 */
47 public $searchKeys = array();
48
49 // After calling the getSearchKeys function this array is populated with the key-positions in the array which contains values matching the search.
50 /**
51 * @var int
52 */
53 public $fixedLgd = 1;
54
55 // If set, the values are truncated with "..." appended if longer than a certain length.
56 /**
57 * @var int
58 */
59 public $regexMode = 0;
60
61 // If set, search for string with regex, otherwise stristr()
62 /**
63 * @var bool
64 */
65 public $searchKeysToo = FALSE;
66
67 // If set, array keys are subject to the search too.
68 /**
69 * @var string
70 */
71 public $varName = '';
72
73 // Set var name here if you want links to the variable name.
74 /**
75 * Make browsable tree
76 * Before calling this function you may want to set some of the internal vars like depthKeys, regexMode and fixedLgd.
77 * For examples see \TYPO3\CMS\Lowlevel\Utility\ArrayBrowser::main()
78 *
79 * @param array $arr The array to display
80 * @param string $depth_in Key-position id. Build up during recursive calls - [key1].[key2].[key3] - an so on.
81 * @param string $depthData Depth-data - basically a prefix for the icons. For calling this function from outside, let it stay blank.
82 * @return string HTML for the tree
83 * @see \TYPO3\CMS\Lowlevel\Utility\ArrayBrowser::main()
84 */
85 public function tree($arr, $depth_in, $depthData) {
86 $HTML = '';
87 $a = 0;
88 if ($depth_in) {
89 $depth_in = $depth_in . '.';
90 }
91 $c = count($arr);
92 foreach ($arr as $key => $value) {
93 $a++;
94 $depth = $depth_in . $key;
95 $goto = 'a' . substr(md5($depth), 0, 6);
96 if (is_object($value) && !$value instanceof \Traversable) {
97 $value = (array)$value;
98 }
99 $isArray = is_array($value) || $value instanceof \Traversable;
100 $deeper = $isArray && ($this->depthKeys[$depth] || $this->expAll);
101 $LN = $a == $c ? 'blank' : 'line';
102 $BTM = $a == $c ? 'bottom' : '';
103 $PM = $isArray ? ($deeper ? 'minus' : 'plus') : 'join';
104 $HTML .= $depthData;
105 $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="" />';
106 if ($PM == 'join') {
107 $HTML .= $theIcon;
108 } else {
109 $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>');
110 }
111 $label = $key;
112 $HTML .= $this->wrapArrayKey($label, $depth, !$isArray ? $value : '');
113 if (!$isArray) {
114 $theValue = $value;
115 if ($this->searchKeys[$depth]) {
116 $HTML .= ' = <span style="color:red;">' . $this->wrapValue($theValue, $depth) . '</span>';
117 } else {
118 $HTML .= ' = ' . $this->wrapValue($theValue, $depth);
119 }
120 }
121 $HTML .= '<br />';
122 if ($deeper) {
123 $HTML .= $this->tree($value, $depth, $depthData . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], ('gfx/ol/' . $LN . '.gif'), 'width="18" height="16"') . ' 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 public function wrapValue($theValue, $depth) {
137 $wrappedValue = '';
138 if ((string)$theValue !== '') {
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 public function wrapArrayKey($label, $depth, $theValue) {
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) . '\'] = ' . (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($theValue) ? '\'' . addslashes($theValue) . '\'' : $theValue) . '; ';
159 $label = '<a href="' . htmlspecialchars((\TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('M')) . '&varname=' . urlencode($variableName))) . '#varname">' . $label . '</a>';
160 }
161
162 return $label;
163 }
164
165 /**
166 * Creates an array with "depthKeys" which will expand the array to show the search results
167 *
168 * @param array $keyArr The array to search for the value
169 * @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]
170 * @param string $searchString The string to search for
171 * @param array $keyArray Key array, for first call pass empty array
172 * @return array
173 */
174 public function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray) {
175 $c = count($keyArr);
176 if ($depth_in) {
177 $depth_in = $depth_in . '.';
178 }
179 foreach ($keyArr as $key => $value) {
180 $depth = $depth_in . $key;
181 $deeper = is_array($keyArr[$key]);
182 if ($this->regexMode) {
183 if (preg_match('/' . $searchString . '/', $keyArr[$key]) || $this->searchKeysToo && preg_match('/' . $searchString . '/', $key)) {
184 $this->searchKeys[$depth] = 1;
185 }
186 } else {
187 if (!$deeper && stristr($keyArr[$key], $searchString) || $this->searchKeysToo && stristr($key, $searchString)) {
188 $this->searchKeys[$depth] = 1;
189 }
190 }
191 if ($deeper) {
192 $cS = count($this->searchKeys);
193 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
194 if ($cS != count($this->searchKeys)) {
195 $keyArray[$depth] = 1;
196 }
197 }
198 }
199 return $keyArray;
200 }
201
202 /**
203 * Function modifying the depthKey array
204 *
205 * @param array $arr Array with instructions to open/close nodes.
206 * @param array $settings Input depth_key array
207 * @return array Output depth_key array with entries added/removed based on $arr
208 * @see \TYPO3\CMS\Lowlevel\Utility\ArrayBrowser::main()
209 */
210 public function depthKeys($arr, $settings) {
211 $tsbrArray = array();
212 foreach ($arr as $theK => $theV) {
213 $theKeyParts = explode('.', $theK);
214 $depth = '';
215 $c = count($theKeyParts);
216 $a = 0;
217 foreach ($theKeyParts as $p) {
218 $a++;
219 $depth .= ($depth ? '.' : '') . $p;
220 $tsbrArray[$depth] = $c == $a ? $theV : 1;
221 }
222 }
223 // Modify settings
224 foreach ($tsbrArray as $theK => $theV) {
225 if ($theV) {
226 $settings[$theK] = 1;
227 } else {
228 unset($settings[$theK]);
229 }
230 }
231 return $settings;
232 }
233
234 }