TYPO3 CMS  TYPO3_8-7
AbstractRecordCollection.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  */
19 
33 {
39  protected static $storageTableName = 'sys_collection';
40 
46  protected static $storageItemsField = 'items';
47 
53  protected $uid = 0;
54 
60  protected $title;
61 
67  protected $description;
68 
74  protected $itemTableName;
75 
81  protected $storage;
82 
86  public function __construct()
87  {
88  $this->storage = new \SplDoublyLinkedList();
89  }
90 
98  public function current()
99  {
100  return $this->storage->current();
101  }
102 
109  public function next()
110  {
111  $this->storage->next();
112  }
113 
121  public function key()
122  {
123  $currentRecord = $this->storage->current();
124  return $currentRecord['uid'];
125  }
126 
134  public function valid()
135  {
136  return $this->storage->valid();
137  }
138 
145  public function rewind()
146  {
147  $this->storage->rewind();
148  }
149 
157  public function serialize()
158  {
159  $data = [
160  'uid' => $this->getIdentifier()
161  ];
162  return serialize($data);
163  }
164 
173  public function unserialize($serialized)
174  {
175  $data = unserialize($serialized);
176  return self::load($data['uid']);
177  }
178 
186  public function count()
187  {
188  return $this->storage->count();
189  }
190 
196  public function getTitle()
197  {
198  return $this->title;
199  }
200 
206  public function getUid()
207  {
208  return $this->uid;
209  }
210 
216  public function getDescription()
217  {
218  return $this->description;
219  }
220 
226  public function setTitle($title)
227  {
228  $this->title = $title;
229  }
230 
236  public function setDescription($desc)
237  {
238  $this->description = $desc;
239  }
240 
246  public function getItemTableName()
247  {
248  return $this->itemTableName;
249  }
250 
256  public function setItemTableName($tableName)
257  {
258  $this->itemTableName = $tableName;
259  }
260 
270  public function usort($callbackFunction)
271  {
272  // @todo Implement usort() method with TCEforms in mind
273  throw new \RuntimeException('This method is not yet supported.', 1322545589);
274  }
275 
285  public function moveItemAt($currentPosition, $newPosition = 0)
286  {
287  // @todo Implement usort() method with TCEforms in mind
288  throw new \RuntimeException('This method is not yet supported.', 1322545626);
289  }
290 
296  public function getIdentifier()
297  {
298  return $this->uid;
299  }
300 
306  public function setIdentifier($id)
307  {
308  $this->uid = (int)$id;
309  }
310 
322  public static function load($id, $fillItems = false)
323  {
324  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(static::$storageTableName);
325  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
326  $collectionRecord = $queryBuilder->select('*')
327  ->from(static::$storageTableName)
328  ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)))
329  ->execute()
330  ->fetch();
331  return self::create($collectionRecord, $fillItems);
332  }
333 
342  public static function create(array $collectionRecord, $fillItems = false)
343  {
344  $collection = new static();
345  $collection->fromArray($collectionRecord);
346  if ($fillItems) {
347  $collection->loadContents();
348  }
349  return $collection;
350  }
351 
355  public function persist()
356  {
357  $uid = $this->getIdentifier() == 0 ? 'NEW' . rand(100000, 999999) : $this->getIdentifier();
358  $data = [
359  trim(static::$storageTableName) => [
360  $uid => $this->getPersistableDataArray()
361  ]
362  ];
363  // New records always must have a pid
364  if ($this->getIdentifier() == 0) {
365  $data[trim(static::$storageTableName)][$uid]['pid'] = 0;
366  }
368  $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
369  $tce->start($data, []);
370  $tce->process_datamap();
371  }
372 
381  abstract protected function getPersistableDataArray();
382 
392  protected function getItemUidList($includeTableName = true)
393  {
394  $list = [];
395  foreach ($this->storage as $entry) {
396  $list[] = ($includeTableName ? $this->getItemTableName() . '_' : '') . $entry['uid'];
397  }
398  return implode(',', $list);
399  }
400 
406  public function toArray()
407  {
408  $itemArray = [];
409  foreach ($this->storage as $item) {
410  $itemArray[] = $item;
411  }
412  return [
413  'uid' => $this->getIdentifier(),
414  'title' => $this->getTitle(),
415  'description' => $this->getDescription(),
416  'table_name' => $this->getItemTableName(),
417  'items' => $itemArray
418  ];
419  }
420 
426  public function fromArray(array $array)
427  {
428  $this->uid = $array['uid'];
429  $this->title = $array['title'];
430  $this->description = $array['description'];
431  $this->itemTableName = $array['table_name'];
432  }
433 }
static create(array $collectionRecord, $fillItems=false)
static makeInstance($className,... $constructorArguments)