[TASK] Remove further t3lib mentions from codebase
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Collection / RecordCollectionRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Collection;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Steffen Ritter <typo3@steffen-ritter.net>
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 * Implements the repository for record collections.
31 *
32 * @author Steffen Ritter <typo3@steffen-ritter.net>
33 */
34 class RecordCollectionRepository {
35
36 const TYPE_Static = 'static';
37 /**
38 * Name of the table the collection records are stored to
39 *
40 * @var string
41 */
42 protected $table = 'sys_collection';
43
44 /**
45 * @var string
46 */
47 protected $typeField = 'type';
48
49 /**
50 * @var string
51 */
52 protected $tableField = 'table_name';
53
54 /**
55 * Finds a record collection by uid.
56 *
57 * @param integer $uid The uid to be looked up
58 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection
59 */
60 public function findByUid($uid) {
61 $result = NULL;
62 $data = $this->getDatabase()->exec_SELECTgetSingleRow('*', $this->table, 'uid=' . intval($uid) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($this->table));
63 if ($data !== NULL) {
64 $result = $this->createDomainObject($data);
65 }
66 return $result;
67 }
68
69 /**
70 * Finds all record collections.
71 *
72 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
73 */
74 public function findAll() {
75 return $this->queryMultipleRecords();
76 }
77
78 /**
79 * Finds record collections by table name.
80 *
81 * @param string $tableName Name of the table to be looked up
82 * @return \TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
83 */
84 public function findByTableName($tableName) {
85 $conditions = array(
86 $this->tableField . '=' . $this->getDatabase()->fullQuoteStr($tableName, $this->table)
87 );
88 return $this->queryMultipleRecords($conditions);
89 }
90
91 /**
92 * Finds record collection by type.
93 *
94 * @param string $type Type to be looked up
95 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
96 */
97 public function findByType($type) {
98 $conditions = array(
99 $this->typeField . '=' . $this->getDatabase()->fullQuoteStr($type, $this->table)
100 );
101 return $this->queryMultipleRecords($conditions);
102 }
103
104 /**
105 * Finds record collections by type and table name.
106 *
107 * @param string $type Type to be looked up
108 * @param string $tableName Name of the table to be looked up
109 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
110 */
111 public function findByTypeAndTableName($type, $tableName) {
112 $conditions = array(
113 $this->typeField . '=' . $this->getDatabase()->fullQuoteStr($type, $this->table),
114 $this->tableField . '=' . $this->getDatabase()->fullQuoteStr($tableName, $this->table)
115 );
116 return $this->queryMultipleRecords($conditions);
117 }
118
119 /**
120 * Deletes a record collection by uid.
121 *
122 * @param integer $uid uid to be deleted
123 * @return void
124 */
125 public function deleteByUid($uid) {
126 $this->getDatabase()->exec_UPDATEquery($this->table, 'uid=' . intval($uid), array('deleted' => 1, 'tstamp' => $GLOBALS['EXEC_TIME']));
127 }
128
129 /**
130 * Queries for multiple records for the given conditions.
131 *
132 * @param array $conditions Conditions concatenated with AND for query
133 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
134 */
135 protected function queryMultipleRecords(array $conditions = array()) {
136 $result = NULL;
137 if (count($conditions) > 0) {
138 $conditionsWhereClause = implode(' AND ', $conditions);
139 } else {
140 $conditionsWhereClause = '1=1';
141 }
142 $data = $this->getDatabase()->exec_SELECTgetRows('*', $this->table, $conditionsWhereClause . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($this->table));
143 if ($data !== NULL) {
144 $result = $this->createMultipleDomainObjects($data);
145 }
146 return $result;
147 }
148
149 /**
150 * Creates a record collection domain object.
151 *
152 * @param array $record Database record to be reconstituted
153 * @return \TYPO3\CMS\Core\Collection\AbstractRecordCollection
154 */
155 protected function createDomainObject(array $record) {
156 switch ($record['type']) {
157 case self::TYPE_Static:
158 $collection = \TYPO3\CMS\Core\Collection\StaticRecordCollection::create($record);
159 break;
160 default:
161 throw new \RuntimeException('Unknown record collection type "' . $record['type'], 1328646798);
162 }
163 return $collection;
164 }
165
166 /**
167 * Creates multiple record collection domain objects.
168 *
169 * @param array $data Array of multiple database records to be reconstituted
170 * @return \TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
171 */
172 protected function createMultipleDomainObjects(array $data) {
173 $collections = array();
174 foreach ($data as $collection) {
175 $collections[] = $this->createDomainObject($collection);
176 }
177 return $collections;
178 }
179
180 /**
181 * Gets the database object.
182 *
183 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
184 */
185 protected function getDatabase() {
186 return $GLOBALS['TYPO3_DB'];
187 }
188
189 }
190
191
192 ?>