6dcad78f4e891919859443c0c9264b294dbbb4e9
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Tree / TableConfiguration / TreeDataProviderFactory.php
1 <?php
2 namespace TYPO3\CMS\Core\Tree\TableConfiguration;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Builds a \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider
19 * object based on some TCA configuration
20 *
21 * @author Steffen Ritter <info@steffen-ritter.net>
22 */
23 class TreeDataProviderFactory {
24
25 /**
26 * Gets the data provider, depending on TCA configuration
27 *
28 * @param array $tcaConfiguration
29 * @param $table
30 * @param $field
31 * @param $currentValue
32 * @return DatabaseTreeDataProvider
33 * @throws \InvalidArgumentException
34 */
35 static public function getDataProvider(array $tcaConfiguration, $table, $field, $currentValue) {
36 /** @var $dataProvider \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider */
37 $dataProvider = NULL;
38 if (!isset($tcaConfiguration['treeConfig']) | !is_array($tcaConfiguration['treeConfig'])) {
39 throw new \InvalidArgumentException('TCA Tree configuration is invalid: "treeConfig" array is missing', 1288215890);
40 }
41
42 if (!empty($tcaConfiguration['treeConfig']['dataProvider'])) {
43 $dataProvider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($tcaConfiguration['treeConfig']['dataProvider'], $tcaConfiguration, $table, $field, $currentValue);
44 }
45 if (!isset($tcaConfiguration['internal_type'])) {
46 $tcaConfiguration['internal_type'] = 'db';
47 }
48 if ($tcaConfiguration['internal_type'] === 'db') {
49 $unselectableUids = array();
50 if ($dataProvider === NULL) {
51 $dataProvider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider::class);
52 }
53 if (isset($tcaConfiguration['foreign_table'])) {
54 $tableName = $tcaConfiguration['foreign_table'];
55 $dataProvider->setTableName($tableName);
56 if ($tableName == $table) {
57 $unselectableUids[] = $currentValue['uid'];
58 }
59 } else {
60 throw new \InvalidArgumentException('TCA Tree configuration is invalid: "foreign_table" not set', 1288215888);
61 }
62 if (isset($tcaConfiguration['foreign_label'])) {
63 $dataProvider->setLabelField($tcaConfiguration['foreign_label']);
64 } else {
65 $dataProvider->setLabelField($GLOBALS['TCA'][$tableName]['ctrl']['label']);
66 }
67 $dataProvider->setTreeId(md5($table . '|' . $field));
68 $dataProvider->setSelectedList($currentValue);
69
70 $treeConfiguration = $tcaConfiguration['treeConfig'];
71 if (isset($treeConfiguration['rootUid'])) {
72 $dataProvider->setRootUid((int)$treeConfiguration['rootUid']);
73 }
74 if (isset($treeConfiguration['appearance']['expandAll'])) {
75 $dataProvider->setExpandAll((bool)$treeConfiguration['appearance']['expandAll']);
76 }
77 if (isset($treeConfiguration['appearance']['maxLevels'])) {
78 $dataProvider->setLevelMaximum((int)$treeConfiguration['appearance']['maxLevels']);
79 }
80 if (isset($treeConfiguration['appearance']['nonSelectableLevels'])) {
81 $dataProvider->setNonSelectableLevelList($treeConfiguration['appearance']['nonSelectableLevels']);
82 } elseif (isset($treeConfiguration['rootUid'])) {
83 $dataProvider->setNonSelectableLevelList('');
84 }
85 if (isset($treeConfiguration['childrenField'])) {
86 $dataProvider->setLookupMode(DatabaseTreeDataProvider::MODE_CHILDREN);
87 $dataProvider->setLookupField($treeConfiguration['childrenField']);
88 } elseif (isset($treeConfiguration['parentField'])) {
89 $dataProvider->setLookupMode(DatabaseTreeDataProvider::MODE_PARENT);
90 $dataProvider->setLookupField($treeConfiguration['parentField']);
91 } else {
92 throw new \InvalidArgumentException('TCA Tree configuration is invalid: neither "childrenField" nor "parentField" is set', 1288215889);
93 }
94 $dataProvider->setItemUnselectableList($unselectableUids);
95
96 } elseif ($tcaConfiguration['internal_type'] === 'file' && $dataProvider === NULL) {
97 // TODO Not implemented yet
98 throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for "internal_type=file" not implemented yet', 1288215891);
99 } elseif ($dataProvider === NULL) {
100 throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for "internal_type=' . $tcaConfiguration['internal_type'] . '" not implemented yet', 1288215892);
101 }
102 return $dataProvider;
103 }
104
105 }