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