TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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  */
18 
23 {
29  protected $types = [];
30 
34  public function __construct()
35  {
36  foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredCollections'] as $type => $class) {
37  $this->registerFileCollectionClass($class, $type);
38  }
39  }
40 
50  public function registerFileCollectionClass($className, $type, $override = false)
51  {
52  if (strlen($type) > 30) {
53  throw new \InvalidArgumentException('FileCollection type can have a max string length of 30 bytes', 1391295611);
54  }
55 
56  if (!class_exists($className)) {
57  throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1391295613);
58  }
59 
60  if (!in_array(AbstractFileCollection::class, class_parents($className), true)) {
61  throw new \InvalidArgumentException('FileCollection ' . $className . ' needs to extend the AbstractFileCollection.', 1391295633);
62  }
63 
64  if (isset($this->types[$type])) {
65  // Return immediately without changing configuration
66  if ($this->types[$type] === $className) {
67  return true;
68  } elseif (!$override) {
69  throw new \InvalidArgumentException('FileCollections ' . $type . ' is already registered.', 1391295643);
70  }
71  }
72 
73  $this->types[$type] = $className;
74  return true;
75  }
76 
86  public function addTypeToTCA($type, $label, $availableFields, array $additionalColumns = [])
87  {
88  $GLOBALS['TCA']['sys_file_collection']['types'][$type] = [
89  'showitem' => 'sys_language_uid, l10n_parent, l10n_diffsource, title, --palette--;;1, type, ' . $availableFields
90  ];
91 
92  // search for existing type when found override label
93  $typeFound = false;
94  foreach ($GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'] as $key => $item) {
95  if ($item[1] === $type) {
96  $typeFound = true;
97  $GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][$key][0] = $label;
98  }
99  }
100  if (!$typeFound) {
101  $GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][] = [
102  0 => $label,
103  1 => $type
104  ];
105  }
106  if ($additionalColumns !== []) {
107  ArrayUtility::mergeRecursiveWithOverrule($GLOBALS['TCA']['sys_file_collection']['columns'], $additionalColumns);
108  }
109  return $GLOBALS['TCA']['sys_file_collection'];
110  }
111 
119  public function getFileCollectionClass($type)
120  {
121  if (!isset($this->types[$type])) {
122  throw new \InvalidArgumentException('Desired FileCollection type "' . $type . '" is not in the list of available FileCollections.', 1391295644);
123  }
124  return $this->types[$type];
125  }
126 
133  public function fileCollectionTypeExists($type)
134  {
135  return isset($this->types[$type]);
136  }
137 }
addTypeToTCA($type, $label, $availableFields, array $additionalColumns=[])
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']