src/EventListener/DistributorListener.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3.   
  4. use Pimcore\Model\Element\ValidationException;
  5. use Pimcore\Model\DataObject\Distributor;
  6. use Pimcore\Event\Model\ElementEventInterface;
  7. use Pimcore\Event\Model\DataObjectEvent;
  8. class DistributorListener 
  9. {
  10.     /**
  11.      * @param ElementEventInterface $event
  12.      */
  13.     public function onPreUpdate(ElementEventInterface $event)
  14.     {
  15.         if ($event instanceof DataObjectEvent) {
  16.             $object $event->getObject();
  17.             if (!$object instanceof Distributor) {
  18.                 return;
  19.             }
  20.             // Called to check validation
  21.             $this->mandatoryValidation($object);
  22.         }
  23.     }
  24.     /**
  25.      * @param ElementEventInterface $event
  26.      */
  27.     public function onPreAdd(ElementEventInterface $event)
  28.     {
  29.         if ($event instanceof DataObjectEvent) {
  30.             $object $event->getObject();
  31.             $currentUser \Pimcore\Tool\Admin::getCurrentUser();
  32.             if (!$object instanceof Distributor || $currentUser) {
  33.                 return;
  34.             }
  35.             // Called to check validation
  36.             $this->mandatoryValidation($object);
  37.         }
  38.     }
  39.     /**
  40.      * @param object $object
  41.      */
  42.     public function mandatoryValidation($object)
  43.     {
  44.         $errors = [];
  45.         if(empty($object->getName())) 
  46.         {
  47.             $errors[] ='Name is mandatory.';
  48.         }
  49.         if (!empty($object->getDistributorCode())) {
  50.             $distributorCodeUnique = new \Pimcore\Model\DataObject\Distributor\Listing();
  51.             if($object->getId()) {
  52.                 $distributorCodeUnique->setCondition('oo_id !=' $object->getId() . ' and distributorCode = "' $object->getDistributorCode() . '"');
  53.             } else {
  54.                 $distributorCodeUnique->setCondition('distributorCode = "' $object->getDistributorCode() . '"');
  55.             }
  56.             $distributorCodeUnique->setUnpublished(true);
  57.             $distributorCodeUnique->setLimit(1);
  58.             $distributorCodeUniqueObject $distributorCodeUnique->current();
  59.             if ($distributorCodeUniqueObject instanceof \Pimcore\Model\DataObject\Distributor && $distributorCodeUniqueObject->getDistributorCode() == $object->getDistributorCode()) {
  60.                 $errors[] = 'Distributor code already exist in the distributor id ' $distributorCodeUniqueObject->getId();
  61.             }
  62.         }
  63.         if ($errors) {
  64.             throw new ValidationException(implode('<br>'$errors));
  65.         }
  66.         if (!isset($_GET['task'])) {
  67.             return;
  68.         }
  69.         $errors = [];
  70.         if (!empty($object->getDistributorCode())) {
  71.             if (strlen($object->getDistributorCode()) < 4) {
  72.                 $errors[] = 'Distributor code must be more than 3 characters long.';
  73.             }
  74.         } elseif (empty($object->getDistributorCode())) {
  75.             $errors[] = 'Distributor code is mandatory';
  76.         }
  77.         if ($errors) {
  78.             throw new ValidationException(implode('<br>'$errors));
  79.         }
  80.     }
  81. }