<?php
namespace App\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Review;
use Pimcore\Model\DataObject\Solution;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Search;
use Pimcore\Model\DataObject\Provider;
use Pimcore\Model\DataObject\ClassDefinition;
use App\Services\SearchService;
use \Pimcore\Config;
class ProviderEventListener
{
public function postUpdate(DataObjectEvent $e)
{
$object = $e->getObject();
if($object instanceof Solution){
if($object->getProvider()){
$idProvider = $object->getProvider()->getId();
$solution = new Solution\Listing();
$solution->addConditionParam('provider__id = ?',$idProvider);
// cập nhật vào Provider
$provider = Provider::getById($idProvider);
$provider->setTotalSolution($solution->count());
$provider->save();
}
}
if($object instanceof Review){
$idSolution = $object->getSolution()->getId();
if($object->getPublished()){
// lấy solution
$detailSolution = Solution::getById($idSolution);
if($detailSolution->getProvider()){
$idProvider = $detailSolution->getProvider()->getId();
// lấy tất cả solution
$solution = new Solution\Listing();
$solution->addConditionParam('provider__id = ?',$idProvider);
$total = 0;
foreach($solution as $value){
$review = new Review\Listing();
$review->addConditionParam("solution__id = ?",$value->getId());
$total+=$review->count();
}
// cập nhật vào Provider
$provider = Provider::getById($idProvider);
// tính tổng tất cả review thuộc provider
$provider->setTotalReview($total);
$provider->save();
}
}
}
}
}