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