[BUGFIX] System>Configuration: Fix search in
[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 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\MathUtility;
20
21 /**
22 * Class for displaying an array as a tree
23 * See the extension 'lowlevel' /config (Backend module 'Tools > Configuration')
24 *
25 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
26 */
27 class ArrayBrowser {
28
29 /**
30 * @var bool
31 */
32 public $expAll = FALSE;
33
34 /**
35 * If set, will expand all (depthKeys is obsolete then) (and no links are applied)
36 *
37 * @var bool
38 */
39 public $dontLinkVar = FALSE;
40
41 /**
42 * If set, the variable keys are not linked.
43 *
44 * @var array
45 */
46 public $depthKeys = array();
47
48 /**
49 * Array defining which keys to expand. Typically set from outside from some session
50 * variable - otherwise the array will collapse.
51 *
52 * @var array
53 */
54 public $searchKeys = array();
55
56 /**
57 * After calling the getSearchKeys function this array is populated with the
58 * key-positions in the array which contains values matching the search.
59 *
60 * @var int
61 */
62 public $fixedLgd = 1;
63
64 /**
65 * If set, the values are truncated with "..." appended if longer than a certain
66 * length.
67 *
68 * @var int
69 */
70 public $regexMode = 0;
71
72 /**
73 * If set, search for string with regex, otherwise stristr()
74 *
75 * @var bool
76 */
77 public $searchKeysToo = FALSE;
78
79 /**
80 * If set, array keys are subject to the search too.
81 *
82 * @var string
83 */
84 public $varName = '';
85
86 /**
87 * Set var name here if you want links to the variable name.
88 *
89 * Make browseable tree
90 * Before calling this function you may want to set some of the internal vars like
91 * depthKeys, regexMode and fixedLgd.
92 *
93 * @param array $array The array to display
94 * @param string $positionKey Key-position id. Build up during recursive calls - [key1].[key2].[key3] - an so on.
95 * @param string $depthData is deprecated since TYPO3 CMS 7, and will be removed with CMS 8
96 * @return string HTML for the tree
97 */
98 public function tree($array, $positionKey, $depthData = NULL) {
99 if ($depthData) {
100 GeneralUtility::deprecationLog('ArrayBrowser::tree parameter $depthData is deprecated since TYPO3 CMS 7 and is not used anymore. Please remove the parameter.');
101 }
102 $output = '<ul class="list-tree text-monospace">';
103 if ($positionKey) {
104 $positionKey = $positionKey . '.';
105 }
106 foreach ($array as $key => $value) {
107 $depth = $positionKey . $key;
108 if (is_object($value) && !$value instanceof \Traversable) {
109 $value = (array)$value;
110 }
111 $isArray = is_array($value) || $value instanceof \Traversable;
112 $isResult = (bool)$this->searchKeys[$depth];
113 $isExpanded = $isArray && ($this->depthKeys[$depth] || $this->expAll);
114 $output .= '<li' . ($isResult ? ' class="active"' : '') . '>';
115 if ($isArray && !$this->expAll) {
116 $goto = 'a' . substr(md5($depth), 0, 6);
117 $output .= '<a class="list-tree-control' . ($isExpanded ? ' list-tree-control-open' : ' list-tree-control-closed') . '" id="' . $goto . '" href="' . htmlspecialchars((BackendUtility::getModuleUrl(GeneralUtility::_GP('M')) . '&node[' . $depth . ']=' . ($isExpanded ? 0 : 1) . '#' . $goto)) . '"><i class="fa"></i></a> ';
118 }
119 $output .= '<span class="list-tree-group">';
120 $output .= $this->wrapArrayKey($key, $depth, !$isArray ? $value : '');
121 if (!$isArray) {
122 $output .= ' = <span class="list-tree-value">' . $this->wrapValue($value) . '</span>';
123 }
124 $output .= '</span>';
125 if ($isExpanded) {
126 $output .= $this->tree(
127 $value,
128 $depth
129 );
130 }
131 $output .= '</li>';
132 }
133 $output .= '</ul>';
134 return $output;
135 }
136
137 /**
138 * Wrapping the value in bold tags etc.
139 *
140 * @param string $theValue The title string
141 * @return string Title string, htmlspecialchars()'ed
142 */
143 public function wrapValue($theValue) {
144 $wrappedValue = '';
145 if ((string)$theValue !== '') {
146 $wrappedValue = htmlspecialchars($theValue);
147 }
148 return $wrappedValue;
149 }
150
151 /**
152 * Wrapping the value in bold tags etc.
153 *
154 * @param string $label The title string
155 * @param string $depth Depth path
156 * @param string $theValue The value for the array entry.
157 * @return string Title string, htmlspecialchars()'ed
158 */
159 public function wrapArrayKey($label, $depth, $theValue) {
160 // Protect label:
161 $label = htmlspecialchars($label);
162
163 // If varname is set:
164 if ($this->varName && !$this->dontLinkVar) {
165 $variableName = $this->varName
166 . '[\'' . str_replace('.', '\'][\'', $depth) . '\'] = '
167 . (!MathUtility::canBeInterpretedAsInteger($theValue) ? '\''
168 . addslashes($theValue) . '\'' : $theValue) . '; ';
169 $label = '<a class="list-tree-label" href="'
170 . htmlspecialchars((BackendUtility::getModuleUrl(GeneralUtility::_GP('M'))
171 . '&varname=' . urlencode($variableName)))
172 . '#varname">' . $label . '</a>';
173 }
174
175 return $label;
176 }
177
178 /**
179 * Creates an array with "depthKeys" which will expand the array to show the search results
180 *
181 * @param array $keyArr The array to search for the value
182 * @param string $depth_in Depth string - blank for first call (will build up during recursive calling creating
183 * an id of the position: [key1].[key2].[key3]
184 * @param string $searchString The string to search for
185 * @param array $keyArray Key array, for first call pass empty array
186 * @return array
187 */
188 public function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray) {
189 if ($depth_in) {
190 $depth_in = $depth_in . '.';
191 }
192 foreach ($keyArr as $key => $value) {
193 $depth = $depth_in . $key;
194 $deeper = is_array($keyArr[$key]);
195 if ($this->regexMode) {
196 if (
197 is_scalar($keyArr[$key]) && preg_match('/' . $searchString . '/', $keyArr[$key])
198 || $this->searchKeysToo && preg_match('/' . $searchString . '/', $key)
199 ) {
200 $this->searchKeys[$depth] = 1;
201 }
202 } else {
203 if (
204 is_scalar($keyArr[$key]) && stristr($keyArr[$key], $searchString)
205 || $this->searchKeysToo && stristr($key, $searchString)
206 ) {
207 $this->searchKeys[$depth] = 1;
208 }
209 }
210 if ($deeper) {
211 $cS = count($this->searchKeys);
212 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
213 if ($cS != count($this->searchKeys)) {
214 $keyArray[$depth] = 1;
215 }
216 }
217 }
218 return $keyArray;
219 }
220
221 /**
222 * Function modifying the depthKey array
223 *
224 * @param array $arr Array with instructions to open/close nodes.
225 * @param array $settings Input depth_key array
226 * @return array Output depth_key array with entries added/removed based on $arr
227 */
228 public function depthKeys($arr, $settings) {
229 $tsbrArray = array();
230 foreach ($arr as $theK => $theV) {
231 $theKeyParts = explode('.', $theK);
232 $depth = '';
233 $c = count($theKeyParts);
234 $a = 0;
235 foreach ($theKeyParts as $p) {
236 $a++;
237 $depth .= ($depth ? '.' : '') . $p;
238 $tsbrArray[$depth] = $c == $a ? $theV : 1;
239 }
240 }
241 // Modify settings
242 foreach ($tsbrArray as $theK => $theV) {
243 if ($theV) {
244 $settings[$theK] = 1;
245 } else {
246 unset($settings[$theK]);
247 }
248 }
249 return $settings;
250 }
251
252 }