TYPO3 CMS  TYPO3_7-6
StaticRecordCollection.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 
21 {
30  public static function create(array $collectionRecord, $fillItems = false)
31  {
34  self::class,
35  $collectionRecord['table_name']
36  );
37  $collection->fromArray($collectionRecord);
38  if ($fillItems) {
39  $collection->loadContents();
40  }
41  return $collection;
42  }
43 
50  public function __construct($tableName = null)
51  {
52  parent::__construct();
53  if (!empty($tableName)) {
54  $this->setItemTableName($tableName);
55  } elseif (empty($this->itemTableName)) {
56  throw new \RuntimeException(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class . ' needs a valid itemTableName.', 1330293778);
57  }
58  }
59 
72  public function loadContents()
73  {
74  $entries = $this->getCollectedRecords();
75  $this->removeAll();
76  foreach ($entries as $entry) {
77  $this->add($entry);
78  }
79  }
80 
89  protected function getPersistableDataArray()
90  {
91  return [
92  'title' => $this->getTitle(),
93  'description' => $this->getDescription(),
94  'items' => $this->getItemUidList(true),
95  'type' => 'static',
96  'table_name' => $this->getItemTableName()
97  ];
98  }
99 
106  public function add($data)
107  {
108  $this->storage->push($data);
109  }
110 
117  public function addAll(CollectionInterface $other)
118  {
119  foreach ($other as $value) {
120  $this->add($value);
121  }
122  }
123 
132  public function remove($data)
133  {
134  $offset = 0;
135  foreach ($this->storage as $value) {
136  if ($value == $data) {
137  break;
138  }
139  $offset++;
140  }
141  $this->storage->offsetUnset($offset);
142  }
143 
151  public function removeAll()
152  {
153  $this->storage = new \SplDoublyLinkedList();
154  }
155 
163  protected function getCollectedRecords()
164  {
165  $relatedRecords = [];
166  $resource = $this->getDatabaseConnection()->exec_SELECT_mm_query(
167  $this->getItemTableName() . '.*',
168  self::$storageTableName,
169  'sys_collection_entries',
170  $this->getItemTableName(),
171  'AND ' . self::$storageTableName . '.uid=' . (int)$this->getIdentifier()
172  );
173  if ($resource) {
174  while ($record = $this->getDatabaseConnection()->sql_fetch_assoc($resource)) {
175  $relatedRecords[] = $record;
176  }
177  $this->getDatabaseConnection()->sql_free_result($resource);
178  }
179  return $relatedRecords;
180  }
181 
187  protected function getDatabaseConnection()
188  {
189  return $GLOBALS['TYPO3_DB'];
190  }
191 }
static create(array $collectionRecord, $fillItems=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']