src/EventListener/ModelListener.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Pimcore\Model\Element\ValidationException;
  4. use Pimcore\Model\DataObject\Model;
  5. use Pimcore\Event\Model\ElementEventInterface;
  6. use Pimcore\Event\Model\DataObjectEvent;
  7. use Symfony\Component\EventDispatcher\GenericEvent;
  8. use Pimcore\Model\DataObject;
  9. use Pimcore\Event\BundleManager\PathsEvent;
  10. class ModelListener 
  11. {
  12.     /**
  13.      * @param ElementEventInterface $event
  14.      */
  15.     public function onPreUpdate(ElementEventInterface $event)
  16.     {
  17.         if ($event instanceof DataObjectEvent) {
  18.             $object $event->getObject();
  19.             if (!$object instanceof Model) {
  20.                 return;
  21.             }
  22.             // Called to check validation
  23.             $this->mandatoryValidation($object);
  24.             $this->addModelWithStore($object);
  25.         }
  26.     }
  27.     public function addModelWithStore($object) {
  28.         $modelId $object->getId();
  29.         $modelName $object->getName();
  30.         $stores = new DataObject\Store\Listing();
  31.         $stores->setCondition("o_published = ?", [true]);
  32.         $stores->load();
  33.     
  34.         $db \Pimcore\Db::getConnection('writer');
  35.         foreach ($stores as $store) {
  36.             $storeId $store->getId();
  37.             
  38.             $sql "SELECT model FROM `app_model_store` WHERE storeId = :storeId AND modelId = :modelId";
  39.             $stmt $db->prepare($sql);
  40.             $stmt->execute([':storeId' => $storeId':modelId' => $modelId]);
  41.             $result $stmt->fetchColumn();
  42.     
  43.             if (!$result) {
  44.                 $sql "INSERT INTO `app_model_store` (storeId, modelId, model) VALUES (:storeId, :modelId, :model)";
  45.                 $stmt $db->prepare($sql);
  46.                 $stmt->execute([':storeId' => $storeId':modelId' => $modelId':model' => $modelName]);
  47.             }
  48.         }
  49.     }    
  50.     /**
  51.      * Event trigger when post delete
  52.      * @param ElementEventInterface $event
  53.      */
  54.     public function onPostDelete(ElementEventInterface $event)
  55.     {
  56.         if ($event instanceof DataObjectEvent) {
  57.             $object $event->getObject();
  58.             if (!$object instanceof Model) {
  59.                 return;
  60.             }
  61.             $modelId $object->getId();
  62.             $db \Pimcore\Db::getConnection('writer');
  63.             $sql "DELETE FROM `app_model_store` WHERE modelId = :modelId";
  64.             $stmt $db->prepare($sql);
  65.             $stmt->execute([':modelId' => $modelId]);
  66.         }
  67.     }
  68.     /**
  69.      * @param ElementEventInterface $event
  70.      */
  71.     public function onPreAdd(ElementEventInterface $event)
  72.     {
  73.         if ($event instanceof DataObjectEvent) {
  74.             $object $event->getObject();
  75.             $currentUser \Pimcore\Tool\Admin::getCurrentUser();
  76.             if (!$object instanceof Model || $currentUser) {
  77.                 return;
  78.             }
  79.             // Called to check validation
  80.             $this->mandatoryValidationPreAdd($object);
  81.         }
  82.     }
  83.     /**
  84.      * @param object $object
  85.      */
  86.     public function mandatoryValidationPreAdd($object)
  87.     {
  88.         $errors = [];
  89.         if(empty($object->getName())) {
  90.             $errors[] = 'Name is mandatory';
  91.         }
  92.         if($errors) {
  93.             throw new ValidationException(implode('<br>'$errors));
  94.         }
  95.     }
  96.     /**
  97.      * @param object $object
  98.      */
  99.     public function mandatoryValidation($object)
  100.     {
  101.         $errors = [];
  102.         if(empty($object->getName())) {
  103.             $errors[] = 'Name is mandatory';
  104.         }
  105.         if($errors) {
  106.             throw new ValidationException(implode('<br>'$errors));
  107.         }
  108.         if(!isset($_GET['task'])){ 
  109.             return;
  110.         }
  111.         $errors = [];
  112.         if (!empty($object->getName()) && strlen($object->getName()) < 4) {
  113.             $errors[] ='Name must be more 4 characters or more.';
  114.         }
  115.         $brand $object->getBrand();
  116.         if(!empty($brand)) {
  117.             if($brand instanceof DataObject\Brand) {
  118.                 if(!$brand->isPublished()) {
  119.                     $errors[] = 'Please select a published Brand';
  120.                 }
  121.             }
  122.         } else {
  123.             $errors[] ='Brand is mandatory';
  124.         }
  125.         if (!empty($object->getHeaderDescription()) && strlen($object->getHeaderDescription()) < 10
  126.         {
  127.             $errors[] ='Header description must be more than 10 characters.';
  128.         }
  129.         if (!empty($object->getFooterDescription()) && strlen($object->getFooterDescription()) < 10
  130.         {
  131.             $errors[] ='Footer description must be more than 10 characters.';
  132.         }
  133.         if($errors) {
  134.             throw new ValidationException(implode('<br>'$errors));
  135.         }
  136.     }
  137.     /**
  138.      * @param GenericEvent $event
  139.      */
  140.     public function objectListBeforeListLoad(GenericEvent $event)
  141.     {
  142.         $list $event->getArgument('list');
  143.         $context $event->getArgument('context');
  144.         if(array_key_exists('class'$context) && $context['class'] == "Brand") {
  145.             $list->addConditionParam("id IN (SELECT oo_id FROM `object_BRD` WHERE o_published = 1)");
  146.         }
  147.         $event->setArgument('list'$list);
  148.     }
  149.     public function addJSFiles(PathsEvent $event)
  150.     {
  151.         $event->setPaths(
  152.             array_merge(
  153.                 $event->getPaths(),
  154.                 [
  155.                     '/admin-static/model/model.js'
  156.                 ]
  157.             )
  158.         );
  159.     }
  160.     public function addCSSFiles(PathsEvent $event)
  161.     {
  162.         $event->setPaths(
  163.             array_merge(
  164.                 $event->getPaths(),
  165.                 [
  166.                     '/admin-static/product/product.css'
  167.                 ]
  168.             )
  169.         );
  170.     }
  171. }