TYPO3 CMS  TYPO3_7-6
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  */
16 
30 {
36  protected static $storageTableName = 'sys_collection';
37 
43  protected static $storageItemsField = 'items';
44 
50  protected $uid = 0;
51 
57  protected $title;
58 
64  protected $description;
65 
71  protected $itemTableName;
72 
78  protected $storage;
79 
83  public function __construct()
84  {
85  $this->storage = new \SplDoublyLinkedList();
86  }
87 
95  public function current()
96  {
97  return $this->storage->current();
98  }
99 
107  public function next()
108  {
109  $this->storage->next();
110  }
111 
119  public function key()
120  {
121  $currentRecord = $this->storage->current();
122  return $currentRecord['uid'];
123  }
124 
132  public function valid()
133  {
134  return $this->storage->valid();
135  }
136 
144  public function rewind()
145  {
146  $this->storage->rewind();
147  }
148 
156  public function serialize()
157  {
158  $data = [
159  'uid' => $this->getIdentifier()
160  ];
161  return serialize($data);
162  }
163 
172  public function unserialize($serialized)
173  {
174  $data = unserialize($serialized);
175  return self::load($data['uid']);
176  }
177 
185  public function count()
186  {
187  return $this->storage->count();
188  }
189 
195  public function getTitle()
196  {
197  return $this->title;
198  }
199 
205  public function getUid()
206  {
207  return $this->uid;
208  }
209 
215  public function getDescription()
216  {
217  return $this->description;
218  }
219 
226  public function setTitle($title)
227  {
228  $this->title = $title;
229  }
230 
237  public function setDescription($desc)
238  {
239  $this->description = $desc;
240  }
241 
247  public function getItemTableName()
248  {
249  return $this->itemTableName;
250  }
251 
258  public function setItemTableName($tableName)
259  {
260  $this->itemTableName = $tableName;
261  }
262 
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 
289  public function moveItemAt($currentPosition, $newPosition = 0)
290  {
291  // @todo Implement usort() method with TCEforms in mind
292  throw new \RuntimeException('This method is not yet supported.', 1322545626);
293  }
294 
300  public function getIdentifier()
301  {
302  return $this->uid;
303  }
304 
311  public function setIdentifier($id)
312  {
313  $this->uid = (int)$id;
314  }
315 
327  public static function load($id, $fillItems = false)
328  {
329  $collectionRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
330  '*',
331  static::$storageTableName,
332  'uid=' . (int)$id . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause(static::$storageTableName)
333  );
334  return self::create($collectionRecord, $fillItems);
335  }
336 
345  public static function create(array $collectionRecord, $fillItems = false)
346  {
347  $collection = new static();
348  $collection->fromArray($collectionRecord);
349  if ($fillItems) {
350  $collection->loadContents();
351  }
352  return $collection;
353  }
354 
360  public function persist()
361  {
362  $uid = $this->getIdentifier() == 0 ? 'NEW' . rand(100000, 999999) : $this->getIdentifier();
363  $data = [
364  trim(static::$storageTableName) => [
365  $uid => $this->getPersistableDataArray()
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 = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
374  $tce->stripslashes_values = 0;
375  $tce->start($data, []);
376  $tce->process_datamap();
377  }
378 
387  abstract protected function getPersistableDataArray();
388 
398  protected function getItemUidList($includeTableName = true)
399  {
400  $list = [];
401  foreach ($this->storage as $entry) {
402  $list[] = ($includeTableName ? $this->getItemTableName() . '_' : '') . $entry['uid'];
403  }
404  return implode(',', $list);
405  }
406 
412  public function toArray()
413  {
414  $itemArray = [];
415  foreach ($this->storage as $item) {
416  $itemArray[] = $item;
417  }
418  return [
419  'uid' => $this->getIdentifier(),
420  'title' => $this->getTitle(),
421  'description' => $this->getDescription(),
422  'table_name' => $this->getItemTableName(),
423  'items' => $itemArray
424  ];
425  }
426 
433  public function fromArray(array $array)
434  {
435  $this->uid = $array['uid'];
436  $this->title = $array['title'];
437  $this->description = $array['description'];
438  $this->itemTableName = $array['table_name'];
439  }
440 }
static create(array $collectionRecord, $fillItems=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static deleteClause($table, $tableAlias='')