<?php
namespace Jabber\WebthinkingBundle\EventListener;
use Jabber\WebthinkingBundle\Model\Job;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Product;
use Pimcore\Model\User;
class PostUpdateListener {
public function __construct(public bool $automationEnabled, public array $dataQualityFields, public int $systemUser) {
}
public function onPostUpdate(ElementEventInterface $e) {
$foo = $e->getArguments();
if (!$e->hasArgument('isAutoSave')) {
if ($e instanceof DataObjectEvent) {
// do something with the object
$dataObject = $e->getObject();
if ($dataObject instanceof Product) {
if ($dataObject->getUserModification() != (int) $this->systemUser) {
if ($dataObject->getProductLevel() != 'CHILD') {
$this->getDataQuality($dataObject);
if ($dataObject->getProductLevel() == 'STYLE') {
$this->createStyleRangeJob($dataObject);
}
}
}
// $this->createWebthinkingDeleteJob($dataObject);
}
}
}
}
public function savedByUser($userId) {
$user = User::getById($userId);
}
public function createStyleRangeJob(Product $dataObject) {
if (!empty($dataObject->get('StyleRanges')) || !empty($dataObject->get('AssociatedProducts'))) {
$categoryJob = new Job();
$categoryJob->setObject_Id($dataObject->getId());
$categoryJob->setType('style_range');
$categoryJob->setSite_Id(0);
$categoryJob->setParams(['sites'=>'all']);
$categoryJob->save();
}
}
public function getDataQuality(Product $dataObject) {
if ($this->automationEnabled == true) {
foreach ($this->dataQualityFields as $siteId => $dataQualityField) {
$qualityValue = $dataObject->get($dataQualityField);
if (is_int($qualityValue) || is_float($qualityValue)) {
$job = new Job();
$job->setType('product');
$job->setSiteId($siteId);
$job->setObjectId($dataObject->getId());
$params = [
'productId'=>$dataObject->getId(),
'type'=>$dataObject->getProductLevel(),
'siteId'=>$siteId,
'active'=>0,
'userId'=>$dataObject->getUserModification(),
'versionCount'=>$dataObject->getVersionCount()
];
if ($qualityValue = 100) {
$params['active'] = 1;
}
$job->setParams($params);
$job->save();
}
}
}
}
}