<?php
namespace App\EventSubscriber;
use Pimcore\Model\DataObject\EwsNotification;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\MannedAlertSubscription;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Pimcore\Model\DataObject;
class EwsNotificationUpdateSubscriber
{
public function onObjectUpdate(DataObjectEvent $event)
{
// Logic to handle after the object is updated
$object = $event->getObject();
if ($object instanceof EwsNotification) {
// Check if the object is of the class you want to handle
$subscriptions = new MannedAlertSubscription\Listing();
$subscriptions->addConditionParam("matchedEwsAlerts like '%," . $object->getId() . ",%'");
if ($subscriptions->count() > 0) {
foreach ($subscriptions as $subscription) {
$objectArray = ($subscription) ? $subscription->getMatchedEwsAlerts() : [];
if ($objectArray) {
foreach ($objectArray as $key => $ewsAlert) {
$currentEwsAlert = $ewsAlert->getObject();
if ($currentEwsAlert->getId() == $object->getId()) {
$versions = $currentEwsAlert->getVersions();
$previousVersion = $versions[count($versions)-2];
$currentEwsAlert = $previousVersion->getData();
// Check if there is a change in region, alertType, phenomena, or alertGovernorate
if (
$currentEwsAlert?->getRegion()->getId() != $object?->getRegion()->getId() ||
$currentEwsAlert?->getAlertType()->getId() != $object?->getAlertType()->getId() ||
$currentEwsAlert?->getAlertStatus()->getId() != $object?->getAlertStatus()->getId()
) {
// If there is a change, update the subscription
unset($objectArray[$key]);
$objectMetadata = new DataObject\Data\ObjectMetadata('matchedEwsAlerts', ['isUpdated'], $currentEwsAlert);
$objectMetadata->setIsUpdated(true);
array_push($objectArray, $objectMetadata);
$subscription->setMatchedEwsAlerts($objectArray);
$subscription->save();
}
break; // Stop the loop once a match is found
}
}
}
}
}
}
}
}