bundles/Jabber/DataEnrichmentBundle/EventListener/PostUpdateListener.php line 19

Open in your IDE?
  1. <?php 
  2. namespace Jabber\DataEnrichmentBundle\EventListener;
  3. use Carbon\Carbon;
  4. use Jabber\DataEnrichmentBundle\Model\Request;
  5. use Pimcore\Event\Model\ElementEventInterface;
  6. use Pimcore\Event\Model\DataObjectEvent;
  7. use Pimcore\Model\DataObject\DataEnrichmentModel;
  8. use Pimcore\Model\DataObject\Product;
  9. use Pimcore\Model\User;
  10. class PostUpdateListener {
  11.     public function __construct(public int $systemUser 2) {
  12.     }
  13.     public function onPostUpdate(ElementEventInterface $e) {
  14.         $foo $e->getArguments();
  15.         if (!$e->hasArgument('isAutoSave')) {
  16.             if ($e instanceof DataObjectEvent) {
  17.                 // do something with the object
  18.                 $dataObject $e->getObject();
  19.                 if ($dataObject instanceof Product) {
  20.                     if ($dataObject->getUserModification() != (int) $this->systemUser) {
  21.                         $enrichmentProfiles $this->getEnrichmentProfiles();
  22.                         foreach ($enrichmentProfiles as $enrichmentProfile) {
  23.                             if (strtoupper($enrichmentProfile->getLevel()) == $dataObject->getProductLevel()) {
  24.                                 if ($enrichmentProfile->getUpdates() == false) {
  25.                                     $isEmpty $this->checkForEmptyValues($enrichmentProfile);
  26.                                     if ($isEmpty) {
  27.                                         $this->createRequest($enrichmentProfile->getId(), $dataObject->getId());
  28.                                     }
  29.                                 } else {
  30.                                     $this->createRequest($enrichmentProfile->getId(), $dataObject->getId(), $enrichmentProfile->getDuration());
  31.                                 }
  32.                             }
  33.                         }
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.     }
  39.     public function createRequest($profileId$productId$duration null) {
  40.         $listing = new Request\Listing();
  41.         $existingRequests $listing->getByProductId($productId$profileId);
  42.         if ($profileId == 366349) {
  43.             $foo "bar";
  44.         }
  45.         if (empty($existingRequests)) {
  46.             $request = new Request();
  47.             $request->setEnrichmentId($profileId);
  48.             $request->setObjectId($productId);
  49.             $request->save();
  50.         } else {
  51.             foreach ($existingRequests as $existingRequest) {
  52.                 if ($duration != null) {
  53.                     $date Carbon::parse($existingRequest->getCreatedAt());
  54.                     $now Carbon::now();
  55.                     $diff $date->diffInDays($now);
  56.                     if ($diff >= $duration) {
  57.                         $newRequest = new Request();
  58.                         $newRequest->setEnrichmentId($profileId);
  59.                         $newRequest->setObjectId($productId);
  60.                         $newRequest->save();
  61.                     }
  62.                 }
  63.                 break;
  64.             }
  65.         }
  66.         
  67.     }
  68.     public function getEnrichmentProfiles() {
  69.         $dataEnrichmentModels DataEnrichmentModel::getList();
  70.         return $dataEnrichmentModels;
  71.     }
  72.     /**
  73.      * Checks the handler class for the function isEmpty
  74.      * 
  75.      * If it exists it'll run the check based on the rules set by the handler
  76.      *
  77.      * @param DataEnrichmentModel $enrichmentProfile
  78.      * @return bool
  79.      */
  80.     public function checkForEmptyValues(DataEnrichmentModel $enrichmentProfile) {
  81.         if (!empty($enrichmentProfile->getHandler())) {
  82.             $handlerClass$enrichmentProfile->getHandler();
  83.             if (class_exists($handlerClass)) {
  84.                 $handler = new $handlerClass();
  85.                 if (method_exists($handler'isEmpty')) {
  86.                     return $handler->isEmpty();
  87.                 }
  88.             }
  89.         }
  90.         return false;
  91.     }
  92. }