[!!!][TASK] Drop additional palette handling
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Collection / FileCollectionRegistry.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Collection;
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 * Registry for FileCollection classes
19 */
20 class FileCollectionRegistry implements \TYPO3\CMS\Core\SingletonInterface {
21
22 /**
23 * Registered FileCollection types
24 *
25 * @var array
26 */
27 protected $types = array();
28
29 /**
30 * Constructor
31 */
32 public function __construct() {
33 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredCollections'] as $type => $class) {
34 $this->registerFileCollectionClass($class, $type);
35 }
36 }
37
38 /**
39 * Register a (new) FileCollection type
40 *
41 * @param string $className
42 * @param string $type FileCollection type max length 30 chars (db field restriction)
43 * @param bool $override existing FileCollection type
44 * @return bool TRUE if registration succeeded
45 * @throws \InvalidArgumentException
46 */
47 public function registerFileCollectionClass($className, $type, $override = FALSE) {
48
49 if (strlen($type) > 30) {
50 throw new \InvalidArgumentException('FileCollection type can have a max string length of 30 bytes', 1391295611);
51 }
52
53 if (!class_exists($className)) {
54 throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1391295613);
55 }
56
57 if (!in_array(\TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection::class, class_parents($className), TRUE)) {
58 throw new \InvalidArgumentException('FileCollection ' . $className . ' needs to extend the AbstractFileCollection.', 1391295633);
59 }
60
61 if (isset($this->types[$type])) {
62 // Return immediately without changing configuration
63 if ($this->types[$type] === $className) {
64 return TRUE;
65 } elseif (!$override) {
66 throw new \InvalidArgumentException('FileCollections ' . $type . ' is already registered.', 1391295643);
67 }
68 }
69
70 $this->types[$type] = $className;
71 return TRUE;
72 }
73
74 /**
75 * Add the type to the TCA of sys_file_collection
76 *
77 * @param string $type
78 * @param string $label
79 * @param string $availableFields comma separated list of fields to show
80 * @param array $additionalColumns Additional columns configuration
81 * @return array adjusted TCA for sys_file_collection
82 */
83 public function addTypeToTCA($type, $label, $availableFields, array $additionalColumns = array()) {
84
85 $GLOBALS['TCA']['sys_file_collection']['types'][$type] = array(
86 'showitem' => 'sys_language_uid, l10n_parent, l10n_diffsource, title, --palette--;;1, type, ' . $availableFields
87 );
88
89 // search for existing type when found override label
90 $typeFound = FALSE;
91 foreach ($GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'] as $key => $item) {
92 if ($item[1] === $type) {
93 $typeFound = TRUE;
94 $GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][$key][0] = $label;
95 }
96 }
97 if (!$typeFound) {
98 $GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][] = array(
99 0 => $label,
100 1 => $type
101 );
102 }
103 if ($additionalColumns !== array()) {
104 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($GLOBALS['TCA']['sys_file_collection']['columns'], $additionalColumns);
105 }
106 return $GLOBALS['TCA']['sys_file_collection'];
107 }
108
109 /**
110 * Returns a class name for a given type
111 *
112 * @param string $type
113 * @return string The class name
114 * @throws \InvalidArgumentException
115 */
116 public function getFileCollectionClass($type) {
117 if (!isset($this->types[$type])) {
118 throw new \InvalidArgumentException('Desired FileCollection type "' . $type . '" is not in the list of available FileCollections.', 1391295644);
119 }
120 return $this->types[$type];
121 }
122
123 /**
124 * Checks if the given FileCollection type exists
125 *
126 * @param string $type Type of the FileCollection
127 * @return bool TRUE if the FileCollection exists, FALSE otherwise
128 */
129 public function fileCollectionTypeExists($type) {
130 return isset($this->types[$type]);
131 }
132
133 }