TYPO3 API  SVNRelease
class.t3lib_tree_pagetree_dataprovider.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2010-2011 TYPO3 Tree Team <http://forge.typo3.org/projects/typo3v4-extjstrees>
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 /**
00029  * Abstract Tree Data Provider
00030  *
00031  * @author Stefan Galinski <stefan.galinski@gmail.com>
00032  * @package TYPO3
00033  * @subpackage t3lib
00034  */
00035 class t3lib_tree_pagetree_DataProvider extends t3lib_tree_AbstractDataProvider {
00036     /**
00037      * Node limit that should be loaded for this request per mount
00038      *
00039      * @var int
00040      */
00041     protected $nodeLimit = 0;
00042 
00043     /**
00044      * Current amount of nodes
00045      *
00046      * @var int
00047      */
00048     protected $nodeCounter = 0;
00049 
00050     /**
00051      * Hidden Records
00052      *
00053      * @var array
00054      */
00055     protected $hiddenRecords = array();
00056 
00057     /**
00058      * Constructor
00059      *
00060      * @param int $nodeLimit (optional)
00061      */
00062     public function __construct($nodeLimit = NULL) {
00063         if ($nodeLimit === NULL) {
00064             $nodeLimit = $GLOBALS['BE']['pageTree']['preloadLimit'];
00065         }
00066         $this->nodeLimit = abs(intval($nodeLimit));
00067 
00068         $this->hiddenRecords = t3lib_div::trimExplode(
00069             ',',
00070             $GLOBALS['BE_USER']->getTSConfigVal('options.hideRecords.pages')
00071         );
00072     }
00073 
00074     /**
00075      * Returns the root node
00076      *
00077      * @return t3lib_tree_Node
00078      */
00079     public function getRoot() {
00080         /** @var $node t3lib_tree_pagetree_Node */
00081         $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node');
00082         $node->setId('root');
00083         $node->setExpanded(true);
00084 
00085         return $node;
00086     }
00087 
00088     /**
00089      * Fetches the sub-nodes of the given node
00090      *
00091      * @param t3lib_tree_Node $node
00092      * @param int $mountPoint
00093      * @param int $level internally used variable as a recursion limiter
00094      * @return t3lib_tree_NodeCollection
00095      */
00096      public function getNodes(t3lib_tree_Node $node, $mountPoint = 0, $level = 0) {
00097         /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
00098         $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
00099         if ($level >= 99) {
00100             return $nodeCollection;
00101         }
00102 
00103         $subpages = $this->getSubpages($node->getId());
00104         if (!is_array($subpages) || !count($subpages)) {
00105             return $nodeCollection;
00106         }
00107 
00108         foreach ($subpages as $subpage) {
00109             if (in_array($subpage['uid'], $this->hiddenRecords)) {
00110                 continue;
00111             }
00112 
00113             $subpage = t3lib_befunc::getRecordWSOL('pages', $subpage['uid'], '*', '', TRUE, TRUE);
00114             if (!$subpage) {
00115                 continue;
00116             }
00117 
00118             $subNode = t3lib_tree_pagetree_Commands::getNewNode($subpage, $mountPoint);
00119             if ($this->nodeCounter < $this->nodeLimit) {
00120                 $childNodes = $this->getNodes($subNode, $mountPoint, $level + 1);
00121                 $subNode->setChildNodes($childNodes);
00122                 $this->nodeCounter += $childNodes->count();
00123             } else {
00124                 $subNode->setLeaf(!$this->hasNodeSubPages($subNode->getId()));
00125             }
00126 
00127             $nodeCollection->append($subNode);
00128         }
00129 
00130         return $nodeCollection;
00131     }
00132 
00133     /**
00134      * Returns a node collection of filtered nodes
00135      *
00136      * @param t3lib_tree_Node $node
00137      * @param string $searchFilter
00138      * @param int $mountPoint
00139      * @return void
00140      */
00141     public function getFilteredNodes(t3lib_tree_Node $node, $searchFilter, $mountPoint = 0) {
00142         /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
00143         $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
00144 
00145         $records = $this->getSubpages(-1, $searchFilter);
00146         if (!is_array($records) || !count($records)) {
00147             return $nodeCollection;
00148         }
00149 
00150         $isNumericSearchFilter = (is_numeric($searchFilter) && $searchFilter > 0);
00151         $nodeId = intval($node->getId());
00152         foreach ($records as $record) {
00153             $record = t3lib_tree_pagetree_Commands::getNodeRecord($record['uid']);
00154             if (intval($record['pid']) === -1 || in_array($record['uid'], $this->hiddenRecords)) {
00155                 continue;
00156             }
00157 
00158             $rootline = t3lib_BEfunc::BEgetRootLine($record['uid'], ' AND uid != ' . $nodeId);
00159             $rootline = array_reverse($rootline);
00160             if ($nodeId === 0) {
00161                 array_shift($rootline);
00162             }
00163             $reference = $nodeCollection;
00164 
00165             $inFilteredRootline = FALSE;
00166             $amountOfRootlineElements = count($rootline);
00167             for ($i = 0; $i < $amountOfRootlineElements; ++$i) {
00168                 $rootlineElement = $rootline[$i];
00169                 if (intval($rootlineElement['pid']) === $nodeId) {
00170                     $inFilteredRootline = TRUE;
00171                 }
00172 
00173                 if (!$inFilteredRootline) {
00174                     continue;
00175                 }
00176 
00177                 $rootlineElement = t3lib_tree_pagetree_Commands::getNodeRecord($rootlineElement['uid']);
00178                 $ident = intval($rootlineElement['sorting']) . intval($rootlineElement['uid']);
00179                 if ($reference->offsetExists($ident)) {
00180                     /** @var $refNode t3lib_tree_pagetree_Node */
00181                     $refNode = $reference->offsetGet($ident);
00182                     $refNode->setExpanded(TRUE);
00183                     $refNode->setLeaf(FALSE);
00184 
00185                     $reference = $refNode->getChildNodes();
00186                     continue;
00187                 }
00188 
00189                 $refNode = t3lib_tree_pagetree_Commands::getNewNode($rootlineElement, $mountPoint);
00190                 $replacement = '<span class="typo3-pagetree-filteringTree-highlight">$1</span>';
00191                 if ($isNumericSearchFilter && intval($rootlineElement['uid']) === intval($searchFilter)) {
00192                     $text = str_replace('$1', $refNode->getText(), $replacement);
00193                 } else {
00194                     $text = preg_replace('/(' . $searchFilter . ')/i', $replacement, $refNode->getText());
00195                 }
00196 
00197                 $refNode->setText(
00198                     $text,
00199                     $refNode->getTextSourceField(),
00200                     $refNode->getPrefix(),
00201                     $refNode->getSuffix()
00202                 );
00203 
00204                 /** @var $childCollection t3lib_tree_pagetree_NodeCollection */
00205                 $childCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
00206 
00207                 if (($i +1) >= $amountOfRootlineElements) {
00208                     $childNodes = $this->getNodes($refNode, $mountPoint);
00209                     foreach ($childNodes as $childNode) {
00210                         /** @var $childNode t3lib_tree_pagetree_Node */
00211                         $childRecord = $childNode->getRecord();
00212                         $childIdent = intval($childRecord['sorting']) . intval($childRecord['uid']);
00213                         $childCollection->offsetSet($childIdent, $childNode);
00214                     }
00215                     $refNode->setChildNodes($childNodes);
00216                 }
00217 
00218                 $refNode->setChildNodes($childCollection);
00219                 $reference->offsetSet($ident, $refNode);
00220                 $reference->ksort();
00221 
00222                 $reference = $childCollection;
00223             }
00224         }
00225 
00226         return $nodeCollection;
00227     }
00228 
00229     /**
00230      * Returns the page tree mounts for the current user
00231      *
00232      * Note: If you add the search filter parameter, the nodes will be filtered by this string.
00233      *
00234      * @param string $searchFilter
00235      * @return array
00236      */
00237     public function getTreeMounts($searchFilter = '') {
00238         /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
00239         $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
00240 
00241         $isTemporaryMountPoint = FALSE;
00242         $mountPoints = intval($GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint']);
00243         if (!$mountPoints) {
00244             $mountPoints = array_map('intval', $GLOBALS['BE_USER']->returnWebmounts());
00245             $mountPoints = array_unique($mountPoints);
00246         } else {
00247             $isTemporaryMountPoint = TRUE;
00248             $mountPoints = array($mountPoints);
00249         }
00250 
00251         if (!count($mountPoints)) {
00252             return $nodeCollection;
00253         }
00254 
00255         $showRootlineAboveMounts = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showPathAboveMounts');
00256         foreach ($mountPoints as $mountPoint) {
00257             if ($mountPoint === 0) {
00258                 $sitename = 'TYPO3';
00259                 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] !== '') {
00260                     $sitename = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
00261                 }
00262 
00263                 $record = array(
00264                     'uid' => 0,
00265                     'title' => $sitename,
00266                 );
00267                 $subNode = t3lib_tree_pagetree_Commands::getNewNode($record);
00268                 $subNode->setLabelIsEditable(FALSE);
00269                 $subNode->setType('pages_root');
00270             } else {
00271                 if (in_array($mountPoint, $this->hiddenRecords)) {
00272                     continue;
00273                 }
00274 
00275                 $record = t3lib_BEfunc::getRecordWSOL('pages', $mountPoint, '*', '', TRUE);
00276                 if (!$record) {
00277                     continue;
00278                 }
00279 
00280                 $subNode = t3lib_tree_pagetree_Commands::getNewNode($record, $mountPoint);
00281                 if ($showRootlineAboveMounts && !$isTemporaryMountPoint) {
00282                     $rootline = t3lib_tree_pagetree_Commands::getMountPointPath($record['uid']);
00283                     $subNode->setReadableRootline($rootline);
00284                 }
00285             }
00286 
00287             if (count($mountPoints) <= 1) {
00288                 $subNode->setExpanded(TRUE);
00289                 $subNode->setCls('typo3-pagetree-node-notExpandable');
00290             }
00291 
00292             $subNode->setIsMountPoint(TRUE);
00293             $subNode->setDraggable(FALSE);
00294             $subNode->setIsDropTarget(FALSE);
00295 
00296             if ($searchFilter === '') {
00297                 $childNodes = $this->getNodes($subNode, $mountPoint);
00298             } else {
00299                 $childNodes = $this->getFilteredNodes($subNode, $searchFilter, $mountPoint);
00300                 $subNode->setExpanded(TRUE);
00301             }
00302 
00303             $subNode->setChildNodes($childNodes);
00304             $nodeCollection->append($subNode);
00305         }
00306 
00307         return $nodeCollection;
00308     }
00309 
00310     /**
00311      * Returns the where clause for fetching pages
00312      *
00313      * @param int $id
00314      * @param string $searchFilter
00315      * @return string
00316      */
00317     protected function getWhereClause($id, $searchFilter = '') {
00318         $where = $GLOBALS['BE_USER']->getPagePermsClause(1) .
00319             t3lib_BEfunc::deleteClause('pages') .
00320             t3lib_BEfunc::versioningPlaceholderClause('pages');
00321 
00322         if (is_numeric($id) && $id >= 0) {
00323             $where .= ' AND pid= ' . $GLOBALS['TYPO3_DB']->fullQuoteStr(intval($id), 'pages');
00324         }
00325 
00326         if ($searchFilter !== '') {
00327             if (is_numeric($searchFilter) && $searchFilter > 0) {
00328                 $seachWhere .= 'uid = ' . intval($searchFilter) . ' OR ';
00329             }
00330 
00331             $searchFilter = $GLOBALS['TYPO3_DB']->fullQuoteStr('%' . $searchFilter . '%', 'pages');
00332             $useNavTitle = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showNavTitle');
00333 
00334             if ($useNavTitle) {
00335                 $seachWhere .= '(nav_title LIKE ' . $searchFilter .
00336                     ' OR (nav_title = "" && title LIKE ' . $searchFilter . '))';
00337             } else {
00338                 $seachWhere .= 'title LIKE ' . $searchFilter;
00339             }
00340 
00341             $where .= ' AND (' . $seachWhere . ')';
00342         }
00343 
00344         return $where;
00345     }
00346 
00347     /**
00348      * Returns all sub-pages of a given id
00349      *
00350      * @param int $id
00351      * @param string $searchFilter
00352      * @return array
00353      */
00354     protected function getSubpages($id, $searchFilter = '') {
00355         $where = $this->getWhereClause($id, $searchFilter);
00356         $subpages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
00357             'uid', 'pages', $where, '', 'sorting', '', 'uid'
00358         );
00359 
00360         return $subpages;
00361     }
00362 
00363     /**
00364      * Returns true if the node has child's
00365      *
00366      * @param int $id
00367      * @return bool
00368      */
00369     protected function hasNodeSubPages($id) {
00370         $where = $this->getWhereClause($id);
00371         $subpage = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
00372             'uid', 'pages', $where, '', 'sorting', '', 'uid'
00373         );
00374 
00375         $returnValue = TRUE;
00376         if (!$subpage['uid']) {
00377             $returnValue = FALSE;
00378         }
00379 
00380         return $returnValue;
00381     }
00382 }
00383 
00384 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/pagetree/class.t3lib_tree_pagetree_dataprovider.php'])) {
00385     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/pagetree/class.t3lib_tree_pagetree_dataprovider.php']);
00386 }
00387 
00388 ?>