9ff4617842a2124b56dd1008146ab201b7f2f110
[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
124 // check if fetching subpages the "root"-page
125 // and in case of a virtual root return the mountpoints as virtual "subpages"
126 if (intval($node->getId()) === 0) {
127 $mountPoints = intval($GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint']);
128 // check no temporary mountpoint is used
129 if (!$mountPoints) {
130 $mountPoints = array_map('intval', $GLOBALS['BE_USER']->returnWebmounts());
131 $mountPoints = array_unique($mountPoints);
132 if (!in_array(0, $mountPoints)) {
133 // using a virtual root node
134 // so then return the mount points here as "subpages" of the first node
135 $subpages = array();
136 foreach ($mountPoints as $mountPoint) {
137 $subpages[] = array('uid' => $mountPoint);
138 }
139 }
140 }
141 }
142
143 if (is_array($subpages) && count($subpages) > 0) {
144
145 foreach ($subpages as $subpage) {
146 if (in_array($subpage['uid'], $this->hiddenRecords)) {
147 continue;
148 }
149
150 $subpage = t3lib_befunc::getRecordWSOL('pages', $subpage['uid'], '*', '', TRUE, TRUE);
151 if (!$subpage) {
152 continue;
153 }
154
155 $subNode = t3lib_tree_pagetree_Commands::getNewNode($subpage, $mountPoint);
156 if ($this->nodeCounter < $this->nodeLimit) {
157 $childNodes = $this->getNodes($subNode, $mountPoint, $level + 1);
158 $subNode->setChildNodes($childNodes);
159 $this->nodeCounter += $childNodes->count();
160 } else {
161 $subNode->setLeaf(!$this->hasNodeSubPages($subNode->getId()));
162 }
163
164 $nodeCollection->append($subNode);
165 }
166 }
167
168
169 foreach ($this->processCollectionHookObjects as $hookObject) {
170 /** @var $hookObject t3lib_tree_pagetree_interfaces_collectionprocessor */
171 $hookObject->postProcessGetNodes($node, $mountPoint, $level, $nodeCollection);
172 }
173
174 return $nodeCollection;
175 }
176
177 /**
178 * Returns a node collection of filtered nodes
179 *
180 * @param t3lib_tree_Node $node
181 * @param string $searchFilter
182 * @param int $mountPoint
183 * @return void
184 */
185 public function getFilteredNodes(t3lib_tree_Node $node, $searchFilter, $mountPoint = 0) {
186 /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
187 $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
188
189 $records = $this->getSubpages(-1, $searchFilter);
190 if (!is_array($records) || !count($records)) {
191 return $nodeCollection;
192 } elseif (count($records) > 500) {
193 return $nodeCollection;
194 }
195
196 $isNumericSearchFilter = (is_numeric($searchFilter) && $searchFilter > 0);
197 $nodeId = intval($node->getId());
198 foreach ($records as $record) {
199 $record = t3lib_tree_pagetree_Commands::getNodeRecord($record['uid']);
200 if (intval($record['pid']) === -1 || in_array($record['uid'], $this->hiddenRecords)) {
201 continue;
202 }
203
204 $rootline = t3lib_BEfunc::BEgetRootLine($record['uid'], ' AND uid != ' . $nodeId);
205 $rootline = array_reverse($rootline);
206 if ($nodeId === 0) {
207 array_shift($rootline);
208 }
209 $reference = $nodeCollection;
210
211 $inFilteredRootline = FALSE;
212 $amountOfRootlineElements = count($rootline);
213 for ($i = 0; $i < $amountOfRootlineElements; ++$i) {
214 $rootlineElement = $rootline[$i];
215 if (intval($rootlineElement['pid']) === $nodeId) {
216 $inFilteredRootline = TRUE;
217 }
218
219 if (!$inFilteredRootline) {
220 continue;
221 }
222
223 $rootlineElement = t3lib_tree_pagetree_Commands::getNodeRecord($rootlineElement['uid']);
224 $ident = intval($rootlineElement['sorting']) . intval($rootlineElement['uid']);
225 if ($reference && $reference->offsetExists($ident)) {
226 /** @var $refNode t3lib_tree_pagetree_Node */
227 $refNode = $reference->offsetGet($ident);
228 $refNode->setExpanded(TRUE);
229 $refNode->setLeaf(FALSE);
230
231 $reference = $refNode->getChildNodes();
232 if ($reference == NULL) {
233 $reference = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
234 $refNode->setChildNodes($reference);
235 }
236 } else {
237 $refNode = t3lib_tree_pagetree_Commands::getNewNode($rootlineElement, $mountPoint);
238 $replacement = '<span class="typo3-pagetree-filteringTree-highlight">$1</span>';
239 if ($isNumericSearchFilter && intval($rootlineElement['uid']) === intval($searchFilter)) {
240 $text = str_replace('$1', $refNode->getText(), $replacement);
241 } else {
242 $text = preg_replace('/(' . $searchFilter . ')/i', $replacement, $refNode->getText());
243 }
244
245 $refNode->setText(
246 $text,
247 $refNode->getTextSourceField(),
248 $refNode->getPrefix(),
249 $refNode->getSuffix()
250 );
251
252 /** @var $childCollection t3lib_tree_pagetree_NodeCollection */
253 $childCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
254
255 if (($i +1) >= $amountOfRootlineElements) {
256 $childNodes = $this->getNodes($refNode, $mountPoint);
257 foreach ($childNodes as $childNode) {
258 /** @var $childNode t3lib_tree_pagetree_Node */
259 $childRecord = $childNode->getRecord();
260 $childIdent = intval($childRecord['sorting']) . intval($childRecord['uid']);
261 $childCollection->offsetSet($childIdent, $childNode);
262 }
263 $refNode->setChildNodes($childNodes);
264 }
265
266 $refNode->setChildNodes($childCollection);
267 $reference->offsetSet($ident, $refNode);
268 $reference->ksort();
269
270 $reference = $childCollection;
271 }
272 }
273 }
274
275 foreach ($this->processCollectionHookObjects as $hookObject) {
276 /** @var $hookObject t3lib_tree_pagetree_interfaces_collectionprocessor */
277 $hookObject->postProcessFilteredNodes($node, $searchFilter, $mountPoint, $nodeCollection);
278 }
279
280 return $nodeCollection;
281 }
282
283 /**
284 * Returns the page tree mounts for the current user
285 *
286 * Note: If you add the search filter parameter, the nodes will be filtered by this string.
287 *
288 * @param string $searchFilter
289 * @return array
290 */
291 public function getTreeMounts($searchFilter = '') {
292 /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
293 $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
294
295 $isTemporaryMountPoint = FALSE;
296 $rootNodeIsVirtual = FALSE;
297 $mountPoints = intval($GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint']);
298 if (!$mountPoints) {
299 $mountPoints = array_map('intval', $GLOBALS['BE_USER']->returnWebmounts());
300 $mountPoints = array_unique($mountPoints);
301 if (!in_array(0, $mountPoints)) {
302 $rootNodeIsVirtual = TRUE;
303 // use a virtual root
304 // the real mountpoints will be fetched in getNodes() then
305 // since those will be the "subpages" of the virtual root
306 $mountPoints = array(0);
307 }
308 } else {
309 $isTemporaryMountPoint = TRUE;
310 $mountPoints = array($mountPoints);
311 }
312
313 if (!count($mountPoints)) {
314 return $nodeCollection;
315 }
316
317 $showRootlineAboveMounts = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showPathAboveMounts');
318 foreach ($mountPoints as $mountPoint) {
319 if ($mountPoint === 0) {
320 $sitename = 'TYPO3';
321 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] !== '') {
322 $sitename = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
323 }
324
325 $record = array(
326 'uid' => 0,
327 'title' => $sitename,
328 );
329 $subNode = t3lib_tree_pagetree_Commands::getNewNode($record);
330 $subNode->setLabelIsEditable(FALSE);
331 if ($rootNodeIsVirtual) {
332 $subNode->setType('virtual_root');
333 } else {
334 $subNode->setType('pages_root');
335 }
336 } else {
337 if (in_array($mountPoint, $this->hiddenRecords)) {
338 continue;
339 }
340
341 $record = t3lib_BEfunc::getRecordWSOL('pages', $mountPoint, '*', '', TRUE);
342 if (!$record) {
343 continue;
344 }
345
346 $subNode = t3lib_tree_pagetree_Commands::getNewNode($record, $mountPoint);
347 if ($showRootlineAboveMounts && !$isTemporaryMountPoint) {
348 $rootline = t3lib_tree_pagetree_Commands::getMountPointPath($record['uid']);
349 $subNode->setReadableRootline($rootline);
350 }
351 }
352
353 if (count($mountPoints) <= 1) {
354 $subNode->setExpanded(TRUE);
355 $subNode->setCls('typo3-pagetree-node-notExpandable');
356 }
357
358 $subNode->setIsMountPoint(TRUE);
359 $subNode->setDraggable(FALSE);
360 $subNode->setIsDropTarget(FALSE);
361
362 if ($searchFilter === '') {
363 $childNodes = $this->getNodes($subNode, $mountPoint);
364 } else {
365 $childNodes = $this->getFilteredNodes($subNode, $searchFilter, $mountPoint);
366 $subNode->setExpanded(TRUE);
367 }
368
369 $subNode->setChildNodes($childNodes);
370 $nodeCollection->append($subNode);
371 }
372
373 foreach ($this->processCollectionHookObjects as $hookObject) {
374 /** @var $hookObject t3lib_tree_pagetree_interfaces_collectionprocessor */
375 $hookObject->postProcessGetTreeMounts($searchFilter, $nodeCollection);
376 }
377
378 return $nodeCollection;
379 }
380
381 /**
382 * Returns the where clause for fetching pages
383 *
384 * @param int $id
385 * @param string $searchFilter
386 * @return string
387 */
388 protected function getWhereClause($id, $searchFilter = '') {
389 $where = $GLOBALS['BE_USER']->getPagePermsClause(1) .
390 t3lib_BEfunc::deleteClause('pages') .
391 t3lib_BEfunc::versioningPlaceholderClause('pages');
392
393 if (is_numeric($id) && $id >= 0) {
394 $where .= ' AND pid= ' . $GLOBALS['TYPO3_DB']->fullQuoteStr(intval($id), 'pages');
395 }
396
397 if ($searchFilter !== '') {
398 if (is_numeric($searchFilter) && $searchFilter > 0) {
399 $seachWhere .= 'uid = ' . intval($searchFilter) . ' OR ';
400 }
401
402 $searchFilter = $GLOBALS['TYPO3_DB']->fullQuoteStr('%' . $searchFilter . '%', 'pages');
403 $useNavTitle = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showNavTitle');
404
405 if ($useNavTitle) {
406 $seachWhere .= '(nav_title LIKE ' . $searchFilter .
407 ' OR (nav_title = "" && title LIKE ' . $searchFilter . '))';
408 } else {
409 $seachWhere .= 'title LIKE ' . $searchFilter;
410 }
411
412 $where .= ' AND (' . $seachWhere . ')';
413 }
414
415 return $where;
416 }
417
418 /**
419 * Returns all sub-pages of a given id
420 *
421 * @param int $id
422 * @param string $searchFilter
423 * @return array
424 */
425 protected function getSubpages($id, $searchFilter = '') {
426 $where = $this->getWhereClause($id, $searchFilter);
427 $subpages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
428 'uid', 'pages', $where, '', 'sorting', '', 'uid'
429 );
430
431 return $subpages;
432 }
433
434 /**
435 * Returns TRUE if the node has child's
436 *
437 * @param int $id
438 * @return bool
439 */
440 protected function hasNodeSubPages($id) {
441 $where = $this->getWhereClause($id);
442 $subpage = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
443 'uid', 'pages', $where, '', 'sorting', '', 'uid'
444 );
445
446 $returnValue = TRUE;
447 if (!$subpage['uid']) {
448 $returnValue = FALSE;
449 }
450
451 return $returnValue;
452 }
453 }
454
455 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/pagetree/class.t3lib_tree_pagetree_dataprovider.php'])) {
456 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/pagetree/class.t3lib_tree_pagetree_dataprovider.php']);
457 }
458
459 ?>