TYPO3 CMS  TYPO3_6-2
CategoryRegistry.php
Go to the documentation of this file.
1 <?php
3 
22 
30 
34  protected $registry = array();
35 
39  protected $extensions = array();
40 
44  protected $addedCategoryTabs = array();
45 
49  protected $template = '';
50 
56  static public function getInstance() {
57  return GeneralUtility::makeInstance(__CLASS__);
58  }
59 
63  public function __construct() {
64  $this->template = str_repeat(PHP_EOL, 3) . 'CREATE TABLE %s (' . PHP_EOL
65  . ' %s int(11) DEFAULT \'0\' NOT NULL' . PHP_EOL . ');' . str_repeat(PHP_EOL, 3);
66  }
67 
85  public function add($extensionKey, $tableName, $fieldName = 'categories', array $options = array()) {
86  $didRegister = FALSE;
87  if (empty($tableName) || !is_string($tableName)) {
88  throw new \InvalidArgumentException('No or invalid table name "' . $tableName . '" given.', 1369122038);
89  }
90  if (empty($extensionKey) || !is_string($extensionKey)) {
91  throw new \InvalidArgumentException('No or invalid extension key "' . $extensionKey . '" given.', 1397836158);
92  }
93 
94  if (!$this->isRegistered($tableName, $fieldName)) {
95  $this->registry[$tableName][$fieldName] = $options;
96  $this->extensions[$extensionKey][$tableName][$fieldName] = $fieldName;
97 
98  if (!isset($GLOBALS['TCA'][$tableName]['columns']) && isset($GLOBALS['TCA'][$tableName]['ctrl']['dynamicConfigFile'])) {
99  // Handle deprecated old style dynamic TCA column loading.
101  }
102 
103  if (isset($GLOBALS['TCA'][$tableName]['columns'])) {
104  $this->applyTcaForTableAndField($tableName, $fieldName);
105  $didRegister = TRUE;
106  }
107  }
108 
109  return $didRegister;
110  }
111 
118  public function get() {
120  return $this->registry;
121  }
122 
128  public function getExtensionKeys() {
129  return array_keys($this->extensions);
130  }
131 
137  public function getCategorizedTables() {
138  return array_keys($this->registry);
139  }
140 
149  public function getCategoryFieldsForTable(array &$configuration) {
150  $table = '';
151  // Define the table being looked up from the type of menu
152  if ($configuration['row']['menu_type'] == 'categorized_pages') {
153  $table = 'pages';
154  } elseif ($configuration['row']['menu_type'] == 'categorized_content') {
155  $table = 'tt_content';
156  }
157  // Return early if no table is defined
158  if (empty($table)) {
159  throw new \UnexpectedValueException('The given menu_type is not supported.', 1381823570);
160  }
161  // Loop on all registries and find entries for the correct table
162  foreach ($this->registry as $tableName => $fields) {
163  if ($tableName === $table) {
164  foreach ($fields as $fieldName => $options) {
165  $fieldLabel = $this->getLanguageService()->sL($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['label']);
166  $configuration['items'][] = array($fieldLabel, $fieldName);
167  }
168  }
169  }
170  }
171 
179  public function isRegistered($tableName, $fieldName = 'categories') {
180  return isset($this->registry[$tableName][$fieldName]);
181  }
182 
188  public function getDatabaseTableDefinitions() {
189  $sql = '';
190  foreach ($this->getExtensionKeys() as $extensionKey) {
191  $sql .= $this->getDatabaseTableDefinition($extensionKey);
192  }
193  return $sql;
194  }
195 
202  public function getDatabaseTableDefinition($extensionKey) {
203  if (!isset($this->extensions[$extensionKey]) || !is_array($this->extensions[$extensionKey])) {
204  return '';
205  }
206  $sql = '';
207 
208  foreach ($this->extensions[$extensionKey] as $tableName => $fields) {
209  foreach ($fields as $fieldName) {
210  $sql .= sprintf($this->template, $tableName, $fieldName);
211  }
212  }
213  return $sql;
214  }
215 
219  public function applyTca() {
221  }
222 
229  public function applyTcaForPreRegisteredTables() {
231  foreach ($this->registry as $tableName => $fields) {
232  foreach ($fields as $fieldName => $_) {
233  $this->applyTcaForTableAndField($tableName, $fieldName);
234  }
235  }
236  }
237 
244  protected function applyTcaForTableAndField($tableName, $fieldName) {
245  $this->addTcaColumn($tableName, $fieldName, $this->registry[$tableName][$fieldName]);
246  $this->addToAllTCAtypes($tableName, $fieldName, $this->registry[$tableName][$fieldName]);
247  }
248 
254  protected function registerDefaultCategorizedTables() {
255  $defaultCategorizedTables = GeneralUtility::trimExplode(
256  ',',
257  $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'],
258  TRUE
259  );
260  foreach ($defaultCategorizedTables as $defaultCategorizedTable) {
261  if (!$this->isRegistered($defaultCategorizedTable)) {
262  $this->add('core', $defaultCategorizedTable, 'categories');
263  }
264  }
265  }
266 
278  protected function addToAllTCAtypes($tableName, $fieldName, array $options) {
279 
280  // Makes sure to add more TCA to an existing structure
281  if (isset($GLOBALS['TCA'][$tableName]['columns'])) {
282 
283  if (empty($options['fieldList'])) {
284  $fieldList = $this->addCategoryTab($tableName, $fieldName);
285  } else {
286  $fieldList = $options['fieldList'];
287  }
288 
289  $typesList = '';
290  if (!empty($options['typesList'])) {
291  $typesList = $options['typesList'];
292  }
293 
294  $position = '';
295  if (!empty($options['position'])) {
296  $position = $options['position'];
297  }
298 
299  // Makes the new "categories" field to be visible in TSFE.
300  ExtensionManagementUtility::addToAllTCAtypes($tableName, $fieldList, $typesList, $position);
301 
302  }
303  }
304 
313  protected function addCategoryTab($tableName, $fieldName) {
314  $fieldList = '';
315  if (!in_array($tableName, $this->addedCategoryTabs)) {
316  $fieldList .= '--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category, ';
317  $this->addedCategoryTabs[] = $tableName;
318  }
319  $fieldList .= $fieldName;
320  return $fieldList;
321  }
322 
336  protected function addTcaColumn($tableName, $fieldName, array $options) {
337  // Makes sure to add more TCA to an existing structure
338  if (isset($GLOBALS['TCA'][$tableName]['columns'])) {
339  // Take specific label into account
340  $label = 'LLL:EXT:lang/locallang_tca.xlf:sys_category.categories';
341  if (!empty($options['label'])) {
342  $label = $options['label'];
343  }
344 
345  // Take specific value of exclude flag into account
346  $exclude = TRUE;
347  if (isset($options['exclude'])) {
348  $exclude = (bool)$options['exclude'];
349  }
350 
351  $fieldConfiguration = empty($options['fieldConfiguration']) ? array() : $options['fieldConfiguration'];
352 
353  $columns = array(
354  $fieldName => array(
355  'exclude' => $exclude,
356  'label' => $label,
357  'config' => static::getTcaFieldConfiguration($tableName, $fieldName, $fieldConfiguration),
358  ),
359  );
360 
361  if (isset($options['l10n_mode'])) {
362  $columns[$fieldName]['l10n_mode'] = $options['l10n_mode'];
363  }
364  if (isset($options['l10n_display'])) {
365  $columns[$fieldName]['l10n_display'] = $options['l10n_display'];
366  }
367  if (isset($options['displayCond'])) {
368  $columns[$fieldName]['displayCond'] = $options['displayCond'];
369  }
370 
371  // Register opposite references for the foreign side of a relation
372  if (empty($GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName])) {
373  $GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName] = array();
374  }
375  if (!in_array($fieldName, $GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName])) {
376  $GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName][] = $fieldName;
377  }
378 
379  // Add field to interface list per default (unless the 'interface' property is FALSE)
380  if (
381  (!isset($options['interface']) || $options['interface'])
382  && !empty($GLOBALS['TCA'][$tableName]['interface']['showRecordFieldList'])
383  && !GeneralUtility::inList($GLOBALS['TCA'][$tableName]['interface']['showRecordFieldList'], $fieldName)
384  ) {
385  $GLOBALS['TCA'][$tableName]['interface']['showRecordFieldList'] .= ',' . $fieldName;
386  }
387 
388  // Adding fields to an existing table definition
389  ExtensionManagementUtility::addTCAcolumns($tableName, $columns);
390  }
391  }
392 
404  static public function getTcaFieldConfiguration($tableName, $fieldName = 'categories', array $fieldConfigurationOverride = array()) {
405  // Forges a new field, default name is "categories"
406  $fieldConfiguration = array(
407  'type' => 'select',
408  'foreign_table' => 'sys_category',
409  'foreign_table_where' => ' AND sys_category.sys_language_uid IN (-1, 0) ORDER BY sys_category.sorting ASC',
410  'MM' => 'sys_category_record_mm',
411  'MM_opposite_field' => 'items',
412  'MM_match_fields' => array(
413  'tablenames' => $tableName,
414  'fieldname' => $fieldName,
415  ),
416  'size' => 10,
417  'autoSizeMax' => 50,
418  'maxitems' => 9999,
419  'renderMode' => 'tree',
420  'treeConfig' => array(
421  'parentField' => 'parent',
422  'appearance' => array(
423  'expandAll' => TRUE,
424  'showHeader' => TRUE,
425  'maxLevels' => 99,
426  ),
427  ),
428  );
429 
430  // Merge changes to TCA configuration
431  if (!empty($fieldConfigurationOverride)) {
433  $fieldConfiguration,
434  $fieldConfigurationOverride
435  );
436  }
437 
438  return $fieldConfiguration;
439  }
440 
448  public function addCategoryDatabaseSchemaToTablesDefinition(array $sqlString) {
450  $sqlString[] = $this->getDatabaseTableDefinitions();
451  return array('sqlString' => $sqlString);
452  }
453 
462  public function addExtensionCategoryDatabaseSchemaToTablesDefinition(array $sqlString, $extensionKey) {
463  $sqlString[] = $this->getDatabaseTableDefinition($extensionKey);
464  return array('sqlString' => $sqlString, 'extensionKey' => $extensionKey);
465  }
466 
470  protected function getLanguageService() {
471  return $GLOBALS['LANG'];
472  }
473 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=TRUE, $includeEmptyValues=TRUE, $enableUnsetFeature=TRUE)
addToAllTCAtypes($tableName, $fieldName, array $options)
$sql
Definition: server.php:82
static getTcaFieldConfiguration($tableName, $fieldName='categories', array $fieldConfigurationOverride=array())
isRegistered($tableName, $fieldName='categories')
static addTCAcolumns($table, $columnArray, $addTofeInterface=FALSE)
addTcaColumn($tableName, $fieldName, array $options)
addExtensionCategoryDatabaseSchemaToTablesDefinition(array $sqlString, $extensionKey)
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
static addToAllTCAtypes($table, $newFieldsString, $typeList='', $position='')
applyTcaForTableAndField($tableName, $fieldName)
add($extensionKey, $tableName, $fieldName='categories', array $options=array())
getCategoryFieldsForTable(array &$configuration)
addCategoryDatabaseSchemaToTablesDefinition(array $sqlString)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]