TYPO3 CMS  TYPO3_8-7
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 
20 
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  }
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  $output .= '<span class="list-tree-group">';
111  if ($isArray && !$this->expAll) {
112  $goto = 'a' . substr(md5($depth), 0, 6);
113  $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> ';
114  }
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 
139  public function wrapValue($theValue)
140  {
142  $wrappedValue = '';
143  if ((string)$theValue !== '') {
144  $wrappedValue = htmlspecialchars($theValue);
145  }
146  return $wrappedValue;
147  }
148 
157  public function wrapArrayKey($label, $depth, $theValue)
158  {
159  // Protect label:
160  $label = htmlspecialchars($label);
161 
162  // If varname is set:
163  if ($this->varName && !$this->dontLinkVar) {
164  $variableName = $this->varName
165  . '[\'' . str_replace('.', '\'][\'', $depth) . '\'] = '
166  . (!MathUtility::canBeInterpretedAsInteger($theValue) ? '\''
167  . addslashes($theValue) . '\'' : $theValue) . '; ';
168  $label = '<a class="list-tree-label" href="'
169  . htmlspecialchars((BackendUtility::getModuleUrl(GeneralUtility::_GP('M'))
170  . '&varname=' . urlencode($variableName)))
171  . '#varname">' . $label . '</a>';
172  }
173  return '<span class="list-tree-label">' . $label . '</span>';
174  }
175 
186  public function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
187  {
188  if ($depth_in) {
189  $depth_in = $depth_in . '.';
190  }
191  foreach ($keyArr as $key => $value) {
192  $depth = $depth_in . $key;
193  $deeper = is_array($keyArr[$key]);
194  if ($this->regexMode) {
195  if (
196  is_scalar($keyArr[$key]) && preg_match('/' . $searchString . '/', $keyArr[$key])
197  || $this->searchKeysToo && preg_match('/' . $searchString . '/', $key)
198  ) {
199  $this->searchKeys[$depth] = 1;
200  }
201  } else {
202  if (
203  is_scalar($keyArr[$key]) && stristr($keyArr[$key], $searchString)
204  || $this->searchKeysToo && stristr($key, $searchString)
205  ) {
206  $this->searchKeys[$depth] = 1;
207  }
208  }
209  if ($deeper) {
210  $cS = count($this->searchKeys);
211  $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
212  if ($cS != count($this->searchKeys)) {
213  $keyArray[$depth] = 1;
214  }
215  }
216  }
217  return $keyArray;
218  }
219 
227  public function depthKeys($arr, $settings)
228  {
229  $tsbrArray = [];
230  foreach ($arr as $theK => $theV) {
231  $theKeyParts = explode('.', $theK);
232  $depth = '';
233  $c = count($theKeyParts);
234  $a = 0;
235  foreach ($theKeyParts as $p) {
236  $a++;
237  $depth .= ($depth ? '.' : '') . $p;
238  $tsbrArray[$depth] = $c == $a ? $theV : 1;
239  }
240  }
241  // Modify settings
242  foreach ($tsbrArray as $theK => $theV) {
243  if ($theV) {
244  $settings[$theK] = 1;
245  } else {
246  unset($settings[$theK]);
247  }
248  }
249  return $settings;
250  }
251 }
getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
wrapArrayKey($label, $depth, $theValue)