class.t3lib_arraybrowser.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2008 Kasper Skaarhoj (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 3439 2008-03-16 19:16:51Z flyguide $
00031  * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
00032  * XHTML compliant
00033  *
00034  * @author  Kasper Skaarhoj <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 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 /**
00069  * Class for displaying an array as a tree
00070  * See the extension 'lowlevel' /config (Backend module 'Tools > Configuration')
00071  *
00072  * @author  Kasper Skaarhoj <kasperYYYY@typo3.com>
00073  * @package TYPO3
00074  * @subpackage t3lib
00075  * @see SC_mod_tools_config_index::main()
00076  */
00077 class t3lib_arrayBrowser    {
00078     var $expAll = FALSE;            // If set, will expand all (depthKeys is obsolete then) (and no links are applied)
00079     var $dontLinkVar = FALSE;       // If set, the variable keys are not linked.
00080     var $depthKeys = array();       // Array defining which keys to expand. Typically set from outside from some session variable - otherwise the array will collapse.
00081     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.
00082     var $fixedLgd=1;                // If set, the values are truncated with "..." appended if longer than a certain length.
00083     var $regexMode=0;               // If set, search for string with regex, otherwise stristr()
00084     var $searchKeysToo=FALSE;       // If set, array keys are subject to the search too.
00085     var $varName='';                // Set var name here if you want links to the variable name.
00086 
00087     /**
00088      * Make browsable tree
00089      * 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()
00090      *
00091      * @param   array       The array to display
00092      * @param   string      Key-position id. Build up during recursive calls - [key1].[key2].[key3] - an so on.
00093      * @param   string      Depth-data - basically a prefix for the icons. For calling this function from outside, let it stay blank.
00094      * @return  string      HTML for the tree
00095      * @see SC_mod_tools_config_index::main()
00096      */
00097     function tree($arr, $depth_in, $depthData)  {
00098         $HTML='';
00099         $a=0;
00100 
00101         if ($depth_in)  {$depth_in = $depth_in.'.';}
00102 
00103         $c=count($arr);
00104         reset($arr);
00105         while (list($key,)=each($arr))  {
00106             $a++;
00107             $depth = $depth_in.$key;
00108             $goto = substr(md5($depth),0,6);
00109 
00110             $deeper = (is_array($arr[$key]) && ($this->depthKeys[$depth] || $this->expAll)) ? 1 : 0;
00111             $PM = 'join';
00112             $LN = ($a==$c)?'blank':'line';
00113             $BTM = ($a==$c)?'bottom':'';
00114             $PM = is_array($arr[$key]) ? ($deeper ? 'minus':'plus') : 'join';
00115 
00116 
00117             $HTML.=$depthData;
00118             $theIcon='<img'.t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'],'gfx/ol/'.$PM.$BTM.'.gif','width="18" height="16"').' align="top" border="0" alt="" />';
00119             if ($PM=='join')    {
00120                 $HTML.=$theIcon;
00121             } else {
00122                 $HTML.=
00123                     ($this->expAll ? '' : '<a name="'.$goto.'" href="'.htmlspecialchars('index.php?node['.$depth.']='.($deeper?0:1).'#'.$goto).'">').
00124                     $theIcon.
00125                     ($this->expAll ? '' : '</a>');
00126             }
00127 
00128             $label = $key;
00129             $HTML.= $this->wrapArrayKey($label,$depth,!is_array($arr[$key]) ? $arr[$key] : '');
00130 
00131             if (!is_array($arr[$key]))  {
00132                 $theValue = $arr[$key];
00133                 if ($this->fixedLgd)    {
00134                     $imgBlocks = ceil(1+strlen($depthData)/77);
00135 //                  debug($imgBlocks);
00136                     $lgdChars = 68-ceil(strlen('['.$key.']')*0.8)-$imgBlocks*3;
00137                     $theValue = $this->fixed_lgd($theValue,$lgdChars);
00138                 }
00139                 if ($this->searchKeys[$depth])  {
00140                     $HTML.='=<span style="color:red;">'.$this->wrapValue($theValue,$depth).'</span>';
00141                 } else {
00142                     $HTML.='='.$this->wrapValue($theValue,$depth);
00143                 }
00144             }
00145             $HTML.='<br />';
00146 
00147             if ($deeper)    {
00148                 $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="" />');
00149             }
00150         }
00151         return $HTML;
00152     }
00153 
00154     /**
00155      * Wrapping the value in bold tags etc.
00156      *
00157      * @param   string      The title string
00158      * @param   string      Depth path
00159      * @return  string      Title string, htmlspecialchars()'ed
00160      */
00161     function wrapValue($theValue,$depth)    {
00162         return '<b>'.htmlspecialchars($theValue).'</b>';
00163     }
00164 
00165     /**
00166      * Wrapping the value in bold tags etc.
00167      *
00168      * @param   string      The title string
00169      * @param   string      Depth path
00170      * @param   string      The value for the array entry.
00171      * @return  string      Title string, htmlspecialchars()'ed
00172      */
00173     function wrapArrayKey($label,$depth,$theValue)  {
00174 
00175             // Protect label:
00176         $label = htmlspecialchars($label);
00177 
00178             // If varname is set:
00179         if ($this->varName && !$this->dontLinkVar) {
00180             $variableName = $this->varName.'[\''.str_replace('.','\'][\'',$depth).'\'] = '.(!t3lib_div::testInt($theValue) ? '\''.addslashes($theValue).'\'' : $theValue).'; ';
00181             $label = '<a href="'.htmlspecialchars('index.php?varname='.$variableName.'#varname').'">'.$label.'</a>';
00182         }
00183 
00184             // Return:
00185         return '['.$label.']';
00186     }
00187 
00188     /**
00189      * Creates an array with "depthKeys" which will expand the array to show the search results
00190      *
00191      * @param   array       The array to search for the value
00192      * @param   string      Depth string - blank for first call (will build up during recursive calling creating an id of the position: [key1].[key2].[key3]
00193      * @param   string      The string to search for
00194      * @param   array       Key array, for first call pass empty array
00195      * @return  array
00196      */
00197     function getSearchKeys($keyArr, $depth_in, $searchString, $keyArray)        {
00198         reset($keyArr);
00199         $c=count($keyArr);
00200         if ($depth_in)  {$depth_in = $depth_in.'.';}
00201         while (list($key,)=each($keyArr))   {
00202             $depth=$depth_in.$key;
00203             $deeper = is_array($keyArr[$key]);
00204 
00205             if ($this->regexMode)   {
00206                 if (ereg($searchString,$keyArr[$key]) || ($this->searchKeysToo && ereg($searchString,$key)))    {   $this->searchKeys[$depth]=1;    }
00207             } else {
00208                 if (stristr($keyArr[$key],$searchString) || ($this->searchKeysToo && stristr($key,$searchString)))  {   $this->searchKeys[$depth]=1;    }
00209             }
00210 
00211             if ($deeper)    {
00212                 $cS = count($this->searchKeys);
00213                 $keyArray = $this->getSearchKeys($keyArr[$key], $depth, $searchString, $keyArray);
00214                 if ($cS != count($this->searchKeys))    {
00215                     $keyArray[$depth]=1;
00216                 }
00217             }
00218         }
00219         return $keyArray;
00220     }
00221 
00222     /**
00223      * Fixed length function
00224      *
00225      * @param   string      String to process
00226      * @param   integer     Max number of chars
00227      * @return  string      Processed string
00228      */
00229     function fixed_lgd($string,$chars)  {
00230         if ($chars >= 4)    {
00231             if(strlen($string)>$chars)  {
00232                 return substr($string, 0, $chars-3).'...';
00233             }
00234         }
00235         return $string;
00236     }
00237 
00238     /**
00239      * Function modifying the depthKey array
00240      *
00241      * @param   array       Array with instructions to open/close nodes.
00242      * @param   array       Input depth_key array
00243      * @return  array       Output depth_key array with entries added/removed based on $arr
00244      * @see SC_mod_tools_config_index::main()
00245      */
00246     function depthKeys($arr,$settings)  {
00247         $tsbrArray=array();
00248         reset($arr);
00249         while(list($theK,$theV)=each($arr)) {
00250             $theKeyParts = explode('.',$theK);
00251             $depth='';
00252             $c=count($theKeyParts);
00253             $a=0;
00254             while(list(,$p)=each($theKeyParts)) {
00255                 $a++;
00256                 $depth.=($depth?'.':'').$p;
00257                 $tsbrArray[$depth]= ($c==$a) ? $theV : 1;
00258             }
00259         }
00260             // Modify settings
00261         reset($tsbrArray);
00262         while(list($theK,$theV)=each($tsbrArray))   {
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') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php'])  {
00274     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_arraybrowser.php']);
00275 }
00276 ?>

Generated on Sat Jan 3 04:23:26 2009 for TYPO3 API by  doxygen 1.4.7