TYPO3 CMS  TYPO3_8-7
CategoryCollection.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 
24 
29 {
35  protected static $storageTableName = 'sys_category';
36 
42  protected $relationFieldName = 'categories';
43 
51  public function __construct($tableName = null, $fieldName = null)
52  {
53  parent::__construct();
54  if (!empty($tableName)) {
55  $this->setItemTableName($tableName);
56  } elseif (empty($this->itemTableName)) {
57  throw new \RuntimeException(self::class . ' needs a valid itemTableName.', 1341826168);
58  }
59  if (!empty($fieldName)) {
60  $this->setRelationFieldName($fieldName);
61  }
62  }
63 
72  public static function create(array $collectionRecord, $fillItems = false)
73  {
75  $collection = GeneralUtility::makeInstance(
76  self::class,
77  $collectionRecord['table_name'],
78  $collectionRecord['field_name']
79  );
80  $collection->fromArray($collectionRecord);
81  if ($fillItems) {
82  $collection->loadContents();
83  }
84  return $collection;
85  }
86 
99  public static function load($id, $fillItems = false, $tableName = '', $fieldName = '')
100  {
101  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
102  ->getQueryBuilderForTable(static::$storageTableName);
103 
104  $queryBuilder->getRestrictions()
105  ->removeAll()
106  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
107 
108  $collectionRecord = $queryBuilder->select('*')
109  ->from(static::$storageTableName)
110  ->where(
111  $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT))
112  )
113  ->setMaxResults(1)
114  ->execute()
115  ->fetch();
116 
117  $collectionRecord['table_name'] = $tableName;
118  $collectionRecord['field_name'] = $fieldName;
119 
120  return self::create($collectionRecord, $fillItems);
121  }
122 
130  protected function getCollectedRecordsQueryBuilder()
131  {
132  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
133  ->getQueryBuilderForTable(static::$storageTableName);
134  $queryBuilder->getRestrictions()->removeAll();
135 
136  $queryBuilder->select($this->getItemTableName() . '.*')
137  ->from(static::$storageTableName)
138  ->join(
139  static::$storageTableName,
140  'sys_category_record_mm',
141  'sys_category_record_mm',
142  $queryBuilder->expr()->eq(
143  'sys_category_record_mm.uid_local',
144  $queryBuilder->quoteIdentifier(static::$storageTableName . '.uid')
145  )
146  )
147  ->join(
148  'sys_category_record_mm',
149  $this->getItemTableName(),
150  $this->getItemTableName(),
151  $queryBuilder->expr()->eq(
152  'sys_category_record_mm.uid_foreign',
153  $queryBuilder->quoteIdentifier($this->getItemTableName() . '.uid')
154  )
155  )
156  ->where(
157  $queryBuilder->expr()->eq(
158  static::$storageTableName . '.uid',
159  $queryBuilder->createNamedParameter($this->getIdentifier(), \PDO::PARAM_INT)
160  ),
161  $queryBuilder->expr()->eq(
162  'sys_category_record_mm.tablenames',
163  $queryBuilder->createNamedParameter($this->getItemTableName(), \PDO::PARAM_STR)
164  ),
165  $queryBuilder->expr()->eq(
166  'sys_category_record_mm.fieldname',
167  $queryBuilder->createNamedParameter($this->getRelationFieldName(), \PDO::PARAM_STR)
168  )
169  );
170 
171  return $queryBuilder;
172  }
173 
180  protected function getCollectedRecords()
181  {
182  $relatedRecords = [];
183 
184  $queryBuilder = $this->getCollectedRecordsQueryBuilder();
185  $result = $queryBuilder->execute();
186 
187  while ($record = $result->fetch()) {
188  $relatedRecords[] = $record;
189  }
190 
191  return $relatedRecords;
192  }
193 
202  public function loadContents()
203  {
204  $entries = $this->getCollectedRecords();
205  $this->removeAll();
206  foreach ($entries as $entry) {
207  $this->add($entry);
208  }
209  }
210 
218  protected function getPersistableDataArray()
219  {
220  return [
221  'title' => $this->getTitle(),
222  'description' => $this->getDescription(),
223  'items' => $this->getItemUidList(true)
224  ];
225  }
226 
232  public function add($data)
233  {
234  $this->storage->push($data);
235  }
236 
242  public function addAll(CollectionInterface $other)
243  {
244  foreach ($other as $value) {
245  $this->add($value);
246  }
247  }
248 
255  public function remove($data)
256  {
257  $offset = 0;
258  foreach ($this->storage as $value) {
259  if ($value == $data) {
260  break;
261  }
262  $offset++;
263  }
264  $this->storage->offsetUnset($offset);
265  }
266 
271  public function removeAll()
272  {
273  $this->storage = new \SplDoublyLinkedList();
274  }
275 
281  public function getItems()
282  {
283  $itemArray = [];
285  foreach ($this->storage as $item) {
286  $itemArray[] = $item;
287  }
288  return $itemArray;
289  }
290 
296  public function setRelationFieldName($field)
297  {
298  $this->relationFieldName = $field;
299  }
300 
306  public function getRelationFieldName()
307  {
309  }
310 
316  public static function getStorageTableName()
317  {
318  return self::$storageTableName;
319  }
320 
326  public static function getStorageItemsField()
327  {
328  return self::$storageItemsField;
329  }
330 }
static load($id, $fillItems=false, $tableName='', $fieldName='')
static create(array $collectionRecord, $fillItems=false)
static makeInstance($className,... $constructorArguments)