[FEATURE] Adds register values to FilesContentObj
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / FilesContentObject.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Ingmar Schlecht <ingmar@typo3.org>
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32 /**
33 * Contains FILES content object
34 *
35 * @author Ingmar Schlecht <ingmar@typo3.org>
36 */
37 class FilesContentObject extends \TYPO3\CMS\Frontend\ContentObject\AbstractContentObject {
38
39 /**
40 * Rendering the cObject FILES
41 *
42 * @param array $conf Array of TypoScript properties
43 * @return string Output
44 */
45 public function render($conf = array()) {
46 /** @var \TYPO3\CMS\Core\Resource\FileRepository $fileRepository */
47 $fileRepository = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
48 $fileObjects = array();
49 // Getting the files
50 if ($conf['references'] || $conf['references.']) {
51 /*
52 The TypoScript could look like this:# all items related to the page.media field:
53 references {
54 table = pages
55 uid.data = page:uid
56 fieldName = media
57 }# or: sys_file_references with uid 27:
58 references = 27
59 */
60 $referencesUid = $this->stdWrapValue('references', $conf);
61 $referencesUidArray = GeneralUtility::intExplode(',', $referencesUid, TRUE);
62 foreach ($referencesUidArray as $referenceUid) {
63 try {
64 $this->addToArray($fileRepository->findFileReferenceByUid($referenceUid), $fileObjects);
65 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
66 /** @var \TYPO3\CMS\Core\Log\Logger $logger */
67 $logger = GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger();
68 $logger->warning('The file-reference with uid "' . $referenceUid . '" could not be found and won\'t be included in frontend output');
69 }
70 }
71
72 // It's important that this always stays "fieldName" and not be renamed to "field" as it would otherwise collide with the stdWrap key of that name
73 $referencesFieldName = $this->stdWrapValue('fieldName', $conf['references.']);
74 if ($referencesFieldName) {
75 $table = $this->cObj->getCurrentTable();
76 if ($table === 'pages' && isset($this->cObj->data['_LOCALIZED_UID']) && intval($this->cObj->data['sys_language_uid']) > 0) {
77 $table = 'pages_language_overlay';
78 }
79 $referencesForeignTable = $this->stdWrapValue('table', $conf['references.'], $table);
80 $referencesForeignUid = $this->stdWrapValue('uid', $conf['references.'], isset($this->cObj->data['_LOCALIZED_UID']) ? $this->cObj->data['_LOCALIZED_UID'] : $this->cObj->data['uid']);
81 $this->addToArray($fileRepository->findByRelation($referencesForeignTable, $referencesFieldName, $referencesForeignUid), $fileObjects);
82 }
83 }
84 if ($conf['files'] || $conf['files.']) {
85 /*
86 The TypoScript could look like this:
87 # with sys_file UIDs:
88 files = 12,14,15# using stdWrap:
89 files.field = some_field
90 */
91 $fileUids = GeneralUtility::trimExplode(',', $this->stdWrapValue('files', $conf), TRUE);
92 foreach ($fileUids as $fileUid) {
93 try {
94 $this->addToArray($fileRepository->findByUid($fileUid), $fileObjects);
95 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
96 /** @var \TYPO3\CMS\Core\Log\Logger $logger */
97 $logger = GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger();
98 $logger->warning('The file with uid "' . $fileUid . '" could not be found and won\'t be included in frontend output');
99 }
100 }
101 }
102 if ($conf['collections'] || $conf['collections.']) {
103 $collectionUids = GeneralUtility::trimExplode(',', $this->stdWrapValue('collections', $conf), TRUE);
104 /** @var \TYPO3\CMS\Core\Resource\FileCollectionRepository $collectionRepository */
105 $collectionRepository = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileCollectionRepository');
106 foreach ($collectionUids as $collectionUid) {
107 try {
108 $fileCollection = $collectionRepository->findByUid($collectionUid);
109 if ($fileCollection instanceof \TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection) {
110 $fileCollection->loadContents();
111 $this->addToArray($fileCollection->getItems(), $fileObjects);
112 }
113 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
114 /** @var \TYPO3\CMS\Core\Log\Logger $logger */
115 $logger = GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger();
116 $logger->warning('The file-collection with uid "' . $collectionUid . '" could not be found or contents could not be loaded and won\'t be included in frontend output');
117 }
118 }
119 }
120 if ($conf['folders'] || $conf['folders.']) {
121 $folderIdentifiers = GeneralUtility::trimExplode(',', $this->stdWrapValue('folders', $conf));
122 /** @var \TYPO3\CMS\Core\Resource\ResourceFactory $fileFactory */
123 $fileFactory = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
124 foreach ($folderIdentifiers as $folderIdentifier) {
125 if ($folderIdentifier) {
126 try {
127 $folder = $fileFactory->getFolderObjectFromCombinedIdentifier($folderIdentifier);
128 if ($folder instanceof \TYPO3\CMS\Core\Resource\Folder) {
129 $this->addToArray($folder->getFiles(), $fileObjects);
130 }
131 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
132 /** @var \TYPO3\CMS\Core\Log\Logger $logger */
133 $logger = GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger();
134 $logger->warning('The folder with identifier "' . $folderIdentifier . '" could not be found and won\'t be included in frontend output');
135 }
136 }
137 }
138 }
139 // Rendering the files
140 $content = '';
141 // optionSplit applied to conf to allow differnt settings per file
142 $splitConf = $GLOBALS['TSFE']->tmpl->splitConfArray($conf, count($fileObjects));
143
144 // Enable sorting for multiple fileObjects
145 $sortingProperty = '';
146 if ($conf['sorting'] || $conf['sorting.']) {
147 $sortingProperty = $this->stdWrapValue('sorting', $conf);
148 }
149 if ($sortingProperty !== '' && count($fileObjects) > 1) {
150 usort($fileObjects, function(\TYPO3\CMS\Core\Resource\FileInterface $a, \TYPO3\CMS\Core\Resource\FileInterface $b) use($sortingProperty) {
151 if ($a->hasProperty($sortingProperty) && $b->hasProperty($sortingProperty)) {
152 return strnatcasecmp($a->getProperty($sortingProperty), $b->getProperty($sortingProperty));
153 } else {
154 return 0;
155 }
156 });
157 }
158
159 $GLOBALS['TSFE']->register['FILES_COUNT'] = count($fileObjects);
160 $fileObjectCounter = 0;
161 foreach ($fileObjects as $key => $fileObject) {
162 $GLOBALS['TSFE']->register['FILE_NUM_CURRENT'] = $fileObjectCounter;
163 $this->cObj->setCurrentFile($fileObject);
164 $content .= $this->cObj->cObjGetSingle($splitConf[$key]['renderObj'], $splitConf[$key]['renderObj.']);
165 $fileObjectCounter++;
166 }
167 $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
168 return $content;
169 }
170
171 /**
172 * Adds $newItems to $theArray, which is passed by reference. Array must only consist of numerical keys.
173 *
174 * @param mixed $newItems Array with new items or single object that's added.
175 * @param array $theArray The array the new items should be added to. Must only contain numeric keys (for array_merge() to add items instead of replacing).
176 */
177 protected function addToArray($newItems, array &$theArray) {
178 if (is_array($newItems)) {
179 $theArray = array_merge($theArray, $newItems);
180 } elseif (is_object($newItems)) {
181 $theArray[] = $newItems;
182 }
183 }
184
185 /**
186 * Gets a configuration value by passing them through stdWrap first and taking a default value if stdWrap doesn't yield a result.
187 *
188 * @param string $key The config variable key (from TS array).
189 * @param array $config The TypoScript array.
190 * @param string $defaultValue Optional default value.
191 * @return string Value of the config variable
192 */
193 protected function stdWrapValue($key, array $config, $defaultValue = '') {
194 return $this->cObj->stdWrap($config[$key], $config[$key . '.']) ? $this->cObj->stdWrap($config[$key], $config[$key . '.']) : $defaultValue;
195 }
196
197 }
198
199
200 ?>