From a83bbef556d43863cb5758f8d2727a5500c5e9c4 Mon Sep 17 00:00:00 2001 From: Matthias Schuhmayer <38959016+mattamon@users.noreply.github.com> Date: Wed, 29 May 2024 10:54:33 +0200 Subject: [PATCH] Add Image Updater (#81) * Add focal point saving * Apply php-cs-fixer changes * Remove constructor --------- Co-authored-by: mattamon --- config/assets.yaml | 4 +- .../Attributes/Property/UpdateAssetImage.php | 36 ++++++++++ .../Request/UpdateAssetRequestBody.php | 2 + src/Asset/Schema/Type/Image/FocalPoint.php | 49 +++++++++++++ src/Asset/Schema/Type/Image/ImageData.php | 41 +++++++++++ src/Asset/Updater/Adapter/ImageAdapter.php | 69 +++++++++++++++++++ src/Updater/Adapter/PropertiesAdapter.php | 3 + src/Updater/Service/UpdateService.php | 4 +- 8 files changed, 203 insertions(+), 5 deletions(-) create mode 100644 src/Asset/Attributes/Property/UpdateAssetImage.php create mode 100644 src/Asset/Schema/Type/Image/FocalPoint.php create mode 100644 src/Asset/Schema/Type/Image/ImageData.php create mode 100644 src/Asset/Updater/Adapter/ImageAdapter.php diff --git a/config/assets.yaml b/config/assets.yaml index 79d494584..56c2d6799 100644 --- a/config/assets.yaml +++ b/config/assets.yaml @@ -28,5 +28,5 @@ services: Pimcore\Bundle\StudioBackendBundle\Asset\Service\DataServiceInterface: class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\DataService - - + Pimcore\Bundle\StudioBackendBundle\Asset\Updater\Adapter\ImageAdapter: + tags: [ 'pimcore.studio_backend.update_adapter' ] \ No newline at end of file diff --git a/src/Asset/Attributes/Property/UpdateAssetImage.php b/src/Asset/Attributes/Property/UpdateAssetImage.php new file mode 100644 index 000000000..d5d142c6d --- /dev/null +++ b/src/Asset/Attributes/Property/UpdateAssetImage.php @@ -0,0 +1,36 @@ +x; + } + + public function getY(): int + { + return $this->y; + } +} diff --git a/src/Asset/Schema/Type/Image/ImageData.php b/src/Asset/Schema/Type/Image/ImageData.php new file mode 100644 index 000000000..4374fd4c3 --- /dev/null +++ b/src/Asset/Schema/Type/Image/ImageData.php @@ -0,0 +1,41 @@ +focalPoint; + } +} diff --git a/src/Asset/Updater/Adapter/ImageAdapter.php b/src/Asset/Updater/Adapter/ImageAdapter.php new file mode 100644 index 000000000..d185a0a3c --- /dev/null +++ b/src/Asset/Updater/Adapter/ImageAdapter.php @@ -0,0 +1,69 @@ +checkFocalPoint($element, $data); + + } + + public function getIndexKey(): string + { + return self::INDEX_KEY; + } + + public function supportedElementTypes(): array + { + return [ + ElementTypes::TYPE_ASSET, + ]; + } + + private function checkFocalPoint(Image $image, array $data): void + { + if ( + !array_key_exists(self::INDEX_KEY, $data) || + !array_key_exists('focalPoint', $data[self::INDEX_KEY]) + ) { + $image->removeCustomSetting('focalPointX'); + $image->removeCustomSetting('focalPointY'); + return; + } + + $image->setCustomSetting('focalPointX', $data[self::INDEX_KEY]['focalPoint']['x']); + $image->setCustomSetting('focalPointY', $data[self::INDEX_KEY]['focalPoint']['y']); + } +} diff --git a/src/Updater/Adapter/PropertiesAdapter.php b/src/Updater/Adapter/PropertiesAdapter.php index 9f3621dd8..4027afec8 100644 --- a/src/Updater/Adapter/PropertiesAdapter.php +++ b/src/Updater/Adapter/PropertiesAdapter.php @@ -33,6 +33,9 @@ public function __construct(private PropertyRepositoryInterface $propertyReposit public function update(ElementInterface $element, array $data): void { + if (!array_key_exists($this->getIndexKey(), $data)) { + return; + } $this->propertyRepository->updateElementProperties($element, $data); } diff --git a/src/Updater/Service/UpdateService.php b/src/Updater/Service/UpdateService.php index b120aa3d2..6169e73c3 100644 --- a/src/Updater/Service/UpdateService.php +++ b/src/Updater/Service/UpdateService.php @@ -44,9 +44,7 @@ public function update(string $elementType, int $id, array $data): void $element = $this->getElement($this->serviceResolver, $elementType, $id); foreach ($this->adapterLoader->loadAdapters($elementType) as $adapter) { - if (array_key_exists($adapter->getIndexKey(), $data)) { - $adapter->update($element, $data); - } + $adapter->update($element, $data); } try {