ew_base/Classes/ViewHelpers/Be/ThumbnailViewHelper.php
2024-12-26 21:44:19 +01:00

130 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
/*
* This file is developed by evoWeb.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
namespace Evoweb\EwBase\ViewHelpers\Be;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Imaging\IconSize;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Resource\ProcessedFile;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
class ThumbnailViewHelper extends AbstractViewHelper
{
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument('row', 'array', 'content data', true);
$this->registerArgument('tableName', 'string', 'table name', true);
$this->registerArgument('fieldName', 'string', 'field name', true);
}
/**
* Render a constant
*
* @return string Value of constant
*/
public function render(): string
{
$table = $this->arguments['tableName'];
$field = $this->arguments['fieldName'];
$row = $this->arguments['row'];
$fileReferences = BackendUtility::resolveFileReferences($table, $field, $row);
$fileObject = is_array($fileReferences) ? $fileReferences[0]->getOriginalFile() : null;
if ($fileObject && $fileObject->isMissing()) {
/** @var IconFactory $iconFactory */
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$label = $this->getLanguageService()->sL(
'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:warning.file_missing'
);
return $iconFactory
->getIcon('mimetypes-other-other', IconSize::MEDIUM, 'overlay-missing')
->setTitle($label . ' ' . $fileObject->getName())
->render();
}
$previewFile = $fileObject->process(
ProcessedFile::CONTEXT_IMAGEPREVIEW,
[
'width' => 64,
'height' => 64,
]
);
return $this->linkEditContent('<img src="' . $previewFile->getPublicUrl() . '"/>', $row );
}
protected function linkEditContent(string $linkText, $row): string
{
if (empty($linkText)) {
return $linkText;
}
$backendUser = $this->getBackendUser();
if (
$backendUser->check('tables_modify', 'tt_content')
&& $backendUser->recordEditAccessInternals('tt_content', $row)
&& (
new Permission(
$backendUser->calcPerms(BackendUtility::getRecord('pages', $row['pid']) ?? [])
)
)->editContentPermissionIsGranted()
) {
$urlParameters = [
'edit' => [
'tt_content' => [
$row['uid'] => 'edit',
],
],
// @extensionScannerIgnoreLine
'returnUrl' => $this->getRequest()->getAttribute('normalizedParams')->getRequestUri()
. '#element-tt_content-' . $row['uid'],
];
/** @var UriBuilder $uriBuilder */
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
return '<a href="' . htmlspecialchars($url) . '" title="'
. htmlspecialchars($this->getLanguageService()->sL(
'LLL:EXT:typo3/sysext/backend/Resources/Private/Language/locallang_layout.xlf:edit'
))
. '">' . $linkText . '</a>';
}
return $linkText;
}
protected function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
protected function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
}