TYPO3 CMS  TYPO3_7-6
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 
96  public function tree($array, $positionKey, $depthData = null)
97  {
98  if ($depthData) {
99  GeneralUtility::deprecationLog('ArrayBrowser::tree parameter $depthData is deprecated since TYPO3 CMS 7 and is not used anymore. Please remove the parameter.');
100  }
101  $output = '<ul class="list-tree text-monospace">';
102  if ($positionKey) {
103  $positionKey = $positionKey . '.';
104  }
105  foreach ($array as $key => $value) {
106  $depth = $positionKey . $key;
107  if (is_object($value) && !$value instanceof \Traversable) {
108  $value = (array)$value;
109  }
110  $isArray = is_array($value) || $value instanceof \Traversable;
111  $isResult = (bool)$this->searchKeys[$depth];
112  $isExpanded = $isArray && ($this->depthKeys[$depth] || $this->expAll);
113  $output .= '<li' . ($isResult ? ' class="active"' : '') . '>';
114  if ($isArray && !$this->expAll) {
115  $goto = 'a' . substr(md5($depth), 0, 6);
116  $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> ';
117  }
118  $output .= '<span class="list-tree-group">';
119  $output .= $this->wrapArrayKey($key, $depth, !$isArray ? $value : '');
120  if (!$isArray) {
121  $output .= ' = <span class="list-tree-value">' . $this->wrapValue($value) . '</span>';
122  }
123  $output .= '</span>';
124  if ($isExpanded) {
125  $output .= $this->tree(
126  $value,
127  $depth
128  );
129  }
130  $output .= '</li>';
131  }
132  $output .= '</ul>';
133  return $output;
134  }
135 
142  public function wrapValue($theValue)
143  {
144  $wrappedValue = '';
145  if ((string)$theValue !== '') {
146  $wrappedValue = htmlspecialchars($theValue);
147  }
148  return $wrappedValue;
149  }
150 
159  public function wrapArrayKey($label, $depth, $theValue)
160  {
161  // Protect label:
162  $label = htmlspecialchars($label);
163 
164  // If varname is set:
165  if ($this->varName && !$this->dontLinkVar) {
166  $variableName = $this->varName
167  . '[\'' . str_replace('.', '\'][\'', $depth) . '\'] = '
168  . (!MathUtility::canBeInterpretedAsInteger($theValue) ? '\''
169  . addslashes($theValue) . '\'' : $theValue) . '; ';
170  $label = '<a class="list-tree-label" href="'
171  . htmlspecialchars((BackendUtility::getModuleUrl(GeneralUtility::_GP('M'))
172  . '&varname=' . urlencode($variableName)))
173  . '#varname">' . $label . '</a>';
174  }
175  return '<span class="list-tree-label">' . $label . '</span>';
176  }
177 
188  public function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
189  {
190  if ($depth_in) {
191  $depth_in = $depth_in . '.';
192  }
193  foreach ($keyArr as $key => $value) {
194  $depth = $depth_in . $key;
195  $deeper = is_array($keyArr[$key]);
196  if ($this->regexMode) {
197  if (
198  is_scalar($keyArr[$key]) && preg_match('/' . $searchString . '/', $keyArr[$key])
199  || $this->searchKeysToo && preg_match('/' . $searchString . '/', $key)
200  ) {
201  $this->searchKeys[$depth] = 1;
202  }
203  } else {
204  if (
205  is_scalar($keyArr[$key]) && stristr($keyArr[$key], $searchString)
206  || $this->searchKeysToo && stristr($key, $searchString)
207  ) {
208  $this->searchKeys[$depth] = 1;
209  }
210  }
211  if ($deeper) {
212  $cS = count($this->searchKeys);
213  $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
214  if ($cS != count($this->searchKeys)) {
215  $keyArray[$depth] = 1;
216  }
217  }
218  }
219  return $keyArray;
220  }
221 
229  public function depthKeys($arr, $settings)
230  {
231  $tsbrArray = [];
232  foreach ($arr as $theK => $theV) {
233  $theKeyParts = explode('.', $theK);
234  $depth = '';
235  $c = count($theKeyParts);
236  $a = 0;
237  foreach ($theKeyParts as $p) {
238  $a++;
239  $depth .= ($depth ? '.' : '') . $p;
240  $tsbrArray[$depth] = $c == $a ? $theV : 1;
241  }
242  }
243  // Modify settings
244  foreach ($tsbrArray as $theK => $theV) {
245  if ($theV) {
246  $settings[$theK] = 1;
247  } else {
248  unset($settings[$theK]);
249  }
250  }
251  return $settings;
252  }
253 }
getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
tree($array, $positionKey, $depthData=null)
wrapArrayKey($label, $depth, $theValue)