TYPO3  7.6
FileCollectionRegistry.php
Go to the documentation of this file.
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 
21 {
27  protected $types = array();
28 
32  public function __construct()
33  {
34  foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredCollections'] as $type => $class) {
35  $this->registerFileCollectionClass($class, $type);
36  }
37  }
38 
48  public function registerFileCollectionClass($className, $type, $override = false)
49  {
50  if (strlen($type) > 30) {
51  throw new \InvalidArgumentException('FileCollection type can have a max string length of 30 bytes', 1391295611);
52  }
53 
54  if (!class_exists($className)) {
55  throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1391295613);
56  }
57 
58  if (!in_array(\TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection::class, class_parents($className), true)) {
59  throw new \InvalidArgumentException('FileCollection ' . $className . ' needs to extend the AbstractFileCollection.', 1391295633);
60  }
61 
62  if (isset($this->types[$type])) {
63  // Return immediately without changing configuration
64  if ($this->types[$type] === $className) {
65  return true;
66  } elseif (!$override) {
67  throw new \InvalidArgumentException('FileCollections ' . $type . ' is already registered.', 1391295643);
68  }
69  }
70 
71  $this->types[$type] = $className;
72  return true;
73  }
74 
84  public function addTypeToTCA($type, $label, $availableFields, array $additionalColumns = array())
85  {
86  $GLOBALS['TCA']['sys_file_collection']['types'][$type] = array(
87  'showitem' => 'sys_language_uid, l10n_parent, l10n_diffsource, title, --palette--;;1, type, ' . $availableFields
88  );
89 
90  // search for existing type when found override label
91  $typeFound = false;
92  foreach ($GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'] as $key => $item) {
93  if ($item[1] === $type) {
94  $typeFound = true;
95  $GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][$key][0] = $label;
96  }
97  }
98  if (!$typeFound) {
99  $GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][] = array(
100  0 => $label,
101  1 => $type
102  );
103  }
104  if ($additionalColumns !== array()) {
105  \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($GLOBALS['TCA']['sys_file_collection']['columns'], $additionalColumns);
106  }
107  return $GLOBALS['TCA']['sys_file_collection'];
108  }
109 
117  public function getFileCollectionClass($type)
118  {
119  if (!isset($this->types[$type])) {
120  throw new \InvalidArgumentException('Desired FileCollection type "' . $type . '" is not in the list of available FileCollections.', 1391295644);
121  }
122  return $this->types[$type];
123  }
124 
131  public function fileCollectionTypeExists($type)
132  {
133  return isset($this->types[$type]);
134  }
135 }