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