[FEATURE] pagetree dataprovider should enable node postprocessing
[Packages/TYPO3.CMS.git] / t3lib / tree / pagetree / class.t3lib_tree_pagetree_dataprovider.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 TYPO3 Tree Team <http://forge.typo3.org/projects/typo3v4-extjstrees>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Abstract Tree Data Provider
30 *
31 * @author Stefan Galinski <stefan.galinski@gmail.com>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 class t3lib_tree_pagetree_DataProvider extends t3lib_tree_AbstractDataProvider {
36 /**
37 * Node limit that should be loaded for this request per mount
38 *
39 * @var int
40 */
41 protected $nodeLimit = 0;
42
43 /**
44 * Current amount of nodes
45 *
46 * @var int
47 */
48 protected $nodeCounter = 0;
49
50 /**
51 * Hidden Records
52 *
53 * @var array
54 */
55 protected $hiddenRecords = array();
56
57 /**
58 * Process collection hook objects
59 *
60 * @var array<t3lib_tree_pagetree_interfaces_collectionprocessor>
61 */
62 protected $processCollectionHookObjects = array();
63
64 /**
65 * Constructor
66 *
67 * @param int $nodeLimit (optional)
68 */
69 public function __construct($nodeLimit = NULL) {
70 if ($nodeLimit === NULL) {
71 $nodeLimit = $GLOBALS['TYPO3_CONF_VARS']['BE']['pageTree']['preloadLimit'];
72 }
73 $this->nodeLimit = abs(intval($nodeLimit));
74
75 $this->hiddenRecords = t3lib_div::trimExplode(
76 ',',
77 $GLOBALS['BE_USER']->getTSConfigVal('options.hideRecords.pages')
78 );
79
80 $hookElements = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/tree/pagetree/class.t3lib_tree_pagetree_dataprovider.php']['postProcessCollections'];
81 if (is_array($hookElements)) {
82 foreach ($hookElements as $classRef) {
83 /** @var $hookObject t3lib_tree_pagetree_interfaces_collectionprocessor */
84 $hookObject = t3lib_div::getUserObj($classRef);
85 if ($hookObject instanceof t3lib_tree_pagetree_interfaces_collectionprocessor) {
86 $this->processCollectionHookObjects[] = $hookObject;
87 }
88 }
89 }
90 }
91
92 /**
93 * Returns the root node
94 *
95 * @return t3lib_tree_Node
96 */
97 public function getRoot() {
98 /** @var $node t3lib_tree_pagetree_Node */
99 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node');
100 $node->setId('root');
101 $node->setExpanded(TRUE);
102
103 return $node;
104 }
105
106 /**
107 * Fetches the sub-nodes of the given node
108 *
109 * @param t3lib_tree_Node $node
110 * @param int $mountPoint
111 * @param int $level internally used variable as a recursion limiter
112 * @return t3lib_tree_NodeCollection
113 */
114 public function getNodes(t3lib_tree_Node $node, $mountPoint = 0, $level = 0) {
115 /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
116 $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
117
118 if ($level >= 99) {
119 return $nodeCollection;
120 }
121
122 $subpages = $this->getSubpages($node->getId());
123 if (!is_array($subpages) || !count($subpages)) {
124 return $nodeCollection;
125 }
126
127 foreach ($subpages as $subpage) {
128 if (in_array($subpage['uid'], $this->hiddenRecords)) {
129 continue;
130 }
131
132 $subpage = t3lib_befunc::getRecordWSOL('pages', $subpage['uid'], '*', '', TRUE, TRUE);
133 if (!$subpage) {
134 continue;
135 }
136
137 $subNode = t3lib_tree_pagetree_Commands::getNewNode($subpage, $mountPoint);
138 if ($this->nodeCounter < $this->nodeLimit) {
139 $childNodes = $this->getNodes($subNode, $mountPoint, $level + 1);
140 $subNode->setChildNodes($childNodes);
141 $this->nodeCounter += $childNodes->count();
142 } else {
143 $subNode->setLeaf(!$this->hasNodeSubPages($subNode->getId()));
144 }
145
146 $nodeCollection->append($subNode);
147 }
148
149 foreach ($this->processCollectionHookObjects as $hookObject) {
150 /** @var $hookObject t3lib_tree_pagetree_interfaces_collectionprocessor */
151 $hookObject->postProcessGetNodes($node, $mountPoint, $level, $nodeCollection);
152 }
153
154 return $nodeCollection;
155 }
156
157 /**
158 * Returns a node collection of filtered nodes
159 *
160 * @param t3lib_tree_Node $node
161 * @param string $searchFilter
162 * @param int $mountPoint
163 * @return void
164 */
165 public function getFilteredNodes(t3lib_tree_Node $node, $searchFilter, $mountPoint = 0) {
166 /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
167 $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
168
169 $records = $this->getSubpages(-1, $searchFilter);
170 if (!is_array($records) || !count($records)) {
171 return $nodeCollection;
172 }
173
174 $isNumericSearchFilter = (is_numeric($searchFilter) && $searchFilter > 0);
175 $nodeId = intval($node->getId());
176 foreach ($records as $record) {
177 $record = t3lib_tree_pagetree_Commands::getNodeRecord($record['uid']);
178 if (intval($record['pid']) === -1 || in_array($record['uid'], $this->hiddenRecords)) {
179 continue;
180 }
181
182 $rootline = t3lib_BEfunc::BEgetRootLine($record['uid'], ' AND uid != ' . $nodeId);
183 $rootline = array_reverse($rootline);
184 if ($nodeId === 0) {
185 array_shift($rootline);
186 }
187 $reference = $nodeCollection;
188
189 $inFilteredRootline = FALSE;
190 $amountOfRootlineElements = count($rootline);
191 for ($i = 0; $i < $amountOfRootlineElements; ++$i) {
192 $rootlineElement = $rootline[$i];
193 if (intval($rootlineElement['pid']) === $nodeId) {
194 $inFilteredRootline = TRUE;
195 }
196
197 if (!$inFilteredRootline) {
198 continue;
199 }
200
201 $rootlineElement = t3lib_tree_pagetree_Commands::getNodeRecord($rootlineElement['uid']);
202 $ident = intval($rootlineElement['sorting']) . intval($rootlineElement['uid']);
203 if ($reference->offsetExists($ident)) {
204 /** @var $refNode t3lib_tree_pagetree_Node */
205 $refNode = $reference->offsetGet($ident);
206 $refNode->setExpanded(TRUE);
207 $refNode->setLeaf(FALSE);
208
209 $reference = $refNode->getChildNodes();
210 continue;
211 }
212
213 $refNode = t3lib_tree_pagetree_Commands::getNewNode($rootlineElement, $mountPoint);
214 $replacement = '<span class="typo3-pagetree-filteringTree-highlight">$1</span>';
215 if ($isNumericSearchFilter && intval($rootlineElement['uid']) === intval($searchFilter)) {
216 $text = str_replace('$1', $refNode->getText(), $replacement);
217 } else {
218 $text = preg_replace('/(' . $searchFilter . ')/i', $replacement, $refNode->getText());
219 }
220
221 $refNode->setText(
222 $text,
223 $refNode->getTextSourceField(),
224 $refNode->getPrefix(),
225 $refNode->getSuffix()
226 );
227
228 /** @var $childCollection t3lib_tree_pagetree_NodeCollection */
229 $childCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
230
231 if (($i +1) >= $amountOfRootlineElements) {
232 $childNodes = $this->getNodes($refNode, $mountPoint);
233 foreach ($childNodes as $childNode) {
234 /** @var $childNode t3lib_tree_pagetree_Node */
235 $childRecord = $childNode->getRecord();
236 $childIdent = intval($childRecord['sorting']) . intval($childRecord['uid']);
237 $childCollection->offsetSet($childIdent, $childNode);
238 }
239 $refNode->setChildNodes($childNodes);
240 }
241
242 $refNode->setChildNodes($childCollection);
243 $reference->offsetSet($ident, $refNode);
244 $reference->ksort();
245
246 $reference = $childCollection;
247 }
248 }
249
250 foreach ($this->processCollectionHookObjects as $hookObject) {
251 /** @var $hookObject t3lib_tree_pagetree_interfaces_collectionprocessor */
252 $hookObject->postProcessFilteredNodes($node, $searchFilter, $mountPoint, $nodeCollection);
253 }
254
255 return $nodeCollection;
256 }
257
258 /**
259 * Returns the page tree mounts for the current user
260 *
261 * Note: If you add the search filter parameter, the nodes will be filtered by this string.
262 *
263 * @param string $searchFilter
264 * @return array
265 */
266 public function getTreeMounts($searchFilter = '') {
267 /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
268 $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
269
270 $isTemporaryMountPoint = FALSE;
271 $mountPoints = intval($GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint']);
272 if (!$mountPoints) {
273 $mountPoints = array_map('intval', $GLOBALS['BE_USER']->returnWebmounts());
274 $mountPoints = array_unique($mountPoints);
275 } else {
276 $isTemporaryMountPoint = TRUE;
277 $mountPoints = array($mountPoints);
278 }
279
280 if (!count($mountPoints)) {
281 return $nodeCollection;
282 }
283
284 $showRootlineAboveMounts = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showPathAboveMounts');
285 foreach ($mountPoints as $mountPoint) {
286 if ($mountPoint === 0) {
287 $sitename = 'TYPO3';
288 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] !== '') {
289 $sitename = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
290 }
291
292 $record = array(
293 'uid' => 0,
294 'title' => $sitename,
295 );
296 $subNode = t3lib_tree_pagetree_Commands::getNewNode($record);
297 $subNode->setLabelIsEditable(FALSE);
298 $subNode->setType('pages_root');
299 } else {
300 if (in_array($mountPoint, $this->hiddenRecords)) {
301 continue;
302 }
303
304 $record = t3lib_BEfunc::getRecordWSOL('pages', $mountPoint, '*', '', TRUE);
305 if (!$record) {
306 continue;
307 }
308
309 $subNode = t3lib_tree_pagetree_Commands::getNewNode($record, $mountPoint);
310 if ($showRootlineAboveMounts && !$isTemporaryMountPoint) {
311 $rootline = t3lib_tree_pagetree_Commands::getMountPointPath($record['uid']);
312 $subNode->setReadableRootline($rootline);
313 }
314 }
315
316 if (count($mountPoints) <= 1) {
317 $subNode->setExpanded(TRUE);
318 $subNode->setCls('typo3-pagetree-node-notExpandable');
319 }
320
321 $subNode->setIsMountPoint(TRUE);
322 $subNode->setDraggable(FALSE);
323 $subNode->setIsDropTarget(FALSE);
324
325 if ($searchFilter === '') {
326 $childNodes = $this->getNodes($subNode, $mountPoint);
327 } else {
328 $childNodes = $this->getFilteredNodes($subNode, $searchFilter, $mountPoint);
329 $subNode->setExpanded(TRUE);
330 }
331
332 $subNode->setChildNodes($childNodes);
333 $nodeCollection->append($subNode);
334 }
335
336 foreach ($this->processCollectionHookObjects as $hookObject) {
337 /** @var $hookObject t3lib_tree_pagetree_interfaces_collectionprocessor */
338 $hookObject->postProcessGetTreeMounts($searchFilter, $nodeCollection);
339 }
340
341 return $nodeCollection;
342 }
343
344 /**
345 * Returns the where clause for fetching pages
346 *
347 * @param int $id
348 * @param string $searchFilter
349 * @return string
350 */
351 protected function getWhereClause($id, $searchFilter = '') {
352 $where = $GLOBALS['BE_USER']->getPagePermsClause(1) .
353 t3lib_BEfunc::deleteClause('pages') .
354 t3lib_BEfunc::versioningPlaceholderClause('pages');
355
356 if (is_numeric($id) && $id >= 0) {
357 $where .= ' AND pid= ' . $GLOBALS['TYPO3_DB']->fullQuoteStr(intval($id), 'pages');
358 }
359
360 if ($searchFilter !== '') {
361 if (is_numeric($searchFilter) && $searchFilter > 0) {
362 $seachWhere .= 'uid = ' . intval($searchFilter) . ' OR ';
363 }
364
365 $searchFilter = $GLOBALS['TYPO3_DB']->fullQuoteStr('%' . $searchFilter . '%', 'pages');
366 $useNavTitle = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showNavTitle');
367
368 if ($useNavTitle) {
369 $seachWhere .= '(nav_title LIKE ' . $searchFilter .
370 ' OR (nav_title = "" && title LIKE ' . $searchFilter . '))';
371 } else {
372 $seachWhere .= 'title LIKE ' . $searchFilter;
373 }
374
375 $where .= ' AND (' . $seachWhere . ')';
376 }
377
378 return $where;
379 }
380
381 /**
382 * Returns all sub-pages of a given id
383 *
384 * @param int $id
385 * @param string $searchFilter
386 * @return array
387 */
388 protected function getSubpages($id, $searchFilter = '') {
389 $where = $this->getWhereClause($id, $searchFilter);
390 $subpages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
391 'uid', 'pages', $where, '', 'sorting', '', 'uid'
392 );
393
394 return $subpages;
395 }
396
397 /**
398 * Returns TRUE if the node has child's
399 *
400 * @param int $id
401 * @return bool
402 */
403 protected function hasNodeSubPages($id) {
404 $where = $this->getWhereClause($id);
405 $subpage = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
406 'uid', 'pages', $where, '', 'sorting', '', 'uid'
407 );
408
409 $returnValue = TRUE;
410 if (!$subpage['uid']) {
411 $returnValue = FALSE;
412 }
413
414 return $returnValue;
415 }
416 }
417
418 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/pagetree/class.t3lib_tree_pagetree_dataprovider.php'])) {
419 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/pagetree/class.t3lib_tree_pagetree_dataprovider.php']);
420 }
421
422 ?>