vendor/elements/universal-license-bundle/src/EventListener/PreUpdateListener.php line 20

Open in your IDE?
  1. <?php
  2. namespace Elements\Bundle\UniversalLicenseBundle\EventListener;
  3. use Elements\Bundle\UniversalLicenseBundle\Model\DataObject\AbstractULicense;
  4. use Pimcore\Event\Model\ElementEventInterface;
  5. use Pimcore\Event\Model\DataObjectEvent;
  6. use Pimcore\Event\Model\AssetEvent;
  7. use Pimcore\Event\Model\DocumentEvent;
  8. use Pimcore\Model\Asset;
  9. use Pimcore\Model\DataObject\ULicense;
  10. use Pimcore\Model\DataObject;
  11. use Pimcore\Model\Document;
  12. use Pimcore\Model\Element\Tag;
  13. class PreUpdateListener
  14. {
  15.     const TAG_PARENT_NAME 'ULicenses';
  16.     public function onPreUpdate(ElementEventInterface $e): void
  17.     {
  18.         if ($e instanceof AssetEvent) {
  19.             $asset $e->getAsset();
  20.             if ($asset instanceof Asset) {
  21.                 $oldVersion Asset::getById($asset->getId(), true);
  22.                 $oldLicense $oldVersion $oldVersion->getProperty(AbstractULicense::LICENSE_PROPERTY) : null;
  23.                 $license $asset->getProperty(AbstractULicense::LICENSE_PROPERTY);
  24.                 if ($oldLicense instanceof ULicense && (!$license instanceof ULicense || ($license->getId() != $oldLicense->getId()))) {
  25.                     if ($tag $oldLicense->getTag()) {
  26.                         try {
  27.                             Tag::removeTagFromElement('asset'$asset->getId(), $tag);
  28.                         } catch (\Throwable $exception) {
  29.                             // ignore
  30.                         }
  31.                     }
  32.                 }
  33.                 if ($license instanceof ULicense) {
  34.                     if ($tag $license->getTag()) {
  35.                         try {
  36.                             Tag::addTagToElement('asset'$asset->getId(), $tag);
  37.                         } catch (\Throwable $exception) {
  38.                             // ignore
  39.                         }
  40.                     }
  41.                 }
  42.             }
  43.         } else if ($e instanceof DocumentEvent) {
  44.             $document $e->getDocument();
  45.             if ($document instanceof Document) {
  46.                 $oldVersion Document::getById($document->getId(), true);
  47.                 $oldLicense $oldVersion $oldVersion->getProperty(AbstractULicense::LICENSE_PROPERTY) : null;
  48.                 $license $document->getProperty(AbstractULicense::LICENSE_PROPERTY);
  49.                 if ($oldLicense instanceof ULicense && (!$license instanceof ULicense || ($license->getId() != $oldLicense->getId()))) {
  50.                     if ($tag $oldLicense->getTag()) {
  51.                         try {
  52.                             Tag::removeTagFromElement('document'$document->getId(), $tag);
  53.                         } catch (\Throwable $exception) {
  54.                             // ignore
  55.                         }
  56.                     }
  57.                 }
  58.                 if ($license instanceof ULicense) {
  59.                     if ($tag $license->getTag()) {
  60.                         try {
  61.                             Tag::addTagToElement('document'$document->getId(), $tag);
  62.                         } catch (\Throwable $exception) {
  63.                             throw $exception;
  64.                             // ignore
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.         } else if ($e instanceof DataObjectEvent) {
  70.             $object $e->getObject();
  71.             if ($object instanceof DataObject) {
  72.                 $oldVersion DataObject::getById($object->getId(), true);
  73.                 $oldLicense $oldVersion $oldVersion->getProperty(AbstractULicense::LICENSE_PROPERTY) : null;
  74.                 $license $object->getProperty(AbstractULicense::LICENSE_PROPERTY);
  75.                 if ($oldLicense instanceof ULicense && (!$license instanceof ULicense || ($license->getId() != $oldLicense->getId()))) {
  76.                     if ($tag $oldLicense->getTag()) {
  77.                         try {
  78.                             Tag::removeTagFromElement('object'$object->getId(), $tag);
  79.                         } catch (\Throwable $exception) {
  80.                             // ignore
  81.                         }
  82.                     }
  83.                 }
  84.                 if ($object instanceof ULicense) {
  85.                     $tag $object->getTag();
  86.                     if ($tag == '') {
  87.                         $tag $this->getTagByCode($object->getCode());
  88.                         $object->setTagId($tag->getId());
  89.                     }
  90.                 } else if ($license instanceof ULicense) {
  91.                     if ($tag $license->getTag()) {
  92.                         try {
  93.                             Tag::addTagToElement('object'$object->getId(), $tag);
  94.                         } catch (\Throwable $exception) {
  95.                             // ignore
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     /**
  103.      * @param $code
  104.      * @return Tag
  105.      */
  106.     private function getTagByCode($code): Tag
  107.     {
  108.         $tagParent $this->getTagParent();
  109.         $tags = new Tag\Listing();
  110.         $tags->addConditionParam('idPath LIKE "/' $tagParent->getId() . '/%"');
  111.         foreach ($tags->load() ?? [] as $tag) {
  112.             if ($tag->getName() == $code) {
  113.                 return $tag;
  114.             }
  115.         }
  116.         $tag = new Tag();
  117.         $tag->setName($code);
  118.         $tag->setParentId($tagParent->getId());
  119.         $tag->save();
  120.         return $tag;
  121.     }
  122.     private function getTagParent(): Tag
  123.     {
  124.         $tags = new Tag\Listing();
  125.         $tags->addConditionParam('name = "' self::TAG_PARENT_NAME '"');
  126.         $tags $tags->load();
  127.         if (!empty($tags)) {
  128.             $tagParent $tags[0];
  129.         } else {
  130.             $tagParent = new Tag();
  131.             $tagParent->setName(self::TAG_PARENT_NAME);
  132.             $tagParent->setParentId(0);
  133.             $tagParent->save();
  134.         }
  135.         return $tagParent;
  136.     }
  137. }