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