a7051053c318fbe80b1c461f943663cc0efea37f
[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 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 browseable 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] - an 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 foreach ($array as $key => $value) {
102 $depth = $positionKey . $key;
103 if (is_object($value) && !$value instanceof \Traversable) {
104 $value = (array)$value;
105 }
106 $isArray = is_array($value) || $value instanceof \Traversable;
107 $isResult = (bool)$this->searchKeys[$depth];
108 $isExpanded = $isArray && ($this->depthKeys[$depth] || $this->expAll);
109 $output .= '<li' . ($isResult ? ' class="active"' : '') . '>';
110 if ($isArray && !$this->expAll) {
111 $goto = 'a' . substr(md5($depth), 0, 6);
112 $output .= '<a class="list-tree-control' . ($isExpanded ? ' list-tree-control-open' : ' list-tree-control-closed') . '" id="' . $goto . '" href="' . htmlspecialchars((BackendUtility::getModuleUrl(GeneralUtility::_GP('route')) . '&node[' . $depth . ']=' . ($isExpanded ? 0 : 1) . '#' . $goto)) . '"><i class="fa"></i></a> ';
113 }
114 $output .= '<span class="list-tree-group">';
115 $output .= $this->wrapArrayKey($key, $depth, !$isArray ? $value : '');
116 if (!$isArray) {
117 $output .= ' = <span class="list-tree-value">' . htmlspecialchars($value) . '</span>';
118 }
119 $output .= '</span>';
120 if ($isExpanded) {
121 $output .= $this->tree(
122 $value,
123 $depth
124 );
125 }
126 $output .= '</li>';
127 }
128 $output .= '</ul>';
129 return $output;
130 }
131
132 /**
133 * Wrapping the value in bold tags etc.
134 *
135 * @param string $label The title string
136 * @param string $depth Depth path
137 * @param string $theValue The value for the array entry.
138 * @return string Title string, htmlspecialchars()'ed
139 */
140 public function wrapArrayKey($label, $depth, $theValue)
141 {
142 // Protect label:
143 $label = htmlspecialchars($label);
144
145 // If varname is set:
146 if ($this->varName && !$this->dontLinkVar) {
147 $variableName = $this->varName
148 . '[\'' . str_replace('.', '\'][\'', $depth) . '\'] = '
149 . (!MathUtility::canBeInterpretedAsInteger($theValue) ? '\''
150 . addslashes($theValue) . '\'' : $theValue) . '; ';
151 $label = '<a class="list-tree-label" href="'
152 . htmlspecialchars((BackendUtility::getModuleUrl(GeneralUtility::_GP('route'))
153 . '&varname=' . urlencode($variableName)))
154 . '#varname">' . $label . '</a>';
155 }
156 return '<span class="list-tree-label">' . $label . '</span>';
157 }
158
159 /**
160 * Creates an array with "depthKeys" which will expand the array to show the search results
161 *
162 * @param array $keyArr The array to search for the value
163 * @param string $depth_in Depth string - blank for first call (will build up during recursive calling creating
164 * an id of the position: [key1].[key2].[key3]
165 * @param string $searchString The string to search for
166 * @param array $keyArray Key array, for first call pass empty array
167 * @return array
168 */
169 public function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
170 {
171 if ($depth_in) {
172 $depth_in = $depth_in . '.';
173 }
174 foreach ($keyArr as $key => $value) {
175 $depth = $depth_in . $key;
176 $deeper = is_array($keyArr[$key]);
177 if ($this->regexMode) {
178 if (
179 is_scalar($keyArr[$key]) && preg_match('/' . $searchString . '/', $keyArr[$key])
180 || $this->searchKeysToo && preg_match('/' . $searchString . '/', $key)
181 ) {
182 $this->searchKeys[$depth] = 1;
183 }
184 } else {
185 if (
186 is_scalar($keyArr[$key]) && stristr($keyArr[$key], $searchString)
187 || $this->searchKeysToo && stristr($key, $searchString)
188 ) {
189 $this->searchKeys[$depth] = 1;
190 }
191 }
192 if ($deeper) {
193 $cS = count($this->searchKeys);
194 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
195 if ($cS != count($this->searchKeys)) {
196 $keyArray[$depth] = 1;
197 }
198 }
199 }
200 return $keyArray;
201 }
202
203 /**
204 * Function modifying the depthKey array
205 *
206 * @param array $arr Array with instructions to open/close nodes.
207 * @param array $settings Input depth_key array
208 * @return array Output depth_key array with entries added/removed based on $arr
209 */
210 public function depthKeys($arr, $settings)
211 {
212 $tsbrArray = [];
213 foreach ($arr as $theK => $theV) {
214 $theKeyParts = explode('.', $theK);
215 $depth = '';
216 $c = count($theKeyParts);
217 $a = 0;
218 foreach ($theKeyParts as $p) {
219 $a++;
220 $depth .= ($depth ? '.' : '') . $p;
221 $tsbrArray[$depth] = $c == $a ? $theV : 1;
222 }
223 }
224 // Modify settings
225 foreach ($tsbrArray as $theK => $theV) {
226 if ($theV) {
227 $settings[$theK] = 1;
228 } else {
229 unset($settings[$theK]);
230 }
231 }
232 return $settings;
233 }
234 }