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