Fixed bug #14050: CleanUp - CGL format of t3lib files - t3lib_scbase
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_treeview.php
old mode 100755 (executable)
new mode 100644 (file)
index f28c767..6e05fe5
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
+*  (c) 1999-2010 Kasper Skårhøj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
  * Contains base class for creating a browsable array/page/folder tree in HTML
  *
  * $Id$
- * Revised for TYPO3 3.6 November/2003 by Kasper Skaarhoj
+ * Revised for TYPO3 3.6 November/2003 by Kasper Skårhøj
  *
- * @author     Kasper Skaarhoj <kasperYYYY@typo3.com>
- * @coauthor   René Fritz <r.fritz@colorcube.de>
+ * @author     Kasper Skårhøj <kasperYYYY@typo3.com>
+ * @coauthor   René Fritz <r.fritz@colorcube.de>
  */
 /**
  * [CLASS/FUNCTION INDEX of SCRIPT]
  *
  *
  *  115: class t3lib_treeView
- *  267:     function init($clause='', $orderByFields='')
- *  298:     function setTreeName($treeName='')
- *  312:     function addField($field,$noCheck=0)
- *  326:     function reset()
+ *  270:     function init($clause='', $orderByFields='')
+ *  301:     function setTreeName($treeName='')
+ *  315:     function addField($field,$noCheck=0)
+ *  329:     function reset()
  *
  *              SECTION: output
- *  346:     function getBrowsableTree()
- *  407:     function printTree($treeArr='')
+ *  349:     function getBrowsableTree()
+ *  412:     function printTree($treeArr='')
  *
  *              SECTION: rendering parts
- *  460:     function PMicon($row,$a,$c,$nextCount,$exp)
- *  482:     function PM_ATagWrap($icon,$cmd,$bMark='')
- *  504:     function wrapTitle($title,$row,$bank=0)
- *  517:     function wrapIcon($icon,$row)
- *  528:     function addTagAttributes($icon,$attr)
- *  540:     function wrapStop($str,$row)
+ *  467:     function PMicon($row,$a,$c,$nextCount,$exp)
+ *  489:     function PM_ATagWrap($icon,$cmd,$bMark='')
+ *  511:     function wrapTitle($title,$row,$bank=0)
+ *  524:     function wrapIcon($icon,$row)
+ *  535:     function addTagAttributes($icon,$attr)
+ *  547:     function wrapStop($str,$row)
  *
  *              SECTION: tree handling
- *  568:     function expandNext($id)
- *  578:     function initializePositionSaving()
- *  605:     function savePosition()
+ *  575:     function expandNext($id)
+ *  585:     function initializePositionSaving()
+ *  612:     function savePosition()
  *
  *              SECTION: Functions that might be overwritten by extended classes
- *  634:     function getRootIcon($rec)
- *  647:     function getIcon($row)
- *  666:     function getTitleStr($row,$titleLen=30)
- *  678:     function getTitleAttrib($row)
- *  688:     function getId($row)
- *  698:     function getJumpToParam($row)
+ *  641:     function getRootIcon($rec)
+ *  654:     function getIcon($row)
+ *  673:     function getTitleStr($row,$titleLen=30)
+ *  685:     function getTitleAttrib($row)
+ *  695:     function getId($row)
+ *  705:     function getJumpToParam($row)
  *
  *              SECTION: tree data buidling
- *  731:     function getTree($uid, $depth=999, $depthData='',$blankLineCode='')
+ *  739:     function getTree($uid, $depth=999, $depthData='',$blankLineCode='',$subCSSclass='')
  *
  *              SECTION: Data handling
- *  828:     function getCount($uid)
- *  855:     function getRootRecord($uid)
- *  868:     function getRecord($uid)
- *  885:     function getDataInit($parentId)
- *  915:     function getDataCount(&$res)
- *  932:     function getDataNext(&$res)
- *  960:     function getDataFree(&$res)
- *  980:     function setDataFromArray(&$dataArr,$traverse=FALSE,$pid=0)
- * 1009:     function setDataFromTreeArray(&$treeArr, &$treeLookupArr)
+ *  839:     function getCount($uid)
+ *  865:     function getRootRecord($uid)
+ *  878:     function getRecord($uid)
+ *  898:     function getDataInit($parentId,$subCSSclass='')
+ *  929:     function getDataCount(&$res)
+ *  947:     function getDataNext(&$res,$subCSSclass='')
+ *  986:     function getDataFree(&$res)
+ * 1006:     function setDataFromArray(&$dataArr,$traverse=FALSE,$pid=0)
+ * 1035:     function setDataFromTreeArray(&$treeArr, &$treeLookupArr)
  *
  * TOTAL FUNCTIONS: 31
  * (This index is automatically created/updated by the extension "extdeveval")
 
 
 
-require_once (PATH_t3lib.'class.t3lib_iconworks.php');
-require_once (PATH_t3lib.'class.t3lib_befunc.php');
-require_once (PATH_t3lib.'class.t3lib_div.php');
 
 
 /**
  * Base class for creating a browsable array/page/folder tree in HTML
  *
- * @author     Kasper Skaarhoj <kasperYYYY@typo3.com>
- * @coauthor   René Fritz <r.fritz@colorcube.de>
+ * @author     Kasper Skårhøj <kasperYYYY@typo3.com>
+ * @coauthor   René Fritz <r.fritz@colorcube.de>
  * @package TYPO3
  * @subpackage t3lib
  * @see t3lib_browsetree, t3lib_pagetree, t3lib_foldertree
@@ -123,10 +120,13 @@ class t3lib_treeView {
        var $addSelfId = 0;                             // If set, the id of the mounts will be added to the internal ids array
        var $title='no title';                  // Used if the tree is made of records (not folders for ex.)
        var $showDefaultTitleAttribute = FALSE;         // If true, a default title attribute showing the UID of the record is shown. This cannot be enabled by default because it will destroy many applications where another title attribute is in fact applied later.
+       var $highlightPagesWithVersions = TRUE;         // If true, pages containing child records which has versions will be highlighted in yellow. This might be too expensive in terms of processing power.
 
        /**
         * Needs to be initialized with $GLOBALS['BE_USER']
         * Done by default in init()
+        *
+        * @var t3lib_beUserAuth
         */
        var $BE_USER='';
 
