[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Category / Collection / CategoryCollection.php
1 <?php
2 namespace TYPO3\CMS\Core\Category\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 * Category Collection to handle records attached to a category
18 *
19 * @author Fabien Udriot <fabien.udriot@typo3.org>
20 */
21 class CategoryCollection extends \TYPO3\CMS\Core\Collection\AbstractRecordCollection implements \TYPO3\CMS\Core\Collection\EditableCollectionInterface {
22
23 /**
24 * The table name collections are stored to
25 *
26 * @var string
27 */
28 static protected $storageTableName = 'sys_category';
29
30 /**
31 * Name of the categories-relation field (used in the MM_match_fields/fieldname property of the TCA)
32 *
33 * @var string
34 */
35 protected $relationFieldName = 'categories';
36
37 /**
38 * Creates this object.
39 *
40 * @param string $tableName Name of the table to be working on
41 * @param string $fieldName Name of the field where the categories relations are defined
42 * @throws \RuntimeException
43 */
44 public function __construct($tableName = NULL, $fieldName = NULL) {
45 parent::__construct();
46 if (!empty($tableName)) {
47 $this->setItemTableName($tableName);
48 } elseif (empty($this->itemTableName)) {
49 throw new \RuntimeException('TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection needs a valid itemTableName.', 1341826168);
50 }
51 if (!empty($fieldName)) {
52 $this->setRelationFieldName($fieldName);
53 }
54 }
55
56 /**
57 * Creates a new collection objects and reconstitutes the
58 * given database record to the new object.
59 *
60 * @param array $collectionRecord Database record
61 * @param boolean $fillItems Populates the entries directly on load, might be bad for memory on large collections
62 * @return \TYPO3\CMS\Core\Category\Collection\CategoryCollection
63 */
64 static public function create(array $collectionRecord, $fillItems = FALSE) {
65 /** @var $collection \TYPO3\CMS\Core\Category\Collection\CategoryCollection */
66 $collection = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
67 'TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection',
68 $collectionRecord['table_name'],
69 $collectionRecord['field_name']
70 );
71 $collection->fromArray($collectionRecord);
72 if ($fillItems) {
73 $collection->loadContents();
74 }
75 return $collection;
76 }
77
78 /**
79 * Loads the collections with the given id from persistence
80 * For memory reasons, per default only f.e. title, database-table,
81 * identifier (what ever static data is defined) is loaded.
82 * Entries can be load on first access.
83 *
84 * @param integer $id Id of database record to be loaded
85 * @param boolean $fillItems Populates the entries directly on load, might be bad for memory on large collections
86 * @param string $tableName Name of table from which entries should be loaded
87 * @param string $fieldName Name of the categories relation field
88 * @return \TYPO3\CMS\Core\Collection\CollectionInterface
89 */
90 static public function load($id, $fillItems = FALSE, $tableName = '', $fieldName = '') {
91 $collectionRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
92 '*',
93 static::$storageTableName,
94 'uid = ' . (int)$id . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause(static::$storageTableName)
95 );
96 $collectionRecord['table_name'] = $tableName;
97 $collectionRecord['field_name'] = $fieldName;
98 return self::create($collectionRecord, $fillItems);
99 }
100
101 /**
102 * Gets the collected records in this collection, by
103 * looking up the MM relations of this record to the
104 * table name defined in the local field 'table_name'.
105 *
106 * @return array
107 */
108 protected function getCollectedRecords() {
109 $relatedRecords = array();
110 // Assemble where clause
111 $where = 'AND ' . self::$storageTableName . '.uid = ' . (int)$this->getIdentifier();
112 // Add condition on tablenames fields
113 $where .= ' AND sys_category_record_mm.tablenames = ' . $this->getDatabaseConnection()->fullQuoteStr(
114 $this->getItemTableName(),
115 'sys_category_record_mm'
116 );
117 // Add condition on fieldname field
118 $where .= ' AND sys_category_record_mm.fieldname = ' . $this->getDatabaseConnection()->fullQuoteStr(
119 $this->getRelationFieldName(),
120 'sys_category_record_mm'
121 );
122 $resource = $this->getDatabaseConnection()->exec_SELECT_mm_query(
123 $this->getItemTableName() . '.*',
124 self::$storageTableName,
125 'sys_category_record_mm',
126 $this->getItemTableName(),
127 $where
128 );
129 if ($resource) {
130 while ($record = $this->getDatabaseConnection()->sql_fetch_assoc($resource)) {
131 $relatedRecords[] = $record;
132 }
133 $this->getDatabaseConnection()->sql_free_result($resource);
134 }
135 return $relatedRecords;
136 }
137
138 /**
139 * Populates the content-entries of the storage
140 * Queries the underlying storage for entries of the collection
141 * and adds them to the collection data.
142 * If the content entries of the storage had not been loaded on creation
143 * ($fillItems = false) this function is to be used for loading the contents
144 * afterwards.
145 *
146 * @return void
147 */
148 public function loadContents() {
149 $entries = $this->getCollectedRecords();
150 $this->removeAll();
151 foreach ($entries as $entry) {
152 $this->add($entry);
153 }
154 }
155
156 /**
157 * Returns an array of the persistable properties and contents
158 * which are processable by TCEmain.
159 * for internal usage in persist only.
160 *
161 * @return array
162 */
163 protected function getPersistableDataArray() {
164 return array(
165 'title' => $this->getTitle(),
166 'description' => $this->getDescription(),
167 'items' => $this->getItemUidList(TRUE)
168 );
169 }
170
171 /**
172 * Adds on entry to the collection
173 *
174 * @param mixed $data
175 * @return void
176 */
177 public function add($data) {
178 $this->storage->push($data);
179 }
180
181 /**
182 * Adds a set of entries to the collection
183 *
184 * @param \TYPO3\CMS\Core\Collection\CollectionInterface $other
185 * @return void
186 */
187 public function addAll(\TYPO3\CMS\Core\Collection\CollectionInterface $other) {
188 foreach ($other as $value) {
189 $this->add($value);
190 }
191 }
192
193 /**
194 * Removes the given entry from collection
195 * Note: not the given "index"
196 *
197 * @param mixed $data
198 * @return void
199 */
200 public function remove($data) {
201 $offset = 0;
202 foreach ($this->storage as $value) {
203 if ($value == $data) {
204 break;
205 }
206 $offset++;
207 }
208 $this->storage->offsetUnset($offset);
209 }
210
211 /**
212 * Removes all entries from the collection
213 * collection will be empty afterwards
214 *
215 * @return void
216 */
217 public function removeAll() {
218 $this->storage = new \SplDoublyLinkedList();
219 }
220
221 /**
222 * Gets the current available items.
223 *
224 * @return array
225 */
226 public function getItems() {
227 $itemArray = array();
228 /** @var $item \TYPO3\CMS\Core\Resource\File */
229 foreach ($this->storage as $item) {
230 $itemArray[] = $item;
231 }
232 return $itemArray;
233 }
234
235 /**
236 * Sets the name of the categories relation field
237 *
238 * @param string $field
239 */
240 public function setRelationFieldName($field) {
241 $this->relationFieldName = $field;
242 }
243
244 /**
245 * Gets the name of the categories relation field
246 *
247 * @return string
248 */
249 public function getRelationFieldName() {
250 return $this->relationFieldName;
251 }
252
253 /**
254 * Getter for the storage table name
255 *
256 * @return string
257 */
258 static public function getStorageTableName() {
259 return self::$storageTableName;
260 }
261
262 /**
263 * Getter for the storage items field
264 *
265 * @return string
266 */
267 static public function getStorageItemsField() {
268 return self::$storageItemsField;
269 }
270
271 /**
272 * Gets the database object.
273 *
274 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
275 */
276 protected function getDatabaseConnection() {
277 return $GLOBALS['TYPO3_DB'];
278 }
279
280 }