Skip to content

Commit

Permalink
Merge pull request #8 from maikschneider/release-4.0.0
Browse files Browse the repository at this point in the history
release 4.0.0
  • Loading branch information
github-actions[bot] authored Sep 24, 2023
2 parents dc9ec10 + dd0bd21 commit 98f0190
Show file tree
Hide file tree
Showing 34 changed files with 952 additions and 1,714 deletions.
19 changes: 6 additions & 13 deletions Classes/DataProcessing/FocuspointProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\DataProcessing\FilesProcessor;
use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;

class FocuspointProcessor extends FilesProcessor
{

/**
* Inject image and decoded focus points into the template
*
* @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj
* @param array $contentObjectConfiguration
* @param array $processorConfiguration
* @param array $processedData
* @return array
*/
public function process(
Expand All @@ -31,14 +25,13 @@ public function process(
return $processedData;
}

/** @var TypoLinkCodecService $typoLinkCodecService */
$typoLinkCodecService = GeneralUtility::makeInstance(TypoLinkCodecService::class);
/** @var \TYPO3\CMS\Core\LinkHandling\TypoLinkCodecService $typoLinkCodecService */
$typoLinkCodecService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\LinkHandling\TypoLinkCodecService::class);

// the TCA is configured to use max. 1 image, however the file collector returns an array
foreach ($processedData['image'] as $key => $file) {

foreach ($processedData['image'] as $file) {
$points = $file->getProperty('focus_points') ?: '[]';
$points = json_decode($points, false);
$points = json_decode((string)$points, false);

foreach ($points as $point) {
$point->x *= 100;
Expand All @@ -50,13 +43,13 @@ public function process(
$point->textY = $point->y + ($point->height / 2);

foreach ($point as $fieldName => &$fieldValue) {

// in case of old typolink syntax (v2.3.3): replace link field with typolink value
if (is_object($fieldValue) && property_exists($fieldValue, 'key')) {
$newLink = 't3://' . $fieldValue->key . '?uid=' . $fieldValue->uid;
if (property_exists($fieldValue, 'target') && $fieldValue->target) {
$newLink .= ' ' . $fieldValue->target;
}

$fieldValue = $newLink;
}

Expand All @@ -69,6 +62,7 @@ public function process(
}
}
}

unset($fieldValue);
}

Expand All @@ -78,5 +72,4 @@ public function process(

return $processedData;
}

}
63 changes: 28 additions & 35 deletions Classes/Form/Element/InputFocuspointElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,45 @@
namespace Blueways\BwFocuspointImages\Form\Element;

use Blueways\BwFocuspointImages\Utility\HelperUtility;
use Exception;
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;
use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

class InputFocuspointElement extends AbstractFormElement
{

/**
* This will render an imageManipulation field
*
* @return array As defined in initializeResultArray() of AbstractNode
* @throws \Exception
* @throws Exception
*/
public function render(): array
{
$helperUtility = GeneralUtility::makeInstance(HelperUtility::class);
$resultArray = $this->initializeResultArray();
$parameterArray = $this->data['parameterArray'];
$config = HelperUtility::getConfigForFormElement($this->data['databaseRow']['pid'],
$parameterArray['fieldConf']['config']);
$config = $helperUtility->getConfigForFormElement(
$this->data['databaseRow']['pid'],
$parameterArray['fieldConf']['config']
);

// migrate saved focuspoints (old link fields to new syntax)
$this->migrateOldTypolinkSyntax($parameterArray, $config);

// get image
$file = $this->getFile($this->data['databaseRow'], $config['file_field']);
if (!$file) {
if (!$file instanceof File) {
return $resultArray;
}

$verionNumberUtility = GeneralUtility::makeInstance(VersionNumberUtility::class);
$version = $verionNumberUtility->convertVersionStringToArray($verionNumberUtility->getNumericTypo3Version());

$fieldInformationResult = $this->renderFieldInformation();
$fieldInformationHtml = $fieldInformationResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
Expand All @@ -55,24 +55,25 @@ public function render(): array
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);

$resultArray['additionalInlineLanguageLabelFiles'][] = 'EXT:bw_focuspoint_images/Resources/Private/Language/locallang_js.xlf';
$resultArray['requireJsModules'][] = [
'TYPO3/CMS/BwFocuspointImages/FocuspointWizard' => 'function(FocuspointWizard){top.require(["jquery-ui/draggable", "jquery-ui/resizable"], function() { FocuspointWizard.initializeTrigger(' . $version['version_main'] . '); }); }',
];
$resultArray['javaScriptModules'][] = JavaScriptModuleInstruction::create('@blueways/bw-focuspoint-images/FocuspointWizard.js')->instance();

$arguments = [
'fieldInformation' => $fieldInformationHtml,
'fieldControl' => $fieldControlHtml,
'fieldWizard' => $fieldWizardHtml,
'isAllowedFileExtension' => in_array(strtolower($file->getExtension()),
GeneralUtility::trimExplode(',', strtolower($config['allowedExtensions'])), true),
'isAllowedFileExtension' => in_array(
strtolower($file->getExtension()),
GeneralUtility::trimExplode(',', strtolower((string)$config['allowedExtensions'])),
true
),
'image' => $file,
'formEngine' => [
'field' => [
'id' => 'bwfocuspointwizard' . random_int(1, 9999),
'value' => $parameterArray['itemFormElValue'],
'name' => $parameterArray['itemFormElName']
'name' => $parameterArray['itemFormElName'],
],
'validation' => '[]'
'validation' => '[]',
],
'config' => $config,
'pid' => $this->data['databaseRow']['pid'],
Expand All @@ -98,32 +99,31 @@ public function render(): array

/**
* Migrate old typolink markup (v2.3.3) to the t3:// syntax
*
* @param array $parameterArray
* @param array $config
*/
protected function migrateOldTypolinkSyntax(array &$parameterArray, array $config): void
{
if (!$parameterArray['itemFormElValue'] || !is_array(json_decode($parameterArray['itemFormElValue'], true))) {
if (!$parameterArray['itemFormElValue'] || !is_array(json_decode((string)$parameterArray['itemFormElValue'], true))) {
return;
}

$itemFormElValue = json_decode($parameterArray['itemFormElValue'], true);
$itemFormElValue = json_decode((string)$parameterArray['itemFormElValue'], true);

if (!count($itemFormElValue)) {
if ($itemFormElValue === []) {
return;
}

$linkFields = array_filter($config['focusPoints']['singlePoint']['fields'], function ($point) {
$linkFields = array_filter($config['focusPoints']['singlePoint']['fields'] ?? [], static function ($point): bool {
return $point['type'] === 'link';
});

foreach ($itemFormElValue as $key => $item) {
foreach ($linkFields as $fieldName => $field) {
if (!isset($item[$fieldName]) || !is_array($item[$fieldName])) {
if (!isset($item[$fieldName])) {
continue;
}
if (!is_array($item[$fieldName])) {
continue;
}

// construct new link
$link = $item[$fieldName];
$target = $link['target'] ?: '-';
Expand All @@ -141,14 +141,12 @@ protected function migrateOldTypolinkSyntax(array &$parameterArray, array $confi
/**
* Get file object
*
* @param array $row
* @param string $fieldName
* @return File|null
*/
protected function getFile(array $row, $fieldName): ?File
{
$file = null;
$fileUid = !empty($row[$fieldName]) ? $row[$fieldName] : null;
$fileUid = empty($row[$fieldName]) ? null : $row[$fieldName];

if (is_array($fileUid) && isset($fileUid[0]['uid'])) {
$fileUid = $fileUid[0]['uid'];
Expand All @@ -159,18 +157,13 @@ protected function getFile(array $row, $fieldName): ?File
/** @var ResourceFactory $resourceFactory */
$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
$file = $resourceFactory->getFileObject($fileUid);
} catch (FileDoesNotExistException | \InvalidArgumentException $e) {
} catch (FileDoesNotExistException|\InvalidArgumentException $e) {
}
}

return $file;
}

/**
* @param array $focusPoints
* @param \TYPO3\CMS\Core\Resource\File $image
* @param int $pid
* @return string
*/
protected function getWizardUri(array $focusPoints, File $image, int $pid): string
{
$routeName = 'ajax_wizard_focuspoint';
Expand Down
46 changes: 23 additions & 23 deletions Classes/Form/Wizard/FocusPointWizard.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
<?php

declare(strict_types=1);

namespace Blueways\BwFocuspointImages\Form\Wizard;

use Blueways\BwFocuspointImages\Utility\HelperUtility;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Fluid\View\StandaloneView;

class FocusPointWizard
{
protected ConfigurationManager $configurationManager;

public function __construct(ConfigurationManager $configurationManager)
{
$this->configurationManager = $configurationManager;
}

/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return \TYPO3\CMS\Core\Http\JsonResponse
* @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws RouteNotFoundException
*/
public function getLinkWizardUrlAction(ServerRequestInterface $request): JsonResponse
{
Expand All @@ -31,15 +37,18 @@ public function getLinkWizardUrlAction(ServerRequestInterface $request): JsonRes
$inputName = $queryParams['inputName'];
$inputValue = $queryParams['inputValue'];
$pid = MathUtility::canBeInterpretedAsInteger($queryParams['pid']) ? (int)$queryParams['pid'] : 0;
$config = HelperUtility::getConfigForWizardAction($pid);
$helperUtility = GeneralUtility::makeInstance(HelperUtility::class);
$config = $helperUtility->getConfigForWizardAction($pid);

$linkBrowserArguments = [];
if (isset($config['fields'][$fieldName]['linkPopup']['blindLinkOptions'])) {
$linkBrowserArguments['blindLinkOptions'] = $config['fields'][$fieldName]['linkPopup']['blindLinkOptions'];
}

if (isset($config['fields'][$fieldName]['linkPopup']['blindLinkFields'])) {
$linkBrowserArguments['blindLinkFields'] = $config['fields'][$fieldName]['linkPopup']['blindLinkFields'];
}

if (isset($config['fields'][$fieldName]['linkPopup']['allowedExtensions'])) {
$linkBrowserArguments['allowedExtensions'] = $config['fields'][$fieldName]['linkPopup']['allowedExtensions'];
}
Expand All @@ -53,29 +62,24 @@ public function getLinkWizardUrlAction(ServerRequestInterface $request): JsonRes
'field' => $inputName,
'formName' => 'editform',
'itemName' => $inputName,
'hmac' => GeneralUtility::hmac('focusPointForm' . $inputName, 'wizard_js')
'hmac' => GeneralUtility::hmac('focusPointForm' . $inputName, 'wizard_js'),
],
];

/** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$url = (string)$uriBuilder->buildUriFromRoute('wizard_link', $urlParameters);

$preview = [];
if ($inputValue) {
$preview = HelperUtility::getLinkExplanation($queryParams['inputValue']);
$preview = $helperUtility->getLinkExplanation($queryParams['inputValue']);
}

return new JsonResponse([
'url' => $url,
'preview' => $preview
'preview' => $preview,
]);
}

/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return \TYPO3\CMS\Core\Http\Response
*/
public function getWizardAction(ServerRequestInterface $request): Response
{
$response = new Response();
Expand All @@ -84,16 +88,14 @@ public function getWizardAction(ServerRequestInterface $request): Response
return $response->withStatus(403);
}

$typoScript = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
$templateView = GeneralUtility::makeInstance(StandaloneView::class);
$templateView->setLayoutRootPaths(['EXT:bw_focuspoint_images/Resources/Private/Layouts']);
$templateView->setPartialRootPaths(['EXT:bw_focuspoint_images/Resources/Private/Partials']);
$templateView->setTemplateRootPaths(['EXT:bw_focuspoint_images/Resources/Private/Templates']);
$templateView->setLayoutRootPaths($typoScript['plugin.']['tx_bwfocuspointimages.']['view.']['layoutRootPaths.']);
$templateView->setPartialRootPaths($typoScript['plugin.']['tx_bwfocuspointimages.']['view.']['partialRootPaths.']);
$templateView->setTemplateRootPaths($typoScript['plugin.']['tx_bwfocuspointimages.']['view.']['templateRootPaths.']);
$templateView->setTemplate('FocuspointWizard');

$verionNumberUtility = GeneralUtility::makeInstance(VersionNumberUtility::class);
$version = $verionNumberUtility->convertVersionStringToArray($verionNumberUtility->getNumericTypo3Version());

$queryParams = json_decode($request->getQueryParams()['arguments'], true);
$queryParams = json_decode((string)$request->getQueryParams()['arguments'], true);
$fileUid = $queryParams['image'];
$image = null;
if (MathUtility::canBeInterpretedAsInteger($fileUid)) {
Expand All @@ -109,7 +111,6 @@ public function getWizardAction(ServerRequestInterface $request): Response
$viewData = [
'image' => $image,
'focusPoints' => $queryParams['focusPoints'],
'typo3Version' => $version['version_main']
];
$templateView->assignMultiple($viewData);
$content = $templateView->render();
Expand All @@ -122,7 +123,6 @@ public function getWizardAction(ServerRequestInterface $request): Response
* Check if hmac signature is correct
*
* @param ServerRequestInterface $request the request with the GET parameters
* @return bool
*/
protected function isSignatureValid(ServerRequestInterface $request): bool
{
Expand Down
Loading

0 comments on commit 98f0190

Please sign in to comment.