TYPO3 CMS  TYPO3_8-7
StaticRecordCollection.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 
19 
24 {
33  public static function create(array $collectionRecord, $fillItems = false)
34  {
36  $collection = GeneralUtility::makeInstance(
37  self::class,
38  $collectionRecord['table_name']
39  );
40  $collection->fromArray($collectionRecord);
41  if ($fillItems) {
42  $collection->loadContents();
43  }
44  return $collection;
45  }
46 
53  public function __construct($tableName = null)
54  {
55  parent::__construct();
56  if (!empty($tableName)) {
57  $this->setItemTableName($tableName);
58  } elseif (empty($this->itemTableName)) {
59  throw new \RuntimeException(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class . ' needs a valid itemTableName.', 1330293778);
60  }
61  }
62 
73  public function loadContents()
74  {
75  $entries = $this->getCollectedRecords();
76  $this->removeAll();
77  foreach ($entries as $entry) {
78  $this->add($entry);
79  }
80  }
81 
90  protected function getPersistableDataArray()
91  {
92  return [
93  'title' => $this->getTitle(),
94  'description' => $this->getDescription(),
95  'items' => $this->getItemUidList(true),
96  'type' => 'static',
97  'table_name' => $this->getItemTableName()
98  ];
99  }
100 
106  public function add($data)
107  {
108  $this->storage->push($data);
109  }
110 
116  public function addAll(CollectionInterface $other)
117  {
118  foreach ($other as $value) {
119  $this->add($value);
120  }
121  }
122 
130  public function remove($data)
131  {
132  $offset = 0;
133  foreach ($this->storage as $value) {
134  if ($value == $data) {
135  break;
136  }
137  $offset++;
138  }
139  $this->storage->offsetUnset($offset);
140  }
141 
147  public function removeAll()
148  {
149  $this->storage = new \SplDoublyLinkedList();
150  }
151 
159  protected function getCollectedRecords()
160  {
161  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
162  ->getQueryBuilderForTable(self::$storageTableName);
163  $queryBuilder->getRestrictions()->removeAll();
164  $statement = $queryBuilder->select($this->getItemTableName() . '.*')
165  ->from(self::$storageTableName)
166  ->join(
167  self::$storageTableName,
168  'sys_collection_entries',
169  'sys_collection_entries',
170  $queryBuilder->expr()->eq(
171  'sys_collection_entries.uid_local',
172  $queryBuilder->quoteIdentifier(self::$storageTableName . '.uid')
173  )
174  )
175  ->join(
176  'sys_collection_entries',
177  $this->getItemTableName(),
178  $this->getItemTableName(),
179  $queryBuilder->expr()->eq(
180  'sys_collection_entries.uid_foreign',
181  $queryBuilder->quoteIdentifier($this->getItemTableName() . '.uid')
182  )
183  )
184  ->where(
185  $queryBuilder->expr()->eq(
186  self::$storageTableName . '.uid',
187  $queryBuilder->createNamedParameter($this->getIdentifier(), \PDO::PARAM_INT)
188  )
189  )
190  ->execute();
191  $relatedRecords = [];
192  while ($record = $statement->fetch()) {
193  $relatedRecords[] = $record;
194  }
195  return $relatedRecords;
196  }
197 }
static create(array $collectionRecord, $fillItems=false)
static makeInstance($className,... $constructorArguments)