TYPO3 CMS  TYPO3_6-2
ArrayBrowser.php
Go to the documentation of this file.
1 <?php
3 
24 class ArrayBrowser {
25 
29  public $expAll = FALSE;
30 
31  // If set, will expand all (depthKeys is obsolete then) (and no links are applied)
35  public $dontLinkVar = FALSE;
36 
37  // If set, the variable keys are not linked.
41  public $depthKeys = array();
42 
43  // Array defining which keys to expand. Typically set from outside from some session variable - otherwise the array will collapse.
47  public $searchKeys = array();
48 
49  // After calling the getSearchKeys function this array is populated with the key-positions in the array which contains values matching the search.
53  public $fixedLgd = 1;
54 
55  // If set, the values are truncated with "..." appended if longer than a certain length.
59  public $regexMode = 0;
60 
61  // If set, search for string with regex, otherwise stristr()
65  public $searchKeysToo = FALSE;
66 
67  // If set, array keys are subject to the search too.
71  public $varName = '';
72 
73  // Set var name here if you want links to the variable name.
86  public function tree($arr, $depth_in, $depthData) {
87  $HTML = '';
88  $a = 0;
89  if ($depth_in) {
90  $depth_in = $depth_in . '.';
91  }
92  $c = count($arr);
93  foreach ($arr as $key => $value) {
94  $a++;
95  $depth = $depth_in . $key;
96  $goto = 'a' . substr(md5($depth), 0, 6);
97  if (is_object($value) && !$value instanceof \Traversable) {
98  $value = (array)$value;
99  }
100  $isArray = is_array($value) || $value instanceof \Traversable;
101  $deeper = $isArray && ($this->depthKeys[$depth] || $this->expAll);
102  $LN = $a == $c ? 'blank' : 'line';
103  $BTM = $a == $c ? 'bottom' : '';
104  $PM = $isArray ? ($deeper ? 'minus' : 'plus') : 'join';
105  $HTML .= $depthData;
106  $theIcon = '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], ('gfx/ol/' . $PM . $BTM . '.gif'), 'width="18" height="16"') . ' align="top" border="0" alt="" />';
107  if ($PM == 'join') {
108  $HTML .= $theIcon;
109  } else {
110  $HTML .= ($this->expAll ? '' : '<a id="' . $goto . '" href="' . htmlspecialchars((\TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('M')) . '&node[' . $depth . ']=' . ($deeper ? 0 : 1) . '#' . $goto)) . '">') . $theIcon . ($this->expAll ? '' : '</a>');
111  }
112  $label = $key;
113  $HTML .= $this->wrapArrayKey($label, $depth, !$isArray ? $value : '');
114  if (!$isArray) {
115  $theValue = $value;
116  if ($this->fixedLgd) {
117  $imgBlocks = ceil(1 + strlen($depthData) / 77);
118  $lgdChars = 68 - ceil(strlen(('[' . $key . ']')) * 0.8) - $imgBlocks * 3;
119  $theValue = $this->fixed_lgd($theValue, $lgdChars);
120  }
121  if ($this->searchKeys[$depth]) {
122  $HTML .= '=<span style="color:red;">' . $this->wrapValue($theValue, $depth) . '</span>';
123  } else {
124  $HTML .= '=' . $this->wrapValue($theValue, $depth);
125  }
126  }
127  $HTML .= '<br />';
128  if ($deeper) {
129  $HTML .= $this->tree($value, $depth, $depthData . '<img' . \TYPO3\CMS\Backend\Utility\IconUtility::skinImg($GLOBALS['BACK_PATH'], ('gfx/ol/' . $LN . '.gif'), 'width="18" height="16"') . ' align="top" alt="" />');
130  }
131  }
132  return $HTML;
133  }
134 
143  public function wrapValue($theValue, $depth) {
144  $wrappedValue = '';
145  if (strlen($theValue) > 0) {
146  $wrappedValue = '<strong>' . htmlspecialchars($theValue) . '</strong>';
147  }
148  return $wrappedValue;
149  }
150 
160  public function wrapArrayKey($label, $depth, $theValue) {
161  // Protect label:
162  $label = htmlspecialchars($label);
163  // If varname is set:
164  if ($this->varName && !$this->dontLinkVar) {
165  $variableName = $this->varName . '[\'' . str_replace('.', '\'][\'', $depth) . '\'] = ' . (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($theValue) ? '\'' . addslashes($theValue) . '\'' : $theValue) . '; ';
166  $label = '<a href="' . htmlspecialchars((\TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('M')) . '&varname=' . urlencode($variableName))) . '#varname">' . $label . '</a>';
167  }
168  // Return:
169  return '[' . $label . ']';
170  }
171 
182  public function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray) {
183  $c = count($keyArr);
184  if ($depth_in) {
185  $depth_in = $depth_in . '.';
186  }
187  foreach ($keyArr as $key => $value) {
188  $depth = $depth_in . $key;
189  $deeper = is_array($keyArr[$key]);
190  if ($this->regexMode) {
191  if (is_scalar($keyArr[$key]) && preg_match('/' . $searchString . '/', $keyArr[$key]) || $this->searchKeysToo && preg_match('/' . $searchString . '/', $key)) {
192  $this->searchKeys[$depth] = 1;
193  }
194  } else {
195  if (is_scalar($keyArr[$key]) && stristr($keyArr[$key], $searchString) || $this->searchKeysToo && stristr($key, $searchString)) {
196  $this->searchKeys[$depth] = 1;
197  }
198  }
199  if ($deeper) {
200  $cS = count($this->searchKeys);
201  $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
202  if ($cS != count($this->searchKeys)) {
203  $keyArray[$depth] = 1;
204  }
205  }
206  }
207  return $keyArray;
208  }
209 
218  public function fixed_lgd($string, $chars) {
219  if ($chars >= 4) {
220  if (strlen($string) > $chars) {
221  return substr($string, 0, ($chars - 3)) . '...';
222  }
223  }
224  return $string;
225  }
226 
236  public function depthKeys($arr, $settings) {
237  $tsbrArray = array();
238  foreach ($arr as $theK => $theV) {
239  $theKeyParts = explode('.', $theK);
240  $depth = '';
241  $c = count($theKeyParts);
242  $a = 0;
243  foreach ($theKeyParts as $p) {
244  $a++;
245  $depth .= ($depth ? '.' : '') . $p;
246  $tsbrArray[$depth] = $c == $a ? $theV : 1;
247  }
248  }
249  // Modify settings
250  foreach ($tsbrArray as $theK => $theV) {
251  if ($theV) {
252  $settings[$theK] = 1;
253  } else {
254  unset($settings[$theK]);
255  }
256  }
257  return $settings;
258  }
259 
260 }
static skinImg($backPath, $src, $wHattribs='', $outputMode=0)
tree($arr, $depth_in, $depthData)
getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
static getModuleUrl($moduleName, $urlParameters=array(), $backPathOverride=FALSE, $returnAbsoluteUrl=FALSE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
wrapArrayKey($label, $depth, $theValue)