[TASK] Clean up the phpDoc of t3lib/tree/
[Packages/TYPO3.CMS.git] / t3lib / tree / tca / class.t3lib_tree_tca_databasetreedataprovider.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Steffen Ritter <info@steffen-ritter.net>
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 * TCA tree data provider
30 *
31 * @author Steffen Ritter <info@steffen-ritter.net>
32 * @package TYPO3
33 * @subpackage t3lib_tree
34 */
35
36 class t3lib_tree_Tca_DatabaseTreeDataProvider extends t3lib_tree_Tca_AbstractTcaTreeDataProvider {
37
38 const MODE_CHILDREN = 1;
39 const MODE_PARENT = 2;
40
41 /**
42 * @var string
43 */
44 protected $tableName = '';
45
46 /**
47 * @var string
48 */
49 protected $treeId = '';
50
51 /**
52 * @var string
53 */
54 protected $labelField = '';
55
56 /**
57 * @var string
58 *
59 */
60 protected $tableWhere = '';
61
62 /**
63 * @var integer
64 */
65 protected $lookupMode = t3lib_tree_tca_DatabaseTreeDataProvider::MODE_CHILDREN;
66
67 /**
68 * @var string
69 */
70 protected $lookupField = '';
71
72 /**
73 * @var integer
74 */
75 protected $rootUid = 0;
76
77 /**
78 * @var array
79 */
80 protected $idCache = array();
81
82 /**
83 * Stores TCA-Configuration of the LookUpField in tableName
84 *
85 * @var array
86 */
87 protected $columnConfiguration;
88
89 /**
90 * node sort values (the orderings from foreign_Table_where evaluation)
91 *
92 * @var array
93 */
94 protected $nodeSortValues = array();
95
96 /**
97 * @var array TCEforms compiled TSConfig array
98 */
99 protected $generatedTSConfig = array();
100
101 /**
102 * Sets the label field
103 *
104 * @param string $labelField
105 * @return void
106 */
107 public function setLabelField($labelField) {
108 $this->labelField = $labelField;
109 }
110
111 /**
112 * Gets the label field
113 *
114 * @return string
115 */
116 public function getLabelField() {
117 return $this->labelField;
118 }
119
120 /**
121 * Sets the table name
122 *
123 * @param string $tableName
124 * @return void
125 */
126 public function setTableName($tableName) {
127 $this->tableName = $tableName;
128 }
129
130 /**
131 * Gets the table name
132 *
133 * @return string
134 */
135 public function getTableName() {
136 return $this->tableName;
137 }
138
139 /**
140 * Sets the lookup field
141 *
142 * @param string $lookupField
143 * @return void
144 */
145 public function setLookupField($lookupField) {
146 $this->lookupField = $lookupField;
147 }
148
149 /**
150 * Gets the lookup field
151 *
152 * @return string
153 */
154 public function getLookupField() {
155 return $this->lookupField;
156 }
157
158 /**
159 * Sets the lookup mode
160 *
161 * @param integer $lookupMode
162 * @return void
163 */
164 public function setLookupMode($lookupMode) {
165 $this->lookupMode = $lookupMode;
166 }
167
168 /**
169 * Gets the lookup mode
170 *
171 * @return integer
172 */
173 public function getLookupMode() {
174 return $this->lookupMode;
175 }
176
177 /**
178 * Gets the nodes
179 *
180 * @param t3lib_tree_Node $node
181 * @return t3lib_tree_NodeCollection
182 */
183 public function getNodes(t3lib_tree_Node $node) {
184
185 }
186
187 /**
188 * Gets the root node
189 *
190 * @return t3lib_tree_tca_DatabaseNode
191 */
192 public function getRoot() {
193 return $this->buildRepresentationForNode($this->treeData);
194 }
195
196 /**
197 * Sets the root uid
198 *
199 * @param integer $rootUid
200 * @return void
201 */
202 public function setRootUid($rootUid) {
203 $this->rootUid = $rootUid;
204 }
205
206 /**
207 * Gets the root uid
208 *
209 * @return integer
210 */
211 public function getRootUid() {
212 return $this->rootUid;
213 }
214
215 /**
216 * Sets the tableWhere clause
217 *
218 * @param string $tableWhere
219 * @return void
220 */
221 public function setTableWhere(string $tableWhere) {
222 $this->tableWhere = $tableWhere;
223 }
224
225 /**
226 * Gets the tableWhere clause
227 *
228 * @return string
229 */
230 public function getTableWhere() {
231 return $this->tableWhere;
232 }
233
234 /**
235 * Builds a complete node including childs
236 *
237 * @param t3lib_tree_Node $basicNode
238 * @param NULL|t3lib_tree_tca_DatabaseNode $parent
239 * @param integer $level
240 * @return An object
241 */
242 protected function buildRepresentationForNode(t3lib_tree_Node $basicNode, t3lib_tree_tca_DatabaseNode $parent = NULL, $level = 0) {
243 $node = t3lib_div::makeInstance('t3lib_tree_tca_DatabaseNode');
244 $row = array();
245 if ($basicNode->getId() == 0) {
246 $node->setSelected(FALSE);
247 $node->setExpanded(TRUE);
248 $node->setLabel($GLOBALS['LANG']->sL($GLOBALS['TCA'][$this->tableName]['ctrl']['title']));
249 } else {
250 $row = t3lib_BEfunc::getRecordWSOL($this->tableName, $basicNode->getId(), '*', '', FALSE);
251 if ($this->getLabelField() !== '') {
252 $node->setLabel($row[$this->getLabelField()]);
253 } else {
254 $node->setLabel($basicNode->getId());
255 }
256 $node->setSelected(t3lib_div::inList($this->getSelectedList(), $basicNode->getId()));
257 $node->setExpanded($this->isExpanded($basicNode));
258 }
259 $node->setId($basicNode->getId());
260
261 $node->setSelectable(
262 !t3lib_div::inList($this->getNonSelectableLevelList(), $level)
263 && !in_array($basicNode->getId(), $this->getItemUnselectableList())
264 );
265 $node->setSortValue($this->nodeSortValues[$basicNode->getId()]);
266
267 $node->setIcon(t3lib_iconWorks::mapRecordTypeToSpriteIconClass($this->tableName, $row));
268 $node->setParentNode($parent);
269 if ($basicNode->hasChildNodes()) {
270 $node->setHasChildren(TRUE);
271
272 $childNodes = t3lib_div::makeInstance('t3lib_tree_SortedNodeCollection');
273 foreach ($basicNode->getChildNodes() as $child) {
274 $childNodes->append($this->buildRepresentationForNode($child, $node, $level + 1));
275 }
276 $node->setChildNodes($childNodes);
277 }
278
279 return $node;
280 }
281
282 /**
283 * Init the tree data
284 *
285 * @return void
286 */
287 public function initializeTreeData() {
288 parent::initializeTreeData();
289 $this->nodeSortValues = array_flip($this->itemWhiteList);
290
291 $this->columnConfiguration = $GLOBALS['TCA'][$this->getTableName()]['columns'][$this->getLookupField()]['config'];
292 if (isset($this->columnConfiguration['foreign_table']) && $this->columnConfiguration['foreign_table'] != $this->getTableName()) {
293 throw new InvalidArgumentException(
294 'TCA Tree configuration is invalid: tree for different node-Tables is not implemented yet',
295 1290944650
296 );
297 }
298
299 $this->treeData = t3lib_div::makeInstance('t3lib_tree_Node');
300 $this->treeData->setId($this->getRootUid());
301 $this->treeData->setParentNode(NULL);
302 $childNodes = $this->getChildrenOf($this->treeData, 0);
303 if ($childNodes !== NULL) {
304 $this->treeData->setChildNodes($childNodes);
305 }
306 }
307
308 /**
309 * Gets node children
310 *
311 * @param t3lib_tree_Node $node
312 * @param integer $level
313 * @return NULL|object
314 */
315 protected function getChildrenOf(t3lib_tree_Node $node, $level) {
316 $nodeData = NULL;
317 if ($node->getId() !== 0) {
318 $nodeData = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
319 '*',
320 $this->tableName,
321 'uid=' . $node->getId()
322 );
323 }
324 if ($nodeData == NULL) {
325 $nodeData = array(
326 'uid' => 0,
327 $this->getLookupField() => '',
328 );
329 }
330 $storage = NULL;
331 $children = $this->getRelatedRecords($nodeData);
332 if (count($children)) {
333 $storage = t3lib_div::makeInstance('t3lib_tree_NodeCollection');
334 foreach ($children as $child) {
335 $node = t3lib_div::makeInstance('t3lib_tree_Node');
336 $node->setId($child);
337 if ($level <= $this->levelMaximum) {
338 $children = $this->getChildrenOf($node, $level + 1);
339 if ($children !== NULL) {
340 $node->setChildNodes($children);
341 }
342 }
343 $storage->append($node);
344 }
345 }
346
347 return $storage;
348 }
349
350 /**
351 * Gets related records depending on TCA configuration
352 *
353 * @param array $row
354 * @return array
355 */
356 protected function getRelatedRecords(array $row) {
357 if ($this->getLookupMode() == t3lib_tree_tca_DatabaseTreeDataProvider::MODE_PARENT) {
358 $children = $this->getChildrenUidsFromParentRelation($row);
359 } else {
360 $children = $this->getChildrenUidsFromChildrenRelation($row);
361 }
362
363 $allowedArray = array();
364 foreach ($children as $child) {
365 if (!in_array($child, $this->idCache) && in_array($child, $this->itemWhiteList)) {
366 $allowedArray[] = $child;
367 }
368 }
369
370 $this->idCache = array_merge($this->idCache, $allowedArray);
371
372 return $allowedArray;
373 }
374
375 /**
376 * Gets related records depending on TCA configuration
377 *
378 * @param array $row
379 * @return array
380 */
381 protected function getChildrenUidsFromParentRelation(array $row) {
382 $uid = $row['uid'];
383
384 switch ((string) $this->columnConfiguration['type']) {
385 case 'inline':
386 case 'select':
387 if ($this->columnConfiguration['MM']) {
388 $dbGroup = t3lib_div::makeInstance('t3lib_loadDBGroup');
389 // Dummy field for setting "look from other site"
390 $this->columnConfiguration['MM_oppositeField'] = 'children';
391
392 $dbGroup->start(
393 $row[$this->getLookupField()],
394 $this->getTableName(),
395 $this->columnConfiguration['MM'],
396 $uid,
397 $this->getTableName(),
398 $this->columnConfiguration
399 );
400
401 $relatedUids = $dbGroup->tableArray[$this->getTableName()];
402 } elseif ($this->columnConfiguration['foreign_field']) {
403 $relatedUids = $this->listFieldQuery($this->columnConfiguration['foreign_field'], $uid);
404 } else {
405 $relatedUids = $this->listFieldQuery($this->getLookupField(), $uid);
406 }
407 break;
408 default:
409 $relatedUids = $this->listFieldQuery($this->getLookupField(), $uid);
410 }
411
412 return $relatedUids;
413 }
414
415 /**
416 * Gets related children records depending on TCA configuration
417 *
418 * @param array $row
419 * @return array
420 */
421 protected function getChildrenUidsFromChildrenRelation(array $row) {
422 $relatedUids = array();
423 $uid = $row['uid'];
424 $value = $row[$this->getLookupField()];
425
426 switch ((string) $this->columnConfiguration['type']) {
427 case 'inline':
428 case 'select':
429 if ($this->columnConfiguration['MM']) {
430 $dbGroup = t3lib_div::makeInstance('t3lib_loadDBGroup');
431 $dbGroup->start(
432 $value,
433 $this->getTableName(),
434 $this->columnConfiguration['MM'],
435 $uid,
436 $this->getTableName(),
437 $this->columnConfiguration
438 );
439
440 $relatedUids = $dbGroup->tableArray[$this->getTableName()];
441 } elseif ($this->columnConfiguration['foreign_field']) {
442 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
443 'uid',
444 $this->getTableName(),
445 $this->columnConfiguration['foreign_field'] . '=' . intval($uid)
446 );
447 foreach ($records as $record) {
448 $relatedUids[] = $record['uid'];
449 }
450 } else {
451 $relatedUids = t3lib_div::intExplode(',', $value, TRUE);
452 }
453 break;
454 default:
455 $relatedUids = t3lib_div::intExplode(',', $value, TRUE);
456 }
457
458 return $relatedUids;
459 }
460
461 /**
462 * Queries the table for an field which might contain a list.
463 *
464 * @param string $fieldName the name of the field to be queried
465 * @param integer $queryId the uid to search for
466 *
467 * @return integer[] all uids found
468 */
469 protected function listFieldQuery($fieldName, $queryId) {
470 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
471 'uid',
472 $this->getTableName(),
473 $GLOBALS['TYPO3_DB']->listQuery($fieldName, intval($queryId), $this->getTableName())
474 . (intval($queryId) == 0 ? (' OR ' . $fieldName . ' = \'\'') : '')
475 );
476 $uidArray = array();
477 foreach ($records as $record) {
478 $uidArray[] = $record['uid'];
479 }
480 return $uidArray;
481 }
482 }
483
484 ?>