e42165ffd122884a3343efe5ea4885e3b4eba3a4
[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 class TreeDataProviderFactory
22 {
23 /**
24 * Gets the data provider, depending on TCA configuration
25 *
26 * @param array $tcaConfiguration
27 * @param $table
28 * @param $field
29 * @param array $currentValue The current database row, handing over 'uid' is enough
30 * @return DatabaseTreeDataProvider
31 * @throws \InvalidArgumentException
32 */
33 public static function getDataProvider(array $tcaConfiguration, $table, $field, $currentValue)
34 {
35 /** @var $dataProvider \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider */
36 $dataProvider = null;
37 if (!isset($tcaConfiguration['treeConfig']) | !is_array($tcaConfiguration['treeConfig'])) {
38 throw new \InvalidArgumentException('TCA Tree configuration is invalid: "treeConfig" array is missing', 1288215890);
39 }
40
41 if (!empty($tcaConfiguration['treeConfig']['dataProvider'])) {
42 $dataProvider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($tcaConfiguration['treeConfig']['dataProvider'], $tcaConfiguration, $table, $field, $currentValue);
43 }
44 if (!isset($tcaConfiguration['internal_type'])) {
45 $tcaConfiguration['internal_type'] = 'db';
46 }
47 if ($tcaConfiguration['internal_type'] === 'db') {
48 if ($dataProvider === null) {
49 $dataProvider = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider::class);
50 }
51 if (isset($tcaConfiguration['foreign_table'])) {
52 $tableName = $tcaConfiguration['foreign_table'];
53 $dataProvider->setTableName($tableName);
54 if ($tableName == $table) {
55 // The uid of the currently opened row can not be selected in a table relation to "self"
56 $unselectableUids = [$currentValue['uid']];
57 $dataProvider->setItemUnselectableList($unselectableUids);
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
69 $treeConfiguration = $tcaConfiguration['treeConfig'];
70 if (isset($treeConfiguration['rootUid'])) {
71 $dataProvider->setRootUid((int)$treeConfiguration['rootUid']);
72 }
73 if (isset($treeConfiguration['appearance']['expandAll'])) {
74 $dataProvider->setExpandAll((bool)$treeConfiguration['appearance']['expandAll']);
75 }
76 if (isset($treeConfiguration['appearance']['maxLevels'])) {
77 $dataProvider->setLevelMaximum((int)$treeConfiguration['appearance']['maxLevels']);
78 }
79 if (isset($treeConfiguration['appearance']['nonSelectableLevels'])) {
80 $dataProvider->setNonSelectableLevelList($treeConfiguration['appearance']['nonSelectableLevels']);
81 } elseif (isset($treeConfiguration['rootUid'])) {
82 $dataProvider->setNonSelectableLevelList('');
83 }
84 if (isset($treeConfiguration['childrenField'])) {
85 $dataProvider->setLookupMode(DatabaseTreeDataProvider::MODE_CHILDREN);
86 $dataProvider->setLookupField($treeConfiguration['childrenField']);
87 } elseif (isset($treeConfiguration['parentField'])) {
88 $dataProvider->setLookupMode(DatabaseTreeDataProvider::MODE_PARENT);
89 $dataProvider->setLookupField($treeConfiguration['parentField']);
90 } else {
91 throw new \InvalidArgumentException('TCA Tree configuration is invalid: neither "childrenField" nor "parentField" is set', 1288215889);
92 }
93 } elseif ($tcaConfiguration['internal_type'] === 'file' && $dataProvider === null) {
94 // @todo Not implemented yet
95 throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for "internal_type=file" not implemented yet', 1288215891);
96 } elseif ($dataProvider === null) {
97 throw new \InvalidArgumentException('TCA Tree configuration is invalid: tree for "internal_type=' . $tcaConfiguration['internal_type'] . '" not implemented yet', 1288215892);
98 }
99 return $dataProvider;
100 }
101 }