PageRenderTime 71ms CodeModel.GetById 54ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68/**
 69 * Class for displaying an array as a tree
 70 * See the extension 'lowlevel' /config (Backend module 'Tools > Configuration')
 71 *
 72 * @author	Kasper Skaarhoj <kasperYYYY@typo3.com>
 73 * @package TYPO3
 74 * @subpackage t3lib
 75 * @see SC_mod_tools_config_index::main()
 76 */
 77class t3lib_arrayBrowser	{
 78	var $expAll = FALSE;			// If set, will expand all (depthKeys is obsolete then) (and no links are applied)
 79	var $dontLinkVar = FALSE;		// If set, the variable keys are not linked.
 80	var $depthKeys = array();		// Array defining which keys to expand. Typically set from outside from some session variable - otherwise the array will collapse.
 81	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.
 82	var $fixedLgd=1;				// If set, the values are truncated with "..." appended if longer than a certain length.
 83	var $regexMode=0;				// If set, search for string with regex, otherwise stristr()
 84	var $searchKeysToo=FALSE;		// If set, array keys are subject to the search too.
 85	var $varName='';				// Set var name here if you want links to the variable name.
 86
 87	/**
 88	 * Make browsable tree
 89	 * 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()
 90	 *
 91	 * @param	array		The array to display
 92	 * @param	string		Key-position id. Build up during recursive calls - [key1].[key2].[key3] - an so on.
 93	 * @param	string		Depth-data - basically a prefix for the icons. For calling this function from outside, let it stay blank.
 94	 * @return	string		HTML for the tree
 95	 * @see SC_mod_tools_config_index::main()
 96	 */
 97	function tree($arr, $depth_in, $depthData)	{
 98		$HTML='';
 99		$a=0;
100
101		if ($depth_in)	{$depth_in = $depth_in.'.';}
102
103		$c=count($arr);
104		foreach ($arr as $key => $value) {
105			$a++;
106			$depth = $depth_in.$key;
107			$goto = 'a' . substr(md5($depth), 0, 6);
108
109			$deeper = (is_array($arr[$key]) && ($this->depthKeys[$depth] || $this->expAll)) ? 1 : 0;
110			$PM = 'join';
111			$LN = ($a==$c)?'blank':'line';
112			$BTM = ($a==$c)?'bottom':'';
113			$PM = is_array($arr[$key]) ? ($deeper ? 'minus':'plus') : 'join';
114
115
116			$HTML.=$depthData;
117			$theIcon='<img'.t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'],'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' align="top" border="0" alt="" />';
118			if ($PM=='join')	{
119				$HTML.=$theIcon;
120			} else {
121				$HTML.=
122					($this->expAll ? '' : '<a id="' . $goto . '" href="' . htmlspecialchars('index.php?node[' . $depth . ']=' . ($deeper ? 0 : 1) . '#' . $goto) . '">') .
123					$theIcon.
124					($this->expAll ? '' : '</a>');
125			}
126
127			$label = $key;
128			$HTML.= $this->wrapArrayKey($label,$depth,!is_array($arr[$key]) ? $arr[$key] : '');
129
130			if (!is_array($arr[$key]))	{
131				$theValue = $arr[$key];
132				if ($this->fixedLgd)	{
133					$imgBlocks = ceil(1+strlen($depthData)/77);
134//					debug($imgBlocks);
135					$lgdChars = 68-ceil(strlen('['.$key.']')*0.8)-$imgBlocks*3;
136					$theValue = $this->fixed_lgd($theValue,$lgdChars);
137				}
138				if ($this->searchKeys[$depth])	{
139					$HTML.='=<span style="color:red;">'.$this->wrapValue($theValue,$depth).'</span>';
140				} else {
141					$HTML.='='.$this->wrapValue($theValue,$depth);
142				}
143			}
144			$HTML.='<br />';
145
146			if ($deeper)	{
147				$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="" />');
148			}
149		}
150		return $HTML;
151	}
152
153	/**
154	 * Wrapping the value in bold tags etc.
155	 *
156	 * @param	string		The title string
157	 * @param	string		Depth path
158	 * @return	string		Title string, htmlspecialchars()'ed
159	 */
160	function wrapValue($theValue,$depth)	{
161		$wrappedValue = '';
162		if (strlen($theValue) > 0) {
163			$wrappedValue = '<strong>' . htmlspecialchars($theValue) . '</strong>';
164		}
165		return $wrappedValue;
166	}
167
168	/**
169	 * Wrapping the value in bold tags etc.
170	 *
171	 * @param	string		The title string
172	 * @param	string		Depth path
173	 * @param	string		The value for the array entry.
174	 * @return	string		Title string, htmlspecialchars()'ed
175	 */
176	function wrapArrayKey($label,$depth,$theValue)	{
177
178			// Protect label:
179		$label = htmlspecialchars($label);
180
181			// If varname is set:
182		if ($this->varName && !$this->dontLinkVar) {
183			$variableName = $this->varName.'[\''.str_replace('.','\'][\'',$depth).'\'] = '.(!t3lib_div::testInt($theValue) ? '\''.addslashes($theValue).'\'' : $theValue).'; ';
184			$label = '<a href="index.php?varname=' . urlencode($variableName) . '#varname">' . $label . '</a>';
185		}
186
187			// Return:
188		return '['.$label.']';
189	}
190
191	/**
192	 * Creates an array with "depthKeys" which will expand the array to show the search results
193	 *
194	 * @param	array		The array to search for the value
195	 * @param	string		Depth string - blank for first call (will build up during recursive calling creating an id of the position: [key1].[key2].[key3]
196	 * @param	string		The string to search for
197	 * @param	array		Key array, for first call pass empty array
198	 * @return	array
199	 */
200	function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)		{
201		$c=count($keyArr);
202		if ($depth_in)	{$depth_in = $depth_in.'.';}
203		foreach ($keyArr as $key => $value) {
204			$depth=$depth_in.$key;
205			$deeper = is_array($keyArr[$key]);
206
207			if ($this->regexMode)	{
208				if (preg_match('/'.$searchString.'/',$keyArr[$key]) || ($this->searchKeysToo && preg_match('/'.$searchString.'/',$key)))	{	$this->searchKeys[$depth]=1;	}
209			} else {
210				if ((!$deeper && stristr($keyArr[$key], $searchString)) || ($this->searchKeysToo && stristr($key, $searchString))) {
211					$this->searchKeys[$depth] = 1;
212				}
213			}
214
215			if ($deeper)	{
216				$cS = count($this->searchKeys);
217				$keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
218				if ($cS != count($this->searchKeys))	{
219					$keyArray[$depth]=1;
220				}
221			}
222		}
223		return $keyArray;
224	}
225
226	/**
227	 * Fixed length function
228	 *
229	 * @param	string		String to process
230	 * @param	integer		Max number of chars
231	 * @return	string		Processed string
232	 */
233	function fixed_lgd($string,$chars)	{
234		if ($chars >= 4)	{
235			if(strlen($string)>$chars)  {
236				return substr($string, 0, $chars-3).'...';
237			}
238		}
239		return $string;
240	}
241
242	/**
243	 * Function modifying the depthKey array
244	 *
245	 * @param	array		Array with instructions to open/close nodes.
246	 * @param	array		Input depth_key array
247	 * @return	array		Output depth_key array with entries added/removed based on $arr
248	 * @see SC_mod_tools_config_index::main()
249	 */
250	function depthKeys($arr,$settings)	{
251		$tsbrArray=array();
252		foreach ($arr as $theK => $theV) {
253			$theKeyParts = explode('.',$theK);
254			$depth='';
255			$c=count($theKeyParts);
256			$a=0;
257			foreach ($theKeyParts as $p) {
258				$a++;
259				$depth.=($depth?'.':'').$p;
260				$tsbrArray[$depth]= ($c==$a) ? $theV : 1;
261			}
262		}
263			// Modify settings
264		foreach ($tsbrArray as $theK => $theV) {
265			if ($theV)	{
266				$settings[$theK] = 1;
267			} else {
268				unset($settings[$theK]);
269			}
270		}
271		return $settings;
272	}
273}
274
275if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php'])	{
276	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php']);
277}
278?>