[+BUGFIX] Extbase (Persistence): Fixed broken IRRE setups. The type of relation is...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Mapper / DataMap.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 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_DataMap {
33
34 /**
35 * The domain class name
36 *
37 * @var string
38 **/
39 protected $className;
40
41 /**
42 * The table name corresponding to the domain class configured in $TCA
43 *
44 * @var string
45 **/
46 protected $tableName;
47
48 /**
49 * An array of column maps configured in $TCA
50 *
51 * @var array
52 **/
53 protected $columnMaps;
54
55 /**
56 * Constructs this DataMap
57 *
58 * @param string $className The class name. This determines the table to fetch the configuration for
59 */
60 // TODO Refactor to factory pattern (DataMapFactory) and value object (DataMap)
61 public function __construct($className, $tableName = '', array $mapping = array()) {
62 $this->setClassName($className);
63 if (empty($tableName)) {
64 $this->setTableName(strtolower($className));
65 } else {
66 $this->setTableName($tableName);
67 }
68 $this->initialize($mapping);
69 }
70
71 /**
72 * Sets the name of the class the colum map represents
73 *
74 * @return void
75 */
76 public function setClassName($className) {
77 $this->className = $className;
78 }
79
80 /**
81 * Returns the name of the class the column map represents
82 *
83 * @return string The class name
84 */
85 public function getClassName() {
86 return $this->className;
87 }
88
89 /**
90 * Sets the name of the table the colum map represents
91 *
92 * @return void
93 */
94 public function setTableName($tableName) {
95 $this->tableName = $tableName;
96 }
97
98 /**
99 * Returns the name of the table the column map represents
100 *
101 * @return string The table name
102 */
103 public function getTableName() {
104 return $this->tableName;
105 }
106
107 /**
108 * Initializes the data map by adding column maps for all the configured columns in the $TCA.
109 * It also resolves the type of values the column is holding and the typo of relation the column
110 * represents.
111 *
112 * @return void
113 */
114 protected function initialize(array $mapping) {
115 $this->addCommonColumns();
116 $columnConfigurations = array();
117 foreach ($this->getColumnsDefinition() as $columnName => $columnDefinition) {
118 $columnConfigurations[$columnName] = $columnDefinition['config'];
119 $columnConfigurations[$columnName]['mapOnProperty'] = Tx_Extbase_Utility_Extension::convertUnderscoredToLowerCamelCase($columnName);
120 }
121 $columnConfigurations = t3lib_div::array_merge_recursive_overrule($columnConfigurations, $mapping);
122 foreach ($columnConfigurations as $columnName => $columnConfiguration) {
123 $columnMap = new Tx_Extbase_Persistence_Mapper_ColumnMap($columnName, $columnConfiguration['mapOnProperty']);
124 $this->setPropertyType($columnMap, $columnConfiguration);
125 $this->setRelations($columnMap, $columnConfiguration);
126 $this->addColumnMap($columnMap);
127 }
128 }
129
130 /**
131 * Returns the TCA columns array of the specified table
132 *
133 * @param string $tableName An optional table name to fetch the columns definition from
134 * @return array The TCA columns definition
135 */
136 public function getColumnsDefinition($tableName = '') {
137 $tableName = strlen($tableName) > 0 ? $tableName : $this->getTableName();
138 if (TYPO3_MODE === 'FE') {
139 $GLOBALS['TSFE']->includeTCA();
140 }
141 t3lib_div::loadTCA($tableName);
142 $columns = is_array($GLOBALS['TCA'][$tableName]['columns']) ? $GLOBALS['TCA'][$tableName]['columns'] : array();
143 return $columns;
144 }
145
146 /**
147 * Adds available common columns (e.g. tstamp or crdate) to the data map. It takes the configured column names
148 * into account.
149 *
150 * @return void
151 */
152 protected function addCommonColumns() {
153 $this->addColumn('uid', NULL, Tx_Extbase_Persistence_PropertyType::LONG);
154 if ($this->hasPidColumn()) {
155 $this->addColumn('pid', NULL, Tx_Extbase_Persistence_PropertyType::LONG);
156 }
157 if ($this->hasTimestampColumn()) {
158 $this->addColumn($this->getTimestampColumnName(), NULL, Tx_Extbase_Persistence_PropertyType::DATE);
159 }
160 if ($this->hasCreationDateColumn()) {
161 $this->addColumn($this->getCreationDateColumnName(), NULL, Tx_Extbase_Persistence_PropertyType::DATE);
162 }
163 if ($this->hasCreatorUidColumn()) {
164 $this->addColumn($this->getCreatorUidColumnName(), NULL, Tx_Extbase_Persistence_PropertyType::LONG);
165 }
166 if ($this->hasDeletedColumn()) {
167 $this->addColumn($this->getDeletedColumnName(), NULL, Tx_Extbase_Persistence_PropertyType::BOOLEAN);
168 }
169 if ($this->hasHiddenColumn()) {
170 $this->addColumn($this->getHiddenColumnName(), NULL, Tx_Extbase_Persistence_PropertyType::BOOLEAN);
171 }
172 }
173
174 /**
175 * This method tries to determine the type of value the column hold by inspectiong the $TCA column configuration
176 * and sets it.
177 *
178 * @param string $columnMap The column map
179 * @param string $columnConfiguration The column configuration from $TCA
180 * @return void
181 */
182 protected function setPropertyType(Tx_Extbase_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
183 $evalConfiguration = t3lib_div::trimExplode(',', $columnConfiguration['eval']);
184 if (in_array('date', $evalConfiguration) || in_array('datetime', $evalConfiguration)) {
185 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::DATE);
186 } elseif ($columnConfiguration['type'] === 'check' && empty($columnConfiguration['items'])) {
187 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::BOOLEAN);
188 } elseif (in_array('int', $evalConfiguration)) {
189 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::LONG);
190 } elseif (in_array('double2', $evalConfiguration)) {
191 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::DOUBLE);
192 } else {
193 if (isset($columnConfiguration['foreign_table'])) {
194 if (isset($columnConfiguration['loadingStrategy'])) {
195 $columnMap->setLoadingStrategy($columnConfiguration['loadingStrategy']);
196 } else {
197 $columnMap->setLoadingStrategy(Tx_Extbase_Persistence_Mapper_ColumnMap::STRATEGY_EAGER);
198 }
199 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::REFERENCE);
200 } else {
201 $columnMap->setPropertyType(Tx_Extbase_Persistence_PropertyType::STRING);
202 }
203 }
204 }
205
206 /**
207 * This method tries to determine the type of type of relation to other tables and sets it based on
208 * the $TCA column configuration
209 *
210 * @param Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap The column map
211 * @param string $columnConfiguration The column configuration from $TCA
212 * @return void
213 */
214 protected function setRelations(Tx_Extbase_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
215 if (isset($columnConfiguration) && $columnConfiguration['type'] !== 'passthrough') {
216 if (isset($columnConfiguration['foreign_table'])) {
217 if (isset($columnConfiguration['MM']) || isset($columnConfiguration['foreign_selector'])) {
218 $this->setManyToManyRelation($columnMap, $columnConfiguration);
219 } else {
220 if ($columnConfiguration['maxitems'] == 1) {
221 $this->setOneToOneRelation($columnMap, $columnConfiguration);
222 } else {
223 $this->setOneToManyRelation($columnMap, $columnConfiguration);
224 }
225 }
226 } else {
227 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_NONE);
228 }
229 }
230 }
231
232 /**
233 * This method sets the configuration for a 1:1 relation based on
234 * the $TCA column configuration
235 *
236 * @param string $columnMap The column map
237 * @param string $columnConfiguration The column configuration from $TCA
238 * @return void
239 */
240 protected function setOneToOneRelation(Tx_Extbase_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
241 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_ONE);
242 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
243 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
244 $columnMap->setChildSortbyFieldName($columnConfiguration['foreign_sortby']);
245 $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
246 $columnMap->setParentTableFieldName($columnConfiguration['foreign_table_field']);
247 }
248
249 /**
250 * This method sets the configuration for a 1:n relation based on
251 * the $TCA column configuration
252 *
253 * @param string $columnMap The column map
254 * @param string $columnConfiguration The column configuration from $TCA
255 * @return void
256 */
257 protected function setOneToManyRelation(Tx_Extbase_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
258 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY);
259 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
260 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
261 $columnMap->setChildSortbyFieldName($columnConfiguration['foreign_sortby']);
262 $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
263 $columnMap->setParentTableFieldName($columnConfiguration['foreign_table_field']);
264 }
265
266 /**
267 * This method sets the configuration for a m:n relation based on
268 * the $TCA column configuration
269 *
270 * @param string $columnMap The column map
271 * @param string $columnConfiguration The column configuration from $TCA
272 * @return void
273 */
274 protected function setManyToManyRelation(Tx_Extbase_Persistence_Mapper_ColumnMap &$columnMap, $columnConfiguration) {
275 // TODO support multi table relationships
276 $columnMap->setTypeOfRelation(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY);
277 if (isset($columnConfiguration['MM'])) {
278 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
279 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
280 $columnMap->setRelationTableName($columnConfiguration['MM']);
281 if (is_array($columnConfiguration['MM_match_fields'])) {
282 $columnMap->setRelationTableMatchFields($columnConfiguration['MM_match_fields']);
283 }
284 if (is_array($columnConfiguration['MM_insert_fields'])) {
285 $columnMap->setRelationTableInsertFields($columnConfiguration['MM_insert_fields']);
286 }
287 $columnMap->setRelationTableWhereStatement($columnConfiguration['MM_table_where']);
288 if (!empty($columnConfiguration['MM_opposite_field'])) {
289 $columnMap->setParentKeyFieldName('uid_foreign');
290 $columnMap->setChildKeyFieldName('uid_local');
291 $columnMap->setChildSortByFieldName('sorting_foreign');
292 } else {
293 $columnMap->setParentKeyFieldName('uid_local');
294 $columnMap->setChildKeyFieldName('uid_foreign');
295 $columnMap->setChildSortByFieldName('sorting');
296 }
297 } elseif (isset($columnConfiguration['foreign_selector'])) {
298 $columns = $this->getColumnsDefinition($columnConfiguration['foreign_table']);
299 if (isset($columnConfiguration['foreign_selector'])) {
300 $childKeyFieldName = $columnConfiguration['foreign_selector'];
301 } else {
302 $childKeyFieldName = 'uid_foreign';
303 }
304 $columnMap->setChildTableName($columns[$childKeyFieldName]['config']['foreign_table']);
305 $columnMap->setRelationTableName($columnConfiguration['foreign_table']);
306 $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
307 $columnMap->setChildKeyFieldName($childKeyFieldName);
308 $columnMap->setChildSortByFieldName($columnConfiguration['foreign_sortby']);
309 } else {
310 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);
311 }
312 }
313
314 /**
315 * Sets the column maps.
316 *
317 * @param array $columnMaps The column maps stored in a flat array.
318 * @return void
319 */
320 public function setColumnMaps(array $columnMaps) {
321 $this->columnMaps = $columnMaps;
322 }
323
324 /**
325 * Adds a given column map to the data map.
326 *
327 * @param Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap The column map
328 * @return void
329 */
330 public function addColumnMap(Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap) {
331 $this->columnMaps[$columnMap->getPropertyName()] = $columnMap;
332 }
333
334 /**
335 * Builds a column map out of the given column name, type of value (optional), and type of
336 * relation (optional) and adds it to the data map.
337 *
338 * @param string $columnName The column name
339 * @param string $propertyName The property name
340 * @param string $propertyType The type of value (default: string)
341 * @param string $typeOfRelation The type of relation (default: none)
342 * @return Tx_Extbase_Persistence_Mapper_DataMap Returns itself for a fluent interface
343 */
344 public function addColumn($columnName, $propertyName = '', $propertyType = Tx_Extbase_Persistence_PropertyType::STRING, $typeOfRelation = Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_NONE) {
345 if (empty($propertyName)) {
346 $propertyName = Tx_Extbase_Utility_Extension::convertUnderscoredToLowerCamelCase($columnName);
347 }
348
349 $columnMap = new Tx_Extbase_Persistence_Mapper_ColumnMap($columnName, $propertyName);
350 $columnMap->setPropertyType($propertyType);
351 $columnMap->setTypeOfRelation($typeOfRelation);
352 $this->addColumnMap($columnMap);
353 return $this;
354 }
355
356 /**
357 * Returns all column maps
358 *
359 * @return array The column maps
360 */
361 public function getColumnMaps() {
362 return $this->columnMaps;
363 }
364
365 /**
366 * Returns the column map corresponding to the given property name.
367 *
368 * @param string $propertyName
369 * @return Tx_Extbase_Persistence_Mapper_ColumnMap|NULL The column map or NULL if no corresponding column map was found.
370 */
371 public function getColumnMap($propertyName) {
372 return $this->columnMaps[$propertyName];
373 }
374
375 /**
376 * Returns TRUE if the property is persistable (configured in $TCA)
377 *
378 * @param string $propertyName The property name
379 * @return boolean TRUE if the property is persistable (configured in $TCA)
380 */
381 public function isPersistableProperty($propertyName) {
382 return isset($this->columnMaps[$propertyName]);
383 }
384
385 /**
386 * Check if versioning is enabled .
387 *
388 * @return boolean
389 */
390 public function isVersionable() {
391 return ($GLOBALS['TCA'] [$this->tableName] ['ctrl'] ['versioningWS'] === '1');
392 }
393
394 /**
395 * Returns TRUE if the table has a pid column holding the id of the page the record is virtually stored on.
396 * Currently we don't support tables without a pid column.
397 *
398 * @return boolean The result
399 */
400 public function hasPidColumn() {
401 // TODO Should we implement a check for having a pid column?
402 return TRUE;
403 }
404
405 /**
406 * Returns the name of a column holding the timestamp the record was modified
407 *
408 * @return string The field name
409 */
410 public function getTimestampColumnName() {
411 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['tstamp'];
412 }
413
414 /**
415 * Returns TRUE if the table has a column holding the timestamp the record was modified
416 *
417 * @return boolean The result
418 */
419 public function hasTimestampColumn() {
420 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['tstamp']);
421 }
422
423 /**
424 * Returns the name of a column holding the creation date timestamp
425 *
426 * @return string The field name
427 */
428 public function getCreationDateColumnName() {
429 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['crdate'];
430 }
431
432 /**
433 * Returns TRUE if the table has olumn holding the creation date timestamp
434 *
435 * @return boolean The result
436 */
437 public function hasCreationDateColumn() {
438 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['crdate']);
439 }
440
441 /**
442 * Returns the name of a column holding the uid of the back-end user who created this record
443 *
444 * @return string The field name
445 */
446 public function getCreatorUidColumnName() {
447 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['cruser_id'];
448 }
449
450 /**
451 * Returns TRUE if the table has a column holding the uid of the back-end user who created this record
452 *
453 * @return boolean The result
454 */
455 public function hasCreatorUidColumn() {
456 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['cruser_id']);
457 }
458
459 /**
460 * Returns the name of a column indicating the 'deleted' state of the row
461 *
462 * @return string The field name
463 */
464 public function getDeletedColumnName() {
465 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['delete'];
466 }
467
468 /**
469 * Returns TRUE if the table has a column indicating the 'deleted' state of the row
470 *
471 * @return boolean The result
472 */
473 public function hasDeletedColumn() {
474 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['delete']);
475 }
476
477 /**
478 * Returns the name of a column indicating the 'hidden' state of the row
479 *
480 * @return string The field name
481 */
482 public function getHiddenColumnName() {
483 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['disabled'];
484 }
485
486 /**
487 * Returns TRUE if the table has a column indicating the 'hidden' state of the row
488 *
489 * @return boolean The result
490 */
491 public function hasHiddenColumn() {
492 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['disabled']);
493 }
494
495 /**
496 * Returns the name of a column holding the timestamp the record should not displayed before
497 *
498 * @return string The field name
499 */
500 public function getStartTimeColumnName() {
501 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['starttime'];
502 }
503
504 /**
505 * Returns TRUE if the table has a column holding the timestamp the record should not displayed before
506 *
507 * @return boolean The result
508 */
509 public function hasStartTimeColumn() {
510 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['starttime']);
511 }
512
513 /**
514 * Returns the name of a column holding the timestamp the record should not displayed afterwards
515 *
516 * @return string The field name
517 */
518 public function getEndTimeColumnName() {
519 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['endtime'];
520 }
521
522 /**
523 * Returns TRUE if the table has a column holding the timestamp the record should not displayed afterwards
524 *
525 * @return boolean The result
526 */
527 public function hasEndTimeColumn() {
528 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['endtime']);
529 }
530
531 /**
532 * Returns the name of a column holding the uid of the front-end user group which is allowed to edit this record
533 *
534 * @return string The field name
535 */
536 public function getFrontEndUserGroupColumnName() {
537 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['fe_group'];
538 }
539
540 /**
541 * Returns TRUE if the table has a column holding the uid of the front-end user group which is allowed to edit this record
542 *
543 * @return boolean The result
544 */
545 public function hasFrontEndUserGroupColumn() {
546 return !empty($GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['fe_group']);
547 }
548
549 /**
550 * Converts a field name to the property name. It respects property name aliases defined in $TCA.
551 *
552 * @param string $fieldName The field name
553 * @return string $propertyName The property name
554 */
555 public function convertFieldNameToPropertyName($fieldName) {
556 $propertyName = $fieldName;
557 return $propertyName; // TODO Implement aliases for field names (see also convertPropertyNameToFieldName())
558 }
559
560 /**
561 * Converts a preoperty name to the field name. It respects property name aliases defined in $TCA.
562 *
563 * @param string $fieldName The field name
564 * @return string $propertyName The property name
565 */
566 public function convertPropertyNameToFieldName($propertyName) {
567 $fieldName = $propertyName;
568 return $fieldName;
569 }
570
571 /**
572 * Converts the given string into the given type
573 *
574 * @param integer $type one of the constants defined in Tx_Extbase_Persistence_PropertyType
575 * @param string $string a string representing a value of the given type
576 *
577 * @return string|int|float|DateTime|boolean
578 */
579 public function convertFieldValueToPropertyValue($type, $string) {
580 switch ($type) {
581 case Tx_Extbase_Persistence_PropertyType::LONG:
582 return (int) $string;
583 case Tx_Extbase_Persistence_PropertyType::DOUBLE:
584 case Tx_Extbase_Persistence_PropertyType::DECIMAL:
585 return (float) $string;
586 case Tx_Extbase_Persistence_PropertyType::DATE:
587 if (empty($string)) { // 0 -> NULL !!!
588 return NULL;
589 } else {
590 return new DateTime(date('c', $string));
591 }
592 case Tx_Extbase_Persistence_PropertyType::BOOLEAN:
593 return (boolean) $string;
594 default:
595 return $string;
596 }
597 }
598
599 /**
600 * Converts a value from a property type to a database field type
601 *
602 * @param mixed $propertyValue The property value
603 * @return mixed The converted value
604 */
605 public function convertPropertyValueToFieldValue($propertyValue) {
606 if (is_bool($propertyValue)) {
607 $convertedValue = $propertyValue ? 1 : 0;
608 } elseif ($propertyValue instanceof Tx_Extbase_DomainObject_AbstractDomainObject) {
609 $convertedValue = $propertyValue->getUid();
610 } elseif ($propertyValue instanceof DateTime) {
611 $convertedValue = $propertyValue->format('U');
612 } elseif (is_int($propertyValue)) {
613 $convertedValue = $propertyValue;
614 } else {
615 $convertedValue = $propertyValue;
616 }
617 return $convertedValue;
618 }
619
620 }