‪TYPO3CMS  ‪main
ArrayBrowser.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
25 
33 {
37  public ‪$expAll = false;
38 
44  public ‪$depthKeys = [];
45 
52  public ‪$searchKeys = [];
53 
60  public ‪$regexMode = false;
61 
67  public ‪$searchKeysToo = true;
68 
72  protected ‪$uriBuilder;
73 
79  protected ‪$route;
80 
81  public function ‪__construct(‪Route ‪$route = null)
82  {
83  $this->route = ‪$route;
84  $this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
85  }
86 
98  public function ‪tree($array, $positionKey)
99  {
100  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
101 
102  ‪$output = '<ul class="list-tree text-monospace">';
103  if ($positionKey) {
104  $positionKey .= '.';
105  }
106  foreach ($array as $key => $value) {
107  $key = (string)$key;
108  $depth = $positionKey . $key;
109  if (is_object($value) && !$value instanceof \Traversable) {
110  $value = (array)$value;
111  }
112  $isArray = is_iterable($value);
113  $isResult = (bool)($this->searchKeys[$depth] ?? false);
114  $isExpanded = $isArray && (!empty($this->‪depthKeys[$depth]) || ‪$this->expAll);
115  ‪$output .= '<li' . ($isResult ? ' class="active"' : '') . '>';
116  ‪$output .= '<span class="list-tree-group">';
117  if ($isArray && !$this->expAll && $this->route) {
118  if ($isExpanded) {
119  $class = 'list-tree-control-open';
120  $icon = $iconFactory->getIcon('actions-caret-down', ‪Icon::SIZE_SMALL);
121  } else {
122  $class = 'list-tree-control-closed';
123  $icon = $iconFactory->getIcon('actions-caret-right', ‪Icon::SIZE_SMALL);
124  }
125 
126  $goto = 'a' . substr(md5($depth), 0, 6);
127  ‪$output .= '<a class="list-tree-control ' . $class . '" id="' . $goto . '" href="' . htmlspecialchars((string)$this->uriBuilder->buildUriFromRoute($this->route->getOption('_identifier'), ['node' => [rawurldecode($depth) => $isExpanded ? 0 : 1]]) . '#' . $goto) . '">' . $icon->render() . '</a> ';
128  }
129  ‪$output .= '<span class="list-tree-label">' . htmlspecialchars((string)$key) . '</span>';
130  if (!$isArray) {
131  ‪$output .= ' = <span class="list-tree-value">' . htmlspecialchars((string)$value) . '</span>';
132  }
133  ‪$output .= '</span>';
134  if ($isExpanded) {
135  ‪$output .= $this->‪tree(
136  $value,
137  $depth
138  );
139  }
140  ‪$output .= '</li>';
141  }
142  ‪$output .= '</ul>';
143  return ‪$output;
144  }
145 
156  public function ‪getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
157  {
158  if ($depth_in) {
159  $depth_in .= '.';
160  }
161  foreach ($keyArr as $key => $value) {
162  $depth = $depth_in . $key;
163  $deeper = is_array($keyArr[$key]);
164  if ($this->regexMode) {
165  if (
166  is_scalar($keyArr[$key]) && preg_match('/' . $searchString . '/', (string)$keyArr[$key])
167  || $this->searchKeysToo && preg_match('/' . $searchString . '/', (string)$key)
168  ) {
169  $this->searchKeys[$depth] = 1;
170  }
171  } else {
172  if (
173  is_scalar($keyArr[$key]) && stripos((string)$keyArr[$key], $searchString) !== false
174  || $this->searchKeysToo && stripos((string)$key, $searchString) !== false
175  ) {
176  $this->searchKeys[$depth] = 1;
177  }
178  }
179  if ($deeper) {
180  $cS = count($this->searchKeys);
181  $keyArray = $this->‪getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
182  if ($cS != count($this->searchKeys)) {
183  $keyArray[$depth] = 1;
184  }
185  }
186  }
187  return $keyArray;
188  }
189 
197  public function ‪depthKeys($arr, $settings)
198  {
199  $tsbrArray = [];
200  foreach ($arr as $theK => $theV) {
201  $theKeyParts = explode('.', (string)$theK);
202  $depth = '';
203  $c = count($theKeyParts);
204  $a = 0;
205  foreach ($theKeyParts as $p) {
206  $a++;
207  $depth .= ($depth ? '.' : '') . $p;
208  $tsbrArray[$depth] = $c == $a ? $theV : 1;
209  }
210  }
211  // Modify settings
212  foreach ($tsbrArray as $theK => $theV) {
213  if ($theV) {
214  $settings[$theK] = 1;
215  } else {
216  unset($settings[$theK]);
217  }
218  }
219  return $settings;
220  }
221 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:35
‪TYPO3\CMS\Backend\View\ArrayBrowser\$searchKeys
‪array $searchKeys
Definition: ArrayBrowser.php:49
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\View\ArrayBrowser\depthKeys
‪array depthKeys($arr, $settings)
Definition: ArrayBrowser.php:190
‪TYPO3\CMS\Backend\View
Definition: ArrayBrowser.php:18
‪TYPO3\CMS\Backend\View\ArrayBrowser\$expAll
‪bool $expAll
Definition: ArrayBrowser.php:36
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\View\ArrayBrowser\$route
‪Route null $route
Definition: ArrayBrowser.php:72
‪TYPO3\CMS\Backend\View\ArrayBrowser\$depthKeys
‪array $depthKeys
Definition: ArrayBrowser.php:42
‪TYPO3\CMS\Backend\View\ArrayBrowser
Definition: ArrayBrowser.php:33
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:42
‪TYPO3\CMS\Backend\View\ArrayBrowser\tree
‪string tree($array, $positionKey)
Definition: ArrayBrowser.php:91
‪TYPO3\CMS\Backend\View\ArrayBrowser\__construct
‪__construct(Route $route=null)
Definition: ArrayBrowser.php:74
‪TYPO3\CMS\Backend\View\ArrayBrowser\$regexMode
‪bool $regexMode
Definition: ArrayBrowser.php:56
‪$output
‪$output
Definition: annotationChecker.php:117
‪TYPO3\CMS\Backend\View\ArrayBrowser\$searchKeysToo
‪bool $searchKeysToo
Definition: ArrayBrowser.php:62
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Backend\View\ArrayBrowser\getSearchKeys
‪array getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
Definition: ArrayBrowser.php:149
‪TYPO3\CMS\Backend\View\ArrayBrowser\$uriBuilder
‪UriBuilder $uriBuilder
Definition: ArrayBrowser.php:66