[BUGFIX] Only load folder contents if folder is initialised
[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 if ($this->folder instanceof \TYPO3\CMS\Core\Resource\Folder) {
73 $entries = $this->folder->getFiles();
74 foreach ($entries as $entry) {
75 $this->add($entry);
76 }
77 }
78 }
79
80 /**
81 * Gets the items criteria.
82 *
83 * @return string
84 */
85 public function getItemsCriteria() {
86 return $this->folder->getCombinedIdentifier();
87 }
88
89 /**
90 * Returns an array of the persistable properties and contents
91 * which are processable by TCEmain.
92 *
93 * @return array
94 */
95 protected function getPersistableDataArray() {
96 return array(
97 'title' => $this->getTitle(),
98 'type' => self::$type,
99 'description' => $this->getDescription(),
100 'folder' => $this->folder->getIdentifier(),
101 'storage' => $this->folder->getStorage()->getUid()
102 );
103 }
104
105 /**
106 * Similar to method in \TYPO3\CMS\Core\Collection\AbstractRecordCollection,
107 * but without $this->itemTableName= $array['table_name'],
108 * but with $this->storageItemsFieldContent = $array[self::$storageItemsField];
109 *
110 * @param array $array
111 */
112 public function fromArray(array $array) {
113 $this->uid = $array['uid'];
114 $this->title = $array['title'];
115 $this->description = $array['description'];
116 if (!empty($array['folder']) && !empty($array['storage'])) {
117 /** @var $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
118 $storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
119 /** @var $storage \TYPO3\CMS\Core\Resource\ResourceStorage */
120 $storage = $storageRepository->findByUid($array['storage']);
121 if ($storage) {
122 $this->folder = $storage->getFolder($array['folder']);
123 }
124 }
125 }
126
127 }