<?php
/**
* Dieser Quellcode ist geistiges Eigentum der Weblizards GmbH
* und darf ohne vorheriges schriftliches Einverständnis nicht
* vervielfältigt werden.
*
* @copyright Copyright (c) Weblizards GmbH <https://www.weblizards.de>
* @license https://www.gnu.org/licenses/gpl-3.0 GPLv3
*/
namespace App\EventListener;
use App\Message\UpdatePackShotImageMessage;
use App\Message\UpdatePlateShotImageMessage;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject;
use Symfony\Component\Messenger\MessageBusInterface;
class DamListener
{
use ListenerTrait;
public static bool $ignoreEvents = false;
public function __construct(private readonly MessageBusInterface $messageBus)
{
}
public function onObjectPostUpdate(DataObjectEvent $event)
{
if (static::$ignoreEvents) {
return;
}
if ($event->hasArgument('saveVersionOnly')) {
return;
}
$object = $event->getObject();
/** @var DataObject\Product[] $products */
$products = [];
if ($object instanceof DataObject\Recipe || $object instanceof DataObject\Erpitem) {
$products = $this->getDependentProducts($object);
} elseif ($object instanceof DataObject\Product) {
$products = [$object];
} else {
// none of our business
return;
}
foreach ($products as $product) {
if ($product->getType() == DataObject\AbstractObject::OBJECT_TYPE_OBJECT) {
// is container: update plate image metadata and of all variants
$this->handleContainer($product);
foreach ($product->getVariants() as $variant) {
$this->handleVariant($variant);
}
} else {
// is variant
$this->handleVariant($product);
}
}
}
protected function handleContainer(DataObject\Product $container): void
{
// plate image
if (($image = $container->getPlateImage()) instanceof Asset\Image) {
$this->messageBus->dispatch(new UpdatePlateShotImageMessage($image->getId(), $container->getId()));
}
}
protected function handleVariant(DataObject\Product $variant): void
{
// packshot image
foreach (\Pimcore\Tool::getValidLanguages() as $language) {
$prevGetFallbackValues = DataObject\Localizedfield::getGetFallbackValues();
try {
DataObject\Localizedfield::setGetFallbackValues(false);
if (($image = $variant->getItemMain($language)) !== null) {
$this->messageBus->dispatch(new UpdatePackShotImageMessage($image->getId(), $variant->getId(), $language));
}
} finally {
DataObject\Localizedfield::setGetFallbackValues($prevGetFallbackValues);
}
}
}
}