2bfe1a05871c1033219442aec726acea65d22384
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Category / Collection / CategoryCollection.php
1 <?php
2 namespace TYPO3\CMS\Core\Category\Collection;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Fabien Udriot <fabien.udriot@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Category Collection to handle records attached to a category
31 *
32 * @author Fabien Udriot <fabien.udriot@typo3.org>
33 */
34 class CategoryCollection extends \TYPO3\CMS\Core\Collection\AbstractRecordCollection implements \TYPO3\CMS\Core\Collection\EditableCollectionInterface {
35
36 /**
37 * The table name collections are stored to
38 *
39 * @var string
40 */
41 static protected $storageTableName = 'sys_category';
42
43 /**
44 * Creates this object.
45 *
46 * @param string $tableName Name of the table to be working on
47 * @throws \RuntimeException
48 */
49 public function __construct($tableName = NULL) {
50 parent::__construct();
51 if (!empty($tableName)) {
52 $this->setItemTableName($tableName);
53 } elseif (empty($this->itemTableName)) {
54 throw new \RuntimeException('TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection needs a valid itemTableName.', 1341826168);
55 }
56 }
57
58 /**
59 * Creates a new collection objects and reconstitutes the
60 * given database record to the new object.
61 *
62 * @param array $collectionRecord Database record
63 * @param boolean $fillItems Populates the entries directly on load, might be bad for memory on large collections
64 * @return \TYPO3\CMS\Core\Category\Collection\CategoryCollection
65 */
66 static public function create(array $collectionRecord, $fillItems = FALSE) {
67 /** @var $collection \TYPO3\CMS\Core\Category\Collection\CategoryCollection */
68 $collection = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection', $collectionRecord['table_name']);
69 $collection->fromArray($collectionRecord);
70 if ($fillItems) {
71 $collection->loadContents();
72 }
73 return $collection;
74 }
75
76 /**
77 * Loads the collections with the given id from persistence
78 * For memory reasons, per default only f.e. title, database-table,
79 * identifier (what ever static data is defined) is loaded.
80 * Entries can be load on first access.
81 *
82 * @param integer $id Id of database record to be loaded
83 * @param boolean $fillItems Populates the entries directly on load, might be bad for memory on large collections
84 * @param string $tableName the table name
85 * @return \TYPO3\CMS\Core\Collection\CollectionInterface
86 */
87 static public function load($id, $fillItems = FALSE, $tableName = '') {
88 $collectionRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', static::$storageTableName, 'uid=' . intval($id) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause(static::$storageTableName));
89 $collectionRecord['table_name'] = $tableName;
90 return self::create($collectionRecord, $fillItems);
91 }
92
93 /**
94 * Gets the collected records in this collection, by
95 * looking up the MM relations of this record to the
96 * table name defined in the local field 'table_name'.
97 *
98 * @return array
99 */
100 protected function getCollectedRecords() {
101 $relatedRecords = array();
102 /** @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
103 $resource = $this->getDatabase()->exec_SELECT_mm_query($this->getItemTableName() . '.*', self::$storageTableName, 'sys_category_record_mm', $this->getItemTableName(), 'AND ' . self::$storageTableName . '.uid=' . intval($this->getIdentifier()));
104 if ($resource) {
105 while ($record = $this->getDatabase()->sql_fetch_assoc($resource)) {
106 $relatedRecords[] = $record;
107 }
108 $this->getDatabase()->sql_free_result($resource);
109 }
110 return $relatedRecords;
111 }
112
113 /**
114 * Populates the content-entries of the storage
115 * Queries the underlying storage for entries of the collection
116 * and adds them to the collection data.
117 * If the content entries of the storage had not been loaded on creation
118 * ($fillItems = false) this function is to be used for loading the contents
119 * afterwards.
120 *
121 * @return void
122 */
123 public function loadContents() {
124 $entries = $this->getCollectedRecords();
125 $this->removeAll();
126 foreach ($entries as $entry) {
127 $this->add($entry);
128 }
129 }
130
131 /**
132 * Returns an array of the persistable properties and contents
133 * which are processable by TCEmain.
134 * for internal usage in persist only.
135 *
136 * @return array
137 */
138 protected function getPersistableDataArray() {
139 return array(
140 'title' => $this->getTitle(),
141 'description' => $this->getDescription(),
142 'items' => $this->getItemUidList(TRUE)
143 );
144 }
145
146 /**
147 * Adds on entry to the collection
148 *
149 * @param mixed $data
150 * @return void
151 */
152 public function add($data) {
153 $this->storage->push($data);
154 }
155
156 /**
157 * Adds a set of entries to the collection
158 *
159 * @param \TYPO3\CMS\Core\Collection\CollectionInterface $other
160 * @return void
161 */
162 public function addAll(\TYPO3\CMS\Core\Collection\CollectionInterface $other) {
163 foreach ($other as $value) {
164 $this->add($value);
165 }
166 }
167
168 /**
169 * Removes the given entry from collection
170 * Note: not the given "index"
171 *
172 * @param mixed $data
173 * @return void
174 */
175 public function remove($data) {
176 $offset = 0;
177 foreach ($this->storage as $value) {
178 if ($value == $data) {
179 break;
180 }
181 $offset++;
182 }
183 $this->storage->offsetUnset($offset);
184 }
185
186 /**
187 * Removes all entries from the collection
188 * collection will be empty afterwards
189 *
190 * @return void
191 */
192 public function removeAll() {
193 $this->storage = new \SplDoublyLinkedList();
194 }
195
196 /**
197 * Gets the current available items.
198 *
199 * @return array
200 */
201 public function getItems() {
202 $itemArray = array();
203 /** @var $item \TYPO3\CMS\Core\Resource\File */
204 foreach ($this->storage as $item) {
205 $itemArray[] = $item;
206 }
207 return $itemArray;
208 }
209
210 /**
211 * Getter for the storage table name
212 *
213 * @return string
214 */
215 static public function getStorageTableName() {
216 return self::$storageTableName;
217 }
218
219 /**
220 * Getter for the storage items field
221 *
222 * @return string
223 */
224 static public function getStorageItemsField() {
225 return self::$storageItemsField;
226 }
227
228 /**
229 * Gets the database object.
230 *
231 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
232 */
233 protected function getDatabase() {
234 return $GLOBALS['TYPO3_DB'];
235 }
236
237 }
238
239
240 ?>