[TASK] Re-work/simplify copyright header in PHP files - Part 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Collection / AbstractFileCollection.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\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 * Abstract collection.
18 *
19 * @author Steffen Ritter <steffen.ritter@typo3.org>
20 */
21 abstract class AbstractFileCollection extends \TYPO3\CMS\Core\Collection\AbstractRecordCollection {
22
23 /**
24 * The table name collections are stored to
25 *
26 * @var string
27 */
28 static protected $storageTableName = 'sys_file_collection';
29
30 /**
31 * The type of file collection
32 * (see \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE constants)
33 *
34 * @var string
35 */
36 static protected $type;
37
38 /**
39 * The name of the field items are handled with
40 * (usually either criteria, items or folder)
41 *
42 * @var string
43 */
44 static protected $itemsCriteriaField;
45
46 /**
47 * Field contents of $itemsCriteriaField. Defines which the items or search criteria for the items
48 * depending on the type (see self::$type above) of this file collection.
49 *
50 * @var mixed
51 */
52 protected $itemsCriteria;
53
54 /**
55 * Name of the table records of this collection are stored in
56 *
57 * @var string
58 */
59 protected $itemTableName = 'sys_file';
60
61 /**
62 * Sets the description.
63 *
64 * @param string $description
65 */
66 public function setDescription($description) {
67 $this->description = $description;
68 }
69
70 /**
71 * Sets the title.
72 *
73 * @param string $title
74 */
75 public function setTitle($title) {
76 $this->title = $title;
77 }
78
79 /**
80 * Return the key of the current element
81 *
82 * @return string
83 */
84 public function key() {
85 /** @var $currentRecord \TYPO3\CMS\Core\Resource\File */
86 $currentRecord = $this->storage->current();
87 return $currentRecord->getIdentifier();
88 }
89
90 /**
91 * Generates comma-separated list of entry uids for usage in TCEmain
92 *
93 * @param boolean $includeTableName
94 * @return string
95 */
96 protected function getItemUidList($includeTableName = FALSE) {
97 $list = array();
98 /** @var $entry \TYPO3\CMS\Core\Resource\File */
99 foreach ($this->storage as $entry) {
100 $list[] = $this->getItemTableName() . '_' . $entry->getUid();
101 }
102 return implode(',', $list);
103 }
104
105 /**
106 * Returns an array of the persistable properties and contents
107 * which are processable by TCEmain.
108 *
109 * @return array
110 */
111 protected function getPersistableDataArray() {
112 return array(
113 'title' => $this->getTitle(),
114 'type' => static::$type,
115 'description' => $this->getDescription(),
116 static::$itemsCriteriaField => $this->getItemsCriteria()
117 );
118 }
119
120 /**
121 * Similar to method in \TYPO3\CMS\Core\Collection\AbstractRecordCollection,
122 * but without 'table_name' => $this->getItemTableName()
123 *
124 * @return array
125 */
126 public function toArray() {
127 $itemArray = array();
128 /** @var $item \TYPO3\CMS\Core\Resource\File */
129 foreach ($this->storage as $item) {
130 $itemArray[] = $item->toArray();
131 }
132 return array(
133 'uid' => $this->getIdentifier(),
134 'title' => $this->getTitle(),
135 'description' => $this->getDescription(),
136 'items' => $itemArray
137 );
138 }
139
140 /**
141 * Gets the current available items.
142 *
143 * @return array
144 */
145 public function getItems() {
146 $itemArray = array();
147 /** @var $item \TYPO3\CMS\Core\Resource\File */
148 foreach ($this->storage as $item) {
149 $itemArray[] = $item;
150 }
151 return $itemArray;
152 }
153
154 /**
155 * Similar to method in \TYPO3\CMS\Core\Collection\AbstractRecordCollection,
156 * but without $this->itemTableName= $array['table_name'],
157 * but with $this->storageItemsFieldContent = $array[self::$storageItemsField];
158 *
159 * @param array $array
160 */
161 public function fromArray(array $array) {
162 $this->uid = $array['uid'];
163 $this->title = $array['title'];
164 $this->description = $array['description'];
165 $this->itemsCriteria = $array[static::$itemsCriteriaField];
166 }
167
168 /**
169 * Gets ths items criteria.
170 *
171 * @return mixed
172 */
173 public function getItemsCriteria() {
174 return $this->itemsCriteria;
175 }
176
177 /**
178 * Sets the items criteria.
179 *
180 * @param mixed $itemsCriteria
181 */
182 public function setItemsCriteria($itemsCriteria) {
183 $this->itemsCriteria = $itemsCriteria;
184 }
185
186 /**
187 * Adds a file to this collection.
188 *
189 * @param \TYPO3\CMS\Core\Resource\FileInterface $data
190 */
191 public function add(\TYPO3\CMS\Core\Resource\FileInterface $data) {
192 $this->storage->push($data);
193 }
194
195 /**
196 * Adds all files of another collection to the corrent one.
197 *
198 * @param \TYPO3\CMS\Core\Collection\CollectionInterface $other
199 */
200 public function addAll(\TYPO3\CMS\Core\Collection\CollectionInterface $other) {
201 /** @var $value \TYPO3\CMS\Core\Resource\File */
202 foreach ($other as $value) {
203 $this->add($value);
204 }
205 }
206
207 /**
208 * Removes a file from this collection.
209 *
210 * @param \TYPO3\CMS\Core\Resource\File $file
211 */
212 public function remove(\TYPO3\CMS\Core\Resource\File $file) {
213 $offset = 0;
214 /** @var $value \TYPO3\CMS\Core\Resource\File */
215 foreach ($this->storage as $value) {
216 if ($value === $file) {
217 break;
218 }
219 $offset++;
220 }
221 $this->storage->offsetUnset($offset);
222 }
223
224 /**
225 * Removes all elements of the current collection.
226 */
227 public function removeAll() {
228 $this->storage = new \SplDoublyLinkedList();
229 }
230
231 }