[TASK] Clean up the phpDoc of t3lib/tree/
[Packages/TYPO3.CMS.git] / t3lib / tree / tca / class.t3lib_tree_tca_dataproviderfactory.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 * Builds a t3lib_tree_Tca_DatabaseTreeDataProvider object based on some TCA configuration
30 *
31 * @author Steffen Ritter <info@steffen-ritter.net>
32 * @package TYPO3
33 * @subpackage t3lib_tree
34 */
35 class t3lib_tree_Tca_DataProviderFactory {
36
37 /**
38 * Gets the data provider, depending on TCA configuration
39 *
40 * @param array $tcaConfiguration
41 * @return t3lib_tree_Tca_DatabaseTreeDataProvider
42 * @throws InvalidArgumentException
43 */
44 public static function getDataProvider(array $tcaConfiguration, $table, $field, $currentValue) {
45 $dataProvider = NULL;
46
47 if (!isset($tcaConfiguration['internal_type'])) {
48 $tcaConfiguration['internal_type'] = 'db';
49 }
50
51 if ($tcaConfiguration['internal_type'] == 'db') {
52 $unselectableUids = array();
53
54 /** @var $dataProvider t3lib_tree_Tca_DatabaseTreeDataProvider */
55 $dataProvider = t3lib_div::makeInstance('t3lib_tree_Tca_DatabaseTreeDataProvider');
56
57 if (isset($tcaConfiguration['foreign_table'])) {
58 $tableName = $tcaConfiguration['foreign_table'];
59 $dataProvider->setTableName($tableName);
60
61 if ($tableName == $table) {
62 $unselectableUids[] = $currentValue['uid'];
63 }
64
65 t3lib_div::loadTCA($tableName);
66 } else {
67 throw new InvalidArgumentException(
68 'TCA Tree configuration is invalid: "foreign_table" not set',
69 1288215888
70 );
71 }
72
73 if (isset($tcaConfiguration['foreign_label'])) {
74 $dataProvider->setLabelField($tcaConfiguration['foreign_label']);
75 } else {
76 $dataProvider->setLabelField($GLOBALS['TCA'][$tableName]['ctrl']['label']);
77 }
78 $dataProvider->setTreeId(md5($table . '|' . $field));
79 $dataProvider->setSelectedList($currentValue);
80 if (isset($tcaConfiguration['treeConfig']) && is_array($tcaConfiguration['treeConfig'])) {
81 $treeConfiguration = $tcaConfiguration['treeConfig'];
82
83 if (isset($treeConfiguration['rootUid'])) {
84 $dataProvider->setRootUid(intval($treeConfiguration['rootUid']));
85 }
86
87 if (isset($treeConfiguration['appearance']['expandAll'])) {
88 $dataProvider->setExpandAll((boolean) $treeConfiguration['appearance']['expandAll']);
89 }
90
91 if (isset($treeConfiguration['appearance']['maxLevels'])) {
92 $dataProvider->setLevelMaximum(intval($treeConfiguration['appearance']['maxLevels']));
93 }
94
95 if (isset($treeConfiguration['appearance']['nonSelectableLevels'])) {
96 $dataProvider->setNonSelectableLevelList($treeConfiguration['appearance']['nonSelectableLevels']);
97 } elseif (isset($treeConfiguration['rootUid'])) {
98 $dataProvider->setNonSelectableLevelList('');
99 }
100
101 if (isset($treeConfiguration['childrenField'])) {
102 $dataProvider->setLookupMode(t3lib_tree_tca_DatabaseTreeDataProvider::MODE_CHILDREN);
103 $dataProvider->setLookupField($treeConfiguration['childrenField']);
104 } elseif (isset($treeConfiguration['parentField'])) {
105 $dataProvider->setLookupMode(t3lib_tree_tca_DatabaseTreeDataProvider::MODE_PARENT);
106 $dataProvider->setLookupField($treeConfiguration['parentField']);
107 } else {
108 throw new InvalidArgumentException(
109 'TCA Tree configuration is invalid: neither "childrenField" nor "parentField" is set',
110 1288215889
111 );
112 }
113
114 $dataProvider->setItemUnselectableList($unselectableUids);
115 } else {
116 throw new InvalidArgumentException(
117 'TCA Tree configuration is invalid: "treeConfig" array is missing',
118 1288215890
119 );
120 }
121
122 } elseif ($tcaConfiguration['internal_type'] == 'file') {
123 // TODO Not implemented yet
124 throw new InvalidArgumentException(
125 'TCA Tree configuration is invalid: tree for "internal_type=file" not implemented yet',
126 1288215891
127 );
128 } else {
129 throw new InvalidArgumentException(
130 'TCA Tree configuration is invalid: tree for "internal_type=' .
131 $tcaConfiguration['internal_type'] .
132 '" not implemented yet',
133 1288215892
134 );
135 }
136
137 return $dataProvider;
138 }
139 }
140
141 ?>