vendor/indabasolutions/csrepresentationbundle/EventListener/DataObjectVersionListener.php line 135

Open in your IDE?
  1. <?php
  2. // created by: Cesar Bianchi
  3. namespace IndabaSolutions\CSRepresentationBundle\EventListener;
  4. use IndabaSolutions\CSRepresentationBundle\Service\ClassificationStoreElements\ClassificationStoreGroupService;
  5. use IndabaSolutions\CSRepresentationBundle\Service\ClassificationStoreElements\ClassificationStoreKeyService;
  6. use Pimcore\Event\Model\VersionEvent;
  7. use Pimcore\Event\Model\DataObjectEvent;
  8. use IndabaSolutions\CSRepresentationBundle\Model\DataObject\GenericParentClass;
  9. class DataObjectVersionListener
  10. {
  11.     protected ClassificationStoreGroupService $classificationStoreGroupService;
  12.     
  13.     protected ClassificationStoreKeyService $classificationStoreKeyService;
  14.     
  15.     protected $keyObjectClassName;
  16.     
  17.     protected $deleteGroupOnDeleteGeneric;
  18.     
  19.     protected $sincOnAddKeyObject;
  20.     
  21.     protected $sincOnAddGeneric;
  22.     
  23.     protected $deleteKeyOnDeleteKeyObject;
  24.     
  25.     protected $addKeyFromUI;
  26.     
  27.     protected $addGenericFromUI;
  28.     
  29.     protected $deleteKeyFromUI;
  30.     
  31.     protected $deleteGenericFromUI;
  32.     
  33.     protected $deleteGenericFromCS;
  34.     public function __construct(
  35.         ClassificationStoreGroupService $classificationStoreGroupService,
  36.         ClassificationStoreKeyService $classificationStoreKeyService,
  37.         $keyObjectClassName,
  38.         $deleteGroupOnDeleteGeneric,
  39.         $sincOnAddKeyObject,
  40.         $sincOnAddGeneric,
  41.         $deleteKeyOnDeleteKeyObject,
  42.         $addKeyFromUI,
  43.         $addGenericFromUI,
  44.         $deleteKeyFromUI,
  45.         $deleteGenericFromUI,
  46.         $deleteGenericFromCS
  47.     )
  48.     {
  49.         $this->classificationStoreGroupService $classificationStoreGroupService;
  50.         $this->classificationStoreKeyService $classificationStoreKeyService;
  51.         $this->keyObjectClassName $keyObjectClassName;
  52.         $this->deleteGroupOnDeleteGeneric $deleteGroupOnDeleteGeneric;
  53.         $this->sincOnAddKeyObject $sincOnAddKeyObject;
  54.         $this->sincOnAddGeneric $sincOnAddGeneric;
  55.         $this->deleteKeyOnDeleteKeyObject $deleteKeyOnDeleteKeyObject;
  56.         $this->addKeyFromUI $addKeyFromUI;
  57.         $this->addGenericFromUI $addGenericFromUI;
  58.         $this->deleteKeyFromUI $deleteKeyFromUI;
  59.         $this->deleteGenericFromUI $deleteGenericFromUI;
  60.         $this->deleteGenericFromCS $deleteGenericFromCS;
  61.     }
  62.     private function getTraceArray()
  63.     {
  64.         $e = new \Exception();
  65.         $trace $e->getTraceAsString();
  66.         return explode("\n"$trace);
  67.     }
  68.     private function IsInstanceOfKeyClass($dataObject)
  69.     {
  70.         $keyClass "Pimcore\Model\DataObject\\$this->keyObjectClassName";
  71.         $keyClass = new $keyClass;
  72.         return $dataObject instanceof $keyClass;
  73.     }
  74.     public function onPreSave(VersionEvent $event)
  75.     {
  76.         $dataObject $event->getVersion();
  77.         $dataObject $dataObject->getData();
  78.         $explodedTrace $this->getTraceArray();
  79.         $fromBundle str_contains($explodedTrace[12], "csrepresentationbundle");
  80.         if ($this->IsInstanceOfKeyClass($dataObject)) {
  81.             if (! $this->addKeyFromUI && (! $fromBundle)) {
  82.                 throw new \Exception("Cannot add Key from UI, please use Classification Store to edit.");
  83.             }
  84.         } elseif ($dataObject instanceof GenericParentClass) {
  85.             if (! $this->addGenericFromUI && ! $fromBundle) {
  86.                 throw new \Exception("Cannot add Generic from UI, please use Classification Store to edit.");
  87.             }
  88.         }
  89.     }
  90.     public function onPreDelete(DataObjectEvent $event)
  91.     {
  92.         $dataObject $event->getObject();
  93.         $explodedTrace $this->getTraceArray();
  94.         $fromBundle str_contains($explodedTrace[7], "csrepresentationbundle");
  95.         if ($this->IsInstanceOfKeyClass($dataObject)) {
  96.             if (! $this->deleteKeyFromUI && ! $fromBundle) {
  97.                 throw new \Exception("Cannot delete Key from UI, please use Classification Store to edit.");
  98.             }
  99.         } elseif ($dataObject instanceof GenericParentClass) {
  100.             if (! $this->deleteGenericFromUI && ! $fromBundle) {
  101.                 throw new \Exception("Cannot delete Generic from UI, please use Classification Store to edit.");
  102.             }
  103.         }
  104.     }
  105.     public function onPostSave(VersionEvent $event)
  106.     {
  107.         $dataObject $event->getVersion();
  108.         $dataObject $dataObject->getData();
  109.         $explodedTrace $this->getTraceArray();
  110.         $fromBundle str_contains($explodedTrace[7], "csrepresentationbundle") ||
  111.                         str_contains($explodedTrace[12], "csrepresentationbundle");
  112.         if ($this->sincOnAddGeneric && !$fromBundle && $dataObject instanceof GenericParentClass) {
  113.             $this->classificationStoreGroupService->sincGroupObjectKeys($dataObject);
  114.         } elseif ($this->sincOnAddKeyObject && !$fromBundle && $this->IsInstanceOfKeyClass($dataObject)) {
  115.             $this->classificationStoreKeyService->sincKeyObjects($dataObject);
  116.         }
  117.     }
  118.     public function onPostDelete(DataObjectEvent $event)
  119.     {
  120.         $dataObject $event->getObject();
  121.         if ($this->deleteGroupOnDeleteGeneric && $dataObject instanceof GenericParentClass) {
  122.             $this->classificationStoreGroupService->deleteGroupObject($dataObject);
  123.         }
  124.         if ($this->deleteKeyOnDeleteKeyObject && $this->IsInstanceOfKeyClass($dataObject)) {
  125.             $this->classificationStoreKeyService->deleteKeyObject($dataObject);
  126.         }
  127.     }
  128. }