class.t3lib_foldertree.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  * Generate a folder tree
00029  *
00030  * $Id: class.t3lib_foldertree.php 4568 2008-12-18 18:15:32Z dmitry $
00031  * Revised for TYPO3 3.6 November/2003 by Kasper Skaarhoj
00032  *
00033  * @author  Kasper Skaarhoj <kasperYYYY@typo3.com>
00034  * @coauthor    René Fritz <r.fritz@colorcube.de>
00035  */
00036 /**
00037  * [CLASS/FUNCTION INDEX of SCRIPT]
00038  *
00039  *
00040  *
00041  *   82: class t3lib_folderTree extends t3lib_treeView
00042  *   89:     function t3lib_folderTree()
00043  *  107:     function wrapIcon($icon,$row)
00044  *  130:     function wrapTitle($title,$row,$bank=0)
00045  *  145:     function getId($v)
00046  *  155:     function getJumpToParam($v)
00047  *  167:     function getTitleStr($row,$titleLen=30)
00048  *  177:     function getBrowsableTree()
00049  *  240:     function getFolderTree($files_path, $depth=999, $depthData='')
00050  *  320:     function getCount($files_path)
00051  *  336:     function initializePositionSaving()
00052  *
00053  * TOTAL FUNCTIONS: 10
00054  * (This index is automatically created/updated by the extension "extdeveval")
00055  *
00056  */
00057 
00058 require_once (PATH_t3lib.'class.t3lib_treeview.php');
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 /**
00074  * Extension class for the t3lib_treeView class, specially made for browsing folders in the File module
00075  *
00076  * @author  Kasper Skaarhoj <kasperYYYY@typo3.com>
00077  * @coauthor    René Fritz <r.fritz@colorcube.de>
00078  * @package TYPO3
00079  * @subpackage t3lib
00080  * @see class t3lib_treeView
00081  */
00082 class t3lib_folderTree extends t3lib_treeView  {
00083 
00084     /**
00085      * Constructor function of the class
00086      *
00087      * @return  void
00088      */
00089     function t3lib_folderTree() {
00090         parent::init();
00091 
00092         $this->MOUNTS = $GLOBALS['FILEMOUNTS'];
00093 
00094         $this->treeName='folder';
00095         $this->titleAttrib=''; //don't apply any title
00096         $this->domIdPrefix = 'folder';
00097     }
00098 
00099     /**
00100      * Wrapping the folder icon
00101      *
00102      * @param   string      The image tag for the icon
00103      * @param   array       The row for the current element
00104      * @return  string      The processed icon input value.
00105      * @access private
00106      */
00107     function wrapIcon($icon,$row)   {
00108             // Add title attribute to input icon tag
00109         $theFolderIcon = $this->addTagAttributes($icon,($this->titleAttrib ? $this->titleAttrib.'="'.$this->getTitleAttrib($row).'"' : ''));
00110 
00111             // Wrap icon in click-menu link.
00112         if (!$this->ext_IconMode)   {
00113             $theFolderIcon = $GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon($theFolderIcon,$row['path'],'',0);
00114         } elseif (!strcmp($this->ext_IconMode,'titlelink')) {
00115             $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$this->bank.');';
00116             $theFolderIcon='<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$theFolderIcon.'</a>';
00117         }
00118         return $theFolderIcon;
00119     }
00120 
00121     /**
00122      * Wrapping $title in a-tags.
00123      *
00124      * @param   string      Title string
00125      * @param   string      Item record
00126      * @param   integer     Bank pointer (which mount point number)
00127      * @return  string
00128      * @access private
00129      */
00130     function wrapTitle($title,$row,$bank=0) {
00131         $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$bank.');';
00132         $CSM = '';
00133         if ($GLOBALS['TYPO3_CONF_VARS']['BE']['useOnContextMenuHandler'])   {
00134             $CSM = ' oncontextmenu="'.htmlspecialchars($GLOBALS['TBE_TEMPLATE']->wrapClickMenuOnIcon('',$row['path'],'',0,'','',TRUE)).'"';
00135         }
00136         return '<a href="#" title="'.htmlspecialchars($row['title']).'" onclick="'.htmlspecialchars($aOnClick).'"'.$CSM.'>'.$title.'</a>';
00137     }
00138 
00139     /**
00140      * Returns the id from the record - for folders, this is an md5 hash.
00141      *
00142      * @param   array       Record array
00143      * @return  integer     The "uid" field value.
00144      */
00145     function getId($v)  {
00146         return t3lib_div::md5Int($v['path']);
00147     }
00148 
00149     /**
00150      * Returns jump-url parameter value.
00151      *
00152      * @param   array       The record array.
00153      * @return  string      The jump-url parameter.
00154      */
00155     function getJumpToParam($v) {
00156         return rawurlencode($v['path']);
00157     }
00158 
00159     /**
00160      * Returns the title for the input record. If blank, a "no title" labele (localized) will be returned.
00161      * '_title' is used for setting an alternative title for folders.
00162      *
00163      * @param   array       The input row array (where the key "_title" is used for the title)
00164      * @param   integer     Title length (30)
00165      * @return  string      The title.
00166      */
00167     function getTitleStr($row,$titleLen=30) {
00168         return $row['_title'] ? $row['_title'] : parent::getTitleStr($row,$titleLen);
00169     }
00170 
00171     /**
00172      * Will create and return the HTML code for a browsable tree of folders.
00173      * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
00174      *
00175      * @return  string      HTML code for the browsable tree
00176      */
00177     function getBrowsableTree() {
00178 
00179             // Get stored tree structure AND updating it if needed according to incoming PM GET var.
00180         $this->initializePositionSaving();
00181 
00182             // Init done:
00183         $titleLen=intval($this->BE_USER->uc['titleLen']);
00184         $treeArr=array();
00185 
00186             // Traverse mounts:
00187         foreach($this->MOUNTS as $key => $val)  {
00188             $md5_uid = md5($val['path']);
00189             $specUID=hexdec(substr($md5_uid,0,6));
00190             $this->specUIDmap[$specUID]=$val['path'];
00191 
00192                 // Set first:
00193             $this->bank=$val['nkey'];
00194             $isOpen = $this->stored[$val['nkey']][$specUID] || $this->expandFirst;
00195             $this->reset();
00196 
00197                 // Set PM icon:
00198             $cmd=$this->bank.'_'.($isOpen?'0_':'1_').$specUID.'_'.$this->treeName;
00199             $icon='<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.($isOpen?'minus':'plus').'only.gif','width="18" height="16"').' alt="" />';
00200             $firstHtml= $this->PM_ATagWrap($icon,$cmd);
00201 
00202             switch ($val['type']) {
00203                 case 'user':
00204                     $icon = 'gfx/i/_icon_ftp_user.gif';
00205                     break;
00206                 case 'group':
00207                     $icon = 'gfx/i/_icon_ftp_group.gif';
00208                     break;
00209                 case 'readonly':
00210                     $icon = 'gfx/i/_icon_ftp_readonly.gif';
00211                     break;
00212                 default:
00213                     $icon = 'gfx/i/_icon_ftp.gif';
00214                     break;
00215             }
00216 
00217                 // Preparing rootRec for the mount
00218             $firstHtml.=$this->wrapIcon('<img'.t3lib_iconWorks::skinImg($this->backPath,$icon,'width="18" height="16"').' alt="" />',$val);
00219                 $row=array();
00220                 $row['path']=$val['path'];
00221                 $row['uid']=$specUID;
00222                 $row['title']=$val['name'];
00223 
00224                 // Add the root of the mount to ->tree
00225             $this->tree[]=array('HTML'=>$firstHtml,'row'=>$row,'bank'=>$this->bank);
00226 
00227                 // If the mount is expanded, go down:
00228             if ($isOpen)    {
00229                     // Set depth:
00230                 $depthD='<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/blank.gif','width="18" height="16"').' alt="" />';
00231                 $this->getFolderTree($val['path'], 999, $depthD, $val['type']);
00232             }
00233 
00234                 // Add tree:
00235             $treeArr=array_merge($treeArr,$this->tree);
00236         }
00237         return $this->printTree($treeArr);
00238     }
00239 
00240     /**
00241      * Fetches the data for the tree
00242      *
00243      * @param   string      Abs file path
00244      * @param   integer     Max depth (recursivity limit)
00245      * @param   string      HTML-code prefix for recursive calls.
00246      * @return  integer     The count of items on the level
00247      * @see getBrowsableTree()
00248      */
00249     function getFolderTree($files_path, $depth=999, $depthData='', $type='') {
00250 
00251             // This generates the directory tree
00252         $dirs = t3lib_div::get_dirs($files_path);
00253 
00254         $c=0;
00255         if (is_array($dirs))    {
00256             $depth=intval($depth);
00257             $HTML='';
00258             $a=0;
00259             $c=count($dirs);
00260             sort($dirs);
00261 
00262             foreach($dirs as $key => $val)  {
00263                 $a++;
00264                 $this->tree[]=array();      // Reserve space.
00265                 end($this->tree);
00266                 $treeKey = key($this->tree);    // Get the key for this space
00267                 $LN = ($a==$c)?'blank':'line';
00268 
00269                 $val = ereg_replace('^\./','',$val);
00270                 $title = $val;
00271                 $path = $files_path.$val.'/';
00272                 $webpath=t3lib_BEfunc::getPathType_web_nonweb($path);
00273 
00274                 $md5_uid = md5($path);
00275                 $specUID=hexdec(substr($md5_uid,0,6));
00276                 $this->specUIDmap[$specUID]=$path;
00277                 $row=array();
00278                 $row['path']=$path;
00279                 $row['uid']=$specUID;
00280                 $row['title']=$title;
00281 
00282                 if ($depth>1 && $this->expandNext($specUID))    {
00283                     $nextCount=$this->getFolderTree(
00284                         $path,
00285                         $depth-1,
00286                         $this->makeHTML ? $depthData.'<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$LN.'.gif','width="18" height="16"').' alt="" />' : '',
00287                         $type
00288                     );
00289                     $exp=1;     // Set "did expand" flag
00290                 } else {
00291                     $nextCount=$this->getCount($path);
00292                     $exp=0;     // Clear "did expand" flag
00293                 }
00294 
00295                     // Set HTML-icons, if any:
00296                 if ($this->makeHTML)    {
00297                     $HTML=$depthData.$this->PMicon($row,$a,$c,$nextCount,$exp);
00298 
00299                     $icon = 'gfx/i/_icon_' .$webpath . 'folders' . ($type == 'readonly' ? '_ro' : '') . '.gif';
00300                     if ($val=='_temp_') {
00301                         $icon = 'gfx/i/sysf.gif';
00302                         $row['title'] = $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:temp', true);
00303                         $row['_title'] = '<strong>' . $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:temp', true) . '</strong>';
00304                     }
00305                     if ($val=='_recycler_') {
00306                         $icon = 'gfx/i/recycler.gif';
00307                         $row['title'] = $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:recycler', true);
00308                         $row['_title'] = '<strong>' . $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_file_list.xml:recycler', true) . '</strong>';
00309                     }
00310                     $HTML.=$this->wrapIcon('<img'.t3lib_iconWorks::skinImg($this->backPath,$icon,'width="18" height="16"').' alt="" />',$row);
00311                 }
00312 
00313                     // Finally, add the row/HTML content to the ->tree array in the reserved key.
00314                 $this->tree[$treeKey] = Array(
00315                     'row'=>$row,
00316                     'HTML'=>$HTML,
00317                     'bank'=>$this->bank
00318                 );
00319             }
00320         }
00321         return $c;
00322     }
00323 
00324     /**
00325      * Counts the number of directories in a file path.
00326      *
00327      * @param   string      File path.
00328      * @return  integer
00329      */
00330     function getCount($files_path)  {
00331             // This generates the directory tree
00332         $dirs = t3lib_div::get_dirs($files_path);
00333         $c=0;
00334         if (is_array($dirs))    {
00335             $c=count($dirs);
00336         }
00337         return $c;
00338     }
00339 
00340     /**
00341      * Get stored tree structure AND updating it if needed according to incoming PM GET var.
00342      *
00343      * @return  void
00344      * @access private
00345      */
00346     function initializePositionSaving() {
00347             // Get stored tree structure:
00348         $this->stored=unserialize($this->BE_USER->uc['browseTrees'][$this->treeName]);
00349 
00350             // Mapping md5-hash to shorter number:
00351         $hashMap=array();
00352         foreach($this->MOUNTS as $key => $val)  {
00353             $nkey = hexdec(substr($key,0,4));
00354             $hashMap[$nkey]=$key;
00355             $this->MOUNTS[$key]['nkey']=$nkey;
00356         }
00357 
00358             // PM action:
00359             // (If an plus/minus icon has been clicked, the PM GET var is sent and we must update the stored positions in the tree):
00360         $PM = explode('_',t3lib_div::_GP('PM'));    // 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
00361         if (count($PM)==4 && $PM[3]==$this->treeName)   {
00362             if (isset($this->MOUNTS[$hashMap[$PM[0]]])) {
00363                 if ($PM[1]) {   // set
00364                     $this->stored[$PM[0]][$PM[2]]=1;
00365                     $this->savePosition($this->treeName);
00366                 } else {    // clear
00367                     unset($this->stored[$PM[0]][$PM[2]]);
00368                     $this->savePosition($this->treeName);
00369                 }
00370             }
00371         }
00372     }
00373 }
00374 
00375 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php'])    {
00376     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_foldertree.php']);
00377 }
00378 
00379 ?>

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