src/EventSubscriber/EwsNotificationUpdateSubscriber.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Pimcore\Model\DataObject\EwsNotification;
  4. use Pimcore\Event\Model\DataObjectEvent;
  5. use Pimcore\Model\DataObject\MannedAlertSubscription;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Pimcore\Model\DataObject;
  8. class EwsNotificationUpdateSubscriber
  9. {
  10.     public function onObjectUpdate(DataObjectEvent $event)
  11.     {
  12.         // Logic to handle after the object is updated
  13.         $object $event->getObject();
  14.         if ($object instanceof EwsNotification) {
  15.             // Check if the object is of the class you want to handle
  16.             $subscriptions = new MannedAlertSubscription\Listing();
  17.             $subscriptions->addConditionParam("matchedEwsAlerts like '%," $object->getId() . ",%'");
  18.             if ($subscriptions->count() > 0) {
  19.                 foreach ($subscriptions as $subscription) {
  20.                     $objectArray = ($subscription) ? $subscription->getMatchedEwsAlerts() : [];
  21.                     if ($objectArray) {
  22.                         foreach ($objectArray as $key => $ewsAlert) {
  23.                             $currentEwsAlert $ewsAlert->getObject();
  24.                             if ($currentEwsAlert->getId() == $object->getId()) {
  25.                                 $versions $currentEwsAlert->getVersions();
  26.                                 // Only proceed if there are at least 2 versions (current and previous)
  27.                                 if (count($versions) >= 2) {
  28.                                     $previousVersion $versions[count($versions)-2];
  29.                                     $previousEwsAlert $previousVersion->getData();
  30.                                     // Check if there is a change in region, alertType, phenomena, or alertGovernorate
  31.                                     if (
  32.                                         $previousEwsAlert?->getRegion()->getId() != $object?->getRegion()->getId() ||
  33.                                         $previousEwsAlert?->getAlertType()->getId() != $object?->getAlertType()->getId() ||
  34.                                         $previousEwsAlert?->getAlertStatus()->getId() != $object?->getAlertStatus()->getId()
  35.                                     ) {
  36.                                         // If there is a change, update the subscription
  37.                                         unset($objectArray[$key]);
  38.                                         $objectMetadata = new DataObject\Data\ObjectMetadata('matchedEwsAlerts', ['isUpdated'],  $currentEwsAlert);
  39.                                         $objectMetadata->setIsUpdated(true);
  40.                                         array_push($objectArray$objectMetadata);
  41.                                         $subscription->setMatchedEwsAlerts($objectArray);
  42.                                         $subscription->save();
  43.                                     }
  44.                                 }
  45.                                
  46.                                 break; // Stop the loop once a match is found
  47.                             }
  48.                         }
  49.                         
  50.                     
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.     }
  56. }