09ac41c0c9fb473a0d75d4e0e6bece6fb290d720
[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 * @return boolean Whether fieldName of tableName is registered
70 */
71 public function add($extensionKey, $tableName, $fieldName) {
72 $result = FALSE;
73 // Makes sure there is an existing table configuration and nothing registered yet:
74 if (!empty($GLOBALS['TCA'][$tableName])) {
75 if (!$this->isRegistered($tableName, $fieldName)) {
76 $this->registry[$extensionKey][$tableName] = $fieldName;
77 }
78 $result = TRUE;
79 }
80 return $result;
81 }
82
83 /**
84 * Gets the registered category configurations.
85 *
86 * @return array
87 */
88 public function get() {
89 return $this->registry;
90 }
91
92 /**
93 * Gets all extension keys that registered a category configuration.
94 *
95 * @return array
96 */
97 public function getExtensionKeys() {
98 return array_keys($this->registry);
99 }
100
101 /**
102 * Tells whether a table has a category configuration in the registry.
103 *
104 * @param string $tableName Name of the table to be looked up
105 * @param string $fieldName Name of the field to be looked up
106 * @return boolean
107 */
108 public function isRegistered($tableName, $fieldName) {
109 $isRegistered = FALSE;
110 foreach ($this->registry as $configuration) {
111 if (!empty($configuration[$tableName]) && $configuration[$tableName] === $fieldName) {
112 $isRegistered = TRUE;
113 break;
114 }
115 }
116 return $isRegistered;
117 }
118
119 /**
120 * Generates tables definitions for all registered tables.
121 *
122 * @return string
123 */
124 public function getDatabaseTableDefinitions() {
125 $sql = '';
126 foreach ($this->getExtensionKeys() as $extensionKey) {
127 $sql .= $this->getDatabaseTableDefinition($extensionKey);
128 }
129 return $sql;
130 }
131
132 /**
133 * Generates table definitions for registered tables by an extension.
134 *
135 * @param string $extensionKey Extension key to have the database definitions created for
136 * @return string
137 */
138 public function getDatabaseTableDefinition($extensionKey) {
139 if (!isset($this->registry[$extensionKey]) || !is_array($this->registry[$extensionKey])) {
140 return '';
141 }
142 $sql = '';
143 foreach ($this->registry[$extensionKey] as $tableName => $fieldName) {
144 $sql .= sprintf($this->template, $tableName, $fieldName);
145 }
146 return $sql;
147 }
148
149 }
150
151
152 ?>