Skip to content

Commit

Permalink
Add Image Updater (#81)
Browse files Browse the repository at this point in the history
* Add focal point saving

* Apply php-cs-fixer changes

* Remove constructor

---------

Co-authored-by: mattamon <mattamon@users.noreply.github.com>
  • Loading branch information
mattamon and mattamon authored May 29, 2024
1 parent a7068f7 commit a83bbef
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 5 deletions.
4 changes: 2 additions & 2 deletions config/assets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]
36 changes: 36 additions & 0 deletions src/Asset/Attributes/Property/UpdateAssetImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Attributes\Property;

use OpenApi\Attributes\Property;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Image\ImageData;

/**
* @internal
*/
final class UpdateAssetImage extends Property
{
public function __construct()
{
parent::__construct(
'image',
ref: ImageData::class,
type: 'object',
nullable: true,
);
}
}
2 changes: 2 additions & 0 deletions src/Asset/Attributes/Request/UpdateAssetRequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioBackendBundle\Asset\Attributes\Property\UpdateAssetImage;
use Pimcore\Bundle\StudioBackendBundle\Property\Attributes\Property\UpdateElementProperties;

/**
Expand All @@ -37,6 +38,7 @@ public function __construct()
new Property('data',
properties: [
new UpdateElementProperties(),
new UpdateAssetImage(),
],
type: 'object',
),
Expand Down
49 changes: 49 additions & 0 deletions src/Asset/Schema/Type/Image/FocalPoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Image;

use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;

/**
* @internal
*/
#[Schema(
title: 'FocalPoint',
type: 'object'
)]
final readonly class FocalPoint
{

public function __construct(
#[Property(description: 'x', type: 'integer', example: 50)]
private int $x,
#[Property(description: 'y', type: 'integer', example: 50)]
private int $y,
) {
}

public function getX(): int
{
return $this->x;
}

public function getY(): int
{
return $this->y;
}
}
41 changes: 41 additions & 0 deletions src/Asset/Schema/Type/Image/ImageData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Image;

use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;

/**
* @internal
*/
#[Schema(
title: 'ImageData',
type: 'object'
)]
final readonly class ImageData
{
public function __construct(
#[Property(ref: FocalPoint::class, description: 'focalPoint', type: 'object')]
private array $focalPoint,
) {
}

public function getFocalPoint(): array
{
return $this->focalPoint;
}
}
69 changes: 69 additions & 0 deletions src/Asset/Updater/Adapter/ImageAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Updater\Adapter;

use Pimcore\Bundle\StudioBackendBundle\Updater\Adapter\UpdateAdapterInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\Element\ElementInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

/**
* @internal
*/
#[AutoconfigureTag('pimcore.studio_backend.update_adapter')]
final readonly class ImageAdapter implements UpdateAdapterInterface
{
private const INDEX_KEY = 'image';

public function update(ElementInterface $element, array $data): void
{
if (!$element instanceof Image) {
return;
}

$this->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']);
}
}
3 changes: 3 additions & 0 deletions src/Updater/Adapter/PropertiesAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 1 addition & 3 deletions src/Updater/Service/UpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a83bbef

Please sign in to comment.