<?php
namespace App\EventListener;
use App\Message\AveryCategoryConcatData;
use App\Message\AveryHandleProductConcat;
use App\Service\AverySettingsService;
use Masterminds\HTML5\Parser\EventHandler;
use Pimcore\Console\Application;
use Pimcore\Event;
use Pimcore\Log\ApplicationLogger;
use Pimcore\Model\DataObject\AveryProdBase;
use Pimcore\Model\DataObject\AveryRegion;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\ProductCategory;
use Pimcore\Model\Notification\Service\NotificationService;
use Pimcore\Model\User;
use Pimcore\Tool\Console;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Workflow\Event\TransitionEvent;
use Symfony\Component\Workflow\Event\EnteredEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Pimcore\Model\DataObject\ProductValueRangeOption;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Pimcore\Mail;
use Symfony\Contracts\Translation\TranslatorInterface;
class PreUpdateObjectListener
{
private static $storedObj;
private static $triggered;
private static $from;
private static $to;
private static $concatGenTriggered;
private static $emailSent = false;
private $translator;
private $messageBus;
private $logger;
private $notificationService;
private AverySettingsService $settingsService;
private $enableConcatGeneration;
public function __construct(ApplicationLogger $logger, NotificationService $notificationService, TranslatorInterface $translator, MessageBusInterface $messageBus, AverySettingsService $settingsService)
{
$this->translator = $translator;
$this->logger = $logger;
$this->notificationService = $notificationService;
$this->messageBus = $messageBus;
$this->settingsService = $settingsService;
$this->enableConcatGeneration = $this->settingsService->getDataFromWebsiteSetting("averyChkEnableAutoConcatGeneration", "de")->getData();
}
public function onObjectPreDelete(ElementEventInterface $e)
{
$object = $e->getObject();
if ($object instanceof ProductValueRangeOption) {
if ($object == null || $object->getParent() == null) {
return;
}
$fieldDefinition = $object->getParent()->getParent();
if ($fieldDefinition == null) {
return;
}
$cache = new FilesystemAdapter();
$cache_hash = '_' . md5($fieldDefinition->getName());
$item = $cache->getItem($cache_hash);
$item->expiresAfter(0);
$cache->save($item);
}
}
public function onObjectPreUpdate(ElementEventInterface $e)
{
$object = $e->getObject();
if ($object instanceof ProductValueRangeOption) {
if ($object == null || $object->getParent() == null) {
return;
}
$fieldDefinition = $object->getParent()->getParent();
if ($fieldDefinition == null) {
return;
}
$cache = new FilesystemAdapter();
$cache_hash = '_' . md5($fieldDefinition->getName());
$item = $cache->getItem($cache_hash);
$item->expiresAfter(0);
$cache->save($item);
}
if ($object instanceof AveryProdBase && self::$concatGenTriggered == false) {
$productId = $object->getId();
if (!$e->hasArgument("saveVersionOnly")) {
try {
$object->averyGenerateConcats($object, $this->translator, $this->logger, $this->settingsService, false);
} catch (\Throwable $t) {
$this->logger->debug("Generating concats failed for product " . $productId . " with key " . $object->getKey() . " and error: " . $t);
}
self::$concatGenTriggered = true;
}
}
}
public function onObjectPostUpdate(ElementEventInterface $e)
{
if ($this->enableConcatGeneration == ('1')) {
$object = $e->getObject();
if ($object instanceof ProductCategory || $object instanceof AveryRegion) {
$categoryId = $object->getId();
if (!$e->hasArgument("saveVersionOnly")) {
$message = new AveryCategoryConcatData($categoryId);
$this->messageBus->dispatch($message);
$this->logger->debug("AVERY CATEGORY CONCAT | Added Category " . $object->getKey() . " into the category generator queue");
}
}
}
}
public function onWorkflowUpdate(TransitionEvent $event)
{
self::$triggered = true;
$obj = $event->getSubject();
$newState = $obj->getWorkflowState();
self::$from = $newState;
}
public function onWorkflowEntered(EnteredEvent $event)
{
self::$triggered = true;
$obj = $event->getSubject();
$newState = $obj->getWorkflowState();
self::$to = $newState;
}
public function onPimWorkflow(Event $event)
{
$obj = $event->getSubject();
$newState = $obj->getWorkflowState();
}
}