[CLEANUP] Initialize variable in EXT:filelist
[Packages/TYPO3.CMS.git] / typo3 / sysext / filelist / Classes / FileFacade.php
1 <?php
2 namespace TYPO3\CMS\Filelist;
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 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Resource\FileInterface;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Class FileFacade
26 *
27 * This class is meant to be a wrapper for Resource\File objects, which do not
28 * provide necessary methods needed in the views of the filelist extension. It
29 * is a first approach to get rid of the FileList class that mixes up PHP,
30 * HTML and JavaScript.
31 */
32 class FileFacade
33 {
34 /**
35 * Cache to count the number of references for each file
36 *
37 * @var array
38 */
39 protected static $referenceCounts = [];
40
41 /**
42 * @var \TYPO3\CMS\Core\Resource\FileInterface
43 */
44 protected $resource;
45
46 /**
47 * @var \TYPO3\CMS\Core\Imaging\IconFactory
48 */
49 protected $iconFactory;
50
51 /**
52 * @param \TYPO3\CMS\Core\Resource\FileInterface $resource
53 * @internal Do not use outside of EXT:filelist!
54 */
55 public function __construct(FileInterface $resource)
56 {
57 $this->resource = $resource;
58 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
59 }
60
61 /**
62 * @return string
63 */
64 public function getIcon()
65 {
66 $title = htmlspecialchars($this->resource->getName() . ' [' . (int)$this->resource->getProperty('uid') . ']');
67 return '<span title="' . $title . '">' . $this->iconFactory->getIconForResource($this->resource, Icon::SIZE_SMALL) . '</span>';
68 }
69
70 /**
71 * @return \TYPO3\CMS\Core\Resource\FileInterface
72 */
73 public function getResource()
74 {
75 return $this->resource;
76 }
77
78 /**
79 * @return bool
80 */
81 public function getIsEditable()
82 {
83 return $this->getIsWritable()
84 && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $this->resource->getExtension());
85 }
86
87 /**
88 * @return bool
89 */
90 public function getIsMetadataEditable()
91 {
92 return $this->resource->isIndexed() && $this->getIsWritable() && $this->getBackendUser()->check('tables_modify', 'sys_file_metadata');
93 }
94
95 /**
96 * @return int
97 */
98 public function getMetadataUid()
99 {
100 $uid = 0;
101 $method = '_getMetadata';
102 if (is_callable([$this->resource, $method])) {
103 $metadata = call_user_func([$this->resource, $method]);
104
105 if (isset($metadata['uid'])) {
106 $uid = (int)$metadata['uid'];
107 }
108 }
109
110 return $uid;
111 }
112
113 /**
114 * @return string
115 */
116 public function getName()
117 {
118 return $this->resource->getName();
119 }
120
121 /**
122 * @return string
123 */
124 public function getPath()
125 {
126 $method = 'getReadablePath';
127 if (is_callable([$this->resource->getParentFolder(), $method])) {
128 return call_user_func([$this->resource->getParentFolder(), $method]);
129 }
130
131 return '';
132 }
133
134 /**
135 * @return string
136 */
137 public function getPublicUrl()
138 {
139 return $this->resource->getPublicUrl(true);
140 }
141
142 /**
143 * @return string
144 */
145 public function getExtension()
146 {
147 return strtoupper($this->resource->getExtension());
148 }
149
150 /**
151 * @return string
152 */
153 public function getIdentifier()
154 {
155 return $this->resource->getStorage()->getUid() . ':' . $this->resource->getIdentifier();
156 }
157
158 /**
159 * @return string
160 */
161 public function getLastModified()
162 {
163 return BackendUtility::date($this->resource->getModificationTime());
164 }
165
166 /**
167 * @return string
168 */
169 public function getSize()
170 {
171 return GeneralUtility::formatSize($this->resource->getSize(), htmlspecialchars($this->getLanguageService()->getLL('byteSizeUnits')));
172 }
173
174 /**
175 * @return bool
176 */
177 public function getIsReadable()
178 {
179 $method = 'checkActionPermission';
180 if (is_callable([$this->resource, $method])) {
181 return call_user_func_array([$this->resource, $method], ['read']);
182 }
183
184 return false;
185 }
186
187 /**
188 * @return bool
189 */
190 public function getIsWritable()
191 {
192 $method = 'checkActionPermission';
193 if (is_callable([$this->resource, $method])) {
194 return call_user_func_array([$this->resource, $method], ['write']);
195 }
196
197 return false;
198 }
199
200 /**
201 * @return bool
202 */
203 public function getIsReplaceable()
204 {
205 $method = 'checkActionPermission';
206 if (is_callable([$this->resource, $method])) {
207 return call_user_func_array([$this->resource, $method], ['replace']);
208 }
209
210 return false;
211 }
212
213 /**
214 * @return bool
215 */
216 public function getIsRenamable()
217 {
218 $method = 'checkActionPermission';
219 if (is_callable([$this->resource, $method])) {
220 return call_user_func_array([$this->resource, $method], ['rename']);
221 }
222
223 return false;
224 }
225
226 /**
227 * @return bool
228 */
229 public function getIsDeletable()
230 {
231 $method = 'checkActionPermission';
232 if (is_callable([$this->resource, $method])) {
233 return call_user_func_array([$this->resource, $method], ['delete']);
234 }
235
236 return false;
237 }
238
239 /**
240 * @return bool
241 */
242 public function getIsImage()
243 {
244 return GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], strtolower($this->getExtension()));
245 }
246
247 /**
248 * Fetch, cache and return the number of references of a file
249 *
250 * @return int
251 */
252 public function getReferenceCount()
253 {
254 $uid = (int)$this->resource->getProperty('uid');
255
256 if ($uid <= 0) {
257 return 0;
258 }
259
260 if (!isset(static::$referenceCounts[$uid])) {
261 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
262 $count = $queryBuilder->count('*')
263 ->from('sys_refindex')
264 ->where(
265 $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
266 $queryBuilder->expr()->eq(
267 'ref_table',
268 $queryBuilder->createNamedParameter('sys_file', \PDO::PARAM_STR)
269 ),
270 $queryBuilder->expr()->eq(
271 'ref_uid',
272 $queryBuilder->createNamedParameter($this->resource->getProperty('uid'), \PDO::PARAM_INT)
273 ),
274 $queryBuilder->expr()->neq(
275 'tablename',
276 $queryBuilder->createNamedParameter('sys_file_metadata', \PDO::PARAM_STR)
277 )
278 )
279 ->execute()
280 ->fetchColumn();
281
282 static::$referenceCounts[$uid] = $count;
283 }
284
285 return static::$referenceCounts[$uid];
286 }
287
288 /**
289 * @param string $method
290 * @param array $arguments
291 *
292 * @return mixed
293 */
294 public function __call($method, $arguments)
295 {
296 if (is_callable([$this->resource, $method])) {
297 return call_user_func_array([$this->resource, $method], $arguments);
298 }
299
300 return null;
301 }
302
303 /**
304 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
305 */
306 protected function getBackendUser()
307 {
308 return $GLOBALS['BE_USER'];
309 }
310
311 /**
312 * @return \TYPO3\CMS\Lang\LanguageService
313 */
314 protected function getLanguageService()
315 {
316 return $GLOBALS['LANG'];
317 }
318 }