2 namespace TYPO3\CMS\Lowlevel\Utility
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Core\Utility\GeneralUtility
;
18 use TYPO3\CMS\Core\Utility\MathUtility
;
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
30 public $expAll = false
;
33 * If set, will expand all (depthKeys is obsolete then) (and no links are applied)
37 public $dontLinkVar = false
;
40 * If set, the variable keys are not linked.
44 public $depthKeys = [];
47 * Array defining which keys to expand. Typically set from outside from some session
48 * variable - otherwise the array will collapse.
52 public $searchKeys = [];
55 * After calling the getSearchKeys function this array is populated with the
56 * key-positions in the array which contains values matching the search.
63 * If set, the values are truncated with "..." appended if longer than a certain
68 public $regexMode = 0;
71 * If set, search for string with regex, otherwise stristr()
75 public $searchKeysToo = false
;
78 * If set, array keys are subject to the search too.
85 * Set var name here if you want links to the variable name.
88 * Before calling this function you may want to set some of the internal vars like
89 * depthKeys, regexMode and fixedLgd.
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
95 public function tree($array, $positionKey)
97 $output = '<ul class="list-tree text-monospace">';
99 $positionKey = $positionKey . '.';
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;
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> ';
117 $output .= $this->wrapArrayKey($key, $depth, !$isArray ?
$value : '');
119 $output .= ' = <span class="list-tree-value">' . htmlspecialchars($value) . '</span>';
121 $output .= '</span>';
123 $output .= $this->tree(
135 * Wrapping the value in bold tags etc.
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
142 public function wrapArrayKey($label, $depth, $theValue)
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>';
159 return '<span class="list-tree-label">' . $label . '</span>';
163 * Creates an array with "depthKeys" which will expand the array to show the search results
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
172 public function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
175 $depth_in = $depth_in . '.';
177 foreach ($keyArr as $key => $value) {
178 $depth = $depth_in . $key;
179 $deeper = is_array($keyArr[$key]);
180 if ($this->regexMode
) {
182 is_scalar($keyArr[$key]) && preg_match('/' . $searchString . '/', $keyArr[$key])
183 ||
$this->searchKeysToo
&& preg_match('/' . $searchString . '/', $key)
185 $this->searchKeys
[$depth] = 1;
189 is_scalar($keyArr[$key]) && stripos($keyArr[$key], $searchString) !== false
190 ||
$this->searchKeysToo
&& stripos($key, $searchString) !== false
192 $this->searchKeys
[$depth] = 1;
196 $cS = count($this->searchKeys
);
197 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
198 if ($cS != count($this->searchKeys
)) {
199 $keyArray[$depth] = 1;
207 * Function modifying the depthKey array
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
213 public function depthKeys($arr, $settings)
216 foreach ($arr as $theK => $theV) {
217 $theKeyParts = explode('.', $theK);
219 $c = count($theKeyParts);
221 foreach ($theKeyParts as $p) {
223 $depth .= ($depth ?
'.' : '') . $p;
224 $tsbrArray[$depth] = $c == $a ?
$theV : 1;
228 foreach ($tsbrArray as $theK => $theV) {
230 $settings[$theK] = 1;
232 unset($settings[$theK]);