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