[FEATURE] TCA-record-collection based on collection interfaces
[Packages/TYPO3.CMS.git] / t3lib / collection / StaticRecordCollection.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Steffen Ritter <typo3@steffen-ritter.net>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28
29 /**
30 * implementation of a RecordCollection for static TCA-Records
31 *
32 * @author Steffen Ritter <typo3@steffen-ritter.net>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36 class t3lib_collection_StaticRecordCollection extends t3lib_collection_AbstractRecordCollection implements t3lib_collection_Editable {
37
38 /**
39 * Populates the content-entries of the storage
40 *
41 * Queries the underlying storage for entries of the collection
42 * and adds them to the collection data.
43 *
44 * If the content entries of the storage had not been loaded on creation
45 * ($fillItems = false) this function is to be used for loading the contents
46 * afterwards.
47 *
48 * @return void
49 */
50 public function loadContents() {
51 /** @var t3lib_TcaRelationService $relationService */
52 $relationService = t3lib_div::makeInstance('t3lib_TcaRelationService', self::$storageTableName, self::$storageItemsField, $this->itemTableName);
53
54 $entries = $relationService->getRecordsWithRelationFromCurrentRecord($this->toArray());
55 $this->removeAll();
56 foreach ($entries as $entry) {
57 $this->add($entry);
58 }
59 }
60
61 /**
62 * Returns an array of the persistable properties and contents
63 * which are processable by TCEmain.
64 *
65 * for internal usage in persist only.
66 *
67 * @return array
68 */
69 protected function getPersistableDataArray() {
70 return array(
71 'title' => $this->getTitle(),
72 'description' => $this->getDescription(),
73 'items' => $this->getItemUidList(TRUE),
74 'type' => 'static',
75 'table_name' => $this->getItemTableName(),
76 );
77 }
78
79 /**
80 * Adds on entry to the collection
81 *
82 * @param mixed $data
83 * @return void
84 */
85 public function add($data) {
86 $this->storage->push($data);
87 }
88
89 /**
90 * Adds a set of entries to the collection
91 *
92 * @param t3lib_collection_Collection $other
93 * @return void
94 */
95 public function addAll(t3lib_collection_Collection $other) {
96 foreach ($other as $value) {
97 $this->add($value);
98 }
99 }
100
101 /**
102 * Removes the given entry from collection
103 *
104 * Note: not the given "index"
105 *
106 * @param mixed $data
107 * @return void
108 */
109 public function remove($data) {
110 $offset = 0;
111 foreach ($this->storage as $value) {
112 if ($value == $data) {
113 break;
114 }
115 $offset++;
116 }
117 $this->storage->offsetUnset($offset);
118 }
119
120 /**
121 * Removes all entries from the collection
122 *
123 * collection will be empty afterwards
124 *
125 * @abstract
126 * @return void
127 */
128 public function removeAll() {
129 $this->storage = new SplDoublyLinkedList();
130 }
131
132
133 }
134
135 ?>