[BUGFIX] tcaTree: make own record unselectable
[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 * @static
41 * @param array $tcaConfiguration
42 * @return t3lib_tree_Tca_DatabaseTreeDataProvider
43 * @throws InvalidArgumentException
44 */
45 public static function getDataProvider(array $tcaConfiguration, $table, $field, $currentValue) {
46 $dataProvider = NULL;
47
48 if (!isset($tcaConfiguration['internal_type'])) {
49 $tcaConfiguration['internal_type'] = 'db';
50 }
51
52 if ($tcaConfiguration['internal_type'] == 'db') {
53 $unselectableUids = array();
54
55 /**
56 * @var $dataProvider t3lib_tree_Tca_DatabaseTreeDataProvider
57 */
58 $dataProvider = t3lib_div::makeInstance('t3lib_tree_Tca_DatabaseTreeDataProvider');
59
60 if (isset($tcaConfiguration['foreign_table'])) {
61 $tableName = $tcaConfiguration['foreign_table'];
62 $dataProvider->setTableName($tableName);
63
64 if ($tableName == $table) {
65 $unselectableUids[] = $currentValue['uid'];
66 }
67
68 t3lib_div::loadTCA($tableName);
69 } else {
70 throw new InvalidArgumentException(
71 'TCA Tree configuration is invalid: "foreign_table" not set',
72 '1288215888'
73 );
74 }
75
76 if (isset($tcaConfiguration['foreign_label'])) {
77 $dataProvider->setLabelField($tcaConfiguration['foreign_label']);
78 } else {
79 $dataProvider->setLabelField($GLOBALS['TCA'][$tableName]['ctrl']['label']);
80 }
81 $dataProvider->setTreeId(md5($table . '|' . $field));
82 $dataProvider->setSelectedList($currentValue);
83 if (isset($tcaConfiguration['treeConfig']) && is_array($tcaConfiguration['treeConfig'])) {
84 $treeConfiguration = $tcaConfiguration['treeConfig'];
85
86 if (isset($treeConfiguration['rootUid'])) {
87 $dataProvider->setRootUid(intval($treeConfiguration['rootUid']));
88 }
89
90 if (isset($treeConfiguration['appearance']['expandAll'])) {
91 $dataProvider->setExpandAll((boolean) $treeConfiguration['appearance']['expandAll']);
92 }
93
94 if (isset($treeConfiguration['appearance']['maxLevels'])) {
95 $dataProvider->setLevelMaximum(intval($treeConfiguration['appearance']['maxLevels']));
96 }
97
98 if (isset($treeConfiguration['appearance']['nonSelectableLevels'])) {
99 $dataProvider->setNonSelectableLevelList($treeConfiguration['appearance']['nonSelectableLevels']);
100 } elseif (isset($treeConfiguration['rootUid'])) {
101 $dataProvider->setNonSelectableLevelList('');
102 }
103
104 if (isset($treeConfiguration['childrenField'])) {
105 $dataProvider->setLookupMode(t3lib_tree_tca_DatabaseTreeDataProvider::MODE_CHILDREN);
106 $dataProvider->setLookupField($treeConfiguration['childrenField']);
107 } elseif (isset($treeConfiguration['parentField'])) {
108 $dataProvider->setLookupMode(t3lib_tree_tca_DatabaseTreeDataProvider::MODE_PARENT);
109 $dataProvider->setLookupField($treeConfiguration['parentField']);
110 } else {
111 throw new InvalidArgumentException(
112 'TCA Tree configuration is invalid: neither "childrenField" nor "parentField" is set',
113 '1288215889'
114 );
115 }
116
117 $dataProvider->setItemUnselectableList($unselectableUids);
118 } else {
119 throw new InvalidArgumentException(
120 'TCA Tree configuration is invalid: "treeConfig" array is missing',
121 '1288215890'
122 );
123 }
124
125 } elseif ($tcaConfiguration['internal_type'] == 'file') {
126 // Not implemented yet
127 throw new InvalidArgumentException(
128 'TCA Tree configuration is invalid: tree for "internal_type=file" not implemented yet',
129 '1288215891'
130 );
131 } else {
132 throw new InvalidArgumentException(
133 'TCA Tree configuration is invalid: tree for "internal_type=' .
134 $tcaConfiguration['internal_type'] .
135 '" not implemented yet',
136 '1288215892'
137 );
138 }
139
140 return $dataProvider;
141 }
142 }
143
144 ?>