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