src/EventListener/PreUpdateObjectListener.php line 112

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Message\AveryCategoryConcatData;
  4. use App\Message\AveryHandleProductConcat;
  5. use App\Service\AverySettingsService;
  6. use Masterminds\HTML5\Parser\EventHandler;
  7. use Pimcore\Console\Application;
  8. use Pimcore\Event;
  9. use Pimcore\Log\ApplicationLogger;
  10. use Pimcore\Model\DataObject\AveryProdBase;
  11. use Pimcore\Model\DataObject\AveryRegion;
  12. use Pimcore\Model\DataObject\Concrete;
  13. use Pimcore\Model\DataObject\ProductCategory;
  14. use Pimcore\Model\Notification\Service\NotificationService;
  15. use Pimcore\Model\User;
  16. use Pimcore\Tool\Console;
  17. use Symfony\Component\Console\Input\ArrayInput;
  18. use Symfony\Component\Console\Output\BufferedOutput;
  19. use Symfony\Component\HttpKernel\KernelInterface;
  20. use Symfony\Component\Workflow\Event\TransitionEvent;
  21. use Symfony\Component\Workflow\Event\EnteredEvent;
  22. use Pimcore\Event\Model\ElementEventInterface;
  23. use Symfony\Component\Messenger\MessageBusInterface;
  24. use Pimcore\Model\DataObject\ProductValueRangeOption;
  25. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  26. use Pimcore\Mail;
  27. use Symfony\Contracts\Translation\TranslatorInterface;
  28. class PreUpdateObjectListener
  29. {
  30.     private static $storedObj;
  31.     private static $triggered;
  32.     private static $from;
  33.     private static $to;
  34.     private static $concatGenTriggered;
  35.     private static $emailSent false;
  36.     private $translator;
  37.     private $messageBus;
  38.     private $logger;
  39.     private $notificationService;
  40.     private AverySettingsService $settingsService;
  41.     private $enableConcatGeneration;
  42.     public function __construct(ApplicationLogger $loggerNotificationService $notificationServiceTranslatorInterface $translatorMessageBusInterface $messageBusAverySettingsService $settingsService)
  43.     {
  44.         $this->translator $translator;
  45.         $this->logger $logger;
  46.         $this->notificationService $notificationService;
  47.         $this->messageBus $messageBus;
  48.         $this->settingsService $settingsService;
  49.         $this->enableConcatGeneration $this->settingsService->getDataFromWebsiteSetting("averyChkEnableAutoConcatGeneration""de")->getData();
  50.     }
  51.     public function onObjectPreDelete(ElementEventInterface $e)
  52.     {
  53.         $object $e->getObject();
  54.         if ($object instanceof ProductValueRangeOption) {
  55.             if ($object == null || $object->getParent() == null) {
  56.                 return;
  57.             }
  58.             $fieldDefinition $object->getParent()->getParent();
  59.             if ($fieldDefinition == null) {
  60.                 return;
  61.             }
  62.             $cache = new FilesystemAdapter();
  63.             $cache_hash '_' md5($fieldDefinition->getName());
  64.             $item $cache->getItem($cache_hash);
  65.             $item->expiresAfter(0);
  66.             $cache->save($item);
  67.         }
  68.     }
  69.     public function onObjectPreUpdate(ElementEventInterface $e)
  70.     {
  71.         $object $e->getObject();
  72.         if ($object instanceof ProductValueRangeOption) {
  73.             if ($object == null || $object->getParent() == null) {
  74.                 return;
  75.             }
  76.             $fieldDefinition $object->getParent()->getParent();
  77.             if ($fieldDefinition == null) {
  78.                 return;
  79.             }
  80.             $cache = new FilesystemAdapter();
  81.             $cache_hash '_' md5($fieldDefinition->getName());
  82.             $item $cache->getItem($cache_hash);
  83.             $item->expiresAfter(0);
  84.             $cache->save($item);
  85.         }
  86.         if ($object instanceof AveryProdBase && self::$concatGenTriggered == false) {
  87.             $productId $object->getId();
  88.             if (!$e->hasArgument("saveVersionOnly")) {
  89.                 try {
  90.                     $object->averyGenerateConcats($object$this->translator$this->logger$this->settingsServicefalse);
  91.                 } catch (\Throwable $t) {
  92.                     $this->logger->debug("Generating concats failed for product " $productId " with key " $object->getKey() . " and error: " $t);
  93.                 }
  94.                 self::$concatGenTriggered true;
  95.             }
  96.         }
  97.     }
  98.     public function onObjectPostUpdate(ElementEventInterface $e)
  99.     {
  100.         if ($this->enableConcatGeneration == ('1')) {
  101.             $object $e->getObject();
  102.             if ($object instanceof ProductCategory || $object instanceof AveryRegion) {
  103.                 $categoryId $object->getId();
  104.                 if (!$e->hasArgument("saveVersionOnly")) {
  105.                     $message = new AveryCategoryConcatData($categoryId);
  106.                     $this->messageBus->dispatch($message);
  107.                     $this->logger->debug("AVERY CATEGORY CONCAT | Added Category " $object->getKey() . " into the category generator queue");
  108.                 }
  109.             }
  110.         }
  111.     }
  112.     public function onWorkflowUpdate(TransitionEvent $event)
  113.     {
  114.         self::$triggered true;
  115.         $obj $event->getSubject();
  116.         $newState $obj->getWorkflowState();
  117.         self::$from $newState;
  118.     }
  119.     public function onWorkflowEntered(EnteredEvent $event)
  120.     {
  121.         self::$triggered true;
  122.         $obj $event->getSubject();
  123.         $newState $obj->getWorkflowState();
  124.         self::$to $newState;
  125.     }
  126.     public function onPimWorkflow(Event $event)
  127.     {
  128.         $obj $event->getSubject();
  129.         $newState $obj->getWorkflowState();
  130.     }
  131. }