<?php
namespace Jabber\WebthinkingBundle\EventListener;
use Jabber\WebthinkingBundle\Model\Job;
use Jabber\WebthinkingBundle\Service\ProductIdentiferService;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Product;
class PreDeleteListener {
public function __construct(public ProductIdentiferService $identifierService, public array $sites) {
}
public function onPostDelete (ElementEventInterface $e) {
if (!$e->hasArgument('isAutoSave')) {
if ($e instanceof DataObjectEvent) {
// do something with the object
$dataObject = $e->getObject();
if ($dataObject instanceof Product) {
$this->createWebthinkingDeleteJob($dataObject);
}
}
}
}
public function createWebthinkingDeleteJob(Product $dataObject) {
$originalInheritance = $dataObject->getGetInheritedValues();
$dataObject->setGetInheritedValues(false);
foreach ($this->sites as $siteId=>$siteName) {
$productIdentifier = $this->identifierService->get($dataObject, $siteId);
if (!empty($productIdentifier)) {
$job = new Job();
$job->setType('delete_product');
$job->setObject_Id($dataObject->getId());
$job->setSiteId($siteId);
$params = [
'product_id'=>$dataObject->getId(),
'product_level'=>$dataObject->getProductLevel(),
'webthinking_id'=>$productIdentifier
];
if ($dataObject->getProductLevel() == 'STYLE') {
$params['webthinking_parent_id'] = $productIdentifier = $this->identifierService->get($dataObject->getParent(), $siteId);
} else if ($dataObject->getProductLevel() == 'CHILD') {
$params['webthinking_parent_id'] = $productIdentifier = $this->identifierService->get($dataObject->getParent()->getParent(), $siteId);
$params['webthinking_style_id'] = $productIdentifier = $this->identifierService->get($dataObject->getParent(), $siteId);
}
$job->setParams($params);
$job->save();
}
}
$dataObject->setGetInheritedValues($originalInheritance);
}
}