<?php
namespace App\EventListener;
use Exception;
use Pimcore\Model\Element\ValidationException;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject\Product;
use Pimcore\Model\DataObject\Category;
use Pimcore\Model\DataObject\Distributor;
use Pimcore\Model\DataObject\Store;
use Pimcore\Model\DataObject\Channel;
use Pimcore\Model\DataObject\AmazonCategory;
use Pimcore\Model\DataObject\Model;
use Pimcore\Model\DataObject\Brand;
use Pimcore\Model\DataObject\ERPImportConfiguration;
use Pimcore\Model\DataObject\EnrichmentFilter;
use Pimcore\Model\DataObject\OemEnrichmentFilter;
use Pimcore\Model\DataObject\OEMFilter;
use Pimcore\Model\DataObject\ExcelUpload;
use Pimcore\Db;
use Pimcore\Model\DataObject\OEMUpload;
class MasterListener
{
public function UpdateFolderOnCreate(ElementEventInterface $event)
{
// if ($event instanceof DataObjectEvent)
if ($event instanceof DataObjectEvent) {
$object = $event->getObject();
if ($object instanceof Category || $object instanceof Distributor || $object instanceof Channel || $object instanceof AmazonCategory || $object instanceof Model || $object instanceof Brand || $object instanceof Store) {
try {
/** Master Folder Structure Start */
$path = "Master Data/" . $object->getClassName();
$pathExists = \Pimcore\Model\DataObject\Service::pathExists($path);
if (empty($pathExists)) {
$folder = \Pimcore\Model\DataObject\Service::createFolderByPath($path);
} else {
$folder = \Pimcore\Model\DataObject\Service::getElementByPath('object', $path);
}
if ($object instanceof Category && $object->getParentCategory()) {
$object->setParentId($object->getParentCategory()->getId());
} else {
$object->setParentId($folder->getId());
}
$object->save();
/** Master Folder Structure End */
} catch (Exception $e) {
throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
}
} elseif ($object instanceof ERPImportConfiguration) {
try {
$path = "SFTP Configuration";
$pathExists = \Pimcore\Model\DataObject\Service::pathExists($path);
if (empty($pathExists)) {
$folder = \Pimcore\Model\DataObject\Service::createFolderByPath($path);
} else {
$folder = \Pimcore\Model\DataObject\Service::getElementByPath('object', $path);
}
$object->setParentId($folder->getId());
$object->save();
} catch (Exception $e) {
throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
}
} elseif ($object instanceof EnrichmentFilter) {
try {
$path = "Enrichment Filters";
$pathExists = \Pimcore\Model\DataObject\Service::pathExists($path);
if (empty($pathExists)) {
$folder = \Pimcore\Model\DataObject\Service::createFolderByPath($path);
} else {
$folder = \Pimcore\Model\DataObject\Service::getElementByPath('object', $path);
}
$object->setParentId($folder->getId());
$object->save();
} catch (Exception $e) {
throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
}
} elseif ($object instanceof OEMFilter) {
try {
$path = "OEM Filters";
$pathExists = \Pimcore\Model\DataObject\Service::pathExists($path);
if (empty($pathExists)) {
$folder = \Pimcore\Model\DataObject\Service::createFolderByPath($path);
} else {
$folder = \Pimcore\Model\DataObject\Service::getElementByPath('object', $path);
}
$object->setParentId($folder->getId());
$object->save();
} catch (Exception $e) {
throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
}
} elseif ($object instanceof ExcelUpload) {
try {
$path = "Enriched Excel";
$pathExists = \Pimcore\Model\DataObject\Service::pathExists($path);
if (empty($pathExists)) {
$folder = \Pimcore\Model\DataObject\Service::createFolderByPath($path);
} else {
$folder = \Pimcore\Model\DataObject\Service::getElementByPath('object', $path);
}
$object->setParentId($folder->getId());
$object->save();
} catch (Exception $e) {
throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
}
}
elseif ($object instanceof OemEnrichmentFilter) {
try {
$path = "OEM Export";
$pathExists = \Pimcore\Model\DataObject\Service::pathExists($path);
if (empty($pathExists)) {
$folder = \Pimcore\Model\DataObject\Service::createFolderByPath($path);
} else {
$folder = \Pimcore\Model\DataObject\Service::getElementByPath('object', $path);
}
$object->setParentId($folder->getId());
$object->save();
} catch (Exception $e) {
throw new \Pimcore\Model\Element\ValidationException($e->getMessage());
}
}
}
}
/**
* Event trigger when pre delete
* @param ElementEventInterface $event
*/
public function onPreDelete(ElementEventInterface $event)
{
if ($event instanceof DataObjectEvent) {
$object = $event->getObject();
$id = $object->getId();
if ($object instanceof Category) {
$products = new Product\Listing();
$products->setUnpublished(true);
$where = "FIND_IN_SET('$id', category)";
$products->setCondition($where);
$products->load();
if ($products->count() > 0) {
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.');
}
} elseif ($object instanceof Channel) {
$products = new Product\Listing();
$products->setUnpublished(true);
$sql = "SELECT productId FROM product_prices WHERE channelId = ?";
$statement = Db::get()->prepare($sql);
$statement->bindValue(1, $object->getId(), \PDO::PARAM_INT);
$statement->execute();
$rows = $statement->fetchAll();
$productIds = [];
foreach ($rows as $row) {
$productIds[] = $row['productId'];
}
if (!empty($productIds)) {
$productIdList = implode(',', $productIds);
$where = "oo_id IN ($productIdList)";
$products->setCondition($where);
$products->load();
if ($products->count() > 0) {
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.');
}
}
} elseif ($object instanceof Distributor) {
$products = new Product\Listing();
$products->setUnpublished(true);
$sql = "SELECT productId FROM product_distributor_prices WHERE distributorId = ?";
$statement = Db::get()->prepare($sql);
$statement->bindValue(1, $object->getId(), \PDO::PARAM_INT);
$statement->execute();
$rows = $statement->fetchAll();
$productIds = [];
foreach ($rows as $row) {
$productIds[] = $row['productId'];
}
if (!empty($productIds)) {
$productIdList = implode(',', $productIds);
$where = "oo_id IN ($productIdList)";
$products->setCondition($where);
$products->load();
if ($products->count() > 0) {
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.');
}
}
} elseif ($object instanceof Store) {
$products = new Product\Listing();
$products->setUnpublished(true);
$sql = "SELECT productId FROM app_product_store WHERE storeId = ?";
$statement = Db::get()->prepare($sql);
$statement->bindValue(1, $object->getId(), \PDO::PARAM_INT);
$statement->execute();
$rows = $statement->fetchAll();
$productIds = [];
foreach ($rows as $row) {
$productIds[] = $row['productId'];
}
if (!empty($productIds)) {
$productIdList = implode(',', $productIds);
$where = "oo_id IN ($productIdList)";
$products->setCondition($where);
$products->load();
if ($products->count() > 0) {
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.');
}
}
} elseif ($object instanceof Model) {
$modelId = $object->getId();
$products = new Product\Listing();
$products->setUnpublished(true);
/*$likeClauses = "partLocation LIKE '%s:4:\"type\";s:6:\"object\";s:2:\"id\";i:$modelId;%'";
$sql = "SELECT oo_id FROM object_store_PRD WHERE $likeClauses";
$statement->bindValue(1, $object->getId(), \PDO::PARAM_INT); */
$sql = "SELECT productId FROM app_part_location WHERE modeId = $modelId";
$statement = Db::get()->prepare($sql);
$statement->execute();
$rows = $statement->fetchAll();
$productIds = [];
foreach ($rows as $row) {
$productIds[] = $row['productId'];
}
if (!empty($productIds)) {
$productIdList = implode(',', $productIds);
$where = "oo_id IN ($productIdList)";
$products->setCondition($where);
$products->load();
if ($products->count() > 0) {
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.');
}
}
} elseif ($object instanceof Brand) {
$products = new Product\Listing();
$products->setUnpublished(true);
$where = "brand__id IN ('$id')";
$products->setCondition($where);
$products->load();
if ($products->count() > 0) {
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.');
} else {
$models = new Model\Listing();
$models->setUnpublished(true);
$where = "brand__id IN ('$id')";
$models->setCondition($where);
$models->load();
if ($models->count() > 0) {
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.');
}
}
} /* elseif ($object instanceof Model) {
$products = new Product\Listing();
$products->setUnpublished(true);
$where = "model__id IN ('$id')";
$products->setCondition($where);
$products->load();
if ($products->count() > 0) {
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.');
}
} */ else {
return;
}
}
}
/**
* Event trigger when post delete
* @param ElementEventInterface $event
*/
public function onPostDelete(ElementEventInterface $event)
{
if ($event instanceof DataObjectEvent) {
$object = $event->getObject();
$id = $object->getId();
// Channel price delete
if ($object instanceof Channel) {
$channelPriceSql = "DELETE FROM `product_prices` WHERE channelId = $id";
$channelStmt = Db::get()->prepare($channelPriceSql);
$channelStmt->execute();
}
// Distributor price delete
if ($object instanceof Distributor) {
$distributorPriceSql = "DELETE FROM `product_distributor_prices` WHERE distributorId = $id";
$distributorStmt = Db::get()->prepare($distributorPriceSql);
$distributorStmt->execute();
}
if ($object instanceof Store) {
$storePriceSql = "DELETE FROM `app_product_store` WHERE storeId = $id";
$storeStmt = Db::get()->prepare($storePriceSql);
$storeStmt->execute();
}
}
}
}