[BUGFIX] Do not add category to empty table names
[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 textfile 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 string
44 */
45 protected $template = '';
46
47 /**
48 * Returns a class instance
49 *
50 * @return \TYPO3\CMS\Core\Category\CategoryRegistry
51 */
52 static public function getInstance() {
53 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Category\\CategoryRegistry');
54 }
55
56 /**
57 * Creates this object.
58 */
59 public function __construct() {
60 $this->template = str_repeat(PHP_EOL, 3) . 'CREATE TABLE %s (' . PHP_EOL . ' %s int(11) DEFAULT \'0\' NOT NULL' . PHP_EOL . ');' . str_repeat(PHP_EOL, 3);
61 }
62
63 /**
64 * Adds a new category configuration to this registry.
65 *
66 * @param string $extensionKey Extension key to be used
67 * @param string $tableName Name of the table to be registered
68 * @param string $fieldName Name of the field to be registered
69 * @param array $options Additional configuration options
70 * + fieldList: field configuration to be added to showitems
71 * + typesList: list of types that shall visualize the categories field
72 * + position: insert position of the categories field
73 * + fieldConfiguration: TCA field config array to override defaults
74 * @return bool
75 * @throws \RuntimeException
76 */
77 public function add($extensionKey, $tableName, $fieldName = 'categories', $options = array()) {
78 $result = FALSE;
79
80 if ($tableName === '') {
81 throw new \RuntimeException('TYPO3\\CMS\\Core\\Category\\CategoryRegistry No tableName given.', 1369122038);
82 }
83
84 // Makes sure there is an existing table configuration and nothing registered yet:
85 if (!$this->isRegistered($tableName, $fieldName)) {
86 $this->registry[$extensionKey][$tableName] = array (
87 'fieldName' => $fieldName,
88 'options' => $options,
89 );
90 $result = TRUE;
91 }
92 return $result;
93 }
94
95 /**
96 * Gets the registered category configurations.
97 *
98 * @return array
99 */
100 public function get() {
101 return $this->registry;
102 }
103
104 /**
105 * Gets all extension keys that registered a category configuration.
106 *
107 * @return array
108 */
109 public function getExtensionKeys() {
110 return array_keys($this->registry);
111 }
112
113 /**
114 * Gets all categorized tables
115 *
116 * @return array
117 */
118 public function getCategorizedTables() {
119 $categorizedTables = array();
120
121 foreach ($this->registry as $registry) {
122 $categorizedTables = array_merge($categorizedTables, array_keys($registry));
123 }
124
125 return $categorizedTables;
126 }
127
128 /**
129 * Tells whether a table has a category configuration in the registry.
130 *
131 * @param string $tableName Name of the table to be looked up
132 * @param string $fieldName Name of the field to be looked up
133 * @return boolean
134 */
135 public function isRegistered($tableName, $fieldName = 'categories') {
136 $isRegistered = FALSE;
137 foreach ($this->registry as $configuration) {
138 if (!empty($configuration[$tableName]['fieldName']) && $configuration[$tableName]['fieldName'] === $fieldName) {
139 $isRegistered = TRUE;
140 break;
141 }
142 }
143 return $isRegistered;
144 }
145
146 /**
147 * Generates tables definitions for all registered tables.
148 *
149 * @return string
150 */
151 public function getDatabaseTableDefinitions() {
152 $sql = '';
153 foreach ($this->getExtensionKeys() as $extensionKey) {
154 $sql .= $this->getDatabaseTableDefinition($extensionKey);
155 }
156 return $sql;
157 }
158
159 /**
160 * Generates table definitions for registered tables by an extension.
161 *
162 * @param string $extensionKey Extension key to have the database definitions created for
163 * @return string
164 */
165 public function getDatabaseTableDefinition($extensionKey) {
166 if (!isset($this->registry[$extensionKey]) || !is_array($this->registry[$extensionKey])) {
167 return '';
168 }
169 $sql = '';
170
171 foreach ($this->registry[$extensionKey] as $tableName => $tableInfo) {
172 $sql .= sprintf($this->template, $tableName, $tableInfo['fieldName']);
173 }
174 return $sql;
175 }
176
177 /**
178 * Apply TCA to all registered tables
179 *
180 * @return void
181 */
182 public function applyTca() {
183
184 $this->registerDefaultCategorizedTables();
185
186 foreach ($this->registry as $registry) {
187 foreach ($registry as $tableName => $tableInfo) {
188 $this->addTcaColumn($tableName, $tableInfo['fieldName'], $tableInfo['options']);
189 $this->addToAllTCAtypes($tableName, $tableInfo['fieldName'], $tableInfo['options']);
190 }
191 }
192 }
193
194 /**
195 * Add default categorized tables to the registry
196 *
197 * @return void
198 */
199 protected function registerDefaultCategorizedTables() {
200 $defaultCategorizedTables = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(
201 ',',
202 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'],
203 TRUE
204 );
205 foreach ($defaultCategorizedTables as $defaultCategorizedTable) {
206 if (!$this->isRegistered($defaultCategorizedTable)) {
207 $this->add('core', $defaultCategorizedTable, 'categories');
208 }
209 }
210 }
211
212 /**
213 * Add a new field into the TCA types -> showitem
214 *
215 * @param string $tableName Name of the table to be categorized
216 * @param string $fieldName Name of the field to be used to store categories
217 * @param array $options Additional configuration options
218 * + fieldList: field configuration to be added to showitems
219 * + typesList: list of types that shall visualize the categories field
220 * + position: insert position of the categories field
221 * @return void
222 */
223 protected function addToAllTCAtypes($tableName, $fieldName, $options) {
224
225 // Makes sure to add more TCA to an existing structure
226 if (isset($GLOBALS['TCA'][$tableName]['columns'])) {
227
228 $fieldList = '--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category, ' . $fieldName;
229 if (!empty($options['fieldList'])) {
230 $fieldList = $options['fieldList'];
231 }
232
233 $typesList = '';
234 if (!empty($options['typesList'])) {
235 $typesList = $options['typesList'];
236 }
237
238 $position = '';
239 if (!empty($options['position'])) {
240 $position = $options['position'];
241 }
242
243 // Makes the new "categories" field to be visible in TSFE.
244 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes($tableName, $fieldList, $typesList, $position);
245
246 }
247 }
248
249 /**
250 * Add a new TCA Column
251 *
252 * @param string $tableName Name of the table to be categorized
253 * @param string $fieldName Name of the field to be used to store categories
254 * @param array $options Additional configuration options
255 * + fieldConfiguration: TCA field config array to override defaults
256 * @return void
257 */
258 protected function addTcaColumn($tableName, $fieldName, $options) {
259
260 // Makes sure to add more TCA to an existing structure
261 if (isset($GLOBALS['TCA'][$tableName]['columns'])) {
262
263 // Forges a new field, default name is "categories"
264 $fieldConfiguration = array(
265 'type' => 'select',
266 'foreign_table' => 'sys_category',
267 'foreign_table_where' => ' ORDER BY sys_category.title ASC',
268 'MM' => 'sys_category_record_mm',
269 'MM_opposite_field' => 'items',
270 'MM_match_fields' => array('tablenames' => $tableName),
271 'size' => 10,
272 'autoSizeMax' => 50,
273 'maxitems' => 9999,
274 'renderMode' => 'tree',
275 'treeConfig' => array(
276 'parentField' => 'parent',
277 'appearance' => array(
278 'expandAll' => TRUE,
279 'showHeader' => TRUE,
280 ),
281 ),
282 'wizards' => array(
283 '_PADDING' => 1,
284 '_VERTICAL' => 1,
285 'edit' => array(
286 'type' => 'popup',
287 'title' => 'Edit',
288 'script' => 'wizard_edit.php',
289 'icon' => 'edit2.gif',
290 'popup_onlyOpenIfSelected' => 1,
291 'JSopenParams' => 'height=350,width=580,status=0,menubar=0,scrollbars=1',
292 ),
293 'add' => Array(
294 'type' => 'script',
295 'title' => 'Create new',
296 'icon' => 'add.gif',
297 'params' => array(
298 'table' => 'sys_category',
299 'pid' => '###CURRENT_PID###',
300 'setValue' => 'prepend'
301 ),
302 'script' => 'wizard_add.php',
303 ),
304 ),
305 );
306
307 if (!empty($options['fieldConfiguration'])) {
308 $fieldConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule(
309 $fieldConfiguration,
310 $options['fieldConfiguration']
311 );
312 }
313
314 $columns = array(
315 $fieldName => array(
316 'exclude' => 0,
317 'label' => 'LLL:EXT:lang/locallang_tca.xlf:sys_category.categories',
318 'config' => $fieldConfiguration,
319 ),
320 );
321
322 // Adding fields to an existing table definition
323 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns($tableName, $columns);
324 }
325 }
326 }
327
328 ?>