‪TYPO3CMS  9.5
ArrayBrowser.php
Go to the documentation of this file.
1 <?php
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 
19 
26 {
30  public ‪$expAll = false;
31 
37  public ‪$dontLinkVar = false;
38 
44  public ‪$depthKeys = [];
45 
52  public ‪$searchKeys = [];
53 
60  public ‪$fixedLgd = 1;
61 
68  public ‪$regexMode = 0;
69 
75  public ‪$searchKeysToo = false;
76 
82  public ‪$varName = '';
83 
95  public function ‪tree($array, $positionKey)
96  {
97  ‪$output = '<ul class="list-tree text-monospace">';
98  if ($positionKey) {
99  $positionKey = $positionKey . '.';
100  }
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;
107  }
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> ';
116  }
117  ‪$output .= $this->‪wrapArrayKey($key, $depth, !$isArray ? $value : '');
118  if (!$isArray) {
119  ‪$output .= ' = <span class="list-tree-value">' . htmlspecialchars($value) . '</span>';
120  }
121  ‪$output .= '</span>';
122  if ($isExpanded) {
123  ‪$output .= $this->‪tree(
124  $value,
125  $depth
126  );
127  }
128  ‪$output .= '</li>';
129  }
130  ‪$output .= '</ul>';
131  return ‪$output;
132  }
133 
142  public function ‪wrapArrayKey($label, $depth, $theValue)
143  {
144  // Protect label:
145  $label = htmlspecialchars($label);
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>';
158  }
159  return '<span class="list-tree-label">' . $label . '</span>';
160  }
161 
172  public function ‪getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
173  {
174  if ($depth_in) {
175  $depth_in = $depth_in . '.';
176  }
177  foreach ($keyArr as $key => $value) {
178  $depth = $depth_in . $key;
179  $deeper = is_array($keyArr[$key]);
180  if ($this->regexMode) {
181  if (
182  is_scalar($keyArr[$key]) && preg_match('/' . $searchString . '/', $keyArr[$key])
183  || $this->searchKeysToo && preg_match('/' . $searchString . '/', $key)
184  ) {
185  $this->searchKeys[$depth] = 1;
186  }
187  } else {
188  if (
189  is_scalar($keyArr[$key]) && stristr($keyArr[$key], $searchString)
190  || $this->searchKeysToo && stristr($key, $searchString)
191  ) {
192  $this->searchKeys[$depth] = 1;
193  }
194  }
195  if ($deeper) {
196  $cS = count($this->searchKeys);
197  $keyArray = $this->‪getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
198  if ($cS != count($this->searchKeys)) {
199  $keyArray[$depth] = 1;
200  }
201  }
202  }
203  return $keyArray;
204  }
205 
213  public function ‪depthKeys($arr, $settings)
214  {
215  $tsbrArray = [];
216  foreach ($arr as $theK => $theV) {
217  $theKeyParts = explode('.', $theK);
218  $depth = '';
219  $c = count($theKeyParts);
220  $a = 0;
221  foreach ($theKeyParts as $p) {
222  $a++;
223  $depth .= ($depth ? '.' : '') . $p;
224  $tsbrArray[$depth] = $c == $a ? $theV : 1;
225  }
226  }
227  // Modify settings
228  foreach ($tsbrArray as $theK => $theV) {
229  if ($theV) {
230  $settings[$theK] = 1;
231  } else {
232  unset($settings[$theK]);
233  }
234  }
235  return $settings;
236  }
237 }
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\$expAll
‪bool $expAll
Definition: ArrayBrowser.php:29
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\$regexMode
‪int $regexMode
Definition: ArrayBrowser.php:62
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser
Definition: ArrayBrowser.php:26
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\$searchKeys
‪array $searchKeys
Definition: ArrayBrowser.php:48
‪TYPO3
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\$depthKeys
‪array $depthKeys
Definition: ArrayBrowser.php:41
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\$dontLinkVar
‪bool $dontLinkVar
Definition: ArrayBrowser.php:35
‪TYPO3\CMS\Lowlevel\Utility
Definition: ArrayBrowser.php:2
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\tree
‪string tree($array, $positionKey)
Definition: ArrayBrowser.php:87
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\$fixedLgd
‪int $fixedLgd
Definition: ArrayBrowser.php:55
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\$searchKeysToo
‪bool $searchKeysToo
Definition: ArrayBrowser.php:68
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\getSearchKeys
‪array getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
Definition: ArrayBrowser.php:164
‪$output
‪$output
Definition: annotationChecker.php:113
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\$varName
‪string $varName
Definition: ArrayBrowser.php:74
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\depthKeys
‪array depthKeys($arr, $settings)
Definition: ArrayBrowser.php:205
‪TYPO3\CMS\Lowlevel\Utility\ArrayBrowser\wrapArrayKey
‪string wrapArrayKey($label, $depth, $theValue)
Definition: ArrayBrowser.php:134