/
AbstractRecordCollection.php
438 lines (401 loc) · 11.3 KB
/
AbstractRecordCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Collection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Abstract implementation of a RecordCollection
*
* RecordCollection is a collections of TCA-Records.
* The collection is meant to be stored in TCA-table sys_collections and is manageable
* via TCEforms.
*
* A RecordCollection might be used to group a set of records (e.g. news, images, contentElements)
* for output in frontend
*
* The AbstractRecordCollection uses SplDoublyLinkedList for internal storage
*/
abstract class AbstractRecordCollection implements RecordCollectionInterface, PersistableCollectionInterface, SortableCollectionInterface
{
/**
* The table name collections are stored to
*
* @var string
*/
protected static $storageTableName = 'sys_collection';
/**
* The table name collections are stored to
*
* @var string
*/
protected static $storageItemsField = 'items';
/**
* Uid of the storage
*
* @var int
*/
protected $uid = 0;
/**
* Collection title
*
* @var string
*/
protected $title;
/**
* Collection description
*
* @var string
*/
protected $description;
/**
* Table name of the records stored in this collection
*
* @var string
*/
protected $itemTableName;
/**
* The local storage
*
* @var \SplDoublyLinkedList
*/
protected $storage;
/**
* Creates this object.
*/
public function __construct()
{
$this->storage = new \SplDoublyLinkedList();
}
/**
* (PHP 5 >= 5.1.0)
* Return the current element
*
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
*/
public function current()
{
return $this->storage->current();
}
/**
* (PHP 5 >= 5.1.0)
* Move forward to next element
*
* @link http://php.net/manual/en/iterator.next.php
*/
public function next()
{
$this->storage->next();
}
/**
* (PHP 5 >= 5.1.0)
* Return the key of the current element
*
* @link http://php.net/manual/en/iterator.key.php
* @return int 0 on failure.
*/
public function key()
{
$currentRecord = $this->storage->current();
return $currentRecord['uid'];
}
/**
* (PHP 5 >= 5.1.0)
* Checks if current position is valid
*
* @link http://php.net/manual/en/iterator.valid.php
* @return bool The return value will be casted to boolean and then evaluated.
*/
public function valid()
{
return $this->storage->valid();
}
/**
* (PHP 5 >= 5.1.0)
* Rewind the Iterator to the first element
*
* @link http://php.net/manual/en/iterator.rewind.php
*/
public function rewind()
{
$this->storage->rewind();
}
/**
* (PHP 5 >= 5.1.0)
* String representation of object
*
* @link http://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or &null;
*/
public function serialize()
{
$data = [
'uid' => $this->getIdentifier()
];
return serialize($data);
}
/**
* (PHP 5 >= 5.1.0)
* Constructs the object
*
* @link http://php.net/manual/en/serializable.unserialize.php
* @param string $serialized The string representation of the object
* @return mixed the original value unserialized.
*/
public function unserialize($serialized)
{
$data = unserialize($serialized);
return self::load($data['uid']);
}
/**
* (PHP 5 >= 5.1.0)
* Count elements of an object
*
* @link http://php.net/manual/en/countable.count.php
* @return int The custom count as an integer.
*/
public function count()
{
return $this->storage->count();
}
/**
* Getter for the title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Getter for the UID
*
* @return int
*/
public function getUid()
{
return $this->uid;
}
/**
* Getter for the description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Setter for the title
*
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Setter for the description
*
* @param string $desc
*/
public function setDescription($desc)
{
$this->description = $desc;
}
/**
* Setter for the name of the data-source table
*
* @return string
*/
public function getItemTableName()
{
return $this->itemTableName;
}
/**
* Setter for the name of the data-source table
*
* @param string $tableName
*/
public function setItemTableName($tableName)
{
$this->itemTableName = $tableName;
}
/**
* Sorts collection via given callBackFunction
*
* The comparison function given as must return an integer less than, equal to, or greater than
* zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
*
* @param callable $callbackFunction
* @see http://www.php.net/manual/en/function.usort.php
*/
public function usort($callbackFunction)
{
// @todo Implement usort() method with TCEforms in mind
throw new \RuntimeException('This method is not yet supported.', 1322545589);
}
/**
* Moves the item within the collection
*
* the item at $currentPosition will be moved to
* $newPosition. Omitting $newPosition will move to top.
*
* @param int $currentPosition
* @param int $newPosition
*/
public function moveItemAt($currentPosition, $newPosition = 0)
{
// @todo Implement usort() method with TCEforms in mind
throw new \RuntimeException('This method is not yet supported.', 1322545626);
}
/**
* Returns the uid of the collection
*
* @return int
*/
public function getIdentifier()
{
return $this->uid;
}
/**
* Sets the identifier of the collection
*
* @param int $id
*/
public function setIdentifier($id)
{
$this->uid = (int)$id;
}
/**
* Loads the collections with the given id from persistence
*
* For memory reasons, per default only f.e. title, database-table,
* identifier (what ever static data is defined) is loaded.
* Entries can be load on first access.
*
* @param int $id Id of database record to be loaded
* @param bool $fillItems Populates the entries directly on load, might be bad for memory on large collections
* @return \TYPO3\CMS\Core\Collection\CollectionInterface
*/
public static function load($id, $fillItems = false)
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(static::$storageTableName);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$collectionRecord = $queryBuilder->select('*')
->from(static::$storageTableName)
->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)))
->execute()
->fetch();
return self::create($collectionRecord, $fillItems);
}
/**
* Creates a new collection objects and reconstitutes the
* given database record to the new object.
*
* @param array $collectionRecord Database record
* @param bool $fillItems Populates the entries directly on load, might be bad for memory on large collections
* @return \TYPO3\CMS\Core\Collection\CollectionInterface
*/
public static function create(array $collectionRecord, $fillItems = false)
{
// [phpstan] Unsafe usage of new static()
// todo: Either mark this class or its constructor final or use new self instead.
$collection = new static();
$collection->fromArray($collectionRecord);
if ($fillItems) {
$collection->loadContents();
}
return $collection;
}
/**
* Persists current collection state to underlying storage
*/
public function persist()
{
$uid = $this->getIdentifier() == 0 ? 'NEW' . random_int(100000, 999999) : $this->getIdentifier();
$data = [
trim(static::$storageTableName) => [
$uid => $this->getPersistableDataArray()
]
];
// New records always must have a pid
if ($this->getIdentifier() == 0) {
$data[trim(static::$storageTableName)][$uid]['pid'] = 0;
}
/** @var \TYPO3\CMS\Core\DataHandling\DataHandler $tce */
$tce = GeneralUtility::makeInstance(DataHandler::class);
$tce->start($data, []);
$tce->process_datamap();
}
/**
* Returns an array of the persistable properties and contents
* which are processable by DataHandler.
*
* For internal usage in persist only.
*
* @return array
*/
abstract protected function getPersistableDataArray();
/**
* Generates comma-separated list of entry uids for usage in DataHandler
*
* also allow to add table name, if it might be needed by DataHandler for
* storing the relation
*
* @param bool $includeTableName
* @return string
*/
protected function getItemUidList($includeTableName = true)
{
$list = [];
foreach ($this->storage as $entry) {
$list[] = ($includeTableName ? $this->getItemTableName() . '_' : '') . $entry['uid'];
}
return implode(',', $list);
}
/**
* Builds an array representation of this collection
*
* @return array
*/
public function toArray()
{
$itemArray = [];
foreach ($this->storage as $item) {
$itemArray[] = $item;
}
return [
'uid' => $this->getIdentifier(),
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'table_name' => $this->getItemTableName(),
'items' => $itemArray
];
}
/**
* Loads the properties of this collection from an array
*
* @param array $array
*/
public function fromArray(array $array)
{
$this->uid = $array['uid'];
$this->title = $array['title'];
$this->description = $array['description'];
$this->itemTableName = $array['table_name'];
}
}