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