‪TYPO3CMS  9.5
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  {
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) => [
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 }
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$title
‪string $title
Definition: AbstractRecordCollection.php:56
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\load
‪static TYPO3 CMS Core Collection CollectionInterface load($id, $fillItems=false)
Definition: AbstractRecordCollection.php:315
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\__construct
‪__construct()
Definition: AbstractRecordCollection.php:79
‪TYPO3\CMS\Core\Collection\RecordCollectionInterface
Definition: RecordCollectionInterface.php:21
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\moveItemAt
‪moveItemAt($currentPosition, $newPosition=0)
Definition: AbstractRecordCollection.php:278
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$storage
‪SplDoublyLinkedList $storage
Definition: AbstractRecordCollection.php:74
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\rewind
‪rewind()
Definition: AbstractRecordCollection.php:138
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getTitle
‪string getTitle()
Definition: AbstractRecordCollection.php:189
‪TYPO3
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\key
‪int key()
Definition: AbstractRecordCollection.php:114
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection
Definition: AbstractRecordCollection.php:33
‪TYPO3\CMS\Core\Collection
Definition: AbstractRecordCollection.php:2
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getItemUidList
‪string getItemUidList($includeTableName=true)
Definition: AbstractRecordCollection.php:385
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\usort
‪usort($callbackFunction)
Definition: AbstractRecordCollection.php:263
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setTitle
‪setTitle($title)
Definition: AbstractRecordCollection.php:219
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\persist
‪persist()
Definition: AbstractRecordCollection.php:348
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$storageItemsField
‪static string $storageItemsField
Definition: AbstractRecordCollection.php:44
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setIdentifier
‪setIdentifier($id)
Definition: AbstractRecordCollection.php:299
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getItemTableName
‪string getItemTableName()
Definition: AbstractRecordCollection.php:239
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\fromArray
‪fromArray(array $array)
Definition: AbstractRecordCollection.php:419
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\toArray
‪array toArray()
Definition: AbstractRecordCollection.php:399
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$uid
‪int $uid
Definition: AbstractRecordCollection.php:50
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\current
‪mixed current()
Definition: AbstractRecordCollection.php:91
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\valid
‪bool valid()
Definition: AbstractRecordCollection.php:127
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\count
‪int count()
Definition: AbstractRecordCollection.php:179
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getUid
‪int getUid()
Definition: AbstractRecordCollection.php:199
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getPersistableDataArray
‪array getPersistableDataArray()
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\next
‪next()
Definition: AbstractRecordCollection.php:102
‪TYPO3\CMS\Core\Collection\SortableCollectionInterface
Definition: SortableCollectionInterface.php:27
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$description
‪string $description
Definition: AbstractRecordCollection.php:62
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getIdentifier
‪int getIdentifier()
Definition: AbstractRecordCollection.php:289
‪TYPO3\CMS\Core\Collection\PersistableCollectionInterface
Definition: PersistableCollectionInterface.php:26
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:26
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\unserialize
‪mixed unserialize($serialized)
Definition: AbstractRecordCollection.php:166
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setItemTableName
‪setItemTableName($tableName)
Definition: AbstractRecordCollection.php:249
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getDescription
‪string getDescription()
Definition: AbstractRecordCollection.php:209
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setDescription
‪setDescription($desc)
Definition: AbstractRecordCollection.php:229
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\serialize
‪string serialize()
Definition: AbstractRecordCollection.php:150
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\create
‪static TYPO3 CMS Core Collection CollectionInterface create(array $collectionRecord, $fillItems=false)
Definition: AbstractRecordCollection.php:335
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$storageTableName
‪static string $storageTableName
Definition: AbstractRecordCollection.php:38
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$itemTableName
‪string $itemTableName
Definition: AbstractRecordCollection.php:68