d3ff29844ba543c5a7cfae9269a6324f37ada0d7
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Collection / RecordCollectionRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Collection;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
19 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
20 use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Implements the repository for record collections.
25 */
26 class RecordCollectionRepository
27 {
28 /**
29 * @var string
30 */
31 const TYPE_Static = 'static';
32
33 /**
34 * Name of the table the collection records are stored to
35 *
36 * @var string
37 */
38 protected $table = 'sys_collection';
39
40 /**
41 * @var string
42 */
43 protected $typeField = 'type';
44
45 /**
46 * @var string
47 */
48 protected $tableField = 'table_name';
49
50 /**
51 * Finds a record collection by uid.
52 *
53 * @param int $uid The uid to be looked up
54 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection
55 */
56 public function findByUid($uid)
57 {
58 $result = null;
59 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
60
61 if ($this->getEnvironmentMode() === 'FE') {
62 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
63 if ($GLOBALS['TSFE']->showHiddenRecords) {
64 $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class);
65 }
66 } else {
67 $queryBuilder->getRestrictions()
68 ->removeAll()
69 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
70 }
71
72 $data = $queryBuilder->select('*')
73 ->from($this->table)
74 ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)))
75 ->execute()
76 ->fetch();
77 if (is_array($data)) {
78 $result = $this->createDomainObject($data);
79 }
80 return $result;
81 }
82
83 /**
84 * Finds all record collections.
85 *
86 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
87 */
88 public function findAll()
89 {
90 return $this->queryMultipleRecords();
91 }
92
93 /**
94 * Finds record collections by table name.
95 *
96 * @param string $tableName Name of the table to be looked up
97 * @return \TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
98 */
99 public function findByTableName($tableName)
100 {
101 $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
102 ->getQueryBuilderForTable($tableName)
103 ->expr();
104
105 return $this->queryMultipleRecords([
106 $expressionBuilder->eq($this->tableField, $expressionBuilder->literal($tableName))
107 ]);
108 }
109
110 /**
111 * Finds record collection by type.
112 *
113 * @param string $type Type to be looked up
114 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
115 */
116 public function findByType($type)
117 {
118 $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
119 ->getQueryBuilderForTable($this->table)
120 ->expr();
121
122 return $this->queryMultipleRecords([
123 $expressionBuilder->eq($this->typeField, $expressionBuilder->literal($type))
124 ]);
125 }
126
127 /**
128 * Finds record collections by type and table name.
129 *
130 * @param string $type Type to be looked up
131 * @param string $tableName Name of the table to be looked up
132 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
133 */
134 public function findByTypeAndTableName($type, $tableName)
135 {
136 $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
137 ->getQueryBuilderForTable($this->table)
138 ->expr();
139
140 return $this->queryMultipleRecords([
141 $expressionBuilder->eq($this->typeField, $expressionBuilder->literal($type)),
142 $expressionBuilder->eq($this->tableField, $expressionBuilder->literal($tableName))
143 ]);
144 }
145
146 /**
147 * Deletes a record collection by uid.
148 *
149 * @param int $uid uid to be deleted
150 */
151 public function deleteByUid($uid)
152 {
153 GeneralUtility::makeInstance(ConnectionPool::class)
154 ->getConnectionForTable($this->table)
155 ->update(
156 $this->table,
157 ['deleted' => 1, 'tstamp' => (int)$GLOBALS['EXEC_TIME']],
158 ['uid' => (int)$uid]
159 );
160 }
161
162 /**
163 * Queries for multiple records for the given conditions.
164 *
165 * @param array $conditions Conditions concatenated with AND for query
166 * @return NULL|\TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
167 */
168 protected function queryMultipleRecords(array $conditions = [])
169 {
170 $result = null;
171
172 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
173 $queryBuilder->getRestrictions()
174 ->removeAll()
175 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
176
177 $queryBuilder->select('*')
178 ->from($this->table);
179
180 if (!empty($conditions)) {
181 $queryBuilder->where(...$conditions);
182 }
183
184 $data = $queryBuilder->execute()->fetchAll();
185 if (!empty($data)) {
186 $result = $this->createMultipleDomainObjects($data);
187 }
188
189 return $result;
190 }
191
192 /**
193 * Creates a record collection domain object.
194 *
195 * @param array $record Database record to be reconstituted
196 * @return \TYPO3\CMS\Core\Collection\AbstractRecordCollection
197 * @throws \RuntimeException
198 */
199 protected function createDomainObject(array $record)
200 {
201 switch ($record['type']) {
202 case self::TYPE_Static:
203 $collection = StaticRecordCollection::create($record);
204 break;
205 default:
206 throw new \RuntimeException('Unknown record collection type "' . $record['type'], 1328646798);
207 }
208 return $collection;
209 }
210
211 /**
212 * Creates multiple record collection domain objects.
213 *
214 * @param array $data Array of multiple database records to be reconstituted
215 * @return \TYPO3\CMS\Core\Collection\AbstractRecordCollection[]
216 */
217 protected function createMultipleDomainObjects(array $data)
218 {
219 $collections = [];
220 foreach ($data as $collection) {
221 $collections[] = $this->createDomainObject($collection);
222 }
223 return $collections;
224 }
225
226 /**
227 * Function to return the current TYPO3_MODE.
228 * This function can be mocked in unit tests to be able to test frontend behaviour.
229 *
230 * @return string
231 */
232 protected function getEnvironmentMode()
233 {
234 return TYPO3_MODE;
235 }
236 }