[!!!][TASK] Remove more deprecated miscellaneous functions and options
[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 = array();
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 = array();
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('M')) . '&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">' . $this->wrapValue($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 $theValue The title string
136 * @return string Title string, htmlspecialchars()'ed
137 */
138 public function wrapValue($theValue)
139 {
140 $wrappedValue = '';
141 if ((string)$theValue !== '') {
142 $wrappedValue = htmlspecialchars($theValue);
143 }
144 return $wrappedValue;
145 }
146
147 /**
148 * Wrapping the value in bold tags etc.
149 *
150 * @param string $label The title string
151 * @param string $depth Depth path
152 * @param string $theValue The value for the array entry.
153 * @return string Title string, htmlspecialchars()'ed
154 */
155 public function wrapArrayKey($label, $depth, $theValue)
156 {
157 // Protect label:
158 $label = htmlspecialchars($label);
159
160 // If varname is set:
161 if ($this->varName && !$this->dontLinkVar) {
162 $variableName = $this->varName
163 . '[\'' . str_replace('.', '\'][\'', $depth) . '\'] = '
164 . (!MathUtility::canBeInterpretedAsInteger($theValue) ? '\''
165 . addslashes($theValue) . '\'' : $theValue) . '; ';
166 $label = '<a class="list-tree-label" href="'
167 . htmlspecialchars((BackendUtility::getModuleUrl(GeneralUtility::_GP('M'))
168 . '&varname=' . urlencode($variableName)))
169 . '#varname">' . $label . '</a>';
170 }
171 return '<span class="list-tree-label">' . $label . '</span>';
172 }
173
174 /**
175 * Creates an array with "depthKeys" which will expand the array to show the search results
176 *
177 * @param array $keyArr The array to search for the value
178 * @param string $depth_in Depth string - blank for first call (will build up during recursive calling creating
179 * an id of the position: [key1].[key2].[key3]
180 * @param string $searchString The string to search for
181 * @param array $keyArray Key array, for first call pass empty array
182 * @return array
183 */
184 public function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
185 {
186 if ($depth_in) {
187 $depth_in = $depth_in . '.';
188 }
189 foreach ($keyArr as $key => $value) {
190 $depth = $depth_in . $key;
191 $deeper = is_array($keyArr[$key]);
192 if ($this->regexMode) {
193 if (
194 is_scalar($keyArr[$key]) && preg_match('/' . $searchString . '/', $keyArr[$key])
195 || $this->searchKeysToo && preg_match('/' . $searchString . '/', $key)
196 ) {
197 $this->searchKeys[$depth] = 1;
198 }
199 } else {
200 if (
201 is_scalar($keyArr[$key]) && stristr($keyArr[$key], $searchString)
202 || $this->searchKeysToo && stristr($key, $searchString)
203 ) {
204 $this->searchKeys[$depth] = 1;
205 }
206 }
207 if ($deeper) {
208 $cS = count($this->searchKeys);
209 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
210 if ($cS != count($this->searchKeys)) {
211 $keyArray[$depth] = 1;
212 }
213 }
214 }
215 return $keyArray;
216 }
217
218 /**
219 * Function modifying the depthKey array
220 *
221 * @param array $arr Array with instructions to open/close nodes.
222 * @param array $settings Input depth_key array
223 * @return array Output depth_key array with entries added/removed based on $arr
224 */
225 public function depthKeys($arr, $settings)
226 {
227 $tsbrArray = array();
228 foreach ($arr as $theK => $theV) {
229 $theKeyParts = explode('.', $theK);
230 $depth = '';
231 $c = count($theKeyParts);
232 $a = 0;
233 foreach ($theKeyParts as $p) {
234 $a++;
235 $depth .= ($depth ? '.' : '') . $p;
236 $tsbrArray[$depth] = $c == $a ? $theV : 1;
237 }
238 }
239 // Modify settings
240 foreach ($tsbrArray as $theK => $theV) {
241 if ($theV) {
242 $settings[$theK] = 1;
243 } else {
244 unset($settings[$theK]);
245 }
246 }
247 return $settings;
248 }
249 }