src/Command/StoreMannedToEwsAlertCommnad.php line 237

Open in your IDE?
  1. <?php
  2. namespace App\Command;
  3. use Datetime;
  4. use Pimcore\Db;
  5. use GuzzleHttp\Client;
  6. use Pimcore\Log\Simple;
  7. use App\Service\RedisCache;
  8. use App\Model\LocationModel;
  9. use GuzzleHttp\Psr7\Request;
  10. use App\Service\EmailService;
  11. use Pimcore\Model\DataObject;
  12. use App\Model\EwsNotificationModel;
  13. use App\Service\NCMWeatherAPIService;
  14. use App\Service\MeteomaticsWeatherService;
  15. use Symfony\Component\Console\Command\Command;
  16. use Symfony\Component\Templating\EngineInterface;
  17. use Symfony\Component\Console\Input\InputArgument;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use App\C2IntegrationBundle\Service\C2Service;
  21. use App\Service\RichService;
  22. use Symfony\Contracts\HttpClient\HttpClientInterface;
  23. use Pimcore\Model\DataObject\FetchSentEwsEmail;
  24. use Symfony\Contracts\Translation\TranslatorInterface;
  25. use Pimcore\Log\ApplicationLogger;
  26. class StoreMannedToEwsAlertCommnad extends Command
  27. {
  28.     protected static $defaultName 'app:get-manned-in-ews-alerts';
  29.     const HOURS 24;
  30.     var $emailService null;
  31.     private $templating;
  32.     private $nCMWeatherAPIService;
  33.     private $redisCache;
  34.     private $locationModel;
  35.     private $ewsNotificationModel;
  36.     private $c2Service;
  37.     private $richService;
  38.     private $httpClient;
  39.     private $logger;
  40.     public function __construct(EngineInterface $templatingRedisCache $redisCacheRichService $richServiceHttpClientInterface $httpClient, protected TranslatorInterface $translatorApplicationLogger $logger)
  41.     {
  42.         parent::__construct();
  43.         $this->templating $templating;
  44.         $this->nCMWeatherAPIService = new NCMWeatherAPIService($redisCache$httpClient);
  45.         $this->redisCache $redisCache;
  46.         $this->locationModel = new LocationModel();
  47.         $this->ewsNotificationModel = new EwsNotificationModel();
  48.         $this->c2Service = new C2Service();
  49.         $this->richService $richService;
  50.         $this->logger $logger;
  51.     }
  52.     protected function configure()
  53.     {
  54.         $this->setDescription('Retrieve manned alerts by location and strore in EwsNotification object');
  55.     }
  56.     protected function execute(InputInterface $inputOutputInterface $output)
  57.     {
  58.         $this->emailService = new EmailService();
  59.         $mateoMaticsService = new MeteomaticsWeatherService($this->redisCache);
  60.         // Fetching alerts 
  61.         $alerts $this->nCMWeatherAPIService->getAlerts();
  62.         if ($alerts) {
  63.             foreach ($alerts as $alert) {
  64.                 try {
  65.                     // Create update alert
  66.                     $ewsAlert $this->createUpdateAlertNotification($alert$output);
  67.                     if (!$ewsAlert) {
  68.                         $output->writeln('Nothing updated on notification = ' $alert['id']);
  69.                         continue;
  70.                     }
  71.                     if ($_ENV['SEND_EMAIL_NOTIFICATION'] == "false") {
  72.                         $output->writeln('Email is not allowed on this environment');
  73.                         continue;
  74.                     }
  75.                     if (!$ewsAlert->getIsSent()) {
  76.                         $mannedAlertSubscription = new DataObject\MannedAlertSubscription\Listing();
  77.                         $mannedAlertSubscription->setOrderKey("o_creationDate");
  78.                         $mannedAlertSubscription->setOrder("desc");
  79.                         foreach ($mannedAlertSubscription as $subscription) {
  80.                             if ($subscription->getIsPublic() == false) {
  81.                                 // $subscriptionRegion = $subscription->getIsPublic() ?? false;
  82.                                 $subscriptionRegion =  false;
  83.                                 if ($subscription->getRegion()) {
  84.                                     if ($subscription->getRegion()->getRegionId() == $alert['regionID']) {
  85.                                         $subscriptionRegion true;
  86.                                     }
  87.                                 }
  88.                                 // $alertType = $subscription->getIsPublic() ?? false;
  89.                                 $alertType =  false;
  90.                                 if ($subscription->getAlertType()) {
  91.                                     foreach ($subscription->getAlertType() as $alertType) {
  92.                                         if ($alertType->getAlertTypeId() == $alert['alertType']) {
  93.                                             $alertType true;
  94.                                             break;
  95.                                         }
  96.                                     }
  97.                                 }
  98.                                 // $alertGovernorate = $subscription->getIsPublic() ?? false;
  99.                                 $alertGovernorate =  false;
  100.                                 $alertGovernoratesIds array_column($alert['governorates'], 'id');
  101.                                 if ($subscription->getGovernorate()) {
  102.                                     foreach ($subscription->getGovernorate() as $governorate) {
  103.                                         if (in_array($governorate->getGovernoteId(), $alertGovernoratesIds)) {
  104.                                             $alertGovernorate true;
  105.                                             break;
  106.                                         }
  107.                                     }
  108.                                 }
  109.                                 // $phenomena = $subscription->getIsPublic() ?? false;
  110.                                 $phenomena false;
  111.                                 if ($subscription->getAlertStatus()) {
  112.                                     foreach ($subscription->getAlertStatus() as $subscriptionAlertStatus) {
  113.                                         if ($subscriptionAlertStatus->getAlertStatusId() == $alert['alertStatusID']) {
  114.                                             $phenomena true;
  115.                                             break;
  116.                                         }
  117.                                     }
  118.                                 }
  119.                                 // // logic for send email to those subscribers whose alert is updated
  120.                                 // $ewsObjectArray = ($subscription) ? $subscription->getMatchedEwsAlerts() : [];
  121.                                 // if ($ewsObjectArray) {
  122.                                 //     // Loop through the alerts
  123.                                 //     foreach ($ewsObjectArray as $ewsAlert) {
  124.                                 //         // Check if the alert ID matches and is updated
  125.                                 //         $currentEwsAlert = $ewsAlert->getObject();
  126.                                 //         $data = $ewsAlert->getData();
  127.                                 //         if ($currentEwsAlert->getId() === $ewsAlert->getId() && $data['isUpdated'] == true) {
  128.                                 //             if (!empty($subscription->getCreator())) {
  129.                                 //                 $users = [];
  130.                                 //                 $owner = $subscription->getCreator();
  131.                                 //                 $subscribers = $subscription->getSubscribers();
  132.                                 //                 // Ensure $owner is an array
  133.                                 //                 $owner = [$owner];
  134.                                 //                 // Merge $owner and $subscribers into $users
  135.                                 //                 $users = array_merge($owner, $subscribers);
  136.                                 //                 // if ($user->getSendEwsEmail() == true || is_null($user->getSendEwsEmail())) {
  137.                                 //                 $email = $this->sendEmailNotification($users, $alert, $this->templating);
  138.                                 //                 // if ($email) {
  139.                                 //                 //     $ewsNotification->setIsEmailSent(true);
  140.                                 //                 //     $ewsNotification->save();
  141.                                 //                 // }
  142.                                 //                 // }
  143.                                 //             }
  144.                                 //             break; // Stop the loop once a match is found
  145.                                 //         }
  146.                                 //     }
  147.                                 // }
  148.                                 // check if all conditions are true then send email
  149.                                 if ($subscriptionRegion && $alertType && $alertGovernorate && $phenomena) {
  150.                                     if (!empty($subscription->getCreator())) {
  151.                                         $users = [];
  152.                                         $owner $subscription->getCreator();
  153.                                         $subscribers $subscription->getSubscribers();
  154.                                         // Ensure $owner is an array
  155.                                         $owner = [$owner];
  156.                                         // Merge $owner and $subscribers into $users
  157.                                         $users array_merge($owner$subscribers);
  158.                                         foreach ($users as $user) {
  159.                                             if ($user instanceof \Pimcore\Model\DataObject\Customer) {
  160.                                                 if ($user->getSevereWeatherAlert()) {
  161.                                                     $email $this->sendEmailNotification($user$alertnulltrue$ewsAlert);
  162.                                                 }
  163.                                             }
  164.                                         }
  165.                                         // // Retrieve existing alerts from the subscription
  166.                                         // $existingAlerts = $subscription->getMatchedEwsAlerts();
  167.                                         // // Assuming $viewNotification has a method getId() to get its unique ID
  168.                                         // $newAlertId = $ewsAlert->getId();
  169.                                         // // Filter out the existing alert with the same ID, if any
  170.                                         // $filteredAlerts = array_filter($existingAlerts, function ($alert) use ($newAlertId) {
  171.                                         //     // Assuming $alert is an ObjectMetadata instance with a method to get the underlying alert ID
  172.                                         //     return $alert->getObject()->getId() !== $newAlertId;
  173.                                         // });
  174.                                         // // Create a new ObjectMetadata instance for the new alert
  175.                                         // $objectMetadata = new DataObject\Data\ObjectMetadata('matchedEwsAlerts', ['isUpdated'], $ewsAlert);
  176.                                         // $objectMetadata->setIsUpdated(false);
  177.                                         // // Add the new ObjectMetadata instance to the filtered alerts array
  178.                                         // $filteredAlerts[] = $objectMetadata;
  179.                                         // // Set the updated array back to the subscription
  180.                                         // $subscription->setMatchedEwsAlerts($filteredAlerts);
  181.                                         // $subscription->save();
  182.                                     }
  183.                                 }
  184.                             }
  185.                         }
  186.                         // if ($userGroupIds) {
  187.                         //     $email = $this->sendExcelEmailNotification($userGroupIds, $alert, $this->templating);
  188.                         // }
  189.                     }
  190.                     $ewsAlert->setIsSent(true);
  191.                     $ewsAlert->save();
  192.                 } catch (\Exception $ex) {
  193.                     p_r($ex->getMessage());
  194.                 }
  195.             }
  196.         }
  197.         return 0;
  198.     }
  199.     private function sendEmailNotification($user$alert$email null$actualUser$alertObject)
  200.     {
  201.         $mailSent null;
  202.         if ($alert) {
  203.             $governatesArr $alert['governorates'];
  204.             $governates = [];
  205.             if ($governatesArr) {
  206.                 foreach ($governatesArr as $gov) {
  207.                     $governates[] = $gov['nameEn'];
  208.                 }
  209.             }
  210.             $alertHazardsArr $alert['alertHazards'];
  211.             $alertHazards = [];
  212.             $alertHazards_ar = [];
  213.             if ($alertHazardsArr) {
  214.                 foreach ($alertHazardsArr as $alertHazard) {
  215.                     $alertHazards[] = $alertHazard['descriptionEn'];
  216.                     $alertHazards_ar[] = ['nameAr' => $alertHazard['descriptionAr']];
  217.                 }
  218.             }
  219.             $alertActionsArr $alert['alertActions'];
  220.             $alertActions = [];
  221.             if ($alertActionsArr) {
  222.                 foreach ($alertActionsArr as $alertAction) {
  223.                     $alertActions[] = $alertAction['descriptionEn'];
  224.                 }
  225.             }
  226.             $data $alert;
  227.             $data['sender'] = 'National Center for Meteorology';
  228.             // $subject =$alert['searchEwsIdAr'] ??'Severe Weather Alert - ' . $alert['regionEn'];
  229.             $subject $alert['alertStatusAr'] . ' - ' $alert['regionAR'];
  230.             if ($user) {
  231.                 $data['name'] = $email $user $user->getName();
  232.                 // extra param js
  233.                 $alertObj \Pimcore\Model\DataObject\AlertType::getByAlertTypeId($alert['alertType'], true);
  234.                 $alertName $alertObj->getColor() ? $alertObj->getColor() : '';
  235.                 $alert['user_name'] = $email $user $user->getName();
  236.                 $alert['host'] = API_BASE_URL;
  237.                 $alert['alertColor'] = $alertName;
  238.                 $currentDate = new \DateTime($alert['fromDate']);
  239.                 $searchIdAr 'النظام Ø§Ù„الي Ù„لإنذار Ø§Ù„مبكر | ' $currentDate->format('dmY') . '-' $alert['id'] . ' | ' $alert['alertTypeAr'] . ' | ' $alert['alertStatusAr'];
  240.                 $alert['searchEwsIdAr'] = $searchIdAr;
  241.                 $alert['alertHazard'] = $alertHazards_ar;
  242.                 $alert['last_modified_date'] =  $alert['lastModified'];
  243.                 $alert['tokenURL'] = null// for now this is null but we have to develop unsubscribe functionality for this as well 
  244.                 if ($actualUser) {
  245.                     // general un subscribe function
  246.                     $token \App\Lib\Utility::getUnSubscribeToken($user->getId());
  247.                     $alert['tokenURL'] = BASE_URL '/unsubscribe/email?token=' $token;
  248.                 }
  249.                 $alert['mannedAlertDatailUrl'] = "https://ncm.gov.sa/Ar/alert/Pages/MapDetail.aspx?AlertId=" $alert['id'];
  250.                 $html $this->templating->render('web2print/_manned_alert_notification_ar.html.twig'$alert);
  251.                 $mannedAlert \Pimcore\Model\DataObject\EwsNotification::getByAlertId($alert['id'], true);
  252.                 $purpose MANNED_ALERT_MESSAGE;
  253.                 if ($email) {
  254.                     $mailSent $this->c2Service->sendDefaultEmail($_ENV['EWS_MAIL_TEMPLATE'], $mannedAlert->getId(), $email$html$subject$purpose);
  255.                 } else {
  256.                     $mailSent $this->c2Service->sendNotificationEmail($_ENV['EWS_MAIL_TEMPLATE'], $mannedAlert->getId(), $user->getId(), $html$subject$purpose);
  257.                 }
  258.                 if ($user->getPhoneNo() && strlen($user->getPhoneNo()) == 9) {
  259.                     $messageSMS sprintf(SMS_MESSAGE["MANNED_ALERT"], strtoupper($alertName) . " alert raised in " $alert['regionAR'] . ' for ' $alert['alertStatusAr'] . " event");
  260.                     $this->richService->sendSms(SAUDI_CALL_CODE $user->getPhoneNo(), $messageSMS);
  261.                 }
  262.                 if ($alertObject->getEnableSmsNotification() && $user->getPhoneNo() && strlen($user->getPhoneNo()) == 9) {
  263.                     $smsTemplate \Pimcore\Model\WebsiteSetting::getByName('MANNED_ALERT'null);
  264.     
  265.                     if ($smsTemplate != null && $smsTemplate->getData() != null) {
  266.                         $messageSMS sprintf(SMS_MESSAGE["MANNED_ALERT"], strtoupper($alertName) . " alert raised in " $alert['regionAR'] . ' for ' $alert['alertStatusAr'] . " event");
  267.     
  268.                         try {
  269.                            
  270.                             $responseSMS $this->richService->sendMannedAlertNotificationSMS($alertObject$user$purpose$messageSMS);
  271.                             if (!$responseSMS) {
  272.                                 $this->logger->error('Failed to send SMS to ' SAUDI_CALL_CODE $user->getPhoneNo());
  273.                             }
  274.                         } catch (\Exception $e) {
  275.                             $this->logger->error('Exception occurred while sending SMS: ' $e->getMessage());
  276.                         }
  277.                     }
  278.                 }
  279.             }
  280.         }
  281.         return $mailSent;
  282.     }
  283.     private function sendExcelEmailNotification($userGroupIds$alert$templating)
  284.     {
  285.         $mailSent null;
  286.         if ($alert) {
  287.             $governatesArr $alert['governorates'];
  288.             $governates = [];
  289.             if ($governatesArr) {
  290.                 foreach ($governatesArr as $gov) {
  291.                     $governates[] = $gov['nameEn'];
  292.                 }
  293.             }
  294.             $data $alert;
  295.             $data['sender'] = 'National Center for Meteorology';
  296.             // $subject = $alert['searchEwsIdAr'] ??'Severe Weather Alert - ' . $gov['nameEn'];
  297.             //$subject = $alert['alertStatusAr'] . ' - ' . $alert['regionAR'];
  298.             // $subject = 'النظام Ø§Ù„الي Ù„لإنذار Ø§Ù„مبكر :' . ' - ' . $alert['searchEwsIdAr'];
  299.             $parts explode('|'$alert['searchEwsIdAr']);
  300.             $excluded implode('|'array_slice($parts1));
  301.             $subject 'النظام Ø§Ù„الي Ù„لإنذار Ø§Ù„مبكر :'  $excluded;
  302.             if ($userGroupIds) {
  303.                 foreach ($userGroupIds as $userGroupId) {
  304.                     if ($userGroupId) {
  305.                         $userGroup \Pimcore\Model\DataObject\EwsAndReportUserGroup::getById($userGroupIdtrue);
  306.                         if ($userGroup instanceof \Pimcore\Model\DataObject\EwsAndReportUserGroup) {
  307.                             foreach (json_decode($userGroup->getJsonData()) as $currentUser) {
  308.                                 if (isset($currentUser->firstName) && isset($currentUser->lastName) && isset($currentUser->email)) {
  309.                                     // extra param js
  310.                                     $alertObj \Pimcore\Model\DataObject\AlertType::getByAlertTypeId($alert['alertType'], true);
  311.                                     $alertName $alertObj->getColor() ? $alertObj->getColor() : '';
  312.                                     $alert['user_name'] = $currentUser->firstName ' ' $currentUser->lastName;
  313.                                     $alert['host'] = API_BASE_URL;
  314.                                     $alert['alertColor'] = $alertName;
  315.                                     $backgroundColor '#fcb82526';
  316.                                     $borderColor '#000000';
  317.                                     $textColor '#000000';
  318.                                     if (isset($alert['alertColor']) && !empty(trim($alert['alertColor']))) {
  319.                                         $alertColorLower strtolower(trim($alert['alertColor']));
  320.                                         if ($alertColorLower === 'red') {
  321.                                             $backgroundColor '#f6000017';
  322.                                             $borderColor '#F60000';
  323.                                             $textColor '#F60000';
  324.                                         } elseif ($alertColorLower === 'orange') {
  325.                                             $backgroundColor '#ff66001f';
  326.                                             $borderColor '#FF6600';
  327.                                             $textColor '#FF6600';
  328.                                         } elseif ($alertColorLower === 'yellow') {
  329.                                             $backgroundColor '#fcb82526';
  330.                                             $borderColor '#FCB825';
  331.                                             $textColor '#FCB825';
  332.                                         }
  333.                                     }
  334.                                     $alert['backgroundColor'] = $backgroundColor;
  335.                                     $alert['borderColor'] = $borderColor;
  336.                                     $alert['textColor'] = $textColor;
  337.                                     $alert['tokenURL'] = null;
  338.                                     $alert['mannedAlertDatailUrl'] = null;
  339.                                     $html $templating->render('web2print/_manned_alert_notification_ar.html.twig'$alert);
  340.                                     // $email = $currentUser->email;
  341.                                     // $param = ['user' => $currentUser, 'message' => $html, 'url' => null];
  342.                                     // $mailSent = $emailService->sendMail($param, "abdul.muqeet@centric.ae", '/email/alert_notification', $subject);
  343.                                     $purpose EWS_MESSAGE;
  344.                                     $mailSent $this->c2Service->sendDefaultEmail($_ENV['EWS_MAIL_TEMPLATE'], $alert['id'], $currentUser->email$html$subject$purpose);
  345.                                     if ($mailSent) {
  346.                                         $status "sent";
  347.                                         $this->saveEmailStatus($currentUser->email$currentUser->firstName ' ' $currentUser->lastName$alert$status);
  348.                                     } else {
  349.                                         $status "not sent";
  350.                                         $this->saveEmailStatus($currentUser->email$currentUser->firstName ' ' $currentUser->lastName$alert$status);
  351.                                     }
  352.                                 }
  353.                             }
  354.                         }
  355.                     }
  356.                 }
  357.             }
  358.         }
  359.         return $mailSent;
  360.     }
  361.     private function saveEmailStatus($userEmail$userName$alert$emailStatus)
  362.     {
  363.         // Get all governorates names in string seprated by ,
  364.         $nameEnArray array_column($alert["governorates"], "nameEn");
  365.         $nameEnString implode(", "$nameEnArray);
  366.         $nameArArray array_column($alert["governorates"], "nameAr");
  367.         $nameArString implode(", "$nameArArray);
  368.         $ewsEmailStatus = new FetchSentEwsEmail();
  369.         $ewsEmailStatus->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath("/Other/FetchSentEwsEmail/"));
  370.         $ewsEmailStatus->setKey(\Pimcore\Model\Element\Service::getValidKey($userEmail "-" strtotime("now") . "-" rand(110000), 'object'));
  371.         $ewsEmailStatus->setEwsId($alert['id']);
  372.         $ewsEmailStatus->setUserName($userName);
  373.         $ewsEmailStatus->setEmail($userEmail);
  374.         //$ewsEmailStatus->setLocationName($locationName);
  375.         $ewsEmailStatus->setAlertType($alert['alertTypeEn'], 'en');
  376.         $ewsEmailStatus->setAlertType($alert['alertTypeAr'], 'ar');
  377.         $ewsEmailStatus->setAlertStatus($alert['alertStatusEn'], 'en');
  378.         $ewsEmailStatus->setAlertStatus($alert['alertStatusAr'], 'ar');
  379.         $ewsEmailStatus->setRegionName($alert['regionEn'], 'en');
  380.         $ewsEmailStatus->setRegionName($alert['regionAR'], 'ar');
  381.         $ewsEmailStatus->setGovernorateNames($nameEnString'en');
  382.         $ewsEmailStatus->setGovernorateNames($nameArString'ar');
  383.         $ewsEmailStatus->setStatus($emailStatus);
  384.         $ewsEmailStatus->setPublished(true);
  385.         $ewsEmailStatus->save();
  386.         return $ewsEmailStatus;
  387.     }
  388.     private function createUpdateAlertNotification($alert$output)
  389.     {
  390.         // Generate the hash for the current alert data
  391.         $alertHash md5(json_encode($alert));
  392.         // Retrieve the existing EwsNotification object by alert ID
  393.         $ewsNotification \Pimcore\Model\DataObject\EwsNotification::getByAlertId($alert['id'], true);
  394.         // Check if the ews alert already exists
  395.         if ($ewsNotification) {
  396.             // Compare the old hash with the new hash
  397.             if ($ewsNotification->getHash() === $alertHash) {
  398.                 // If the hashes match, the alert has not changed; ignore it
  399.                 return null;
  400.             }
  401.         } else {
  402.             // If the alert does not exist, create a new MannedAlertNotification object
  403.             $ewsNotification = new DataObject\EwsNotification();
  404.             $ewsNotification->setKey($alert['id']);
  405.             $date date('Y-m-d'strtotime($alert['fromDate']));
  406.             $ewsNotification->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/EWSNotification/' $date));
  407.             $ewsNotification->setGuid(\App\Lib\Utility::getUUID()->toRfc4122());
  408.         }
  409.         $ewsNotification->setIsManned(true);
  410.         // Update the MannedAlertNotification object with the new alert data
  411.         $ewsNotification->setAlertId($alert['id']);
  412.         $ewsNotification->setTitle($alert['title']);
  413.         $ewsNotification->setAlertType($this->getAlertType($alert['alertType'], $alert['alertTypeAr'], $alert['alertTypeEn']));
  414.         $fromDate \Carbon\Carbon::createFromFormat('m/d/Y h:i:s A'$alert['fromDate']);
  415.         $toDate \Carbon\Carbon::createFromFormat('m/d/Y h:i:s A'$alert['toDate']);
  416.         $lastModified \Carbon\Carbon::createFromFormat('m/d/Y h:i:s A'$alert['lastModified']);
  417.         $ewsNotification->setStartDate($fromDate);
  418.         $ewsNotification->setEndDate($toDate);
  419.         $ewsNotification->setLastModifiedDate($lastModified);
  420.         $ewsNotification->setAlertStatusCategory($this->getAlertStatusCategory($alert['alertStatusCategory'])); // need  to fix
  421.         $ewsNotification->setStatus("active");
  422.         $alertStatus $this->getAlertStatus($alert['alertStatusID'], $alert['alertStatusAr'], $alert['alertStatusEn']);
  423.         if ($alertStatus) {
  424.             $ewsNotification->setAlertStatus($alertStatus);
  425.         }
  426.         $alertHazardArr = [];
  427.         if ($alert['alertHazards']) {
  428.             foreach ($alert['alertHazards'] as $alertHazard) {
  429.                 $alertHazardArr[] = $this->getAlertHazard($alertHazard['id'], $alertHazard['descriptionAr'], $alertHazard['descriptionEn']);
  430.             }
  431.             $ewsNotification->setAlertHazard(array_filter($alertHazardArr));
  432.         }
  433.         $ewsNotification->setRegion($this->getRegion($alert['regionID'], $alert['regionAR'], $alert['regionEn']));
  434.         $alertGovernorateArr = [];
  435.         if ($alert['governorates']) {
  436.             foreach ($alert['governorates'] as $alertGovernorate) {
  437.                 $alertGovernorateArr[] = $this->getGovernorates($alertGovernorate['id'], $alertGovernorate['nameAr'], $alertGovernorate['nameEn'], $alertGovernorate['longitude'], $alertGovernorate['latitude']);
  438.             }
  439.             $ewsNotification->setGovernorate(array_filter($alertGovernorateArr));
  440.         }
  441.         $ewsNotification->setOtherLocation($alert['otherLocationsEn'], 'en');
  442.         $ewsNotification->setOtherLocation($alert['otherLocationsAr'], 'ar');
  443.         if ($alert['otherLocationsAr']) {
  444.             $otherLocation \Pimcore\Model\DataObject\EwsOtherLocation::getByName($alert['otherLocationsAr'], 'en'true);
  445.             $ewsNotification->setEwsOtherLocations($otherLocation);
  446.         }
  447.         $ewsNotification->settweetID($alert['tweetID']);
  448.         $alertAlertActionArr = [];
  449.         if ($alert['alertActions']) {
  450.             foreach ($alert['alertActions'] as $alertAlertAction) {
  451.                 $alertAlertActionArr[] = $this->getAlertAction($alertAlertAction['id'], $alertAlertAction['descriptionAr'], $alertAlertAction['descriptionEn']);
  452.             }
  453.             $ewsNotification->setAlertAction(array_filter($alertAlertActionArr));
  454.         }
  455.         // Set the hash for the alert
  456.         $ewsNotification->setHash($alertHash);
  457.         $ewsNotification->setRawText(json_encode($alert));
  458.         $ewsNotification->setIsSent(false);
  459.         $ewsNotification->setOmitMandatoryCheck(true);
  460.         // saving here just to get the id
  461.         $ewsNotification->save();
  462.         $currentDate = new \DateTime();
  463.         $formattedDate $currentDate->format('dmY') . '-' $ewsNotification->getId();
  464.         $searchIdEn 'Early Warning System | ' $formattedDate ' | ' ucfirst($ewsNotification->getAlertType()?->getColor()) . ' Alert | ' $ewsNotification->getAlertStatus()?->getName("en");
  465.         $searchIdAr $this->translator->trans('Early Warning System', [], null"ar") . ' | ' $formattedDate ' | ' $this->translator->trans(ucfirst($ewsNotification->getAlertType()?->getColor()) . ' Alert', [], null"ar") . ' | ' $ewsNotification->getAlertStatus()?->getName("ar");
  466.         $ewsNotification->setEwsSearchId($searchIdEn"en");
  467.         $ewsNotification->setEwsSearchId($searchIdAr"ar");
  468.         // Save the updated MannedAlertNotification object
  469.         $ewsNotification->setPublished(TRUE);
  470.         $ewsNotification->save();
  471.         return $ewsNotification;
  472.     }
  473.     private function getAlertType($id$nameAr$nameEn)
  474.     {
  475.         $alertType \Pimcore\Model\DataObject\AlertType::getByAlertTypeId($idtrue);
  476.         if (!$alertType) {
  477.             $alertType = new \Pimcore\Model\DataObject\AlertType();
  478.             $alertType->setKey($id);
  479.             $alertType->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/AlertType'));
  480.             $alertType->setAlertTypeId($id);
  481.             $alertType->setName($nameAr"en");
  482.             $alertType->setName($nameEn"ar");
  483.             $alertType->setPublished(true);
  484.             $alertType->save();
  485.         }
  486.         return $alertType;
  487.     }
  488.     private function getAlertStatusCategory($name)
  489.     {
  490.         $alertStatusCategory \Pimcore\Model\DataObject\AlertStatusCategory::getByName($nametrue);
  491.         if (!$alertStatusCategory) {
  492.             $alertStatusCategory = new \Pimcore\Model\DataObject\AlertStatusCategory();
  493.             $alertStatusCategory->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/AlertStatusCategory'));
  494.             $alertStatusCategory->setKey($name);
  495.             $alertStatusCategory->setName($name);
  496.             $alertStatusCategory->setPublished(true);
  497.             $alertStatusCategory->save();
  498.         }
  499.         return $alertStatusCategory;
  500.     }
  501.     private function getAlertStatus($id$nameAr$nameEn)
  502.     {
  503.         $alertStatus \Pimcore\Model\DataObject\AlertStatus::getByAlertStatusId($idtrue);
  504.         if (!$alertStatus) {
  505.             $alertStatus = new \Pimcore\Model\DataObject\AlertStatus();
  506.             $alertStatus->setKey($id);
  507.             $alertStatus->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/AlertStatus'));
  508.             $alertStatus->setAlertStatusId($id);
  509.             $alertStatus->setName($nameAr'ar');
  510.             $alertStatus->setName($nameEn'en');
  511.             $alertStatus->setPublished(true);
  512.             $alertStatus->save();
  513.         }
  514.         return $alertStatus;
  515.     }
  516.     private function getAlertHazard($id$descriptionAr$descriptionEn)
  517.     {
  518.         $alertHazard \Pimcore\Model\DataObject\AlertHazard::getByAlertHazardId($idtrue);
  519.         if (!$alertHazard) {
  520.             $alertHazard = new \Pimcore\Model\DataObject\AlertHazard();
  521.             $alertHazard->setKey($id);
  522.             $alertHazard->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/AlertHazard'));
  523.             $alertHazard->setAlertHazardId($id);
  524.             $alertHazard->setName($descriptionAr"ar");
  525.             $alertHazard->setName($descriptionEn"en");
  526.             $alertHazard->setPublished(true);
  527.             $alertHazard->save();
  528.         } else {
  529.             $alertHazard->setName($descriptionAr"ar");
  530.             $alertHazard->setName($descriptionEn"en");
  531.             $alertHazard->save();
  532.         }
  533.         return $alertHazard;
  534.     }
  535.     private function getRegion($id$nameAr$nameEn)
  536.     {
  537.         $region \Pimcore\Model\DataObject\Region::getByRegionId($idtrue);
  538.         if (!$region) {
  539.             $region = new \Pimcore\Model\DataObject\Region();
  540.             $region->setKey($id);
  541.             $region->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/Region'));
  542.             $region->setRegionId($id);
  543.             $region->setName($nameAr'ar');
  544.             $region->setName($nameEn'en');
  545.             $region->setPublished(true);
  546.             $region->save();
  547.         }
  548.         return $region;
  549.     }
  550.     private function getGovernorates($id$nameAr$nameEn$longitude$latitude)
  551.     {
  552.         $governorate \Pimcore\Model\DataObject\Governorate::getByGovernoteId($idtrue);
  553.         if (!$governorate) {
  554.             $governorate = new \Pimcore\Model\DataObject\Governorate();
  555.             $governorate->setKey($id);
  556.             $governorate->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/Governorate'));
  557.             $governorate->setGovernoteId($id);
  558.             $governorate->setName($nameAr"ar");
  559.             $governorate->setName($nameEn"en");
  560.             $governorate->setLongitude($longitude);
  561.             $governorate->setLatitude($latitude);
  562.             $governorate->setPublished(true);
  563.             $governorate->save();
  564.         }
  565.         return $governorate;
  566.     }
  567.     private function getAlertAction($id$descriptionAr$descriptionEn)
  568.     {
  569.         $alertAction \Pimcore\Model\DataObject\AlertAction::getByAlertActionId($idtrue);
  570.         if (!$alertAction) {
  571.             $alertAction = new \Pimcore\Model\DataObject\AlertAction();
  572.             $alertAction->setKey($id);
  573.             $alertAction->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/AlertAction'));
  574.             $alertAction->setAlertActionId($id);
  575.             $alertAction->setName($descriptionAr"ar");
  576.             $alertAction->setName($descriptionEn"en");
  577.             $alertAction->setPublished(true);
  578.             $alertAction->save();
  579.         }
  580.         return $alertAction;
  581.     }
  582. }