[TASK] Add more fixers for php-cs-fixer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Collection / StaticRecordCollection.php
1 <?php
2 namespace TYPO3\CMS\Core\Collection;
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
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Implementation of a RecordCollection for static TCA-Records
22 */
23 class StaticRecordCollection extends AbstractRecordCollection implements EditableCollectionInterface
24 {
25 /**
26 * Creates a new collection objects and reconstitutes the
27 * given database record to the new object.
28 *
29 * @param array $collectionRecord Database record
30 * @param bool $fillItems Populates the entries directly on load, might be bad for memory on large collections
31 * @return \TYPO3\CMS\Core\Collection\StaticRecordCollection
32 */
33 public static function create(array $collectionRecord, $fillItems = false)
34 {
35 /** @var $collection StaticRecordCollection */
36 $collection = GeneralUtility::makeInstance(
37 self::class,
38 $collectionRecord['table_name']
39 );
40 $collection->fromArray($collectionRecord);
41 if ($fillItems) {
42 $collection->loadContents();
43 }
44 return $collection;
45 }
46
47 /**
48 * Creates this object.
49 *
50 * @param string $tableName Name of the table to be working on
51 * @throws \RuntimeException
52 */
53 public function __construct($tableName = null)
54 {
55 parent::__construct();
56 if (!empty($tableName)) {
57 $this->setItemTableName($tableName);
58 } elseif (empty($this->itemTableName)) {
59 throw new \RuntimeException(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class . ' needs a valid itemTableName.', 1330293778);
60 }
61 }
62
63 /**
64 * Populates the content-entries of the storage
65 *
66 * Queries the underlying storage for entries of the collection
67 * and adds them to the collection data.
68 *
69 * If the content entries of the storage had not been loaded on creation
70 * ($fillItems = false) this function is to be used for loading the contents
71 * afterwards.
72 *
73 * @return void
74 */
75 public function loadContents()
76 {
77 $entries = $this->getCollectedRecords();
78 $this->removeAll();
79 foreach ($entries as $entry) {
80 $this->add($entry);
81 }
82 }
83
84 /**
85 * Returns an array of the persistable properties and contents
86 * which are processable by TCEmain.
87 *
88 * for internal usage in persist only.
89 *
90 * @return array
91 */
92 protected function getPersistableDataArray()
93 {
94 return [
95 'title' => $this->getTitle(),
96 'description' => $this->getDescription(),
97 'items' => $this->getItemUidList(true),
98 'type' => 'static',
99 'table_name' => $this->getItemTableName()
100 ];
101 }
102
103 /**
104 * Adds on entry to the collection
105 *
106 * @param mixed $data
107 * @return void
108 */
109 public function add($data)
110 {
111 $this->storage->push($data);
112 }
113
114 /**
115 * Adds a set of entries to the collection
116 *
117 * @param CollectionInterface $other
118 * @return void
119 */
120 public function addAll(CollectionInterface $other)
121 {
122 foreach ($other as $value) {
123 $this->add($value);
124 }
125 }
126
127 /**
128 * Removes the given entry from collection
129 *
130 * Note: not the given "index"
131 *
132 * @param mixed $data
133 * @return void
134 */
135 public function remove($data)
136 {
137 $offset = 0;
138 foreach ($this->storage as $value) {
139 if ($value == $data) {
140 break;
141 }
142 $offset++;
143 }
144 $this->storage->offsetUnset($offset);
145 }
146
147 /**
148 * Removes all entries from the collection
149 *
150 * collection will be empty afterwards
151 *
152 * @return void
153 */
154 public function removeAll()
155 {
156 $this->storage = new \SplDoublyLinkedList();
157 }
158
159 /**
160 * Gets the collected records in this collection, by
161 * looking up the MM relations of this record to the
162 * table name defined in the local field 'table_name'.
163 *
164 * @return array
165 */
166 protected function getCollectedRecords()
167 {
168 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::$storageTableName);
169 $queryBuilder->getRestrictions()->removeAll();
170 $statement = $queryBuilder->select($this->getItemTableName() . '.*')
171 ->from(self::$storageTableName)
172 ->join(
173 self::$storageTableName,
174 'sys_collection_entries',
175 'sys_collection_entries_join',
176 $queryBuilder->expr()->eq(
177 'sys_collection_entries_join.uid_local',
178 $queryBuilder->quoteIdentifier(self::$storageTableName . '.uid')
179 )
180 )
181 ->join(
182 'sys_collection_entries_join',
183 $this->getItemTableName(),
184 $this->getItemTableName() . '_join',
185 $queryBuilder->expr()->eq(
186 'sys_collection_entries_join.uid_local',
187 $queryBuilder->quoteIdentifier($this->getItemTableName() . '_join.uid')
188 )
189 )
190 ->where(
191 $queryBuilder->expr()->eq(self::$storageTableName . '.uid', (int)$this->getIdentifier())
192 )
193 ->execute();
194 $relatedRecords = [];
195 while ($record = $statement->fetch()) {
196 $relatedRecords[] = $record;
197 }
198 return $relatedRecords;
199 }
200
201 /**
202 * Gets the database object.
203 *
204 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
205 */
206 protected function getDatabaseConnection()
207 {
208 return $GLOBALS['TYPO3_DB'];
209 }
210 }