[BUGFIX] Warning on fetching undefined category definitions
[Packages/TYPO3.CMS.git] / t3lib / category / Registry.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Fabien Udriot <fabien.udriot@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Class to register category configurations.
30 *
31 * @author Fabien Udriot <fabien.udriot@typo3.org>
32 * @author Oliver Hader <oliver.hader@typo3.org>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36 class t3lib_category_Registry implements t3lib_Singleton {
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 t3lib_category_Registry
51 */
52 public static function getInstance() {
53 return t3lib_div::makeInstance('t3lib_category_Registry');
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 .
61 ' %s int(11) DEFAULT \'0\' NOT NULL' . PHP_EOL .
62 ');' . str_repeat(PHP_EOL, 3);
63 }
64
65 /**
66 * Adds a new category configuration to this registry.
67 *
68 * @param string $extensionKey Extension key to be used
69 * @param string $tableName Name of the table to be registered
70 * @param string $fieldName Name of the field to be registered
71 * @return boolean Whether fieldName of tableName is registered
72 */
73 public function add($extensionKey, $tableName, $fieldName) {
74 $result = FALSE;
75
76 // Makes sure there is an existing table configuration and nothing registered yet:
77 if (!empty($GLOBALS['TCA'][$tableName])) {
78 if (!$this->isRegistered($tableName, $fieldName)) {
79 $this->registry[$extensionKey][$tableName] = $fieldName;
80 }
81
82 $result = TRUE;
83 }
84
85 return $result;
86 }
87
88 /**
89 * Gets the registered category configurations.
90 *
91 * @return array
92 */
93 public function get() {
94 return $this->registry;
95 }
96
97 /**
98 * Gets all extension keys that registered a category configuration.
99 *
100 * @return array
101 */
102 public function getExtensionKeys() {
103 return array_keys($this->registry);
104 }
105
106 /**
107 * Tells whether a table has a category configuration in the registry.
108 *
109 * @param string $tableName Name of the table to be looked up
110 * @param string $fieldName Name of the field to be looked up
111 * @return boolean
112 */
113 public function isRegistered($tableName, $fieldName) {
114 $isRegistered = FALSE;
115
116 foreach ($this->registry as $configuration) {
117 if (!empty($configuration[$tableName]) && $configuration[$tableName] === $fieldName) {
118 $isRegistered = TRUE;
119 break;
120 }
121 }
122
123 return $isRegistered;
124 }
125
126 /**
127 * Generates tables definitions for all registered tables.
128 *
129 * @return string
130 */
131 public function getDatabaseTableDefinitions() {
132 $sql = '';
133
134 foreach ($this->getExtensionKeys() as $extensionKey) {
135 $sql .= $this->getDatabaseTableDefinition($extensionKey);
136 }
137
138 return $sql;
139 }
140
141 /**
142 * Generates table definitions for registered tables by an extension.
143 *
144 * @param string $extensionKey Extension key to have the database definitions created for
145 * @return string
146 */
147 public function getDatabaseTableDefinition($extensionKey) {
148 if (!isset($this->registry[$extensionKey]) || !is_array($this->registry[$extensionKey])) {
149 return '';
150 }
151
152 $sql = '';
153 foreach ($this->registry[$extensionKey] as $tableName => $fieldName) {
154 $sql .= sprintf($this->template, $tableName, $fieldName);
155 }
156
157 return $sql;
158 }
159 }
160 ?>