TYPO3 CMS  TYPO3_8-7
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 
22 
27 {
31  const TYPE_Static = 'static';
32 
38  protected $table = 'sys_collection';
39 
43  protected $typeField = 'type';
44 
48  protected $tableField = 'table_name';
49 
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 
88  public function findAll()
89  {
90  return $this->queryMultipleRecords();
91  }
92 
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 
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 
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 
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 
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 
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 
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 
232  protected function getEnvironmentMode()
233  {
234  return TYPO3_MODE;
235  }
236 }
static create(array $collectionRecord, $fillItems=false)
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']