From 8c7194d4c4e4e67ec2001e805b0963f3fcb668ef Mon Sep 17 00:00:00 2001 From: Fabien Udriot Date: Tue, 3 May 2022 22:13:10 +0200 Subject: [PATCH] [TASK] TYPO3 v11 compatibility --- Classes/Controller/MediaUploadController.php | 8 -- .../Widget/ShowUploadedViewHelper.php | 70 ++++++---- .../ViewHelpers/Widget/UploadViewHelper.php | 130 ++++++++++++++---- 3 files changed, 150 insertions(+), 58 deletions(-) diff --git a/Classes/Controller/MediaUploadController.php b/Classes/Controller/MediaUploadController.php index 90b6cc6..768ea59 100644 --- a/Classes/Controller/MediaUploadController.php +++ b/Classes/Controller/MediaUploadController.php @@ -25,9 +25,6 @@ class MediaUploadController extends ActionController /** * Initialize actions. These actions are meant to be called by an logged-in FE User. - * @throws \TYPO3\CMS\Core\Resource\Exception - * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException - * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException */ public function initializeAction() { @@ -63,9 +60,6 @@ public function initializeAction() * Delete a file being just uploaded. * * @return string - * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException - * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException - * @throws \InvalidArgumentException */ public function deleteAction() { @@ -143,8 +137,6 @@ protected function getFrontendUser() * Signal that is emitted before upload processing is called. * * @return void - * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException - * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException */ protected function emitBeforeHandleUploadSignal() { diff --git a/Classes/ViewHelpers/Widget/ShowUploadedViewHelper.php b/Classes/ViewHelpers/Widget/ShowUploadedViewHelper.php index f05715f..4145d48 100644 --- a/Classes/ViewHelpers/Widget/ShowUploadedViewHelper.php +++ b/Classes/ViewHelpers/Widget/ShowUploadedViewHelper.php @@ -8,41 +8,63 @@ * LICENSE.md file that was distributed with this source code. */ -use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper; +use Fab\MediaUpload\Service\UploadFileService; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Fluid\View\StandaloneView; +use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; +use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; /** * Widget which displays a media upload. */ -class ShowUploadedViewHelper extends AbstractWidgetViewHelper +class ShowUploadedViewHelper extends AbstractViewHelper { - - /** - * @var \Fab\MediaUpload\ViewHelpers\Widget\Controller\ShowUploadedController - */ - protected $controller; - - /** - * @param \Fab\MediaUpload\ViewHelpers\Widget\Controller\ShowUploadedController $controller - */ - public function injectController(\Fab\MediaUpload\ViewHelpers\Widget\Controller\ShowUploadedController $controller ) { - $this->controller = $controller ; - } - /** * @return void */ public function initializeArguments() { - $this->registerArgument('property', 'int', 'The property name used for identifying and grouping uploaded files. Required if form contains multiple upload fields', FALSE, ''); + $this->registerArgument( + 'property', + 'int', + 'The property name used for identifying and grouping uploaded files. Required if form contains multiple upload fields', + false, + '', + ); } - - /** - * Returns an carousel widget - * - * @return string - */ - public function render() + public function render(): string { - return $this->initiateSubRequest(); + $uploadFileService = GeneralUtility::makeInstance( + UploadFileService::class, + ); + + return static::renderStatic( + [ + 'property' => $this->arguments['property'], + 'uploadedFileList' => $uploadFileService->getUploadedFileList( + $this->arguments['property'], + ), + 'uploadedFiles' => $uploadFileService->getUploadedFiles( + $this->arguments['property'], + ), + ], + $this->buildRenderChildrenClosure(), + $this->renderingContext, + ); + } + + public static function renderStatic( + array $arguments, + \Closure $renderChildrenClosure, + RenderingContextInterface $renderingContext + ): string { + /** @var StandaloneView $view */ + $view = GeneralUtility::makeInstance(StandaloneView::class); + + $view->setTemplatePathAndFilename( + 'EXT:media_upload/Resources/Private/Templates/ViewHelpers/Widget/ShowUploaded/Index.html', + ); + $view->assignMultiple($arguments); + return $view->render(); } } diff --git a/Classes/ViewHelpers/Widget/UploadViewHelper.php b/Classes/ViewHelpers/Widget/UploadViewHelper.php index bd141c1..bc9bec5 100644 --- a/Classes/ViewHelpers/Widget/UploadViewHelper.php +++ b/Classes/ViewHelpers/Widget/UploadViewHelper.php @@ -8,47 +8,125 @@ * LICENSE.md file that was distributed with this source code. */ -use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper; +use Fab\Media\Utility\PermissionUtility; +use Fab\MediaUpload\Service\UploadFileService; +use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Fluid\View\StandaloneView; +use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; +use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; /** * Widget which displays a media upload. */ -class UploadViewHelper extends AbstractWidgetViewHelper +class UploadViewHelper extends AbstractViewHelper { + public function initializeArguments() + { + $this->registerArgument( + 'allowedExtensions', + 'string', + 'Allowed extension to be uploaded.', + false, + '', + ) + ->registerArgument( + 'maximumSize', + 'int', + 'Maximum file size in Mo by default.', + false, + 0, + ) + ->registerArgument( + 'sizeUnit', + 'string', + 'Whether it is Ko or Mo.', + false, + 'Mo', + ) + ->registerArgument( + 'storage', + 'int', + 'The final storage identifier to which the file will be added eventually.', + true, + ) + ->registerArgument( + 'maximumItems', + 'int', + 'Maximum items to be uploaded', + false, + 10, + ) + ->registerArgument( + 'property', + 'int', + 'The property name used for identifying and grouping uploaded files. Required if form contains multiple upload fields', + false, + '', + ); + } - /** - * @var \Fab\MediaUpload\ViewHelpers\Widget\Controller\UploadController - */ - protected $controller; + public function render(): string + { + $uploadFileService = GeneralUtility::makeInstance( + UploadFileService::class, + ); - /** - * @param \Fab\MediaUpload\ViewHelpers\Widget\Controller\UploadController $controller - */ - public function injectController(\Fab\MediaUpload\ViewHelpers\Widget\Controller\UploadController $controller ) { - $this->controller = $controller ; + return static::renderStatic( + [ + 'allowedExtensions' => $this->arguments['allowedExtensions'], + 'maximumSize' => $this->arguments['maximumSize'], + 'maximumSizeLabel' => self::getMaximumSizeLabel( + (int) $this->arguments['maximumSize'], + ), + 'sizeUnit' => $this->arguments['sizeUnit'], + 'storage' => $this->arguments['storage'], + 'maximumItems' => $this->arguments['maximumItems'], + 'property' => $this->arguments['property'], + 'uploadedFileList' => $uploadFileService->getUploadedFileList( + $this->arguments['property'], + ), + 'widgetIdentifier' => uniqid(), + ], + $this->buildRenderChildrenClosure(), + $this->renderingContext, + ); } - /** - * @return void - * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception - */ - public function initializeArguments() + public static function renderStatic( + array $arguments, + \Closure $renderChildrenClosure, + RenderingContextInterface $renderingContext + ): string { + /** @var StandaloneView $view */ + $view = GeneralUtility::makeInstance(StandaloneView::class); + + $view->setTemplatePathAndFilename( + 'EXT:media_upload/Resources/Private/Templates/ViewHelpers/Widget/Upload/Index.html', + ); + $view->assignMultiple($arguments); + return $view->render(); + } + + public static function getMaximumSizeLabel(int $maximumSize = 0): int { - $this->registerArgument('allowedExtensions', 'string', 'Allowed extension to be uploaded.', FALSE, ''); - $this->registerArgument('maximumSize', 'int', 'Maximum file size in Mo by default.', FALSE, 0); - $this->registerArgument('sizeUnit', 'string', 'Whether it is Ko or Mo.', FALSE, 'Mo'); - $this->registerArgument('storage', 'int', 'The final storage identifier to which the file will be added eventually.', TRUE); - $this->registerArgument('maximumItems', 'int', 'Maximum items to be uploaded', FALSE, 10); - $this->registerArgument('property', 'int', 'The property name used for identifying and grouping uploaded files. Required if form contains multiple upload fields', FALSE, ''); + $maximumSizeLabel = GeneralUtility::getMaxUploadFileSize() / 1024; + if ($maximumSize > 0) { + $maximumSizeLabel = $maximumSize; + } + + return (int) $maximumSizeLabel; } /** - * Returns an carousel widget - * + * @param string $property * @return string */ - public function render() + public static function getUploadedFileList($property = ''): string { - return $this->initiateSubRequest(); + $parameters = GeneralUtility::_GPmerged('tx_mediaupload_pi1'); + return empty($parameters['uploadedFiles'][$property]) + ? '' + : $parameters['uploadedFiles'][$property]; } }