‪TYPO3CMS  10.4
RecordCollectionRepository.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
28 {
32  const ‪TYPE_Static = 'static';
33 
39  protected ‪$table = 'sys_collection';
40 
44  protected ‪$typeField = 'type';
45 
49  protected ‪$tableField = 'table_name';
50 
57  public function ‪findByUid($uid)
58  {
59  $result = null;
60  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
61 
62  if ($this->‪getEnvironmentMode() === 'FE') {
63  $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
64  } else {
65  $queryBuilder->getRestrictions()
66  ->removeAll()
67  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
68  }
69 
70  $data = $queryBuilder->select('*')
71  ->from($this->table)
72  ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)))
73  ->execute()
74  ->fetch();
75  if (is_array($data)) {
76  $result = $this->‪createDomainObject($data);
77  }
78  return $result;
79  }
80 
86  public function ‪findAll()
87  {
88  return $this->‪queryMultipleRecords();
89  }
90 
97  public function ‪findByTableName($tableName)
98  {
99  $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
100  ->getQueryBuilderForTable($tableName)
101  ->expr();
102 
103  return $this->‪queryMultipleRecords([
104  $expressionBuilder->eq($this->tableField, $expressionBuilder->literal($tableName))
105  ]);
106  }
107 
114  public function ‪findByType($type)
115  {
116  $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
117  ->getQueryBuilderForTable($this->table)
118  ->expr();
119 
120  return $this->‪queryMultipleRecords([
121  $expressionBuilder->eq($this->typeField, $expressionBuilder->literal($type))
122  ]);
123  }
124 
132  public function ‪findByTypeAndTableName($type, $tableName)
133  {
134  $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
135  ->getQueryBuilderForTable($this->table)
136  ->expr();
137 
138  return $this->‪queryMultipleRecords([
139  $expressionBuilder->eq($this->typeField, $expressionBuilder->literal($type)),
140  $expressionBuilder->eq($this->tableField, $expressionBuilder->literal($tableName))
141  ]);
142  }
143 
149  public function ‪deleteByUid($uid)
150  {
151  GeneralUtility::makeInstance(ConnectionPool::class)
152  ->getConnectionForTable($this->table)
153  ->update(
154  $this->table,
155  ['deleted' => 1, 'tstamp' => (int)‪$GLOBALS['EXEC_TIME']],
156  ['uid' => (int)$uid]
157  );
158  }
159 
166  protected function ‪queryMultipleRecords(array $conditions = [])
167  {
168  $result = null;
169 
170  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
171  $queryBuilder->getRestrictions()
172  ->removeAll()
173  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
174 
175  $queryBuilder->select('*')
176  ->from($this->table);
177 
178  if (!empty($conditions)) {
179  $queryBuilder->where(...$conditions);
180  }
181 
182  $data = $queryBuilder->execute()->fetchAll();
183  if (!empty($data)) {
184  $result = $this->‪createMultipleDomainObjects($data);
185  }
186 
187  return $result;
188  }
189 
197  protected function ‪createDomainObject(array $record)
198  {
199  switch ($record['type']) {
201  $collection = ‪StaticRecordCollection::create($record);
202  break;
203  default:
204  throw new \RuntimeException('Unknown record collection type "' . $record['type'], 1328646798);
205  }
206  return $collection;
207  }
208 
215  protected function ‪createMultipleDomainObjects(array $data)
216  {
217  $collections = [];
218  foreach ($data as $collection) {
219  $collections[] = $this->‪createDomainObject($collection);
220  }
221  return $collections;
222  }
223 
230  protected function ‪getEnvironmentMode(): string
231  {
232  return (‪$GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController ? 'FE' : 'BE';
233  }
234 }
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\findByUid
‪TYPO3 CMS Core Collection AbstractRecordCollection null findByUid($uid)
Definition: RecordCollectionRepository.php:54
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\findAll
‪TYPO3 CMS Core Collection AbstractRecordCollection[] null findAll()
Definition: RecordCollectionRepository.php:83
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\createMultipleDomainObjects
‪TYPO3 CMS Core Collection AbstractRecordCollection[] createMultipleDomainObjects(array $data)
Definition: RecordCollectionRepository.php:212
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\queryMultipleRecords
‪TYPO3 CMS Core Collection AbstractRecordCollection[] null queryMultipleRecords(array $conditions=[])
Definition: RecordCollectionRepository.php:163
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\findByType
‪TYPO3 CMS Core Collection AbstractRecordCollection[] null findByType($type)
Definition: RecordCollectionRepository.php:111
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\TYPE_Static
‪const TYPE_Static
Definition: RecordCollectionRepository.php:32
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\createDomainObject
‪TYPO3 CMS Core Collection AbstractRecordCollection createDomainObject(array $record)
Definition: RecordCollectionRepository.php:194
‪TYPO3\CMS\Core\Collection
Definition: AbstractRecordCollection.php:16
‪TYPO3\CMS\Core\Collection\StaticRecordCollection\create
‪static TYPO3 CMS Core Collection StaticRecordCollection create(array $collectionRecord, $fillItems=false)
Definition: StaticRecordCollection.php:34
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\findByTypeAndTableName
‪TYPO3 CMS Core Collection AbstractRecordCollection[] null findByTypeAndTableName($type, $tableName)
Definition: RecordCollectionRepository.php:129
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\findByTableName
‪TYPO3 CMS Core Collection AbstractRecordCollection[] findByTableName($tableName)
Definition: RecordCollectionRepository.php:94
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\$typeField
‪string $typeField
Definition: RecordCollectionRepository.php:42
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\$tableField
‪string $tableField
Definition: RecordCollectionRepository.php:46
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\getEnvironmentMode
‪string getEnvironmentMode()
Definition: RecordCollectionRepository.php:227
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\$table
‪string $table
Definition: RecordCollectionRepository.php:38
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository
Definition: RecordCollectionRepository.php:28
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer
Definition: FrontendRestrictionContainer.php:31
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\deleteByUid
‪deleteByUid($uid)
Definition: RecordCollectionRepository.php:146