‪TYPO3CMS  9.5
RecordCollectionRepository.php
Go to the documentation of this file.
1 <?php
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 
21 
26 {
30  const ‪TYPE_Static = 'static';
31 
37  protected ‪$table = 'sys_collection';
38 
42  protected ‪$typeField = 'type';
43 
47  protected ‪$tableField = 'table_name';
48 
55  public function ‪findByUid($uid)
56  {
57  $result = null;
58  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
59 
60  if ($this->‪getEnvironmentMode() === 'FE') {
61  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
62  } else {
63  $queryBuilder->getRestrictions()
64  ->removeAll()
65  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
66  }
67 
68  $data = $queryBuilder->select('*')
69  ->from($this->table)
70  ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)))
71  ->execute()
72  ->fetch();
73  if (is_array($data)) {
74  $result = $this->‪createDomainObject($data);
75  }
76  return $result;
77  }
78 
84  public function ‪findAll()
85  {
86  return $this->‪queryMultipleRecords();
87  }
88 
95  public function ‪findByTableName($tableName)
96  {
97  $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
98  ->getQueryBuilderForTable($tableName)
99  ->expr();
100 
101  return $this->‪queryMultipleRecords([
102  $expressionBuilder->eq($this->tableField, $expressionBuilder->literal($tableName))
103  ]);
104  }
105 
112  public function ‪findByType($type)
113  {
114  $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
115  ->getQueryBuilderForTable($this->table)
116  ->expr();
117 
118  return $this->‪queryMultipleRecords([
119  $expressionBuilder->eq($this->typeField, $expressionBuilder->literal($type))
120  ]);
121  }
122 
130  public function ‪findByTypeAndTableName($type, $tableName)
131  {
132  $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
133  ->getQueryBuilderForTable($this->table)
134  ->expr();
135 
136  return $this->‪queryMultipleRecords([
137  $expressionBuilder->eq($this->typeField, $expressionBuilder->literal($type)),
138  $expressionBuilder->eq($this->tableField, $expressionBuilder->literal($tableName))
139  ]);
140  }
141 
147  public function ‪deleteByUid($uid)
148  {
149  GeneralUtility::makeInstance(ConnectionPool::class)
150  ->getConnectionForTable($this->table)
151  ->update(
152  $this->table,
153  ['deleted' => 1, 'tstamp' => (int)‪$GLOBALS['EXEC_TIME']],
154  ['uid' => (int)$uid]
155  );
156  }
157 
164  protected function ‪queryMultipleRecords(array $conditions = [])
165  {
166  $result = null;
167 
168  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
169  $queryBuilder->getRestrictions()
170  ->removeAll()
171  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
172 
173  $queryBuilder->select('*')
174  ->from($this->table);
175 
176  if (!empty($conditions)) {
177  $queryBuilder->where(...$conditions);
178  }
179 
180  $data = $queryBuilder->execute()->fetchAll();
181  if (!empty($data)) {
182  $result = $this->‪createMultipleDomainObjects($data);
183  }
184 
185  return $result;
186  }
187 
195  protected function ‪createDomainObject(array $record)
196  {
197  switch ($record['type']) {
199  $collection = ‪StaticRecordCollection::create($record);
200  break;
201  default:
202  throw new \RuntimeException('Unknown record collection type "' . $record['type'], 1328646798);
203  }
204  return $collection;
205  }
206 
213  protected function ‪createMultipleDomainObjects(array $data)
214  {
215  $collections = [];
216  foreach ($data as $collection) {
217  $collections[] = $this->‪createDomainObject($collection);
218  }
219  return $collections;
220  }
221 
228  protected function ‪getEnvironmentMode()
229  {
230  return TYPO3_MODE;
231  }
232 }
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\findByUid
‪TYPO3 CMS Core Collection AbstractRecordCollection null findByUid($uid)
Definition: RecordCollectionRepository.php:52
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\findAll
‪TYPO3 CMS Core Collection AbstractRecordCollection[] null findAll()
Definition: RecordCollectionRepository.php:81
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\createMultipleDomainObjects
‪TYPO3 CMS Core Collection AbstractRecordCollection[] createMultipleDomainObjects(array $data)
Definition: RecordCollectionRepository.php:210
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\queryMultipleRecords
‪TYPO3 CMS Core Collection AbstractRecordCollection[] null queryMultipleRecords(array $conditions=[])
Definition: RecordCollectionRepository.php:161
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\findByType
‪TYPO3 CMS Core Collection AbstractRecordCollection[] null findByType($type)
Definition: RecordCollectionRepository.php:109
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\TYPE_Static
‪const TYPE_Static
Definition: RecordCollectionRepository.php:30
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\createDomainObject
‪TYPO3 CMS Core Collection AbstractRecordCollection createDomainObject(array $record)
Definition: RecordCollectionRepository.php:192
‪TYPO3\CMS\Core\Collection
Definition: AbstractRecordCollection.php:2
‪TYPO3\CMS\Core\Collection\StaticRecordCollection\create
‪static TYPO3 CMS Core Collection StaticRecordCollection create(array $collectionRecord, $fillItems=false)
Definition: StaticRecordCollection.php:33
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\findByTypeAndTableName
‪TYPO3 CMS Core Collection AbstractRecordCollection[] null findByTypeAndTableName($type, $tableName)
Definition: RecordCollectionRepository.php:127
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\findByTableName
‪TYPO3 CMS Core Collection AbstractRecordCollection[] findByTableName($tableName)
Definition: RecordCollectionRepository.php:92
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\$typeField
‪string $typeField
Definition: RecordCollectionRepository.php:40
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\$tableField
‪string $tableField
Definition: RecordCollectionRepository.php:44
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\getEnvironmentMode
‪string getEnvironmentMode()
Definition: RecordCollectionRepository.php:225
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\$table
‪string $table
Definition: RecordCollectionRepository.php:36
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository
Definition: RecordCollectionRepository.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:26
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:29
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\deleteByUid
‪deleteByUid($uid)
Definition: RecordCollectionRepository.php:144