[BUGFIX] Move forgotten getEnvironmentMode()
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / AbstractRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Andreas Wolf <andreas.wolf@ikt-werk.de>
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 * Abstract repository implementing the basic repository methods
31 *
32 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
33 * @author Ingmar Schlecht <ingmar@typo3.org>
34 */
35 abstract class AbstractRepository implements \TYPO3\CMS\Extbase\Persistence\RepositoryInterface, \TYPO3\CMS\Core\SingletonInterface {
36
37 /**
38 * @var string
39 */
40 protected $table = '';
41
42 /**
43 * @var ResourceFactory
44 */
45 protected $factory;
46
47 /**
48 * @var string
49 */
50 protected $typeField = '';
51
52 /**
53 * @var string
54 */
55 protected $type = '';
56
57 /**
58 * Creates this object.
59 */
60 public function __construct() {
61 $this->factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
62 }
63
64 /**
65 * Adds an object to this repository.
66 *
67 * @param object $object The object to add
68 * @return void
69 * @api
70 */
71 public function add($object) {
72
73 }
74
75 /**
76 * Removes an object from this repository.
77 *
78 * @param object $object The object to remove
79 * @return void
80 * @api
81 */
82 public function remove($object) {
83
84 }
85
86 /**
87 * Replaces an object by another.
88 *
89 * @param object $existingObject The existing object
90 * @param object $newObject The new object
91 * @return void
92 * @api
93 */
94 public function replace($existingObject, $newObject) {
95
96 }
97
98 /**
99 * Replaces an existing object with the same identifier by the given object
100 *
101 * @param object $modifiedObject The modified object
102 * @api
103 */
104 public function update($modifiedObject) {
105
106 }
107
108 /**
109 * Returns all objects of this repository add()ed but not yet persisted to
110 * the storage layer.
111 *
112 * @return array An array of objects
113 */
114 public function getAddedObjects() {
115
116 }
117
118 /**
119 * Returns an array with objects remove()d from the repository that
120 * had been persisted to the storage layer before.
121 *
122 * @return array
123 */
124 public function getRemovedObjects() {
125
126 }
127
128 /**
129 * Returns all objects of this repository.
130 *
131 * @return array An array of objects, empty if no objects found
132 * @api
133 */
134 public function findAll() {
135 $itemList = array();
136 $whereClause = '1=1';
137 if ($this->type != '') {
138 $whereClause .= ' AND ' . $this->typeField . ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->type, $this->table) . ' ';
139 }
140 $whereClause .= $this->getWhereClauseForEnabledFields();
141 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->table, $whereClause);
142 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
143 $itemList[] = $this->createDomainObject($row);
144 }
145 $GLOBALS['TYPO3_DB']->sql_free_result($res);
146 return $itemList;
147 }
148
149 /**
150 * Creates an object managed by this repository.
151 *
152 * @abstract
153 * @param array $databaseRow
154 * @return object
155 */
156 abstract protected function createDomainObject(array $databaseRow);
157
158 /**
159 * Returns the total number objects of this repository.
160 *
161 * @return integer The object count
162 * @api
163 */
164 public function countAll() {
165
166 }
167
168 /**
169 * Removes all objects of this repository as if remove() was called for
170 * all of them.
171 *
172 * @return void
173 * @api
174 */
175 public function removeAll() {
176
177 }
178
179 /**
180 * Finds an object matching the given identifier.
181 *
182 * @param int $uid The identifier of the object to find
183 *
184 * @throws \RuntimeException
185 * @throws \InvalidArgumentException
186 * @return object The matching object
187 * @api
188 */
189 public function findByUid($uid) {
190 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
191 throw new \InvalidArgumentException('uid has to be integer.', 1316779798);
192 }
193 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', $this->table, 'uid=' . intval($uid) . $this->getWhereClauseForEnabledFields());
194 if (empty($row) || !is_array($row)) {
195 throw new \RuntimeException('Could not find row with uid "' . $uid . '" in table ' . $this->table, 1314354065);
196 }
197 return $this->createDomainObject($row);
198 }
199
200 /**
201 * get the WHERE clause for the enabled fields of this TCA table
202 * depending on the context
203 *
204 * @return string the additional where clause, something like " AND deleted=0 AND hidden=0"
205 */
206 protected function getWhereClauseForEnabledFields() {
207 if ($this->getEnvironmentMode() === 'FE') {
208 // frontend context
209 $whereClause = $GLOBALS['TSFE']->sys_page->enableFields($this->table);
210 $whereClause .= $GLOBALS['TSFE']->sys_page->deleteClause($this->table);
211 } else {
212 // backend context
213 $whereClause = \TYPO3\CMS\Backend\Utility\BackendUtility::BEenableFields($this->table);
214 $whereClause .= \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($this->table);
215 }
216 return $whereClause;
217 }
218
219 /**
220 * Sets the property names to order the result by per default.
221 * Expected like this:
222 * array(
223 * 'foo' => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING,
224 * 'bar' => Tx_Extbase_Persistence_QueryInterface::ORDER_DESCENDING
225 * )
226 *
227 * @param array $defaultOrderings The property names to order by
228 *
229 * @throws \BadMethodCallException
230 * @return void
231 * @api
232 */
233 public function setDefaultOrderings(array $defaultOrderings) {
234 throw new \BadMethodCallException('Repository does not support the setDefaultOrderings() method.', 1313185906);
235 }
236
237 /**
238 * Sets the default query settings to be used in this repository
239 *
240 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
241 *
242 * @throws \BadMethodCallException
243 * @return void
244 * @api
245 */
246 public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings) {
247 throw new \BadMethodCallException('Repository does not support the setDefaultQuerySettings() method.', 1313185907);
248 }
249
250 /**
251 * Returns a query for objects of this repository
252 *
253 * @throws \BadMethodCallException
254 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
255 * @api
256 */
257 public function createQuery() {
258 throw new \BadMethodCallException('Repository does not support the createQuery() method.', 1313185908);
259 }
260
261 /**
262 * Finds an object matching the given identifier.
263 *
264 * @param mixed $identifier The identifier of the object to find
265 * @return object The matching object if found, otherwise NULL
266 * @api
267 */
268 public function findByIdentifier($identifier) {
269 return $this->findByUid($identifier);
270 }
271
272 /**
273 * Magic call method for repository methods.
274 *
275 * @param string $method Name of the method
276 * @param array $arguments The arguments
277 * @return void
278 */
279 public function __call($method, $arguments) {
280 // deliberately empty
281 }
282
283 /**
284 * Returns the object type this repository is managing.
285 *
286 * @return string
287 * @api
288 */
289 public function getEntityClassName() {
290 return $this->objectType;
291 }
292
293 /**
294 * Function to return the current TYPO3_MODE.
295 * This function can be mocked in unit tests to be able to test frontend behaviour.
296 *
297 * @return string
298 */
299 protected function getEnvironmentMode() {
300 return TYPO3_MODE;
301 }
302
303 }
304
305
306 ?>