fb42c5e7949c0c96f8bd1f3266ef53d13b1bcfc5
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Collection / FolderBasedFileCollection.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Collection;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Andreas Wolf <andreas.wolf@ikt-werk.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * A collection containing a a set files to be represented as a (virtual) folder.
31 * This collection is persisted to the database with the accordant folder reference.
32 *
33 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
34 */
35 class FolderBasedFileCollection extends \TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection {
36
37 /**
38 * @var string
39 */
40 static protected $storageTableName = 'sys_file_collection';
41
42 /**
43 * @var string
44 */
45 static protected $type = 'folder';
46
47 /**
48 * @var string
49 */
50 static protected $itemsCriteriaField = 'folder';
51
52 /**
53 * The folder
54 *
55 * @var \TYPO3\CMS\Core\Resource\Folder
56 */
57 protected $folder;
58
59 /**
60 * Populates the content-entries of the storage
61 *
62 * Queries the underlying storage for entries of the collection
63 * and adds them to the collection data.
64 *
65 * If the content entries of the storage had not been loaded on creation
66 * ($fillItems = false) this function is to be used for loading the contents
67 * afterwards.
68 *
69 * @return void
70 */
71 public function loadContents() {
72 $entries = $this->folder->getFiles();
73 foreach ($entries as $entry) {
74 $this->add($entry);
75 }
76 }
77
78 /**
79 * Gets the items criteria.
80 *
81 * @return string
82 */
83 public function getItemsCriteria() {
84 return $this->folder->getCombinedIdentifier();
85 }
86
87 /**
88 * Returns an array of the persistable properties and contents
89 * which are processable by TCEmain.
90 *
91 * @return array
92 */
93 protected function getPersistableDataArray() {
94 return array(
95 'title' => $this->getTitle(),
96 'type' => self::$type,
97 'description' => $this->getDescription(),
98 'folder' => $this->folder->getIdentifier(),
99 'storage' => $this->folder->getStorage()->getUid()
100 );
101 }
102
103 /**
104 * Similar to method in \TYPO3\CMS\Core\Collection\AbstractRecordCollection,
105 * but without $this->itemTableName= $array['table_name'],
106 * but with $this->storageItemsFieldContent = $array[self::$storageItemsField];
107 *
108 * @param array $array
109 */
110 public function fromArray(array $array) {
111 $this->uid = $array['uid'];
112 $this->title = $array['title'];
113 $this->description = $array['description'];
114 if (!empty($array['folder']) && !empty($array['storage'])) {
115 /** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
116 $storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
117 /** @var $storage \TYPO3\CMS\Core\Resource\ResourceStorage */
118 $storage = $storageRepository->findByUid($array['storage']);
119 if ($storage) {
120 $this->folder = $storage->getFolder($array['folder']);
121 }
122 }
123 }
124
125 }