ew_base/Classes/ViewHelpers/Be/ThumbnailViewHelper.php
2025-11-27 21:09:43 +01:00

207 lines
7.3 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 Psr\Log\LoggerInterface;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\RelationHandler;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Imaging\IconSize;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Resource\ProcessedFile;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Schema\Field\FileFieldType;
use TYPO3\CMS\Core\Schema\TcaSchema;
use TYPO3\CMS\Core\Schema\TcaSchemaFactory;
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 = $this->resolveFileReferences($table, $field, $row);
$fileObject = is_array($fileReferences) ? current($fileReferences)->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 );
}
public function resolveFileReferences(
string $tableName,
string $fieldName,
array $element,
?int $workspaceId = null
): ?array {
if (!($schema = self::getTcaSchema($tableName))?->hasField($fieldName)) {
return null;
}
$field = $schema->getField($fieldName);
if ($field instanceof FileFieldType === false) {
return null;
}
$fileReferences = [];
$relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
if ($workspaceId !== null) {
$relationHandler->setWorkspaceId($workspaceId);
}
$relationHandler->initializeForField(
$tableName,
$field->getConfiguration(),
$element,
$element[$fieldName],
);
$relationHandler->processDeletePlaceholder();
$referenceUids = $relationHandler->tableArray[$field->getConfiguration()['foreign_table']] ?? [];
foreach ($referenceUids as $referenceUid) {
try {
$fileReference = GeneralUtility::makeInstance(ResourceFactory::class)->getFileReferenceObject(
$referenceUid,
[],
$workspaceId === 0
);
$fileReferences[$fileReference->getUid()] = $fileReference;
} catch (FileDoesNotExistException $e) {
/**
* We just catch the exception here
* Reasoning: There is nothing an editor or even admin could do
*/
} catch (\InvalidArgumentException $e) {
/**
* The storage does not exist anymore
* Log the exception message for admins as they maybe can restore the storage
*/
// @extensionScannerIgnoreLine
self::getLogger()->error($e->getMessage(), [
'table' => $tableName,
'fieldName' => $fieldName,
'referenceUid' => $referenceUid,
'exception' => $e,
]);
}
}
return $fileReferences;
}
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 static function getLogger(): LoggerInterface
{
return GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
}
protected static function getTcaSchema(string $tableName): ?TcaSchema
{
$schemaFactory = GeneralUtility::makeInstance(TcaSchemaFactory::class);
return $schemaFactory->has($tableName) ? $schemaFactory->get($tableName) : null;
}
protected function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
protected function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
}