src/EventListener/MasterListener.php line 270

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Exception;
  4. use Pimcore\Model\Element\ValidationException;
  5. use Pimcore\Event\Model\DataObjectEvent;
  6. use Pimcore\Event\Model\ElementEventInterface;
  7. use Pimcore\Model\DataObject\Product;
  8. use Pimcore\Model\DataObject\Category;
  9. use Pimcore\Model\DataObject\Distributor;
  10. use Pimcore\Model\DataObject\Store;
  11. use Pimcore\Model\DataObject\Channel;
  12. use Pimcore\Model\DataObject\AmazonCategory;
  13. use Pimcore\Model\DataObject\Model;
  14. use Pimcore\Model\DataObject\Brand;
  15. use Pimcore\Model\DataObject\ERPImportConfiguration;
  16. use Pimcore\Model\DataObject\EnrichmentFilter;
  17. use Pimcore\Model\DataObject\OemEnrichmentFilter;
  18. use Pimcore\Model\DataObject\OEMFilter;
  19. use Pimcore\Model\DataObject\ExcelUpload;
  20. use Pimcore\Db;
  21. use Pimcore\Model\DataObject\OEMUpload;
  22. class MasterListener
  23. {
  24.     public function UpdateFolderOnCreate(ElementEventInterface $event)
  25.     {
  26.         // if ($event instanceof DataObjectEvent)
  27.         if ($event instanceof DataObjectEvent) {
  28.             $object $event->getObject();
  29.             if ($object instanceof Category || $object instanceof Distributor || $object instanceof Channel || $object instanceof AmazonCategory || $object instanceof Model || $object instanceof Brand || $object instanceof Store) {
  30.                 try {
  31.                     /** Master Folder Structure Start  */
  32.                     $path "Master Data/" $object->getClassName();
  33.                     $pathExists \Pimcore\Model\DataObject\Service::pathExists($path);
  34.                     if (empty($pathExists)) {
  35.                         $folder \Pimcore\Model\DataObject\Service::createFolderByPath($path);
  36.                     } else {
  37.                         $folder \Pimcore\Model\DataObject\Service::getElementByPath('object'$path);
  38.                     }
  39.                     if ($object instanceof Category && $object->getParentCategory()) {
  40.                         $object->setParentId($object->getParentCategory()->getId());
  41.                     } else {
  42.                         $object->setParentId($folder->getId());
  43.                     }
  44.                     $object->save();
  45.                     /** Master Folder Structure End   */
  46.                 } catch (Exception $e) {
  47.                     throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
  48.                 }
  49.             } elseif ($object instanceof ERPImportConfiguration) {
  50.                 try {
  51.                     $path "SFTP Configuration";
  52.                     $pathExists \Pimcore\Model\DataObject\Service::pathExists($path);
  53.                     if (empty($pathExists)) {
  54.                         $folder \Pimcore\Model\DataObject\Service::createFolderByPath($path);
  55.                     } else {
  56.                         $folder \Pimcore\Model\DataObject\Service::getElementByPath('object'$path);
  57.                     }
  58.                     $object->setParentId($folder->getId());
  59.                     $object->save();
  60.                 } catch (Exception $e) {
  61.                     throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
  62.                 }
  63.             } elseif ($object instanceof EnrichmentFilter) {
  64.                 try {
  65.                     $path "Enrichment Filters";
  66.                     $pathExists \Pimcore\Model\DataObject\Service::pathExists($path);
  67.                     if (empty($pathExists)) {
  68.                         $folder \Pimcore\Model\DataObject\Service::createFolderByPath($path);
  69.                     } else {
  70.                         $folder \Pimcore\Model\DataObject\Service::getElementByPath('object'$path);
  71.                     }
  72.                     $object->setParentId($folder->getId());
  73.                     $object->save();
  74.                 } catch (Exception $e) {
  75.                     throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
  76.                 }
  77.             } elseif ($object instanceof OEMFilter) {
  78.                 try {
  79.                     $path "OEM Filters";
  80.                     $pathExists \Pimcore\Model\DataObject\Service::pathExists($path);
  81.                     if (empty($pathExists)) {
  82.                         $folder \Pimcore\Model\DataObject\Service::createFolderByPath($path);
  83.                     } else {
  84.                         $folder \Pimcore\Model\DataObject\Service::getElementByPath('object'$path);
  85.                     }
  86.                     $object->setParentId($folder->getId());
  87.                     $object->save();
  88.                 } catch (Exception $e) {
  89.                     throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
  90.                 }
  91.             }  elseif ($object instanceof ExcelUpload) {
  92.                 try {
  93.                     $path "Enriched Excel";
  94.                     $pathExists \Pimcore\Model\DataObject\Service::pathExists($path);
  95.                     if (empty($pathExists)) {
  96.                         $folder \Pimcore\Model\DataObject\Service::createFolderByPath($path);
  97.                     } else {
  98.                         $folder \Pimcore\Model\DataObject\Service::getElementByPath('object'$path);
  99.                     }
  100.                     $object->setParentId($folder->getId());
  101.                     $object->save();
  102.                 } catch (Exception $e) {
  103.                     throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
  104.                 }
  105.             }
  106.             elseif ($object instanceof OemEnrichmentFilter) {
  107.                 try {
  108.                     $path "OEM Export";
  109.                     $pathExists \Pimcore\Model\DataObject\Service::pathExists($path);
  110.                     if (empty($pathExists)) {
  111.                         $folder \Pimcore\Model\DataObject\Service::createFolderByPath($path);
  112.                     } else {
  113.                         $folder \Pimcore\Model\DataObject\Service::getElementByPath('object'$path);
  114.                     }
  115.                     $object->setParentId($folder->getId());
  116.                     $object->save();
  117.                 } catch (Exception $e) {
  118.                     throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
  119.                 }
  120.             }
  121.         }
  122.     }
  123.     /**
  124.      * Event trigger when pre delete
  125.      * @param ElementEventInterface $event
  126.      */
  127.     public function onPreDelete(ElementEventInterface $event)
  128.     {
  129.         if ($event instanceof DataObjectEvent) {
  130.             $object $event->getObject();
  131.             $id $object->getId();
  132.             if ($object instanceof Category) {
  133.                 $products = new Product\Listing();
  134.                 $products->setUnpublished(true);
  135.                 $where "FIND_IN_SET('$id', category)";
  136.                 $products->setCondition($where);
  137.                 $products->load();
  138.                 if ($products->count() > 0) {
  139.                     throw new ValidationException('You can\'t delete the category ' $object->getName() . ' because it is associated with the products. Firstly you have to remove assoication with products then can delete.');
  140.                 }
  141.             } elseif ($object instanceof Channel) {
  142.                 $products = new Product\Listing();
  143.                 $products->setUnpublished(true);
  144.                 $sql "SELECT productId FROM product_prices WHERE channelId = ?";
  145.                 $statement Db::get()->prepare($sql);
  146.                 $statement->bindValue(1$object->getId(), \PDO::PARAM_INT);
  147.                 $statement->execute();
  148.                 $rows $statement->fetchAll();
  149.                 $productIds = [];
  150.                 foreach ($rows as $row) {
  151.                     $productIds[] = $row['productId'];
  152.                 }
  153.                 if (!empty($productIds)) {
  154.                     $productIdList implode(','$productIds);
  155.                     $where "oo_id IN ($productIdList)";
  156.                     $products->setCondition($where);
  157.                     $products->load();
  158.                     if ($products->count() > 0) {
  159.                         throw new ValidationException('You can\'t delete the channel ' $object->getName() . ' because it is associated with products. First, you have to remove the association with products before you can delete the channel.');
  160.                     }
  161.                 }
  162.             } elseif ($object instanceof Distributor) {
  163.                 $products = new Product\Listing();
  164.                 $products->setUnpublished(true);
  165.                 $sql "SELECT productId FROM product_distributor_prices WHERE distributorId = ?";
  166.                 $statement Db::get()->prepare($sql);
  167.                 $statement->bindValue(1$object->getId(), \PDO::PARAM_INT);
  168.                 $statement->execute();
  169.                 $rows $statement->fetchAll();
  170.                 $productIds = [];
  171.                 foreach ($rows as $row) {
  172.                     $productIds[] = $row['productId'];
  173.                 }
  174.                 if (!empty($productIds)) {
  175.                     $productIdList implode(','$productIds);
  176.                     $where "oo_id IN ($productIdList)";
  177.                     $products->setCondition($where);
  178.                     $products->load();
  179.                     if ($products->count() > 0) {
  180.                         throw new ValidationException('You can\'t delete the distributor ' $object->getName() . ' because it is associated with products. First, you have to remove the association with products before you can delete the distributor.');
  181.                     }
  182.                 }
  183.             } elseif ($object instanceof Store) {
  184.                 $products = new Product\Listing();
  185.                 $products->setUnpublished(true);
  186.                 $sql "SELECT productId FROM app_product_store WHERE storeId = ?";
  187.                 $statement Db::get()->prepare($sql);
  188.                 $statement->bindValue(1$object->getId(), \PDO::PARAM_INT);
  189.                 $statement->execute();
  190.                 $rows $statement->fetchAll();
  191.                 $productIds = [];
  192.                 foreach ($rows as $row) {
  193.                     $productIds[] = $row['productId'];
  194.                 }
  195.                 if (!empty($productIds)) {
  196.                     $productIdList implode(','$productIds);
  197.                     $where "oo_id IN ($productIdList)";
  198.                     $products->setCondition($where);
  199.                     $products->load();
  200.                     if ($products->count() > 0) {
  201.                         throw new ValidationException('You can\'t delete the store ' $object->getName() . ' because it is associated with products. First, you have to remove the association with products before you can delete the store.');
  202.                     }
  203.                 }
  204.             } elseif ($object instanceof Model) {
  205.                 $modelId $object->getId();
  206.                 $products = new Product\Listing();
  207.                 $products->setUnpublished(true);
  208.                 /*$likeClauses = "partLocation LIKE '%s:4:\"type\";s:6:\"object\";s:2:\"id\";i:$modelId;%'";
  209.                 $sql = "SELECT oo_id FROM object_store_PRD WHERE $likeClauses";
  210.                 $statement->bindValue(1, $object->getId(), \PDO::PARAM_INT); */
  211.                 $sql "SELECT productId FROM app_part_location WHERE modeId = $modelId";
  212.                 $statement Db::get()->prepare($sql);
  213.                 $statement->execute();
  214.                 $rows $statement->fetchAll();
  215.                 $productIds = [];
  216.                 foreach ($rows as $row) {
  217.                     $productIds[] = $row['productId'];
  218.                 }
  219.                 if (!empty($productIds)) {
  220.                     $productIdList implode(','$productIds);
  221.                     $where "oo_id IN ($productIdList)";
  222.                     $products->setCondition($where);
  223.                     $products->load();
  224.                     if ($products->count() > 0) {
  225.                         throw new ValidationException('You can\'t delete the model ' $object->getName() . ' because it is associated with products. First, you have to remove the association with products before you can delete the model.');
  226.                     }
  227.                 }
  228.             } elseif ($object instanceof Brand) {
  229.                 $products = new Product\Listing();
  230.                 $products->setUnpublished(true);
  231.                 $where "brand__id IN ('$id')";
  232.                 $products->setCondition($where);
  233.                 $products->load();
  234.                 if ($products->count() > 0) {
  235.                     throw new ValidationException('You can\'t delete the brand ' $object->getName() . ' because it is associated with the products. Firstly you have to remove assoication with products then can delete.');
  236.                 } else {
  237.                     $models = new Model\Listing();
  238.                     $models->setUnpublished(true);
  239.                     $where "brand__id IN ('$id')";
  240.                     $models->setCondition($where);
  241.                     $models->load();
  242.                     if ($models->count() > 0) {
  243.                         throw new ValidationException('You can\'t delete the brand ' $object->getName() . ' because it is associated with the models. Firstly you have to remove assoication with models then can delete.');
  244.                     }
  245.                 }
  246.             } /* elseif ($object instanceof Model) {
  247.                 $products = new Product\Listing();
  248.                 $products->setUnpublished(true);
  249.                 $where = "model__id IN ('$id')";
  250.                 $products->setCondition($where);
  251.                 $products->load();
  252.                 if ($products->count() > 0) {
  253.                     throw new ValidationException('You can\'t delete the model ' . $object->getName() . ' because it is associated with the products. Firstly you have to remove assoication with products then can delete.');
  254.                 }
  255.             } */ else {
  256.                 return;
  257.             }
  258.         }
  259.     }
  260.     /**
  261.      * Event trigger when post delete
  262.      * @param ElementEventInterface $event
  263.      */
  264.     public function onPostDelete(ElementEventInterface $event)
  265.     {
  266.         if ($event instanceof DataObjectEvent) {
  267.             $object $event->getObject();
  268.             $id $object->getId();
  269.             // Channel price delete
  270.             if ($object instanceof Channel) {
  271.                 $channelPriceSql "DELETE FROM `product_prices` WHERE channelId = $id";
  272.                 $channelStmt Db::get()->prepare($channelPriceSql);
  273.                 $channelStmt->execute();
  274.             }
  275.             // Distributor price delete
  276.             if ($object instanceof Distributor) {
  277.                 $distributorPriceSql "DELETE FROM `product_distributor_prices` WHERE distributorId = $id";
  278.                 $distributorStmt Db::get()->prepare($distributorPriceSql);
  279.                 $distributorStmt->execute();
  280.             }
  281.             if ($object instanceof Store) {
  282.                 $storePriceSql "DELETE FROM `app_product_store` WHERE storeId = $id";
  283.                 $storeStmt Db::get()->prepare($storePriceSql);
  284.                 $storeStmt->execute();
  285.             }
  286.         }
  287.     }
  288. }