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) {
  25.                                 continue;
  26.                             }
  27.                             if ($currentEwsAlert->getId() == $object->getId()) {
  28.                                 $versions $currentEwsAlert->getVersions();
  29.                                 $previousVersion $versions[count($versions)-2];
  30.                                 $currentEwsAlert $previousVersion->getData();
  31.                                 // Check if there is a change in region, alertType, phenomena, or alertGovernorate
  32.                                 if (!$currentEwsAlert) {
  33.                                     continue; // or handle accordingly
  34.                                 }
  35.                                 if (
  36.                                     $currentEwsAlert?->getRegion()->getId() != $object?->getRegion()->getId() ||
  37.                                     $currentEwsAlert?->getAlertType()->getId() != $object?->getAlertType()->getId() ||
  38.                                     $currentEwsAlert?->getAlertStatus()->getId() != $object?->getAlertStatus()->getId()
  39.                                 ) {
  40.                                     // If there is a change, update the subscription
  41.                                     unset($objectArray[$key]);
  42.                                     if ($currentEwsAlert) {
  43.                                         $objectMetadata = new DataObject\Data\ObjectMetadata('matchedEwsAlerts', ['isUpdated'],  $currentEwsAlert);
  44.                                         $objectMetadata->setIsUpdated(true);
  45.                                         array_push($objectArray$objectMetadata);
  46.                                         $subscription->setMatchedEwsAlerts($objectArray);
  47.                                         $subscription->save();
  48.                                     }
  49.                                 }
  50.                                
  51.                                 break; // Stop the loop once a match is found
  52.                             }
  53.                         }
  54.                         
  55.                     
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.     }
  61. }