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