97368498da26c0e843cd190cbeb307cf658289fb
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Mapper / DataMapFactory.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
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 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * A factory for a data map to map a single table configured in $TCA on a domain object.
27 *
28 * @package Extbase
29 * @subpackage Persistence\Mapper
30 * @version $ID:$
31 */
32 class Tx_Extbase_Persistence_Mapper_DataMapFactory implements t3lib_Singleton {
33
34 /**
35 * @var Tx_Extbase_Reflection_Service
36 */
37 protected $reflectionService;
38
39 /**
40 * Injects the reflection service
41 *
42 * @param Tx_Extbase_Reflection_Service $reflectionService
43 * @return void
44 */
45 public function injectReflectionService(Tx_Extbase_Reflection_Service $reflectionService) {
46 $this->reflectionService = $reflectionService;
47 }
48
49 /**
50 * Builds a data map by adding column maps for all the configured columns in the $TCA.
51 * It also resolves the type of values the column is holding and the typo of relation the column
52 * represents.
53 *
54 * @param string $className The class name you want to fetch the Data Map for
55 * @return Tx_Extbase_Persistence_Mapper_DataMap The data map
56 */
57 public function buildDataMap($className) {
58 if (!class_exists($className)) {
59 throw new Tx_Extbase_Persistence_Exception_InvalidClass('Could not find class definition for name "' . $className . '". This could be caused by a mis-spelling of the class name in the class definition.');
60 }
61
62 $recordType = NULL;
63 $subclasses = array();
64 $tableName = strtolower($className);
65 $columnMapping = array();
66
67 $extbaseFrameworkConfiguration = Tx_Extbase_Dispatcher::getExtbaseFrameworkConfiguration();
68 $classSettings = $extbaseFrameworkConfiguration['persistence']['classes'][$className];
69 if ($classSettings !== NULL) {
70 if (isset($classSettings['subclasses']) && is_array($classSettings['subclasses'])) {
71 $subclasses = $classSettings['subclasses'];
72 }
73 if (isset($classSettings['mapping']['recordType']) && strlen($classSettings['mapping']['recordType']) > 0) {
74 $recordType = $classSettings['mapping']['recordType'];
75 }
76 if (isset($classSettings['mapping']['tableName']) && strlen($classSettings['mapping']['tableName']) > 0) {
77 $tableName = $classSettings['mapping']['tableName'];
78 }
79 $classHierachy = array_merge(array($className), class_parents($className));
80 foreach ($classHierachy as $currentClassName) {
81 if (in_array($currentClassName, array('Tx_Extbase_DomainObject_AbstractEntity', 'Tx_Extbase_DomainObject_AbstractValueObject'))) {
82 break;
83 }
84 $currentTableName = strtolower($currentClassName);
85 $currentClassSettings = $extbaseFrameworkConfiguration['persistence']['classes'][$currentClassName];
86 if ($currentClassSettings !== NULL) {
87 if (isset($currentClassSettings['mapping']['columns']) && is_array($currentClassSettings['mapping']['columns'])) {
88 $columnMapping = t3lib_div::array_merge_recursive_overrule($columnMapping, $currentClassSettings['mapping']['columns'], 0, FALSE); // FALSE means: do not include empty values form 2nd array
89 }
90 }
91 }
92 }
93
94 $dataMap = new Tx_Extbase_Persistence_Mapper_DataMap($className, $tableName, $recordType, $subclasses);
95 $dataMap = $this->addMetaDataColumnNames($dataMap, $tableName);
96
97 // $classPropertyNames = $this->reflectionService->getClassPropertyNames($className);
98 $tcaColumnsDefinition = $this->getColumnsDefinition($tableName);
99 $tcaColumnsDefinition = t3lib_div::array_merge_recursive_overrule($tcaColumnsDefinition, $columnMapping); // TODO Is this is too powerful?
100 foreach ($tcaColumnsDefinition as $columnName => $columnDefinition) {
101 if (isset($columnDefinition['mapOnProperty'])) {
102 $propertyName = $columnDefinition['mapOnProperty'];
103 } else {
104 $propertyName = Tx_Extbase_Utility_Extension::convertUnderscoredToLowerCamelCase($columnName);
105 }
106 // if (in_array($propertyName, $classPropertyNames)) { // TODO Enable check for property existance
107 $columnMap = new Tx_Extbase_Persistence_Mapper_ColumnMap($columnName, $propertyName);
108 $propertyMetaData = $this->reflectionService->getClassSchema($className)->getProperty($propertyName);
109 $columnMap = $this->setRelations($columnMap, $columnDefinition['config'], $propertyMetaData);
110 $dataMap->addColumnMap($columnMap);
111 // }
112 }
113 // debug($dataMap);
114 return $dataMap;
115 }
116
117 /**
118 * Returns the TCA ctrl section of the specified table; or NULL if not set
119 *
120 * @param string $tableName An optional table name to fetch the columns definition from
121 * @return array The TCA columns definition
122 */
123 protected function getControlSection($tableName) {
124 $this->includeTca($tableName);
125 return is_array($GLOBALS['TCA'][$tableName]['ctrl']) ? $GLOBALS['TCA'][$tableName]['ctrl'] : NULL;
126 }
127
128 /**
129 * Returns the TCA columns array of the specified table
130 *
131 * @param string $tableName An optional table name to fetch the columns definition from
132 * @return array The TCA columns definition
133 */
134 protected function getColumnsDefinition($tableName) {
135 $this->includeTca($tableName);
136 return is_array($GLOBALS['TCA'][$tableName]['columns']) ? $GLOBALS['TCA'][$tableName]['columns'] : array();
137 }
138
139 /**
140 * Includes the TCA for the given table
141 *
142 * @param string $tableName An optional table name to fetch the columns definition from
143 * @return void
144 */
145 protected function includeTca($tableName) {
146 if (TYPO3_MODE === 'FE') {
147 $GLOBALS['TSFE']->includeTCA();
148 }
149 t3lib_div::loadTCA($tableName);
150 }
151
152 protected function addMetaDataColumnNames(Tx_Extbase_Persistence_Mapper_DataMap $dataMap, $tableName) {
153 $controlSection = $GLOBALS['TCA'][$tableName]['ctrl'];
154 $dataMap->setPageIdColumnName('pid');
155 if (isset($controlSection['tstamp'])) $dataMap->setModificationDateColumnName($controlSection['tstamp']);
156 if (isset($controlSection['crdate'])) $dataMap->setCreationDateColumnName($controlSection['crdate']);
157 if (isset($controlSection['cruser_id'])) $dataMap->setCreatorColumnName($controlSection['cruser_id']);
158 if (isset($controlSection['delete'])) $dataMap->setDeletedFlagColumnName($controlSection['delete']);
159 if (isset($controlSection['languageField'])) $dataMap->setLanguageIdColumnName($controlSection['languageField']);
160 if (isset($controlSection['transOrigPointerField'])) $dataMap->setTranslationOriginColumnName($controlSection['transOrigPointerField']);
161 if (isset($controlSection['type'])) $dataMap->setRecordTypeColumnName($controlSection['type']);
162 if (isset($controlSection['enablecolumns']['disabled'])) $dataMap->setDisabledFlagColumnName($controlSection['enablecolumns']['disabled']);
163 if (isset($controlSection['enablecolumns']['starttime'])) $dataMap->setStartTimeColumnName($controlSection['enablecolumns']['starttime']);
164 if (isset($controlSection['enablecolumns']['endtime'])) $dataMap->setEndTimeColumnName($controlSection['enablecolumns']['endtime']);
165 if (isset($controlSection['enablecolumns']['fe_group'])) $dataMap->setFrontEndUserGroupColumnName($controlSection['enablecolumns']['fe_group']);
166 return $dataMap;
167 }
168
169 /**
170 * This method tries to determine the type of type of relation to other tables and sets it based on
171 * the $TCA column configuration
172 *
173 * @param Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap The column map
174 * @param string $columnConfiguration The column configuration from $TCA
175 * @param array $propertyMetaData The property metadata as delivered by the reflection service
176 * @return void
177 */
178 protected function setRelations(Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap, $columnConfiguration, $propertyMetaData) {
179 if (isset($columnConfiguration)) {
180 if (isset($columnConfiguration['MM']) || isset($columnConfiguration['foreign_selector'])) {
181 $columnMap = $this->setManyToManyRelation($columnMap, $columnConfiguration);
182 } elseif (isset($propertyMetaData['elementType'])) {
183 $columnMap = $this->setOneToManyRelation($columnMap, $columnConfiguration);
184 } elseif (isset($propertyMetaData['type']) && strpos($propertyMetaData['type'], '_') !== FALSE) {
185 $columnMap = $this->setOneToOneRelation($columnMap, $columnConfiguration);
186 } else {
187 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_NONE);
188 }
189 } else {
190 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_NONE);
191 }
192 return $columnMap;
193 }
194
195 /**
196 * This method sets the configuration for a 1:1 relation based on
197 * the $TCA column configuration
198 *
199 * @param string $columnMap The column map
200 * @param string $columnConfiguration The column configuration from $TCA
201 * @return void
202 */
203 protected function setOneToOneRelation(Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap, $columnConfiguration) {
204 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_ONE);
205 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
206 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
207 $columnMap->setChildSortbyFieldName($columnConfiguration['foreign_sortby']);
208 $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
209 $columnMap->setParentTableFieldName($columnConfiguration['foreign_table_field']);
210 return $columnMap;
211 }
212
213 /**
214 * This method sets the configuration for a 1:n relation based on
215 * the $TCA column configuration
216 *
217 * @param string $columnMap The column map
218 * @param string $columnConfiguration The column configuration from $TCA
219 * @return void
220 */
221 protected function setOneToManyRelation(Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap, $columnConfiguration) {
222 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY);
223 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
224 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
225 $columnMap->setChildSortbyFieldName($columnConfiguration['foreign_sortby']);
226 $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
227 $columnMap->setParentTableFieldName($columnConfiguration['foreign_table_field']);
228 return $columnMap;
229 }
230
231 /**
232 * This method sets the configuration for a m:n relation based on
233 * the $TCA column configuration
234 *
235 * @param string $columnMap The column map
236 * @param string $columnConfiguration The column configuration from $TCA
237 * @return void
238 */
239 protected function setManyToManyRelation(Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap, $columnConfiguration) {
240 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY);
241 if (isset($columnConfiguration['MM'])) {
242 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
243 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
244 $columnMap->setRelationTableName($columnConfiguration['MM']);
245 if (is_array($columnConfiguration['MM_match_fields'])) {
246 $columnMap->setRelationTableMatchFields($columnConfiguration['MM_match_fields']);
247 }
248 if (is_array($columnConfiguration['MM_insert_fields'])) {
249 $columnMap->setRelationTableInsertFields($columnConfiguration['MM_insert_fields']);
250 }
251 $columnMap->setRelationTableWhereStatement($columnConfiguration['MM_table_where']);
252 if (!empty($columnConfiguration['MM_opposite_field'])) {
253 $columnMap->setParentKeyFieldName('uid_foreign');
254 $columnMap->setChildKeyFieldName('uid_local');
255 $columnMap->setChildSortByFieldName('sorting_foreign');
256 } else {
257 $columnMap->setParentKeyFieldName('uid_local');
258 $columnMap->setChildKeyFieldName('uid_foreign');
259 $columnMap->setChildSortByFieldName('sorting');
260 }
261 } elseif (isset($columnConfiguration['foreign_selector'])) {
262 $columns = $this->getColumnsDefinition($columnConfiguration['foreign_table']);
263 $childKeyFieldName = $columnConfiguration['foreign_selector'];
264 $columnMap->setChildTableName($columns[$childKeyFieldName]['config']['foreign_table']);
265 $columnMap->setRelationTableName($columnConfiguration['foreign_table']);
266 $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
267 $columnMap->setChildKeyFieldName($childKeyFieldName);
268 $columnMap->setChildSortByFieldName($columnConfiguration['foreign_sortby']);
269 } else {
270 throw new Tx_Extbase_Persistence_Exception_UnsupportedRelation('The given information to build a many-to-many-relation was not sufficient. Check your TCA definitions. mm-relations with IRRE must have at least a defined "MM" or "foreign_selector".', 1268817963);
271 }
272 if ($this->getControlSection($columnMap->getRelationTableName()) !== NULL) {
273 $columnMap->setRelationTablePageIdColumnName('pid');
274 }
275 return $columnMap;
276 }
277
278 }