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