* added isRelation() method to Column Map
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Mapper / TX_EXTMVC_Persistence_Mapper_DataMap.php
1 <?php
2 declare(ENCODING = 'utf-8');
3
4 /* *
5 * This script belongs to the FLOW3 framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Utility/TX_EXTMVC_Utility_Strings.php');
25 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/Mapper/TX_EXTMVC_Persistence_Mapper_ColumnMap.php');
26
27 /**
28 * A data map to map a single table configured in $TCA on a domain object.
29 *
30 * @version $Id:$
31 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
32 */
33 class TX_EXTMVC_Persistence_Mapper_DataMap {
34
35 /**
36 * The domain class name
37 *
38 * @var string
39 **/
40 protected $className;
41
42 /**
43 * The table name corresponding to the domain class configured in $TCA
44 *
45 * @var string
46 **/
47 protected $tableName;
48
49 /**
50 * An array of column maps configured in $TCA
51 *
52 * @var array
53 **/
54 protected $columnMaps;
55
56 public function __construct($className) {
57 $this->setClassName($className);
58 $this->setTableName(strtolower($this->className));
59 t3lib_div::loadTCA($this->getTableName());
60 }
61
62 public function setClassName($className) {
63 $this->className = $className;
64 }
65
66 public function getClassName() {
67 return $this->className;
68 }
69
70 public function setTableName($tableName) {
71 $this->tableName = $tableName;
72 }
73
74 public function getTableName() {
75 return $this->tableName;
76 }
77
78 public function initialize() {
79 $columns = $GLOBALS['TCA'][$this->getTableName()]['columns'];
80 if (is_array($columns)) {
81 $this->addColumn('uid', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
82 $this->addColumn('pid', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
83 $this->addColumn('tstamp', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
84 $this->addColumn('crdate', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
85 $this->addColumn('cruser_id', TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
86 if ($this->getDeletedColumnName() !== NULL) {
87 $this->addColumn($this->getDeletedColumnName(), TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN);
88 }
89 if ($this->getHiddenColumnName() !== NULL) {
90 $this->addColumn($this->getHiddenColumnName(), TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN);
91 }
92
93 $columnMapClassName = t3lib_div::makeInstanceClassName('TX_EXTMVC_Persistence_Mapper_ColumnMap');
94 foreach ($columns as $columnName => $columnConfiguration) {
95 $columnMap = new $columnMapClassName($columnName, $this);
96 if (strpos($columnConfiguration['config']['eval'], 'date') !== FALSE
97 || strpos($columnConfiguration['config']['eval'], 'datetime') !== FALSE) {
98 $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE);
99 } elseif ($columnConfiguration['config']['type'] === 'check' && empty($columnConfiguration['config']['items'])) {
100 $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN);
101 } elseif (strpos($columnConfiguration['config']['eval'], 'int') !== FALSE) {
102 $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_INTEGER);
103 } elseif (strpos($columnConfiguration['config']['eval'], 'double2') !== FALSE) {
104 $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_FLOAT);
105 } else {
106 $columnMap->setTypeOfValue(TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_STRING);
107 }
108
109 // TODO support for IRRE
110 // TODO support for MM_insert_fields and MM_match_fields
111 if (array_key_exists('foreign_table', $columnConfiguration['config']) && !array_key_exists('MM', $columnConfiguration['config'])) {
112 $columnMap->setTypeOfRelation(TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY);
113 $columnMap->setChildClassName($columnConfiguration['config']['foreign_class']);
114 $columnMap->setChildTableName($columnConfiguration['config']['foreign_table']);
115 $columnMap->setChildTableWhere($columnConfiguration['config']['foreign_table_where']);
116 $columnMap->setChildSortbyFieldName($columnConfiguration['config']['foreign_sortby']);
117 $columnMap->setParentKeyFieldName($columnConfiguration['config']['foreign_field']);
118 $columnMap->setParentTableFieldName($columnConfiguration['config']['foreign_table_field']);
119 } elseif (array_key_exists('MM', $columnConfiguration['config'])) {
120 $columnMap->setTypeOfRelation(TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY);
121 $columnMap->setChildClassName($columnConfiguration['config']['foreign_class']);
122 $columnMap->setChildTableName($columnConfiguration['config']['foreign_table']);
123 $columnMap->setRelationTableName($columnConfiguration['config']['MM']);
124 } else {
125 $columnMap->setTypeOfRelation(TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_NONE);
126 }
127 $this->addColumnMap($columnMap);
128 }
129 } else {
130 // TODO Throw exception
131 }
132 }
133
134 public function setColumnMaps(array $columnMaps) {
135 $this->columnMaps = $columnMaps;
136 }
137
138 public function addColumnMap(TX_EXTMVC_Persistence_Mapper_ColumnMap $columnMap) {
139 $this->columnMaps[$columnMap->getPropertyName()] = $columnMap;
140 }
141
142 public function addColumn($columnName, $typeOfValue = TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_STRING, $typeOfRelation = TX_EXTMVC_Persistence_Mapper_ColumnMap::RELATION_NONE) {
143 $columnMapClassName = t3lib_div::makeInstanceClassName('TX_EXTMVC_Persistence_Mapper_ColumnMap');
144 $columnMap = new $columnMapClassName($columnName);
145 $columnMap->setTypeOfValue($typeOfValue);
146 $columnMap->setTypeOfRelation($typeOfRelation);
147 $this->addColumnMap($columnMap);
148 return $this;
149 }
150
151 public function getColumnMaps() {
152 return $this->columnMaps;
153 }
154
155 public function getColumnMap($propertyName) {
156 return $this->columnMaps[$propertyName];
157 }
158
159 public function getColumnList() {
160 $columnList = '';
161 foreach ($this->columnMaps as $columnMap) {
162 if ($columnList !== '') {
163 $columnList .= ',';
164 }
165 $columnList .= $columnMap->getColumnName();
166 }
167 return $columnList;
168 }
169
170 /**
171 * Returns TRUE if the property is persistable (configured in $TCA)
172 *
173 * @param string $propertyName The property name
174 * @return boolean TRUE if the property is persistable (configured in $TCA)
175 * @author Jochen Rau <jochen.rau@typoplanet.de>
176 */
177 public function isPersistableProperty($propertyName) {
178 if (array_key_exists($propertyName, $this->columnMaps)) return TRUE;
179 return FALSE;
180 }
181
182 /**
183 * Returns the name of a column indicating the 'deleted' state of the row
184 *
185 * @return string The class name
186 * @author Jochen Rau <jochen.rau@typoplanet.de>
187 */
188 public function getDeletedColumnName() {
189 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['delete'];
190 }
191
192 /**
193 * Returns the name of a column indicating the 'hidden' state of the row
194 *
195 * @author Jochen Rau <jochen.rau@typoplanet.de>
196 */
197 public function getHiddenColumnName() {;
198 return $GLOBALS['TCA'][$this->getTableName()]['ctrl']['enablecolumns']['disabled'];
199 }
200
201 /**
202 * Converts a value from a database field type to a property type
203 *
204 * @param string $className The class name
205 * @param string $propertyName The property name
206 * @param mixed $fieldValue The field value
207 * @return mixed The converted value
208 * @author Jochen Rau <jochen.rau@typoplanet.de>
209 */
210 public function convertFieldValueToPropertyValue($propertyName, $fieldValue) {
211 $columnMap = $this->getColumnMap($propertyName);
212 if ($columnMap->getTypeOfValue() === TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_DATE) {
213 $convertedValue = new DateTime(strftime('%Y-%m-%d %H:%M', $fieldValue));
214 } elseif ($columnMap->getTypeOfValue() === TX_EXTMVC_Persistence_Mapper_ColumnMap::TYPE_BOOLEAN) {
215 if ($fieldValue === '0') {
216 $convertedValue = FALSE;
217 } else {
218 $convertedValue = TRUE;
219 }
220 } else {
221 $convertedValue = $fieldValue;
222 }
223 return $convertedValue;
224 }
225
226 /**
227 * Converts a value from a property type to a database field type
228 *
229 * @param mixed $propertyValue The property value
230 * @return mixed The converted value
231 * @author Jochen Rau <jochen.rau@typoplanet.de>
232 */
233 public function convertPropertyValueToFieldValue($propertyValue) {
234 if ($propertyValue instanceof DateTime) {
235 $convertedValue = $propertyValue->format('U');
236 } elseif (is_bool($propertyValue)) {
237 $convertedValue = $propertyValue ? 1 : 0;
238 } else {
239 $convertedValue = $propertyValue;
240 }
241 return $convertedValue;
242 }
243
244 }
245 ?>