[TASK] Unify element- and linkbrowser styling
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / ImageHandler / AddImageHandler.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\ImageHandler;
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 Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Backend\Tree\View\ElementBrowserFolderTreeView;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21 use TYPO3\CMS\Core\Imaging\Icon;
22 use TYPO3\CMS\Core\Imaging\IconFactory;
23 use TYPO3\CMS\Core\Page\PageRenderer;
24 use TYPO3\CMS\Core\Resource\Exception;
25 use TYPO3\CMS\Core\Resource\File;
26 use TYPO3\CMS\Core\Resource\FileInterface;
27 use TYPO3\CMS\Core\Resource\FileRepository;
28 use TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter;
29 use TYPO3\CMS\Core\Resource\Folder;
30 use TYPO3\CMS\Core\Resource\ProcessedFile;
31 use TYPO3\CMS\Core\Resource\ResourceFactory;
32 use TYPO3\CMS\Core\Utility\GeneralUtility;
33 use TYPO3\CMS\Lang\LanguageService;
34 use TYPO3\CMS\Recordlist\Controller\AbstractLinkBrowserController;
35 use TYPO3\CMS\Recordlist\LinkHandler\LinkHandlerInterface;
36 use TYPO3\CMS\Recordlist\Tree\View\LinkParameterProviderInterface;
37 use TYPO3\CMS\Recordlist\View\FolderUtilityRenderer;
38 use TYPO3\CMS\Rtehtmlarea\Controller\SelectImageController;
39
40 class AddImageHandler implements LinkParameterProviderInterface, LinkHandlerInterface
41 {
42 /**
43 * Current mode: One of 'magic' or 'plain'
44 *
45 * @var string
46 */
47 protected $mode;
48
49 /**
50 * @var SelectImageController
51 */
52 protected $selectImageController;
53
54 /**
55 * Relevant for RTE mode "plain": the maximum width an image must have to be selectable.
56 *
57 * @var int
58 */
59 protected $plainMaxWidth;
60
61 /**
62 * Relevant for RTE mode "plain": the maximum height an image must have to be selectable.
63 *
64 * @var int
65 */
66 protected $plainMaxHeight;
67
68 /**
69 * @var string|NULL
70 */
71 protected $expandFolder;
72
73 /**
74 * @var string
75 */
76 protected $defaultClass;
77
78 /**
79 * @var Folder
80 */
81 protected $selectedFolder;
82
83 /**
84 * Holds information about files
85 *
86 * @var mixed[][]
87 */
88 protected $elements = [];
89
90 /**
91 * @var string
92 */
93 protected $searchWord;
94
95 /**
96 * @var FileRepository
97 */
98 protected $fileRepository;
99
100 /**
101 * URL of current request
102 *
103 * @var string
104 */
105 protected $thisScript = '';
106
107 /**
108 * @var IconFactory
109 */
110 protected $iconFactory;
111
112 /**
113 * Initialize the handler
114 *
115 * @param AbstractLinkBrowserController $linkBrowser
116 * @param string $identifier
117 * @param array $configuration Page TSconfig
118 *
119 * @throws \InvalidArgumentException
120 */
121 public function initialize(AbstractLinkBrowserController $linkBrowser, $identifier, array $configuration)
122 {
123 $this->fileRepository = GeneralUtility::makeInstance(FileRepository::class);
124 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
125
126 $this->expandFolder = GeneralUtility::_GP('expandFolder');
127 $this->searchWord = (string)GeneralUtility::_GP('searchWord');
128
129 if ($identifier !== 'plain' && $identifier !== 'magic') {
130 throw new \InvalidArgumentException('The given identifier "' . $identifier . '" is not supported by this handler."', 1455499720);
131 }
132 if (!$linkBrowser instanceof SelectImageController) {
133 throw new \InvalidArgumentException('The given $linkBrowser must be of type SelectImageController."', 1455499721);
134 }
135 $this->mode = $identifier;
136 $this->selectImageController = $linkBrowser;
137
138 $buttonConfiguration = $linkBrowser->getButtonConfiguration();
139 $this->plainMaxWidth = empty($buttonConfiguration['options.']['plain.']['maxWidth'])
140 ? 640
141 : $buttonConfiguration['options.']['plain.']['maxWidth'];
142 $this->plainMaxHeight = empty($buttonConfiguration['options.']['plain.']['maxHeight'])
143 ? 680
144 : $buttonConfiguration['options.']['plain.']['maxHeight'];
145
146 $this->getLanguageService()->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_selectimagecontroller.xlf');
147 }
148
149 /**
150 * Render the link handler
151 *
152 * @param ServerRequestInterface $request
153 *
154 * @return string
155 */
156 public function render(ServerRequestInterface $request)
157 {
158 GeneralUtility::makeInstance(PageRenderer::class)->loadRequireJsModule('TYPO3/CMS/Rtehtmlarea/AddImage');
159
160 $backendUser = $this->getBackendUser();
161
162 // The key number 3 of the bparams contains the "allowed" string. Disallowed is not passed to
163 // the element browser at all but only filtered out in DataHandler afterwards
164 $bparams = explode('|', $this->selectImageController->getUrlParameters()['bparams']);
165 if (isset($bparams[3])) {
166 $allowedFileExtensions = GeneralUtility::trimExplode(',', $bparams[3], true);
167 } else {
168 $allowedFileExtensions = GeneralUtility::trimExplode(
169 ',',
170 $this->mode === 'plain'
171 ? SelectImageController::PLAIN_MODE_IMAGE_FILE_EXTENSIONS
172 : $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
173 true
174 );
175 }
176 if (!empty($allowedFileExtensions) && $allowedFileExtensions[0] !== 'sys_file' && $allowedFileExtensions[0] !== '*') {
177 // Create new filter object
178 $filterObject = GeneralUtility::makeInstance(FileExtensionFilter::class);
179 $filterObject->setAllowedFileExtensions($allowedFileExtensions);
180 // Set file extension filters on all storages
181 $storages = $backendUser->getFileStorages();
182 /** @var $storage \TYPO3\CMS\Core\Resource\ResourceStorage */
183 foreach ($storages as $storage) {
184 $storage->addFileAndFolderNameFilter([$filterObject, 'filterFileList']);
185 }
186 }
187 if ($this->expandFolder) {
188 $fileOrFolderObject = null;
189
190 // Try to fetch the folder the user had open the last time he browsed files
191 // Fallback to the default folder in case the last used folder is not existing
192 try {
193 $fileOrFolderObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->expandFolder);
194 } catch (Exception $accessException) {
195 // We're just catching the exception here, nothing to be done if folder does not exist or is not accessible.
196 } catch (\InvalidArgumentException $driverMissingExecption) {
197 // We're just catching the exception here, nothing to be done if the driver does not exist anymore.
198 }
199
200 if ($fileOrFolderObject instanceof Folder) {
201 // It's a folder
202 $this->selectedFolder = $fileOrFolderObject;
203 } elseif ($fileOrFolderObject instanceof FileInterface) {
204 // It's a file
205 $this->selectedFolder = $fileOrFolderObject->getParentFolder();
206 }
207 }
208 // Or get the user's default upload folder
209 if (!$this->selectedFolder) {
210 try {
211 $this->selectedFolder = $backendUser->getDefaultUploadFolder();
212 } catch (\Exception $e) {
213 // The configured default user folder does not exist
214 }
215 }
216 // Build the file upload and folder creation form
217 $uploadForm = '';
218 $createFolder = '';
219 if ($this->selectedFolder) {
220 $folderUtilityRenderer = GeneralUtility::makeInstance(FolderUtilityRenderer::class, $this);
221 $uploadForm = $folderUtilityRenderer->uploadForm($this->selectedFolder, $allowedFileExtensions);
222 $createFolder = $folderUtilityRenderer->createFolder($this->selectedFolder);
223 }
224
225 // Getting flag for showing/not showing thumbnails:
226 $noThumbs = $backendUser->getTSConfigVal('options.noThumbsInRTEimageSelect');
227 $_MOD_SETTINGS = [];
228 if (!$noThumbs) {
229 // MENU-ITEMS, fetching the setting for thumbnails from File>List module:
230 $_MOD_MENU = ['displayThumbs' => ''];
231 $_MCONF['name'] = 'file_list';
232 $_MOD_SETTINGS = BackendUtility::getModuleData($_MOD_MENU, GeneralUtility::_GP('SET'), $_MCONF['name']);
233 }
234 $noThumbs = $noThumbs ?: !$_MOD_SETTINGS['displayThumbs'];
235 // Create folder tree:
236 /** @var ElementBrowserFolderTreeView $folderTree */
237 $folderTree = GeneralUtility::makeInstance(ElementBrowserFolderTreeView::class);
238 $folderTree->setLinkParameterProvider($this);
239 $tree = $folderTree->getBrowsableTree();
240 if ($this->selectedFolder) {
241 $files = $this->renderFilesInFolder($this->selectedFolder, $allowedFileExtensions, $noThumbs);
242 } else {
243 $files = '';
244 }
245
246 $content = '';
247 // Insert the upload form on top, if so configured
248 if ($backendUser->getTSConfigVal('options.uploadFieldsInTopOfEB')) {
249 $content .= $uploadForm;
250 }
251
252 // Putting the parts together, side by side:
253 $content .= '
254 <!-- Wrapper table for folder tree / filelist: -->
255 <div class="element-browser-section element-browser-filetree">
256 <h3>' . htmlspecialchars($this->getLanguageService()->getLL('folderTree')) . ':</h3>
257 ' . $tree . '
258 ' . $files . '
259 </div>
260 ';
261
262 // Add help message
263 switch ($this->mode) {
264 case 'plain':
265 $content .= sprintf($this->getLanguageService()->getLL('plainImage_msg'), $this->plainMaxWidth, $this->plainMaxHeight);
266 break;
267 case 'magic':
268 $content .= sprintf($this->getLanguageService()->getLL('magicImage_msg'));
269 break;
270 }
271 // Adding create folder + upload forms if applicable:
272 if (!$backendUser->getTSConfigVal('options.uploadFieldsInTopOfEB')) {
273 $content .= $uploadForm;
274 }
275 $content .= $createFolder;
276
277 return $content;
278 }
279
280 /**
281 * For TYPO3 Element Browser: Expand folder of files.
282 *
283 * @param Folder $folder The folder path to expand
284 * @param array $extensionList List of fileextensions to show
285 * @param bool $noThumbs Whether to show thumbnails or not. If set, no thumbnails are shown.
286 * @return string HTML output
287 */
288 public function renderFilesInFolder(Folder $folder, array $extensionList = [], $noThumbs = false)
289 {
290 if (!$folder->checkActionPermission('read')) {
291 return '';
292 }
293 $lang = $this->getLanguageService();
294 $titleLen = (int)$this->getBackendUser()->uc['titleLen'];
295
296 if ($this->searchWord !== '') {
297 $files = $this->fileRepository->searchByName($folder, $this->searchWord);
298 } else {
299 $extensionList = !empty($extensionList) && $extensionList[0] === '*' ? [] : $extensionList;
300 $files = $this->getFilesInFolder($folder, $extensionList);
301 }
302 $filesCount = count($files);
303
304 $lines = [];
305
306 // Create the header of current folder:
307 $folderIcon = $this->iconFactory->getIconForResource($folder, Icon::SIZE_SMALL);
308
309 $lines[] = '
310 <tr>
311 <th class="col-title" nowrap="nowrap">' . $folderIcon . ' ' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($folder->getIdentifier(), $titleLen)) . '</th>
312 <th class="col-control" nowrap="nowrap"></th>
313 <th class="col-clipboard" nowrap="nowrap">
314 <a href="#" class="btn btn-default" id="t3js-importSelection" title="' . htmlspecialchars($lang->getLL('importSelection')) . '">' . $this->iconFactory->getIcon('actions-document-import-t3d', Icon::SIZE_SMALL) . '</a>
315 <a href="#" class="btn btn-default" id="t3js-toggleSelection" title="' . htmlspecialchars($lang->getLL('toggleSelection')) . '">' . $this->iconFactory->getIcon('actions-document-select', Icon::SIZE_SMALL) . '</a>
316 </th>
317 <th nowrap="nowrap">&nbsp;</th>
318 </tr>';
319
320 if ($filesCount === 0) {
321 $lines[] = '
322 <tr>
323 <td colspan="4">No files found.</td>
324 </tr>';
325 }
326
327 foreach ($files as $fileObject) {
328 $fileExtension = $fileObject->getExtension();
329 // Thumbnail/size generation:
330 $imgInfo = [];
331 if (!$noThumbs && GeneralUtility::inList(strtolower($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] . ',' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']), strtolower($fileExtension))) {
332 $processedFile = $fileObject->process(
333 ProcessedFile::CONTEXT_IMAGEPREVIEW,
334 ['width' => 64, 'height' => 64]
335 );
336 $imageUrl = $processedFile->getPublicUrl(true);
337 $imgInfo = [
338 $fileObject->getProperty('width'),
339 $fileObject->getProperty('height')
340 ];
341 $pDim = $imgInfo[0] . 'x' . $imgInfo[1] . ' pixels';
342 $clickIcon = '<img src="' . $imageUrl . '"'
343 . ' width="' . $processedFile->getProperty('width') . '"'
344 . ' height="' . $processedFile->getProperty('height') . '"'
345 . ' hspace="5" vspace="5" border="1" />';
346 } else {
347 $clickIcon = '';
348 $pDim = '';
349 }
350 // Create file icon:
351 $size = ' (' . GeneralUtility::formatSize($fileObject->getSize()) . 'bytes' . ($pDim ? ', ' . $pDim : '') . ')';
352 $icon = '<span title="' . htmlspecialchars($fileObject->getName() . $size) . '">' . $this->iconFactory->getIconForResource($fileObject, Icon::SIZE_SMALL) . '</span>';
353 // Create links for adding the file:
354 $filesIndex = count($this->elements);
355 $this->elements['file_' . $filesIndex] = [
356 'type' => 'file',
357 'table' => 'sys_file',
358 'uid' => $fileObject->getUid(),
359 'fileName' => $fileObject->getName(),
360 'filePath' => $fileObject->getUid(),
361 'fileExt' => $fileExtension,
362 'fileIcon' => $icon
363 ];
364 if ($this->fileIsSelectableInFileList($fileObject, $imgInfo)) {
365 $ATag = '<a href="#" class="btn btn-default" title="' . htmlspecialchars($fileObject->getName()) . '" data-file-index="' . htmlspecialchars($filesIndex) . '" data-close="0">';
366 $ATag_alt = '<a href="#" title="' . htmlspecialchars($fileObject->getName()) . '" data-file-index="' . htmlspecialchars($filesIndex) . '" data-close="1">';
367 $ATag_e = '</a>';
368 $bulkCheckBox = '<label class="btn btn-default btn-checkbox"><input type="checkbox" class="typo3-bulk-item" name="file_' . $filesIndex . '" value="0" /><span class="t3-icon fa"></span></label>';
369 } else {
370 $ATag = '';
371 $ATag_alt = '';
372 $ATag_e = '';
373 $bulkCheckBox = '';
374 }
375 // Create link to showing details about the file in a window:
376 $Ahref = BackendUtility::getModuleUrl('show_item', [
377 'type' => 'file',
378 'table' => '_FILE',
379 'uid' => $fileObject->getCombinedIdentifier(),
380 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
381 ]);
382
383 // Combine the stuff:
384 $filenameAndIcon = $ATag_alt . $icon . htmlspecialchars(GeneralUtility::fixed_lgd_cs($fileObject->getName(), $titleLen)) . $ATag_e;
385 // Show element:
386 $lines[] = '
387 <tr class="file_list_normal">
388 <td class="col-title" nowrap="nowrap">' . $filenameAndIcon . '&nbsp;</td>
389 <td class="col-control">
390 <div class="btn-group">' . $ATag . '<span title="' . htmlspecialchars($lang->getLL('addToList')) . '">' . $this->iconFactory->getIcon('actions-edit-add', Icon::SIZE_SMALL)->render() . '</span>' . $ATag_e . '
391 <a href="' . htmlspecialchars($Ahref) . '" class="btn btn-default" title="' . htmlspecialchars($lang->getLL('info')) . '">' . $this->iconFactory->getIcon('actions-document-info', Icon::SIZE_SMALL) . '</a>
392 </td>
393 <td class="col-clipboard" valign="top">' . $bulkCheckBox . '</td>
394 <td nowrap="nowrap">&nbsp;' . $pDim . '</td>
395 </tr>';
396 if ($pDim) {
397 $lines[] = '
398 <tr>
399 <td class="filelistThumbnail" colspan="4">' . $ATag_alt . $clickIcon . $ATag_e . '</td>
400 </tr>';
401 }
402 }
403
404 $out = '<h3>' . htmlspecialchars($lang->getLL('files')) . ' ' . $filesCount . ':</h3>';
405 $out .= GeneralUtility::makeInstance(FolderUtilityRenderer::class, $this)->getFileSearchField($this->searchWord);
406 $out .= '<div id="filelist">';
407 $out .= $this->getBulkSelector($filesCount);
408
409 // Wrap all the rows in table tags:
410 $out .= '
411 <!-- Filelisting -->
412 <div class="table-fit">
413 <table class="table table-striped table-hover" id="typo3-filelist">
414 ' . implode('', $lines) . '
415 </table>
416 </div>';
417
418 // Return accumulated content for filelisting:
419 $out .= '</div>';
420 return $out;
421 }
422
423 /**
424 * Get a list of Files in a folder filtered by extension
425 *
426 * @param Folder $folder
427 * @param array $extensionList
428 * @return File[]
429 */
430 protected function getFilesInFolder(Folder $folder, array $extensionList)
431 {
432 if (!empty($extensionList)) {
433 /** @var FileExtensionFilter $filter */
434 $filter = GeneralUtility::makeInstance(FileExtensionFilter::class);
435 $filter->setAllowedFileExtensions($extensionList);
436 $folder->setFileAndFolderNameFilters([[$filter, 'filterFileList']]);
437 }
438 return $folder->getFiles();
439 }
440
441 /**
442 * Get the HTML data required for a bulk selection of files of the TYPO3 Element Browser.
443 *
444 * @param int $filesCount Number of files currently displayed
445 * @return string HTML data required for a bulk selection of files - if $filesCount is 0, nothing is returned
446 */
447 protected function getBulkSelector($filesCount)
448 {
449 if (!$filesCount) {
450 return '';
451 }
452
453 $lang = $this->getLanguageService();
454 $out = '';
455
456 // Getting flag for showing/not showing thumbnails:
457 $noThumbsInEB = $this->getBackendUser()->getTSConfigVal('options.noThumbsInEB');
458 if (!$noThumbsInEB && $this->selectedFolder) {
459 // MENU-ITEMS, fetching the setting for thumbnails from File>List module:
460 $_MOD_MENU = ['displayThumbs' => ''];
461 $_MCONF['name'] = 'file_list';
462 $_MOD_SETTINGS = BackendUtility::getModuleData($_MOD_MENU, GeneralUtility::_GP('SET'), $_MCONF['name']);
463 $addParams = GeneralUtility::implodeArrayForUrl('', $this->getUrlParameters(['identifier' => $this->selectedFolder->getCombinedIdentifier()]));
464 $thumbNailCheck = '<div class="checkbox" style="padding:5px 0 15px 0"><label for="checkDisplayThumbs">'
465 . BackendUtility::getFuncCheck(
466 '',
467 'SET[displayThumbs]',
468 $_MOD_SETTINGS['displayThumbs'],
469 $this->thisScript,
470 $addParams,
471 'id="checkDisplayThumbs"'
472 )
473 . htmlspecialchars($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_file_list.xlf:displayThumbs')) . '</label></div>';
474 $out .= $thumbNailCheck;
475 } else {
476 $out .= '<div style="padding-top: 15px;"></div>';
477 }
478 return $out;
479 }
480
481 /**
482 * Checks if the given file is selectable in the filelist.
483 *
484 * In "plain" RTE mode only image files with a maximum width and height are selectable.
485 *
486 * @param FileInterface $file
487 * @param array $imgInfo Image dimensions from \TYPO3\CMS\Core\Imaging\GraphicalFunctions::getImageDimensions()
488 * @return bool TRUE if file is selectable.
489 */
490 protected function fileIsSelectableInFileList(FileInterface $file, array $imgInfo)
491 {
492 return $this->mode !== 'plain'
493 || (GeneralUtility::inList(SelectImageController::PLAIN_MODE_IMAGE_FILE_EXTENSIONS, strtolower($file->getExtension()))
494 && $imgInfo[0] <= $this->plainMaxWidth && $imgInfo[1] <= $this->plainMaxHeight);
495 }
496
497 /**
498 * @return string[] Array of body-tag attributes
499 */
500 public function getBodyTagAttributes()
501 {
502 return [
503 'data-elements' => json_encode($this->elements)
504 ];
505 }
506
507 /**
508 * Returns the URL of the current script
509 *
510 * @return string
511 */
512 public function getScriptUrl()
513 {
514 return $this->selectImageController->getScriptUrl();
515 }
516
517 /**
518 * Provides an array or GET parameters for URL generation
519 *
520 * @param array $values Array of values to include into the parameters or which might influence the parameters
521 *
522 * @return string[] Array of parameters which have to be added to URLs
523 */
524 public function getUrlParameters(array $values)
525 {
526 $parameters = [
527 'expandFolder' => isset($values['identifier']) ? $values['identifier'] : (string)$this->expandFolder
528 ];
529 return array_merge($this->selectImageController->getUrlParameters($values), $parameters);
530 }
531
532 /**
533 * Return TRUE if the handler supports to update a link.
534 *
535 * This is useful for file or page links, when only attributes are changed.
536 *
537 * @return bool
538 */
539 public function isUpdateSupported()
540 {
541 return false;
542 }
543
544 /**
545 * @return array
546 */
547 public function getLinkAttributes()
548 {
549 return [];
550 }
551
552 /**
553 * @param string[] $fieldDefinitions Array of link attribute field definitions
554 * @return string[]
555 */
556 public function modifyLinkAttributes(array $fieldDefinitions)
557 {
558 return $fieldDefinitions;
559 }
560
561 /**
562 * Check if given value is currently the selected item
563 *
564 * This method is only used in the page tree.
565 *
566 * @param array $values Values to be checked
567 *
568 * @return bool Returns TRUE if the given values match the currently selected item
569 */
570 public function isCurrentlySelectedItem(array $values)
571 {
572 return false;
573 }
574
575 /**
576 * Checks if this is the handler for the given link
577 *
578 * The handler may store this information locally for later usage.
579 *
580 * @param array $linkParts Link parts as returned from TypoLinkCodecService
581 *
582 * @return bool
583 */
584 public function canHandleLink(array $linkParts)
585 {
586 return false;
587 }
588
589 /**
590 * Format the current link for HTML output
591 *
592 * @return string
593 */
594 public function formatCurrentUrl()
595 {
596 return '';
597 }
598
599 /**
600 * @return LanguageService
601 */
602 protected function getLanguageService()
603 {
604 return $GLOBALS['LANG'];
605 }
606
607 /**
608 * @return BackendUserAuthentication
609 */
610 protected function getBackendUser()
611 {
612 return $GLOBALS['BE_USER'];
613 }
614 }