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