Project

General

Profile

Bug #88021 » ImageViewHelper.php

Danilo Caccialanza, 2020-06-05 09:41

 
<?php

namespace TICH\Theme\ViewHelpers\Uri;
use PDO;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper as CoreImageViewHelper;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;

class ImageViewHelper extends CoreImageViewHelper
{

use CompileWithRenderStatic;


public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('table', 'string', 'Table of contents', true);
$this->registerArgument('fieldName', 'string', 'Image content field name', true);
$this->registerArgument('currentContentUid', 'int', 'Content uid of versioned record', true);
$this->registerArgument('currentImage', 'object', 'Current image', true);
}

/**
* @return mixed
*/
protected static function getCurrentWorkspaceId()
{
return GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('workspace', 'id');
}

/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
*
* @return string
* @throws \Exception
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {

$table = $arguments['table'];
$fieldName = $arguments['fieldName'];
$versionedContentUid = $arguments['currentContentUid'];
$currentImageReference = $arguments['currentImage'];

if (self::getCurrentWorkspaceId() > 0) {
$versionedUrl = self::getVersionedImageUrl($table, $fieldName, $versionedContentUid);
if (!$versionedUrl) {
$defaultBehavior = true;
} else {
return $versionedUrl;
}
} else {
$defaultBehavior = true;
}

if ($defaultBehavior) {
$arguments['image'] = $currentImageReference;
return parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);
}
}

/**
* Get original content uid from versioned id
*
* @param string $table
* @param string $fieldName
* @param int $currentContentId is the versioned content uid (if in WORKSPACE)
*
* @return string|false
*
* @throws \TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
*/
protected static function getVersionedImageUrl(string $table, string $fieldName, int $currentContentId)
{
$contentLiveRecord = BackendUtility::getRecord($table, $currentContentId,
'pid, t3ver_oid, t3ver_wsid, t3ver_id');

//fetch live record related to versioned record
$wsRecordDraft = self::getWsRecordDraft($currentContentId, $fieldName, self::getCurrentWorkspaceId(),
$contentLiveRecord['pid']);

if (self::isAWsRecord($wsRecordDraft)) {
$imageUrl = self::getFilePublicUrl($wsRecordDraft['uid_local']);
return $imageUrl;
}
return false;
}


private static function isAWsRecord($contentLiveRecord)
{
return $contentLiveRecord &&
$contentLiveRecord['t3ver_oid'] > 0 &&
$contentLiveRecord['t3ver_wsid'] > 0 &&
$contentLiveRecord['t3ver_id'] > 0;
}


protected static function getWsRecordDraft($fileReferenceUid, $fieldName, $wsId, $pid)
{

$queryBuilder = self::getQueryBuilderForTable('sys_file_reference');

// set table and where clause
$queryBuilder
->select(...GeneralUtility::trimExplode(',', 'fr.uid,fr.uid_local', true))
->from('sys_file_reference', 'fr')
->where($queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('fr.uid_foreign',
$queryBuilder->createNamedParameter((int)$fileReferenceUid, PDO::PARAM_INT)),
$queryBuilder->expr()->eq('fr.pid',
$queryBuilder->createNamedParameter((int)$pid, PDO::PARAM_INT)),
$queryBuilder->expr()->eq('fr.t3ver_wsid',
$queryBuilder->createNamedParameter((int)$wsId, PDO::PARAM_INT)),
$queryBuilder->expr()->eq('fr.t3ver_state', 1),
$queryBuilder->expr()->eq('fr.fieldname',
$queryBuilder->createNamedParameter($fieldName, PDO::PARAM_STR))
));

$row = $queryBuilder->execute()->fetch();
if ($row) {
$wsVersionRecord = BackendUtility::getWorkspaceVersionOfRecord($wsId, 'sys_file_reference', $row['uid']);
return $wsVersionRecord;
}
return false;
}


/**
* @param string $table
*
* @return QueryBuilder
*/
protected static function getQueryBuilderForTable($table)
{
return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
}


/**
* Get file using FAL APIs.
*
* @param $fileUid
*
* @return string|null
* @throws \TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
*/
protected static function getFilePublicUrl($fileUid)
{
if ($fileUid > 0) {
$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
$file = $resourceFactory->getFileObject($fileUid);
if ($file) {
return $file->getPublicUrl();
}
}
return '';
}

}
(1-1/2)