0e769c5e40c9754f06b503b4208224ba448f2fa9
[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 * Constructor
59 *
60 * @param int $nodeLimit (optional)
61 */
62 public function __construct($nodeLimit = NULL) {
63 if ($nodeLimit === NULL) {
64 $nodeLimit = $GLOBALS['TYPO3_CONF_VARS']['BE']['pageTree']['preloadLimit'];
65 }
66 $this->nodeLimit = abs(intval($nodeLimit));
67
68 $this->hiddenRecords = t3lib_div::trimExplode(
69 ',',
70 $GLOBALS['BE_USER']->getTSConfigVal('options.hideRecords.pages')
71 );
72 }
73
74 /**
75 * Returns the root node
76 *
77 * @return t3lib_tree_Node
78 */
79 public function getRoot() {
80 /** @var $node t3lib_tree_pagetree_Node */
81 $node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node');
82 $node->setId('root');
83 $node->setExpanded(true);
84
85 return $node;
86 }
87
88 /**
89 * Fetches the sub-nodes of the given node
90 *
91 * @param t3lib_tree_Node $node
92 * @param int $mountPoint
93 * @param int $level internally used variable as a recursion limiter
94 * @return t3lib_tree_NodeCollection
95 */
96 public function getNodes(t3lib_tree_Node $node, $mountPoint = 0, $level = 0) {
97 /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
98 $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
99
100 if ($level >= 99) {
101 return $nodeCollection;
102 }
103
104 $subpages = $this->getSubpages($node->getId());
105 if (!is_array($subpages) || !count($subpages)) {
106 return $nodeCollection;
107 }
108
109 foreach ($subpages as $subpage) {
110 if (in_array($subpage['uid'], $this->hiddenRecords)) {
111 continue;
112 }
113
114 $subpage = t3lib_befunc::getRecordWSOL('pages', $subpage['uid'], '*', '', TRUE, TRUE);
115 if (!$subpage) {
116 continue;
117 }
118
119 $subNode = t3lib_tree_pagetree_Commands::getNewNode($subpage, $mountPoint);
120 if ($this->nodeCounter < $this->nodeLimit) {
121 $childNodes = $this->getNodes($subNode, $mountPoint, $level + 1);
122 $subNode->setChildNodes($childNodes);
123 $this->nodeCounter += $childNodes->count();
124 } else {
125 $subNode->setLeaf(!$this->hasNodeSubPages($subNode->getId()));
126 }
127
128 $nodeCollection->append($subNode);
129 }
130
131 return $nodeCollection;
132 }
133
134 /**
135 * Returns a node collection of filtered nodes
136 *
137 * @param t3lib_tree_Node $node
138 * @param string $searchFilter
139 * @param int $mountPoint
140 * @return void
141 */
142 public function getFilteredNodes(t3lib_tree_Node $node, $searchFilter, $mountPoint = 0) {
143 /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
144 $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
145
146 $records = $this->getSubpages(-1, $searchFilter);
147 if (!is_array($records) || !count($records)) {
148 return $nodeCollection;
149 } elseif (count($records) > 500) {
150 return $nodeCollection;
151 }
152
153 $isNumericSearchFilter = (is_numeric($searchFilter) && $searchFilter > 0);
154 $nodeId = intval($node->getId());
155 foreach ($records as $record) {
156 $record = t3lib_tree_pagetree_Commands::getNodeRecord($record['uid']);
157 if (intval($record['pid']) === -1 || in_array($record['uid'], $this->hiddenRecords)) {
158 continue;
159 }
160
161 $rootline = t3lib_BEfunc::BEgetRootLine($record['uid'], ' AND uid != ' . $nodeId);
162 $rootline = array_reverse($rootline);
163 if ($nodeId === 0) {
164 array_shift($rootline);
165 }
166 $reference = $nodeCollection;
167
168 $inFilteredRootline = FALSE;
169 $amountOfRootlineElements = count($rootline);
170 for ($i = 0; $i < $amountOfRootlineElements; ++$i) {
171 $rootlineElement = $rootline[$i];
172 if (intval($rootlineElement['pid']) === $nodeId) {
173 $inFilteredRootline = TRUE;
174 }
175
176 if (!$inFilteredRootline) {
177 continue;
178 }
179
180 $rootlineElement = t3lib_tree_pagetree_Commands::getNodeRecord($rootlineElement['uid']);
181 $ident = intval($rootlineElement['sorting']) . intval($rootlineElement['uid']);
182 if ($reference && $reference->offsetExists($ident)) {
183 /** @var $refNode t3lib_tree_pagetree_Node */
184 $refNode = $reference->offsetGet($ident);
185 $refNode->setExpanded(TRUE);
186 $refNode->setLeaf(FALSE);
187
188 $reference = $refNode->getChildNodes();
189 if ($reference == NULL) {
190 $reference = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
191 $refNode->setChildNodes($reference);
192 }
193 } else {
194 $refNode = t3lib_tree_pagetree_Commands::getNewNode($rootlineElement, $mountPoint);
195 $replacement = '<span class="typo3-pagetree-filteringTree-highlight">$1</span>';
196 if ($isNumericSearchFilter && intval($rootlineElement['uid']) === intval($searchFilter)) {
197 $text = str_replace('$1', $refNode->getText(), $replacement);
198 } else {
199 $text = preg_replace('/(' . $searchFilter . ')/i', $replacement, $refNode->getText());
200 }
201
202 $refNode->setText(
203 $text,
204 $refNode->getTextSourceField(),
205 $refNode->getPrefix(),
206 $refNode->getSuffix()
207 );
208
209 /** @var $childCollection t3lib_tree_pagetree_NodeCollection */
210 $childCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
211
212 if (($i +1) >= $amountOfRootlineElements) {
213 $childNodes = $this->getNodes($refNode, $mountPoint);
214 foreach ($childNodes as $childNode) {
215 /** @var $childNode t3lib_tree_pagetree_Node */
216 $childRecord = $childNode->getRecord();
217 $childIdent = intval($childRecord['sorting']) . intval($childRecord['uid']);
218 $childCollection->offsetSet($childIdent, $childNode);
219 }
220 $refNode->setChildNodes($childNodes);
221 }
222
223 $refNode->setChildNodes($childCollection);
224 $reference->offsetSet($ident, $refNode);
225 $reference->ksort();
226
227 $reference = $childCollection;
228 }
229 }
230 }
231
232 return $nodeCollection;
233 }
234
235 /**
236 * Returns the page tree mounts for the current user
237 *
238 * Note: If you add the search filter parameter, the nodes will be filtered by this string.
239 *
240 * @param string $searchFilter
241 * @return array
242 */
243 public function getTreeMounts($searchFilter = '') {
244 /** @var $nodeCollection t3lib_tree_pagetree_NodeCollection */
245 $nodeCollection = t3lib_div::makeInstance('t3lib_tree_pagetree_NodeCollection');
246
247 $isTemporaryMountPoint = FALSE;
248 $mountPoints = intval($GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint']);
249 if (!$mountPoints) {
250 $mountPoints = array_map('intval', $GLOBALS['BE_USER']->returnWebmounts());
251 $mountPoints = array_unique($mountPoints);
252 } else {
253 $isTemporaryMountPoint = TRUE;
254 $mountPoints = array($mountPoints);
255 }
256
257 if (!count($mountPoints)) {
258 return $nodeCollection;
259 }
260
261 $showRootlineAboveMounts = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showPathAboveMounts');
262 foreach ($mountPoints as $mountPoint) {
263 if ($mountPoint === 0) {
264 $sitename = 'TYPO3';
265 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] !== '') {
266 $sitename = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
267 }
268
269 $record = array(
270 'uid' => 0,
271 'title' => $sitename,
272 );
273 $subNode = t3lib_tree_pagetree_Commands::getNewNode($record);
274 $subNode->setLabelIsEditable(FALSE);
275 $subNode->setType('pages_root');
276 } else {
277 if (in_array($mountPoint, $this->hiddenRecords)) {
278 continue;
279 }
280
281 $record = t3lib_BEfunc::getRecordWSOL('pages', $mountPoint, '*', '', TRUE);
282 if (!$record) {
283 continue;
284 }
285
286 $subNode = t3lib_tree_pagetree_Commands::getNewNode($record, $mountPoint);
287 if ($showRootlineAboveMounts && !$isTemporaryMountPoint) {
288 $rootline = t3lib_tree_pagetree_Commands::getMountPointPath($record['uid']);
289 $subNode->setReadableRootline($rootline);
290 }
291 }
292
293 if (count($mountPoints) <= 1) {
294 $subNode->setExpanded(TRUE);
295 $subNode->setCls('typo3-pagetree-node-notExpandable');
296 }
297
298 $subNode->setIsMountPoint(TRUE);
299 $subNode->setDraggable(FALSE);
300 $subNode->setIsDropTarget(FALSE);
301
302 if ($searchFilter === '') {
303 $childNodes = $this->getNodes($subNode, $mountPoint);
304 } else {
305 $childNodes = $this->getFilteredNodes($subNode, $searchFilter, $mountPoint);
306 $subNode->setExpanded(TRUE);
307 }
308
309 $subNode->setChildNodes($childNodes);
310 $nodeCollection->append($subNode);
311 }
312
313 return $nodeCollection;
314 }
315
316 /**
317 * Returns the where clause for fetching pages
318 *
319 * @param int $id
320 * @param string $searchFilter
321 * @return string
322 */
323 protected function getWhereClause($id, $searchFilter = '') {
324 $where = $GLOBALS['BE_USER']->getPagePermsClause(1) .
325 t3lib_BEfunc::deleteClause('pages') .
326 t3lib_BEfunc::versioningPlaceholderClause('pages');
327
328 if (is_numeric($id) && $id >= 0) {
329 $where .= ' AND pid= ' . $GLOBALS['TYPO3_DB']->fullQuoteStr(intval($id), 'pages');
330 }
331
332 if ($searchFilter !== '') {
333 if (is_numeric($searchFilter) && $searchFilter > 0) {
334 $seachWhere .= 'uid = ' . intval($searchFilter) . ' OR ';
335 }
336
337 $searchFilter = $GLOBALS['TYPO3_DB']->fullQuoteStr('%' . $searchFilter . '%', 'pages');
338 $useNavTitle = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showNavTitle');
339
340 if ($useNavTitle) {
341 $seachWhere .= '(nav_title LIKE ' . $searchFilter .
342 ' OR (nav_title = "" && title LIKE ' . $searchFilter . '))';
343 } else {
344 $seachWhere .= 'title LIKE ' . $searchFilter;
345 }
346
347 $where .= ' AND (' . $seachWhere . ')';
348 }
349
350 return $where;
351 }
352
353 /**
354 * Returns all sub-pages of a given id
355 *
356 * @param int $id
357 * @param string $searchFilter
358 * @return array
359 */
360 protected function getSubpages($id, $searchFilter = '') {
361 $where = $this->getWhereClause($id, $searchFilter);
362 $subpages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
363 'uid', 'pages', $where, '', 'sorting', '', 'uid'
364 );
365
366 return $subpages;
367 }
368
369 /**
370 * Returns true if the node has child's
371 *
372 * @param int $id
373 * @return bool
374 */
375 protected function hasNodeSubPages($id) {
376 $where = $this->getWhereClause($id);
377 $subpage = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
378 'uid', 'pages', $where, '', 'sorting', '', 'uid'
379 );
380
381 $returnValue = TRUE;
382 if (!$subpage['uid']) {
383 $returnValue = FALSE;
384 }
385
386 return $returnValue;
387 }
388 }
389
390 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/pagetree/class.t3lib_tree_pagetree_dataprovider.php'])) {
391 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/pagetree/class.t3lib_tree_pagetree_dataprovider.php']);
392 }
393
394 ?>