@@ -237,6 +237,7 @@ class t3lib_treeView {
                // For record trees:
        var $ids = Array();                             // one-dim array of the uid's selected.
        var $ids_hierarchy = array();   // The hierarchy of element uids
+       var $orig_ids_hierarchy = array();      // The hierarchy of versioned element uids
        var $buffer_idH = array();              // Temporary, internal array
 
                // For FOLDER trees:
@@ -321,7 +322,7 @@ class t3lib_treeView {
 
 
        /**
-        * Resets the tree, recs, ids, and ids_hierarchy internal variables. Use it if you need it.
+        * Resets the tree, recs, ids, ids_hierarchy and orig_ids_hierarchy internal variables. Use it if you need it.
         *
         * @return      void
         */
@@ -330,6 +331,7 @@ class t3lib_treeView {
                $this->recs = array();
                $this->ids = array();
                $this->ids_hierarchy = array();
+               $this->orig_ids_hierarchy = array();
        }
 
 
@@ -508,7 +510,7 @@ class t3lib_treeView {
         * @access private
         */
        function wrapTitle($title,$row,$bank=0) {
-               $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'_'.$bank.'\');';
+               $aOnClick = 'return jumpTo(\''.$this->getJumpToParam($row).'\',this,\''.$this->domIdPrefix.$this->getId($row).'\','.$bank.');';
                return '<a href="#" onclick="'.htmlspecialchars($aOnClick).'">'.$title.'</a>';
        }
 
@@ -532,7 +534,7 @@ class t3lib_treeView {
         * @return      string          Image tag, modified with $attr attributes added.
         */
        function addTagAttributes($icon,$attr)  {
-               return ereg_replace(' ?\/?>$','',$icon).' '.$attr.' />';
+               return preg_replace('/ ?\/?>$/','',$icon).' '.$attr.' />';
        }
 
        /**
@@ -545,7 +547,7 @@ class t3lib_treeView {
         */
        function wrapStop($str,$row)    {
                if ($row['php_tree_stop'])      {
-                       $str.='<span class="typo3-red">+ </span>';
+                       $str .= '<span class="typo3-red"><a href="' . htmlspecialchars(t3lib_div::linkThisScript(array('setTempDBmount' => $row['uid']))) . '" class="typo3-red">+</a> </span>';
                }
                return $str;
        }
@@ -638,7 +640,7 @@ class t3lib_treeView {
         * @return      string          Icon image tag.
         */
        function getRootIcon($rec) {
-               return $this->wrapIcon('<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/i/_icon_website.gif','width="18" height="16"').' alt="" />',$rec);
+               return $this->wrapIcon(t3lib_iconWorks::getSpriteIcon('apps-pagetree-root'),$rec);
        }
 
 
@@ -654,7 +656,12 @@ class t3lib_treeView {
                if ($this->iconPath && $this->iconName) {
                        $icon = '<img'.t3lib_iconWorks::skinImg('',$this->iconPath.$this->iconName,'width="18" height="16"').' alt=""'.($this->showDefaultTitleAttribute ? ' title="UID: '.$row['uid'].'"':'').' />';
                } else {
-                       $icon = t3lib_iconWorks::getIconImage($this->table,$row,$this->backPath,'align="top" class="c-recIcon"'.($this->showDefaultTitleAttribute ? ' title="UID: '.$row['uid'].'"':''));
+
+                       $icon = t3lib_iconWorks::getSpriteIconForRecord($this->table, $row, array(
+                               'title' => ($this->showDefaultTitleAttribute ? 'UID: ' . $row['uid'] : $this->getTitleAttrib($row)),
+                               'class' => 'c-recIcon'
+                       ));
+
                }
 
                return $this->wrapIcon($icon,$row);
@@ -662,7 +669,7 @@ class t3lib_treeView {
 
 
        /**
-        * Returns the title for the input record. If blank, a "no title" labele (localized) will be returned.
+        * Returns the title for the input record. If blank, a "no title" label (localized) will be returned.
         * Do NOT htmlspecialchar the string from this function - has already been done.
         *
         * @param       array           The input row array (where the key "title" is used for the title)
@@ -670,7 +677,16 @@ class t3lib_treeView {
         * @return      string          The title.
         */
        function getTitleStr($row,$titleLen=30) {
-               $title = (!strcmp(trim($row['title']),'')) ? '<em>['.$GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:labels.no_title',1).']</em>' : htmlspecialchars(t3lib_div::fixed_lgd_cs($row['title'],$titleLen));
+               if ($this->ext_showNavTitle && strlen(trim($row['nav_title'])) > 0)     {
+                       $title = '<span title="'.$GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_tca.xml:title',1).' '.htmlspecialchars(trim($row['title'])).'">'.htmlspecialchars(t3lib_div::fixed_lgd_cs($row['nav_title'],$titleLen)).'</span>';
+               } else {
+                       $title = htmlspecialchars(t3lib_div::fixed_lgd_cs($row['title'],$titleLen));
+                       if (strlen(trim($row['nav_title'])) > 0)        {
+                               $title = '<span title="'.$GLOBALS['LANG']->sL('LLL:EXT:cms/locallang_tca.xml:pages.nav_title',1).' '.htmlspecialchars(trim($row['nav_title'])).'">'.$title.'</span>';
+                       }
+                       $title = (strlen(trim($row['title'])) == 0) ? '<em>['.$GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:labels.no_title',1).']</em>' : $title;
+               }
+
                return $title;
        }
 
@@ -740,7 +756,6 @@ class t3lib_treeView {
                        // Buffer for id hierarchy is reset:
                $this->buffer_idH=array();
 
-
                        // Init vars
                $depth=intval($depth);
                $HTML='';
@@ -750,12 +765,19 @@ class t3lib_treeView {
                $c = $this->getDataCount($res);
                $crazyRecursionLimiter = 999;
 
+               $idH = array();
+
                        // Traverse the records:
                while ($crazyRecursionLimiter>0 && $row = $this->getDataNext($res,$subCSSclass))        {
                        $a++;
                        $crazyRecursionLimiter--;
 
-                       $newID =$row['uid'];
+                       $newID = $row['uid'];
+
+                       if ($newID == 0) {
+                               throw new RuntimeException('Endless recursion detected: TYPO3 has detected an error in the database. Please fix it manually (e.g. using phpMyAdmin) and change the UID of '.$this->table.':0 to a new value.<br /><br />See <a href="http://bugs.typo3.org/view.php?id=3495" target="_blank">bugs.typo3.org/view.php?id=3495</a> to get more information about a possible cause.');
+                       }
+
                        $this->tree[]=array();          // Reserve space.
                        end($this->tree);
                        $treeKey = key($this->tree);    // Get the key for this space
@@ -767,8 +789,9 @@ class t3lib_treeView {
                        }
 
                                // Accumulate the id of the element in the internal arrays
-                       $this->ids[]=$idH[$row['uid']]['uid'] = $row['uid'];
+                       $this->ids[] = $idH[$row['uid']]['uid'] = $row['uid'];
                        $this->ids_hierarchy[$depth][] = $row['uid'];
+                       $this->orig_ids_hierarchy[$depth][] = $row['_ORIG_uid'] ? $row['_ORIG_uid'] : $row['uid'];
 
                                // Make a recursive call to the next level
                        $HTML_depthData = $depthData.'<img'.t3lib_iconWorks::skinImg($this->backPath,'gfx/ol/'.$LN.'.gif','width="18" height="16"').' alt="" />';
@@ -840,18 +863,14 @@ class t3lib_treeView {
                        $res = $this->getDataInit($uid);
                        return $this->getDataCount($res);
                } else {
-                       $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
-                                               'count(*)',
-                                               $this->table,
-                                               $this->parentField.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($uid, $this->table).
-                                                       t3lib_BEfunc::deleteClause($this->table).
-                                                       t3lib_BEfunc::versioningPlaceholderClause($this->table).
-                                                       $this->clause,  // whereClauseMightContainGroupOrderBy
-                                               '',
-                                               '' //$this->orderByFields // sorting is senseless for count(*) !?!
-                                       );
-                       $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
-                       return $row[0];
+                       return $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
+                               'uid',
+                               $this->table,
+                               $this->parentField . '=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($uid, $this->table) .
+                                       t3lib_BEfunc::deleteClause($this->table) .
+                                       t3lib_BEfunc::versioningPlaceholderClause($this->table) .
+                                       $this->clause   // whereClauseMightContainGroupOrderBy
+                       );
                }
        }
 
@@ -864,7 +883,7 @@ class t3lib_treeView {
         * @return      array           Array with title/uid keys with values of $this->title/0 (zero)
         */
        function getRootRecord($uid) {
-               return array('title'=>$this->title, 'uid'=>0);
+               return array('title' => $this->title, 'uid' => 0);
        }
 
 
@@ -880,10 +899,7 @@ class t3lib_treeView {
                if (is_array($this->data)) {
                        return $this->dataLookup[$uid];
                } else {
-                       $row = t3lib_befunc::getRecord($this->table,$uid);
-                       t3lib_BEfunc::workspaceOL($this->table, $row, $this->BE_USER->workspace);
-
-                       return $row;
+                       return t3lib_BEfunc::getRecordWSOL($this->table, $uid);
                }
        }
 
@@ -893,6 +909,7 @@ class t3lib_treeView {
         * For arrays: This will return key to the ->dataLookup array
         *
         * @param       integer         parent item id
+        * @param       string          Class for sub-elements.
         * @return      mixed           data handle (Tables: An sql-resource, arrays: A parentId integer. -1 is returned if there were NO subLevel.)
         * @access private
         */
@@ -940,11 +957,12 @@ class t3lib_treeView {
         * Getting the tree data: next entry
         *
         * @param       mixed           data handle
+        * @param       string          CSS class for sub elements (workspace related)
         * @return      array           item data array OR FALSE if end of elements.
         * @access private
         * @see getDataInit()
         */
-       function getDataNext(&$res,$subCSSclass=''){
+       function getDataNext(&$res,$subCSSclass='')     {
                if (is_array($this->data)) {
                        if ($res<0) {
                                $row=FALSE;
@@ -958,11 +976,18 @@ class t3lib_treeView {
                        }
                        return $row;
                } else {
-                       $row = @$GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
-                       t3lib_BEfunc::workspaceOL($this->table, $row, $this->BE_USER->workspace);
+                       while($row = @$GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))      {
+                               t3lib_BEfunc::workspaceOL($this->table, $row, $this->BE_USER->workspace, TRUE);
+                               if (is_array($row))     break;
+                       }
 
                                // Passing on default <td> class for subelements:
                        if (is_array($row) && $subCSSclass!=='')        {
+
+                               if ($this->table==='pages' && $this->highlightPagesWithVersions && !isset($row['_CSSCLASS']) && count(t3lib_BEfunc::countVersionsOfRecordsOnPage($this->BE_USER->workspace, $row['uid'], TRUE)))        {
+                                       $row['_CSSCLASS'] = 'ver-versions';
+                               }
+
                                if (!isset($row['_CSSCLASS']))  $row['_CSSCLASS'] = $subCSSclass;
                                if (!isset($row['_SUBCSSCLASS']))       $row['_SUBCSSCLASS'] = $subCSSclass;
                        }
@@ -978,10 +1003,8 @@ class t3lib_treeView {
         * @return      void
         * @access private
         */
-       function getDataFree(&$res){
-               if (is_array($this->data)) {
-               #       unset();
-               } else {
+       function getDataFree(&$res) {
+               if (!is_array($this->data)) {
                        $GLOBALS['TYPO3_DB']->sql_free_result($res);
                }
        }
@@ -1075,4 +1098,5 @@ class t3lib_treeView {
 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_treeview.php']) {
        include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_treeview.php']);
 }
-?>
+
+?>
\ No newline at end of file