‪TYPO3CMS  10.4
AbstractRecordCollection.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
36 {
42  protected static ‪$storageTableName = 'sys_collection';
43 
49  protected static ‪$storageItemsField = 'items';
50 
56  protected ‪$uid = 0;
57 
63  protected ‪$title;
64 
70  protected ‪$description;
71 
77  protected ‪$itemTableName;
78 
84  protected ‪$storage;
85 
89  public function ‪__construct()
90  {
91  $this->storage = new \SplDoublyLinkedList();
92  }
93 
101  public function ‪current()
102  {
103  return $this->storage->current();
104  }
105 
112  public function ‪next()
113  {
114  $this->storage->next();
115  }
116 
124  public function ‪key()
125  {
126  $currentRecord = $this->storage->current();
127  return $currentRecord['uid'];
128  }
129 
137  public function ‪valid()
138  {
139  return $this->storage->valid();
140  }
141 
148  public function ‪rewind()
149  {
150  $this->storage->rewind();
151  }
152 
160  public function ‪serialize()
161  {
162  $data = [
163  'uid' => $this->‪getIdentifier()
164  ];
165  return ‪serialize($data);
166  }
167 
176  public function ‪unserialize($serialized)
177  {
178  $data = ‪unserialize($serialized);
179  return ‪self::load($data['uid']);
180  }
181 
189  public function ‪count()
190  {
191  return $this->storage->count();
192  }
193 
199  public function ‪getTitle()
200  {
201  return ‪$this->title;
202  }
203 
209  public function ‪getUid()
210  {
211  return ‪$this->uid;
212  }
213 
219  public function ‪getDescription()
220  {
221  return ‪$this->description;
222  }
223 
229  public function ‪setTitle(‪$title)
230  {
231  $this->title = ‪$title;
232  }
233 
239  public function ‪setDescription($desc)
240  {
241  $this->description = $desc;
242  }
243 
249  public function ‪getItemTableName()
250  {
252  }
253 
259  public function ‪setItemTableName($tableName)
260  {
261  $this->itemTableName = $tableName;
262  }
263 
273  public function ‪usort($callbackFunction)
274  {
275  // @todo Implement usort() method with TCEforms in mind
276  throw new \RuntimeException('This method is not yet supported.', 1322545589);
277  }
278 
288  public function ‪moveItemAt($currentPosition, $newPosition = 0)
289  {
290  // @todo Implement usort() method with TCEforms in mind
291  throw new \RuntimeException('This method is not yet supported.', 1322545626);
292  }
293 
299  public function ‪getIdentifier()
300  {
301  return ‪$this->uid;
302  }
303 
309  public function ‪setIdentifier($id)
310  {
311  $this->uid = (int)$id;
312  }
313 
325  public static function ‪load($id, $fillItems = false)
326  {
327  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(static::$storageTableName);
328  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
329  $collectionRecord = $queryBuilder->select('*')
330  ->from(static::$storageTableName)
331  ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)))
332  ->execute()
333  ->fetch();
334  return ‪self::create($collectionRecord, $fillItems);
335  }
336 
345  public static function ‪create(array $collectionRecord, $fillItems = false)
346  {
347  // [phpstan] Unsafe usage of new static()
348  // todo: Either mark this class or its constructor final or use new self instead.
349  $collection = new static();
350  $collection->fromArray($collectionRecord);
351  if ($fillItems) {
352  $collection->loadContents();
353  }
354  return $collection;
355  }
356 
360  public function ‪persist()
361  {
362  ‪$uid = $this->‪getIdentifier() == 0 ? 'NEW' . random_int(100000, 999999) : $this->‪getIdentifier();
363  $data = [
364  trim(static::$storageTableName) => [
366  ]
367  ];
368  // New records always must have a pid
369  if ($this->‪getIdentifier() == 0) {
370  $data[trim(static::$storageTableName)][‪$uid]['pid'] = 0;
371  }
373  $tce = GeneralUtility::makeInstance(DataHandler::class);
374  $tce->start($data, []);
375  $tce->process_datamap();
376  }
377 
386  abstract protected function ‪getPersistableDataArray();
387 
397  protected function ‪getItemUidList($includeTableName = true)
398  {
399  $list = [];
400  foreach ($this->storage as $entry) {
401  $list[] = ($includeTableName ? $this->‪getItemTableName() . '_' : '') . $entry['uid'];
402  }
403  return implode(',', $list);
404  }
405 
411  public function ‪toArray()
412  {
413  $itemArray = [];
414  foreach ($this->storage as $item) {
415  $itemArray[] = $item;
416  }
417  return [
418  'uid' => $this->‪getIdentifier(),
419  'title' => $this->‪getTitle(),
420  'description' => $this->‪getDescription(),
421  'table_name' => $this->‪getItemTableName(),
422  'items' => $itemArray
423  ];
424  }
425 
431  public function ‪fromArray(array $array)
432  {
433  $this->uid = $array['uid'];
434  $this->title = $array['title'];
435  $this->description = $array['description'];
436  $this->itemTableName = $array['table_name'];
437  }
438 
439  protected static function ‪getCollectionDatabaseTable(): string
440  {
441  if (!empty(static::$storageTableName)) {
442  return static::$storageTableName;
443  }
444  throw new \RuntimeException('No storage table name was defined the class "' . static::class . '".', 1592207959);
445  }
446 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:84
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$title
‪string $title
Definition: AbstractRecordCollection.php:59
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getCollectionDatabaseTable
‪static getCollectionDatabaseTable()
Definition: AbstractRecordCollection.php:432
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\load
‪static TYPO3 CMS Core Collection CollectionInterface load($id, $fillItems=false)
Definition: AbstractRecordCollection.php:318
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\__construct
‪__construct()
Definition: AbstractRecordCollection.php:82
‪TYPO3\CMS\Core\Collection\RecordCollectionInterface
Definition: RecordCollectionInterface.php:22
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\moveItemAt
‪moveItemAt($currentPosition, $newPosition=0)
Definition: AbstractRecordCollection.php:281
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$storage
‪SplDoublyLinkedList $storage
Definition: AbstractRecordCollection.php:77
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\rewind
‪rewind()
Definition: AbstractRecordCollection.php:141
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getTitle
‪string getTitle()
Definition: AbstractRecordCollection.php:192
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\key
‪int key()
Definition: AbstractRecordCollection.php:117
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection
Definition: AbstractRecordCollection.php:36
‪TYPO3\CMS\Core\Collection
Definition: AbstractRecordCollection.php:16
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getItemUidList
‪string getItemUidList($includeTableName=true)
Definition: AbstractRecordCollection.php:390
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\usort
‪usort($callbackFunction)
Definition: AbstractRecordCollection.php:266
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setTitle
‪setTitle($title)
Definition: AbstractRecordCollection.php:222
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\persist
‪persist()
Definition: AbstractRecordCollection.php:353
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$storageItemsField
‪static string $storageItemsField
Definition: AbstractRecordCollection.php:47
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setIdentifier
‪setIdentifier($id)
Definition: AbstractRecordCollection.php:302
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getItemTableName
‪string getItemTableName()
Definition: AbstractRecordCollection.php:242
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\fromArray
‪fromArray(array $array)
Definition: AbstractRecordCollection.php:424
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\toArray
‪array toArray()
Definition: AbstractRecordCollection.php:404
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$uid
‪int $uid
Definition: AbstractRecordCollection.php:53
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\current
‪mixed current()
Definition: AbstractRecordCollection.php:94
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\valid
‪bool valid()
Definition: AbstractRecordCollection.php:130
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\count
‪int count()
Definition: AbstractRecordCollection.php:182
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getUid
‪int getUid()
Definition: AbstractRecordCollection.php:202
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getPersistableDataArray
‪array getPersistableDataArray()
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\next
‪next()
Definition: AbstractRecordCollection.php:105
‪TYPO3\CMS\Core\Collection\SortableCollectionInterface
Definition: SortableCollectionInterface.php:28
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$description
‪string $description
Definition: AbstractRecordCollection.php:65
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getIdentifier
‪int getIdentifier()
Definition: AbstractRecordCollection.php:292
‪TYPO3\CMS\Core\Collection\PersistableCollectionInterface
Definition: PersistableCollectionInterface.php:27
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\unserialize
‪mixed unserialize($serialized)
Definition: AbstractRecordCollection.php:169
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setItemTableName
‪setItemTableName($tableName)
Definition: AbstractRecordCollection.php:252
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getDescription
‪string getDescription()
Definition: AbstractRecordCollection.php:212
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setDescription
‪setDescription($desc)
Definition: AbstractRecordCollection.php:232
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\serialize
‪string serialize()
Definition: AbstractRecordCollection.php:153
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\create
‪static TYPO3 CMS Core Collection CollectionInterface create(array $collectionRecord, $fillItems=false)
Definition: AbstractRecordCollection.php:338
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$storageTableName
‪static string $storageTableName
Definition: AbstractRecordCollection.php:41
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$itemTableName
‪string $itemTableName
Definition: AbstractRecordCollection.php:71