[TASK] Add possibility for a custom TCA TreeProvider
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Tree / TableConfiguration / TreeDataProviderFactory.php
1 <?php
2 namespace TYPO3\CMS\Core\Tree\TableConfiguration;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Steffen Ritter <info@steffen-ritter.net>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Builds a t3lib_tree_Tca_DatabaseTreeDataProvider object based on some TCA configuration
31 *
32 * @author Steffen Ritter <info@steffen-ritter.net>
33 * @package TYPO3
34 * @subpackage t3lib_tree
35 */
36 class TreeDataProviderFactory {
37
38 /**
39 * Gets the data provider, depending on TCA configuration
40 *
41 * @param array $tcaConfiguration
42 * @param $table
43 * @param $field
44 * @param $currentValue
45 * @return DatabaseTreeDataProvider
46 * @throws \InvalidArgumentException
47 */
48 static public function getDataProvider(array $tcaConfiguration, $table, $field, $currentValue) {
49 /** @var $dataProvider \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider */
50 $dataProvider = NULL;
51 if (!isset($tcaConfiguration['treeConfig']) | !is_array($tcaConfiguration['treeConfig'])) {
52 throw new \InvalidArgumentException('TCA Tree configuration is invalid: "treeConfig" array is missing', 1288215890);
53 }
54
55 if (!empty($tcaConfiguration['treeConfig']['dataProvider'])) {
56 $dataProvider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($tcaConfiguration['treeConfig']['dataProvider'], $tcaConfiguration, $table, $field, $currentValue);
57 }
58 if (!isset($tcaConfiguration['internal_type'])) {
59 $tcaConfiguration['internal_type'] = 'db';
60 }
61 if ($tcaConfiguration['internal_type'] === 'db') {
62 $unselectableUids = array();
63 if ($dataProvider === NULL) {
64 $dataProvider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Tree\\TableConfiguration\\DatabaseTreeDataProvider');
65 }
66 if (isset($tcaConfiguration['foreign_table'])) {
67 $tableName = $tcaConfiguration['foreign_table'];
68 $dataProvider->setTableName($tableName);
69 if ($tableName == $table) {
70 $unselectableUids[] = $currentValue['uid'];
71 }
72 \TYPO3\CMS\Core\Utility\GeneralUtility::loadTCA($tableName);
73 } else {
74 throw new \InvalidArgumentException('TCA Tree configuration is invalid: "foreign_table" not set', 1288215888);
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
84 $treeConfiguration = $tcaConfiguration['treeConfig'];
85 if (isset($treeConfiguration['rootUid'])) {
86 $dataProvider->setRootUid(intval($treeConfiguration['rootUid']));
87 }
88 if (isset($treeConfiguration['appearance']['expandAll'])) {
89 $dataProvider->setExpandAll((bool) $treeConfiguration['appearance']['expandAll']);
90 }
91 if (isset($treeConfiguration['appearance']['maxLevels'])) {
92 $dataProvider->setLevelMaximum(intval($treeConfiguration['appearance']['maxLevels']));
93 }
94 if (isset($treeConfiguration['appearance']['nonSelectableLevels'])) {
95 $dataProvider->setNonSelectableLevelList($treeConfiguration['appearance']['nonSelectableLevels']);
96 } elseif (isset($treeConfiguration['rootUid'])) {
97 $dataProvider->setNonSelectableLevelList('');
98 }
99 if (isset($treeConfiguration['childrenField'])) {
100 $dataProvider->setLookupMode(\t3lib_tree_tca_DatabaseTreeDataProvider::MODE_CHILDREN);
101 $dataProvider->setLookupField($treeConfiguration['childrenField']);
102 } elseif (isset($treeConfiguration['parentField'])) {
103 $dataProvider->setLookupMode(\t3lib_tree_tca_DatabaseTreeDataProvider::MODE_PARENT);
104 $dataProvider->setLookupField($treeConfiguration['parentField']);
105 } else {
106 throw new \InvalidArgumentException('TCA Tree configuration is invalid: neither "childrenField" nor "parentField" is set', 1288215889);
107 }
108 $dataProvider->setItemUnselectableList($unselectableUids);
109
110 } elseif ($tcaConfiguration['internal_type'] === 'file' && $dataProvider === NULL) {
111 // TODO Not implemented yet
112 throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for "internal_type=file" not implemented yet', 1288215891);
113 } elseif ($dataProvider === NULL) {
114 throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for "internal_type=' . $tcaConfiguration['internal_type'] . '" not implemented yet', 1288215892);
115 }
116 return $dataProvider;
117 }
118
119 }
120
121
122 ?>