PageRenderTime 41ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/t3lib/class.t3lib_arraybrowser.php

https://bitbucket.org/linxpinx/mercurial
PHP | 278 lines | 129 code | 35 blank | 114 comment | 32 complexity | 1284b6b5698507617e5b871e009d8a4d MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Unlicense, LGPL-2.1, Apache-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 1999-2010 Kasper Skaarhoj (kasperYYYY@typo3.com)
  6. * All rights reserved
  7. *
  8. * This script is part of the TYPO3 project. The TYPO3 project is
  9. * free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * The GNU General Public License can be found at
  15. * http://www.gnu.org/copyleft/gpl.html.
  16. * A copy is found in the textfile GPL.txt and important notices to the license
  17. * from the author is found in LICENSE.txt distributed with these scripts.
  18. *
  19. *
  20. * This script is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * This copyright notice MUST APPEAR in all copies of the script!
  26. ***************************************************************/
  27. /**
  28. * Class for displaying an array as a tree
  29. *
  30. * $Id: class.t3lib_arraybrowser.php 7905 2010-06-13 14:42:33Z ohader $
  31. * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
  32. * XHTML compliant
  33. *
  34. * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
  35. */
  36. /**
  37. * [CLASS/FUNCTION INDEX of SCRIPT]
  38. *
  39. *
  40. *
  41. * 77: class t3lib_arrayBrowser
  42. * 96: function tree($arr, $depth_in, $depthData)
  43. * 160: function wrapValue($theValue,$depth)
  44. * 172: function wrapArrayKey($label,$depth,$theValue)
  45. * 196: function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
  46. * 228: function fixed_lgd($string,$chars)
  47. * 245: function depthKeys($arr,$settings)
  48. *
  49. * TOTAL FUNCTIONS: 6
  50. * (This index is automatically created/updated by the extension "extdeveval")
  51. *
  52. */
  53. /**
  54. * Class for displaying an array as a tree
  55. * See the extension 'lowlevel' /config (Backend module 'Tools > Configuration')
  56. *
  57. * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
  58. * @package TYPO3
  59. * @subpackage t3lib
  60. * @see SC_mod_tools_config_index::main()
  61. */
  62. class t3lib_arrayBrowser {
  63. var $expAll = FALSE; // If set, will expand all (depthKeys is obsolete then) (and no links are applied)
  64. var $dontLinkVar = FALSE; // If set, the variable keys are not linked.
  65. var $depthKeys = array(); // Array defining which keys to expand. Typically set from outside from some session variable - otherwise the array will collapse.
  66. var $searchKeys = array(); // After calling the getSearchKeys function this array is populated with the key-positions in the array which contains values matching the search.
  67. var $fixedLgd=1; // If set, the values are truncated with "..." appended if longer than a certain length.
  68. var $regexMode=0; // If set, search for string with regex, otherwise stristr()
  69. var $searchKeysToo=FALSE; // If set, array keys are subject to the search too.
  70. var $varName=''; // Set var name here if you want links to the variable name.
  71. /**
  72. * Make browsable tree
  73. * Before calling this function you may want to set some of the internal vars like depthKeys, regexMode and fixedLgd. For examples see SC_mod_tools_config_index::main()
  74. *
  75. * @param array The array to display
  76. * @param string Key-position id. Build up during recursive calls - [key1].[key2].[key3] - an so on.
  77. * @param string Depth-data - basically a prefix for the icons. For calling this function from outside, let it stay blank.
  78. * @return string HTML for the tree
  79. * @see SC_mod_tools_config_index::main()
  80. */
  81. function tree($arr, $depth_in, $depthData) {
  82. $HTML='';
  83. $a=0;
  84. if ($depth_in) {$depth_in = $depth_in.'.';}
  85. $c=count($arr);
  86. foreach ($arr as $key => $value) {
  87. $a++;
  88. $depth = $depth_in.$key;
  89. $goto = 'a' . substr(md5($depth), 0, 6);
  90. $deeper = (is_array($arr[$key]) && ($this->depthKeys[$depth] || $this->expAll)) ? 1 : 0;
  91. $PM = 'join';
  92. $LN = ($a==$c)?'blank':'line';
  93. $BTM = ($a==$c)?'bottom':'';
  94. $PM = is_array($arr[$key]) ? ($deeper ? 'minus':'plus') : 'join';
  95. $HTML.=$depthData;
  96. $theIcon='<img'.t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'],'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' align="top" border="0" alt="" />';
  97. if ($PM=='join') {
  98. $HTML.=$theIcon;
  99. } else {
  100. $HTML.=
  101. ($this->expAll ? '' : '<a id="' . $goto . '" href="' . htmlspecialchars('index.php?node[' . $depth . ']=' . ($deeper ? 0 : 1) . '#' . $goto) . '">') .
  102. $theIcon.
  103. ($this->expAll ? '' : '</a>');
  104. }
  105. $label = $key;
  106. $HTML.= $this->wrapArrayKey($label,$depth,!is_array($arr[$key]) ? $arr[$key] : '');
  107. if (!is_array($arr[$key])) {
  108. $theValue = $arr[$key];
  109. if ($this->fixedLgd) {
  110. $imgBlocks = ceil(1+strlen($depthData)/77);
  111. // debug($imgBlocks);
  112. $lgdChars = 68-ceil(strlen('['.$key.']')*0.8)-$imgBlocks*3;
  113. $theValue = $this->fixed_lgd($theValue,$lgdChars);
  114. }
  115. if ($this->searchKeys[$depth]) {
  116. $HTML.='=<span style="color:red;">'.$this->wrapValue($theValue,$depth).'</span>';
  117. } else {
  118. $HTML.='='.$this->wrapValue($theValue,$depth);
  119. }
  120. }
  121. $HTML.='<br />';
  122. if ($deeper) {
  123. $HTML.=$this->tree($arr[$key], $depth, $depthData.'<img'.t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'],'gfx/ol/'.$LN.'.gif','width="18" height="16"').' align="top" alt="" />');
  124. }
  125. }
  126. return $HTML;
  127. }
  128. /**
  129. * Wrapping the value in bold tags etc.
  130. *
  131. * @param string The title string
  132. * @param string Depth path
  133. * @return string Title string, htmlspecialchars()'ed
  134. */
  135. function wrapValue($theValue,$depth) {
  136. $wrappedValue = '';
  137. if (strlen($theValue) > 0) {
  138. $wrappedValue = '<strong>' . htmlspecialchars($theValue) . '</strong>';
  139. }
  140. return $wrappedValue;
  141. }
  142. /**
  143. * Wrapping the value in bold tags etc.
  144. *
  145. * @param string The title string
  146. * @param string Depth path
  147. * @param string The value for the array entry.
  148. * @return string Title string, htmlspecialchars()'ed
  149. */
  150. function wrapArrayKey($label,$depth,$theValue) {
  151. // Protect label:
  152. $label = htmlspecialchars($label);
  153. // If varname is set:
  154. if ($this->varName && !$this->dontLinkVar) {
  155. $variableName = $this->varName.'[\''.str_replace('.','\'][\'',$depth).'\'] = '.(!t3lib_div::testInt($theValue) ? '\''.addslashes($theValue).'\'' : $theValue).'; ';
  156. $label = '<a href="index.php?varname=' . urlencode($variableName) . '#varname">' . $label . '</a>';
  157. }
  158. // Return:
  159. return '['.$label.']';
  160. }
  161. /**
  162. * Creates an array with "depthKeys" which will expand the array to show the search results
  163. *
  164. * @param array The array to search for the value
  165. * @param string Depth string - blank for first call (will build up during recursive calling creating an id of the position: [key1].[key2].[key3]
  166. * @param string The string to search for
  167. * @param array Key array, for first call pass empty array
  168. * @return array
  169. */
  170. function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray) {
  171. $c=count($keyArr);
  172. if ($depth_in) {$depth_in = $depth_in.'.';}
  173. foreach ($keyArr as $key => $value) {
  174. $depth=$depth_in.$key;
  175. $deeper = is_array($keyArr[$key]);
  176. if ($this->regexMode) {
  177. if (preg_match('/'.$searchString.'/',$keyArr[$key]) || ($this->searchKeysToo && preg_match('/'.$searchString.'/',$key))) { $this->searchKeys[$depth]=1; }
  178. } else {
  179. if ((!$deeper && stristr($keyArr[$key], $searchString)) || ($this->searchKeysToo && stristr($key, $searchString))) {
  180. $this->searchKeys[$depth] = 1;
  181. }
  182. }
  183. if ($deeper) {
  184. $cS = count($this->searchKeys);
  185. $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
  186. if ($cS != count($this->searchKeys)) {
  187. $keyArray[$depth]=1;
  188. }
  189. }
  190. }
  191. return $keyArray;
  192. }
  193. /**
  194. * Fixed length function
  195. *
  196. * @param string String to process
  197. * @param integer Max number of chars
  198. * @return string Processed string
  199. */
  200. function fixed_lgd($string,$chars) {
  201. if ($chars >= 4) {
  202. if(strlen($string)>$chars) {
  203. return substr($string, 0, $chars-3).'...';
  204. }
  205. }
  206. return $string;
  207. }
  208. /**
  209. * Function modifying the depthKey array
  210. *
  211. * @param array Array with instructions to open/close nodes.
  212. * @param array Input depth_key array
  213. * @return array Output depth_key array with entries added/removed based on $arr
  214. * @see SC_mod_tools_config_index::main()
  215. */
  216. function depthKeys($arr,$settings) {
  217. $tsbrArray=array();
  218. foreach ($arr as $theK => $theV) {
  219. $theKeyParts = explode('.',$theK);
  220. $depth='';
  221. $c=count($theKeyParts);
  222. $a=0;
  223. foreach ($theKeyParts as $p) {
  224. $a++;
  225. $depth.=($depth?'.':'').$p;
  226. $tsbrArray[$depth]= ($c==$a) ? $theV : 1;
  227. }
  228. }
  229. // Modify settings
  230. foreach ($tsbrArray as $theK => $theV) {
  231. if ($theV) {
  232. $settings[$theK] = 1;
  233. } else {
  234. unset($settings[$theK]);
  235. }
  236. }
  237. return $settings;
  238. }
  239. }
  240. if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php']) {
  241. include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php']);
  242. }
  243. ?>