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