TYPO3 API  SVNRelease
class.t3lib_arraybrowser.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 /**
00028  * Class for displaying an array as a tree
00029  *
00030  * $Id: class.t3lib_arraybrowser.php 10121 2011-01-18 20:15:30Z ohader $
00031  * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
00032  * XHTML compliant
00033  *
00034  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00035  */
00036 /**
00037  * [CLASS/FUNCTION INDEX of SCRIPT]
00038  *
00039  *
00040  *
00041  *   77: class t3lib_arrayBrowser
00042  *   96:     function tree($arr, $depth_in, $depthData)
00043  *  160:     function wrapValue($theValue,$depth)
00044  *  172:     function wrapArrayKey($label,$depth,$theValue)
00045  *  196:     function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)
00046  *  228:     function fixed_lgd($string,$chars)
00047  *  245:     function depthKeys($arr,$settings)
00048  *
00049  * TOTAL FUNCTIONS: 6
00050  * (This index is automatically created/updated by the extension "extdeveval")
00051  *
00052  */
00053 
00054 
00055 /**
00056  * Class for displaying an array as a tree
00057  * See the extension 'lowlevel' /config (Backend module 'Tools > Configuration')
00058  *
00059  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00060  * @package TYPO3
00061  * @subpackage t3lib
00062  * @see SC_mod_tools_config_index::main()
00063  */
00064 class t3lib_arrayBrowser {
00065     var $expAll = FALSE; // If set, will expand all (depthKeys is obsolete then) (and no links are applied)
00066     var $dontLinkVar = FALSE; // If set, the variable keys are not linked.
00067     var $depthKeys = array(); // Array defining which keys to expand. Typically set from outside from some session variable - otherwise the array will collapse.
00068     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.
00069     var $fixedLgd = 1; // If set, the values are truncated with "..." appended if longer than a certain length.
00070     var $regexMode = 0; // If set, search for string with regex, otherwise stristr()
00071     var $searchKeysToo = FALSE; // If set, array keys are subject to the search too.
00072     var $varName = ''; // Set var name here if you want links to the variable name.
00073 
00074     /**
00075      * Make browsable tree
00076      * 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()
00077      *
00078      * @param   array       The array to display
00079      * @param   string      Key-position id. Build up during recursive calls - [key1].[key2].[key3] - an so on.
00080      * @param   string      Depth-data - basically a prefix for the icons. For calling this function from outside, let it stay blank.
00081      * @return  string      HTML for the tree
00082      * @see SC_mod_tools_config_index::main()
00083      */
00084     function tree($arr, $depth_in, $depthData) {
00085         $HTML = '';
00086         $a = 0;
00087 
00088         if ($depth_in) {
00089             $depth_in = $depth_in . '.';
00090         }
00091 
00092         $c = count($arr);
00093         foreach ($arr as $key => $value) {
00094             $a++;
00095             $depth = $depth_in . $key;
00096             $goto = 'a' . substr(md5($depth), 0, 6);
00097 
00098             $deeper = (is_array($arr[$key]) && ($this->depthKeys[$depth] || $this->expAll)) ? 1 : 0;
00099             $PM = 'join';
00100             $LN = ($a == $c) ? 'blank' : 'line';
00101             $BTM = ($a == $c) ? 'bottom' : '';
00102             $PM = is_array($arr[$key]) ? ($deeper ? 'minus' : 'plus') : 'join';
00103 
00104 
00105             $HTML .= $depthData;
00106             $theIcon = '<img' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/ol/' . $PM . $BTM . '.gif', 'width="18" height="16"') .
00107                 ' align="top" border="0" alt="" />';
00108             if ($PM == 'join') {
00109                 $HTML .= $theIcon;
00110             } else {
00111                 $HTML .=
00112                         ($this->expAll ? '' : '<a id="' . $goto . '" href="' . htmlspecialchars('index.php?node[' .
00113                                 $depth . ']=' . ($deeper ? 0 : 1) . '#' . $goto) . '">') .
00114                                 $theIcon .
00115                                 ($this->expAll ? '' : '</a>');
00116             }
00117 
00118             $label = $key;
00119             $HTML .= $this->wrapArrayKey($label, $depth, !is_array($arr[$key]) ? $arr[$key] : '');
00120 
00121             if (!is_array($arr[$key])) {
00122                 $theValue = $arr[$key];
00123                 if ($this->fixedLgd) {
00124                     $imgBlocks = ceil(1 + strlen($depthData) / 77);
00125                         //                  debug($imgBlocks);
00126                     $lgdChars = 68 - ceil(strlen('[' . $key . ']') * 0.8) - $imgBlocks * 3;
00127                     $theValue = $this->fixed_lgd($theValue, $lgdChars);
00128                 }
00129                 if ($this->searchKeys[$depth]) {
00130                     $HTML .= '=<span style="color:red;">' . $this->wrapValue($theValue, $depth) . '</span>';
00131                 } else {
00132                     $HTML .= '=' . $this->wrapValue($theValue, $depth);
00133                 }
00134             }
00135             $HTML .= '<br />';
00136 
00137             if ($deeper) {
00138                 $HTML .= $this->tree($arr[$key], $depth, $depthData . '<img' .
00139                     t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/ol/' . $LN . '.gif', 'width="18" height="16"') .
00140                     ' align="top" alt="" />');
00141             }
00142         }
00143         return $HTML;
00144     }
00145 
00146     /**
00147      * Wrapping the value in bold tags etc.
00148      *
00149      * @param   string      The title string
00150      * @param   string      Depth path
00151      * @return  string      Title string, htmlspecialchars()'ed
00152      */
00153     function wrapValue($theValue, $depth) {
00154         $wrappedValue = '';
00155         if (strlen($theValue) > 0) {
00156             $wrappedValue = '<strong>' . htmlspecialchars($theValue) . '</strong>';
00157         }
00158         return $wrappedValue;
00159     }
00160 
00161     /**
00162      * Wrapping the value in bold tags etc.
00163      *
00164      * @param   string      The title string
00165      * @param   string      Depth path
00166      * @param   string      The value for the array entry.
00167      * @return  string      Title string, htmlspecialchars()'ed
00168      */
00169     function wrapArrayKey($label, $depth, $theValue) {
00170 
00171             // Protect label:
00172         $label = htmlspecialchars($label);
00173 
00174             // If varname is set:
00175         if ($this->varName && !$this->dontLinkVar) {
00176             $variableName = $this->varName . '[\'' . str_replace('.', '\'][\'', $depth) . '\'] = ' .
00177                 (!t3lib_div::testInt($theValue) ? '\'' . addslashes($theValue) . '\'' : $theValue) . '; ';
00178             $label = '<a href="index.php?varname=' . urlencode($variableName) . '#varname">' . $label . '</a>';
00179         }
00180 
00181             // Return:
00182         return '[' . $label . ']';
00183     }
00184 
00185     /**
00186      * Creates an array with "depthKeys" which will expand the array to show the search results
00187      *
00188      * @param   array       The array to search for the value
00189      * @param   string      Depth string - blank for first call (will build up during recursive calling creating an id of the position: [key1].[key2].[key3]
00190      * @param   string      The string to search for
00191      * @param   array       Key array, for first call pass empty array
00192      * @return  array
00193      */
00194     function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray) {
00195         $c = count($keyArr);
00196         if ($depth_in) {
00197             $depth_in = $depth_in . '.';
00198         }
00199         foreach ($keyArr as $key => $value) {
00200             $depth = $depth_in . $key;
00201             $deeper = is_array($keyArr[$key]);
00202 
00203             if ($this->regexMode) {
00204                 if (preg_match('/' . $searchString . '/', $keyArr[$key]) || ($this->searchKeysToo && preg_match('/' . $searchString . '/', $key))) {
00205                     $this->searchKeys[$depth] = 1;
00206                 }
00207             } else {
00208                 if ((!$deeper && stristr($keyArr[$key], $searchString)) || ($this->searchKeysToo && stristr($key, $searchString))) {
00209                     $this->searchKeys[$depth] = 1;
00210                 }
00211             }
00212 
00213             if ($deeper) {
00214                 $cS = count($this->searchKeys);
00215                 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
00216                 if ($cS != count($this->searchKeys)) {
00217                     $keyArray[$depth] = 1;
00218                 }
00219             }
00220         }
00221         return $keyArray;
00222     }
00223 
00224     /**
00225      * Fixed length function
00226      *
00227      * @param   string      String to process
00228      * @param   integer     Max number of chars
00229      * @return  string      Processed string
00230      */
00231     function fixed_lgd($string, $chars) {
00232         if ($chars >= 4) {
00233             if (strlen($string) > $chars) {
00234                 return substr($string, 0, $chars - 3) . '...';
00235             }
00236         }
00237         return $string;
00238     }
00239 
00240     /**
00241      * Function modifying the depthKey array
00242      *
00243      * @param   array       Array with instructions to open/close nodes.
00244      * @param   array       Input depth_key array
00245      * @return  array       Output depth_key array with entries added/removed based on $arr
00246      * @see SC_mod_tools_config_index::main()
00247      */
00248     function depthKeys($arr, $settings) {
00249         $tsbrArray = array();
00250         foreach ($arr as $theK => $theV) {
00251             $theKeyParts = explode('.', $theK);
00252             $depth = '';
00253             $c = count($theKeyParts);
00254             $a = 0;
00255             foreach ($theKeyParts as $p) {
00256                 $a++;
00257                 $depth .= ($depth ? '.' : '') . $p;
00258                 $tsbrArray[$depth] = ($c == $a) ? $theV : 1;
00259             }
00260         }
00261             // Modify settings
00262         foreach ($tsbrArray as $theK => $theV) {
00263             if ($theV) {
00264                 $settings[$theK] = 1;
00265             } else {
00266                 unset($settings[$theK]);
00267             }
00268         }
00269         return $settings;
00270     }
00271 }
00272 
00273 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php'])) {
00274     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php']);
00275 }
00276 ?>