e49407aabfc526e95b203fa04b9c91bc51ce23d2
[Packages/TYPO3.CMS.git] / t3lib / collection / RecordCollectionRepository.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011-2012 Steffen Ritter <typo3@steffen-ritter.net>
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 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Implements the repository for record collections.
30 *
31 * @author Steffen Ritter <typo3@steffen-ritter.net>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 class t3lib_collection_RecordCollectionRepository {
36 const TYPE_Static = 'static';
37
38 /**
39 * Name of the table the collection records are stored to
40 *
41 * @var string
42 */
43 protected $table = 'sys_collection';
44
45 /**
46 * @var string
47 */
48 protected $typeField = 'type';
49
50 /**
51 * @var string
52 */
53 protected $tableField = 'table_name';
54
55 /**
56 * Finds a record collection by uid.
57 *
58 * @param integer $uid The uid to be looked up
59 * @return NULL|t3lib_collection_RecordCollection
60 */
61 public function findByUid($uid) {
62 $result = NULL;
63
64 $data = $this->getDatabase()->exec_SELECTgetSingleRow(
65 'uid',
66 $this->table,
67 'uid=' . intval($uid) . t3lib_BEfunc::deleteClause($this->table)
68 );
69
70 if ($data !== NULL) {
71 $result = $this->createDomainObject($data);
72 }
73
74 return $result;
75 }
76
77 /**
78 * Finds record collections by table name.
79 *
80 * @param string $tableName Name of the table to be looked up
81 * @return t3lib_collection_RecordCollection[]
82 */
83 public function findByTableName($tableName) {
84 $conditions = array(
85 $this->tableField . '=' . $this->getDatabase()->fullQuoteStr($tableName, $this->table),
86 );
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|t3lib_collection_RecordCollection[]
96 */
97 public function findByType($type) {
98 $conditions = array(
99 $this->typeField . '=' . $this->getDatabase()->fullQuoteStr($type, $this->table),
100 );
101
102 return $this->queryMultipleRecords($conditions);
103 }
104
105 /**
106 * Finds record collections by type and table name.
107 *
108 * @param string $type Type to be looked up
109 * @param string $tableName Name of the table to be looked up
110 * @return NULL|t3lib_collection_RecordCollection[]
111 */
112 public function findByTypeAndTableName($type, $tableName) {
113 $conditions = array(
114 $this->typeField . '=' . $this->getDatabase()->fullQuoteStr($type, $this->table),
115 $this->tableField . '=' . $this->getDatabase()->fullQuoteStr($tableName, $this->table),
116 );
117
118 return $this->queryMultipleRecords($conditions);
119 }
120
121 /**
122 * Deletes a record collection by uid.
123 *
124 * @param integer $uid uid to be deleted
125 */
126 public function deleteByUid($uid) {
127 $this->getDatabase()->exec_UPDATEquery(
128 $this->table, 'uid=' . intval($uid),
129 array('deleted' => 1, 'tstamp' => $GLOBALS['EXEC_TIME'])
130 );
131 }
132
133 /**
134 * Queries for multiple records for the given conditions.
135 *
136 * @param array $conditions Conditions concatenated with AND for query
137 * @return NULL|t3lib_collection_RecordCollection[]
138 */
139 protected function queryMultipleRecords(array $conditions = array()) {
140 $result = NULL;
141
142 $data = $this->getDatabase()->exec_SELECTgetRows(
143 '*',
144 $this->table,
145 implode(' AND ', $conditions) . t3lib_BEfunc::deleteClause($this->table)
146 );
147
148 if ($data !== NULL) {
149 $result = $this->createMultipleDomainObjects($data);
150 }
151
152 return $result;
153 }
154
155 /**
156 * Creates a record collection domain object.
157 *
158 * @param array $record Database record to be reconstituted
159 * @return t3lib_collection_RecordCollection
160 */
161 protected function createDomainObject(array $record) {
162 switch ($record['type']) {
163 case self::TYPE_Static:
164 $collection = t3lib_collection_StaticRecordCollection::create($record);
165 break;
166 default:
167 throw new RuntimeException('Unknown record collection type "' . $record['type'], 1328646798);
168 }
169
170 return $collection;
171 }
172
173 /**
174 * Creates multiple record collection domain objects.
175 *
176 * @param array $data Array of multiple database records to be reconstituted
177 * @return t3lib_collection_RecordCollection[]
178 */
179 protected function createMultipleDomainObjects(array $data) {
180 $collections = array();
181
182 foreach ($data as $collection) {
183 $collections[] = $this->createDomainObject($collection);
184 }
185
186 return $collections;
187 }
188
189 /**
190 * Gets the database object.
191 *
192 * @return t3lib_DB
193 */
194 protected function getDatabase() {
195 return $GLOBALS['TYPO3_DB'];
196 }
197 }
198 ?>