[BUGFIX] remove enableFields for sys_file_processedfile
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Category / CategoryRegistry.php
1 <?php
2 namespace TYPO3\CMS\Core\Category;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Fabien Udriot <fabien.udriot@typo3.org>
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 * Class to register category configurations.
31 *
32 * @author Fabien Udriot <fabien.udriot@typo3.org>
33 * @author Oliver Hader <oliver.hader@typo3.org>
34 */
35 class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
36
37 /**
38 * @var array
39 */
40 protected $registry = array();
41
42 /**
43 * @var array
44 */
45 protected $addedCategoryTabs = array();
46
47 /**
48 * @var string
49 */
50 protected $template = '';
51
52 /**
53 * Returns a class instance
54 *
55 * @return \TYPO3\CMS\Core\Category\CategoryRegistry
56 */
57 static public function getInstance() {
58 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(__CLASS__);
59 }
60
61 /**
62 * Creates this object.
63 */
64 public function __construct() {
65 $this->template = str_repeat(PHP_EOL, 3) . 'CREATE TABLE %s (' . PHP_EOL
66 . ' %s int(11) DEFAULT \'0\' NOT NULL' . PHP_EOL . ');' . str_repeat(PHP_EOL, 3);
67 }
68
69 /**
70 * Adds a new category configuration to this registry.
71 *
72 * @param string $extensionKey Extension key to be used
73 * @param string $tableName Name of the table to be registered
74 * @param string $fieldName Name of the field to be registered
75 * @param array $options Additional configuration options
76 * + fieldList: field configuration to be added to showitems
77 * + typesList: list of types that shall visualize the categories field
78 * + position: insert position of the categories field
79 * + label: backend label of the categories field
80 * + fieldConfiguration: TCA field config array to override defaults
81 * @return boolean
82 * @throws \InvalidArgumentException
83 */
84 public function add($extensionKey, $tableName, $fieldName = 'categories', array $options = array()) {
85 $result = FALSE;
86
87 if ($tableName === '') {
88 throw new \InvalidArgumentException('TYPO3\\CMS\\Core\\Category\\CategoryRegistry No tableName given.', 1369122038);
89 }
90
91 // Makes sure nothing was registered yet.
92 if (!$this->isRegistered($tableName, $fieldName)) {
93 $this->registry[$extensionKey][$tableName][$fieldName] = $options;
94 $result = TRUE;
95 }
96 return $result;
97 }
98
99 /**
100 * Gets the registered category configurations.
101 *
102 * @deprecated since 6.2 will be removed two versions later - Use ->isRegistered to get information about registered category fields.
103 * @return array
104 */
105 public function get() {
106 \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
107 return $this->registry;
108 }
109
110 /**
111 * Gets all extension keys that registered a category configuration.
112 *
113 * @return array
114 */
115 public function getExtensionKeys() {
116 return array_keys($this->registry);
117 }
118
119 /**
120 * Gets all categorized tables
121 *
122 * @return array
123 */
124 public function getCategorizedTables() {
125 $categorizedTables = array();
126
127 foreach ($this->registry as $registry) {
128 $categorizedTables = array_merge($categorizedTables, array_keys($registry));
129 }
130
131 return $categorizedTables;
132 }
133
134 /**
135 * Returns a list of category fields for a given table for populating selector "category_field"
136 * in tt_content table (called as itemsProcFunc).
137 *
138 * @param array $configuration Current field configuration
139 * @param \TYPO3\CMS\Backend\Form\FormEngine $formObject Back-reference to the calling object
140 * @throws \UnexpectedValueException
141 * @return void
142 */
143 public function getCategoryFieldsForTable(array &$configuration, \TYPO3\CMS\Backend\Form\FormEngine $formObject) {
144 $table = '';
145 // Define the table being looked up from the type of menu
146 if ($configuration['row']['menu_type'] == 'categorized_pages') {
147 $table = 'pages';
148 } elseif ($configuration['row']['menu_type'] == 'categorized_content') {
149 $table = 'tt_content';
150 }
151 // Return early if no table is defined
152 if (empty($table)) {
153 throw new \UnexpectedValueException('The given menu_type is not supported.', 1381823570);
154 }
155 // Loop on all registries and find entries for the correct table
156 foreach ($this->registry as $registry) {
157 foreach ($registry as $tableName => $fields) {
158 if ($tableName === $table) {
159 foreach ($fields as $fieldName => $options) {
160 $fieldLabel = $GLOBALS['LANG']->sL($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['label']);
161 $configuration['items'][] = array($fieldLabel, $fieldName);
162 }
163 }
164 }
165 }
166 }
167
168 /**
169 * Tells whether a table has a category configuration in the registry.
170 *
171 * @param string $tableName Name of the table to be looked up
172 * @param string $fieldName Name of the field to be looked up
173 * @return boolean
174 */
175 public function isRegistered($tableName, $fieldName = 'categories') {
176 $isRegistered = FALSE;
177 foreach ($this->registry as $configuration) {
178 if (isset($configuration[$tableName][$fieldName])) {
179 $isRegistered = TRUE;
180 break;
181 }
182 }
183 return $isRegistered;
184 }
185
186 /**
187 * Generates tables definitions for all registered tables.
188 *
189 * @return string
190 */
191 public function getDatabaseTableDefinitions() {
192 $sql = '';
193 foreach ($this->getExtensionKeys() as $extensionKey) {
194 $sql .= $this->getDatabaseTableDefinition($extensionKey);
195 }
196 return $sql;
197 }
198
199 /**
200 * Generates table definitions for registered tables by an extension.
201 *
202 * @param string $extensionKey Extension key to have the database definitions created for
203 * @return string
204 */
205 public function getDatabaseTableDefinition($extensionKey) {
206 if (!isset($this->registry[$extensionKey]) || !is_array($this->registry[$extensionKey])) {
207 return '';
208 }
209 $sql = '';
210
211 foreach ($this->registry[$extensionKey] as $tableName => $fields) {
212 foreach (array_keys($fields) as $fieldName) {
213 $sql .= sprintf($this->template, $tableName, $fieldName);
214 }
215 }
216 return $sql;
217 }
218
219 /**
220 * Apply TCA to all registered tables
221 *
222 * @return void
223 * @internal
224 */
225 public function applyTca() {
226
227 $this->registerDefaultCategorizedTables();
228
229 foreach ($this->registry as $registry) {
230 foreach ($registry as $tableName => $fields) {
231 foreach ($fields as $fieldName => $options) {
232 $this->addTcaColumn($tableName, $fieldName, $options);
233 $this->addToAllTCAtypes($tableName, $fieldName, $options);
234 }
235 }
236 }
237 }
238
239 /**
240 * Add default categorized tables to the registry
241 *
242 * @return void
243 */
244 protected function registerDefaultCategorizedTables() {
245 $defaultCategorizedTables = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(
246 ',',
247 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'],
248 TRUE
249 );
250 foreach ($defaultCategorizedTables as $defaultCategorizedTable) {
251 if (!$this->isRegistered($defaultCategorizedTable)) {
252 $this->add('core', $defaultCategorizedTable, 'categories');
253 }
254 }
255 }
256
257 /**
258 * Add a new field into the TCA types -> showitem
259 *
260 * @param string $tableName Name of the table to be categorized
261 * @param string $fieldName Name of the field to be used to store categories
262 * @param array $options Additional configuration options
263 * + fieldList: field configuration to be added to showitems
264 * + typesList: list of types that shall visualize the categories field
265 * + position: insert position of the categories field
266 * @return void
267 */
268 protected function addToAllTCAtypes($tableName, $fieldName, array $options) {
269
270 // Makes sure to add more TCA to an existing structure
271 if (isset($GLOBALS['TCA'][$tableName]['columns'])) {
272
273 if (empty($options['fieldList'])) {
274 $fieldList = $this->addCategoryTab($tableName, $fieldName);
275 } else {
276 $fieldList = $options['fieldList'];
277 }
278
279 $typesList = '';
280 if (!empty($options['typesList'])) {
281 $typesList = $options['typesList'];
282 }
283
284 $position = '';
285 if (!empty($options['position'])) {
286 $position = $options['position'];
287 }
288
289 // Makes the new "categories" field to be visible in TSFE.
290 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes($tableName, $fieldList, $typesList, $position);
291
292 }
293 }
294
295 /**
296 * Creates the 'fieldList' string for $fieldName which includes a categories tab.
297 * But only one categories tab is added per table.
298 *
299 * @param string $tableName
300 * @param string $fieldName
301 * @return string
302 */
303 protected function addCategoryTab($tableName, $fieldName) {
304 $fieldList = '';
305 if (!in_array($tableName, $this->addedCategoryTabs)) {
306 $fieldList .= '--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category, ';
307 $this->addedCategoryTabs[] = $tableName;
308 }
309 $fieldList .= $fieldName;
310 return $fieldList;
311 }
312
313 /**
314 * Add a new TCA Column
315 *
316 * @param string $tableName Name of the table to be categorized
317 * @param string $fieldName Name of the field to be used to store categories
318 * @param array $options Additional configuration options
319 * + fieldConfiguration: TCA field config array to override defaults
320 * + label: backend label of the categories field
321 * @return void
322 */
323 protected function addTcaColumn($tableName, $fieldName, array $options) {
324
325 // Makes sure to add more TCA to an existing structure
326 if (isset($GLOBALS['TCA'][$tableName]['columns'])) {
327
328 // Forges a new field, default name is "categories"
329 $fieldConfiguration = array(
330 'type' => 'select',
331 'foreign_table' => 'sys_category',
332 'foreign_table_where' => ' AND sys_category.sys_language_uid IN (-1, 0) ORDER BY sys_category.sorting ASC',
333 'MM' => 'sys_category_record_mm',
334 'MM_opposite_field' => 'items',
335 'MM_match_fields' => array(
336 'tablenames' => $tableName,
337 'fieldname' => $fieldName,
338 ),
339 'size' => 10,
340 'autoSizeMax' => 50,
341 'maxitems' => 9999,
342 'renderMode' => 'tree',
343 'treeConfig' => array(
344 'parentField' => 'parent',
345 'appearance' => array(
346 'expandAll' => TRUE,
347 'showHeader' => TRUE,
348 'maxLevels' => 99,
349 ),
350 ),
351 );
352
353 // Merge changes to TCA configuration
354 if (!empty($options['fieldConfiguration'])) {
355 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule(
356 $fieldConfiguration,
357 $options['fieldConfiguration']
358 );
359 }
360
361 // Take specific label into account
362 $label = 'LLL:EXT:lang/locallang_tca.xlf:sys_category.categories';
363 if (!empty($options['label'])) {
364 $label = $options['label'];
365 }
366
367 // Take specific value of exclude flag into account
368 $exclude = TRUE;
369 if (isset($options['exclude'])) {
370 $exclude = (bool)$options['exclude'];
371 }
372
373 $columns = array(
374 $fieldName => array(
375 'exclude' => $exclude,
376 'label' => $label,
377 'config' => $fieldConfiguration,
378 ),
379 );
380
381 if (empty($GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName])) {
382 $GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName] = array();
383 }
384 if (!in_array($fieldName, $GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName])) {
385 $GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName][] = $fieldName;
386 }
387
388 // Adding fields to an existing table definition
389 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns($tableName, $columns);
390 }
391 }
392
393 /**
394 * A slot method to inject the required category database fields to the
395 * tables definition string
396 *
397 * @param array $sqlString
398 * @return array
399 */
400 public function addCategoryDatabaseSchemaToTablesDefinition(array $sqlString) {
401 $this->registerDefaultCategorizedTables();
402 $sqlString[] = $this->getDatabaseTableDefinitions();
403 return array('sqlString' => $sqlString);
404 }
405
406 /**
407 * A slot method to inject the required category database fields of an
408 * extension to the tables definition string
409 *
410 * @param array $sqlString
411 * @param string $extensionKey
412 * @return array
413 */
414 public function addExtensionCategoryDatabaseSchemaToTablesDefinition(array $sqlString, $extensionKey) {
415 $sqlString[] = $this->getDatabaseTableDefinition($extensionKey);
416 return array('sqlString' => $sqlString, 'extensionKey' => $extensionKey);
417 }
418 }