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 $templating, RedisCache $redisCache, RichService $richService, HttpClientInterface $httpClient, protected TranslatorInterface $translator, ApplicationLogger $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 $input, OutputInterface $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, $alert, null, true, $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. if ($smsTemplate != null && $smsTemplate->getData() != null) {
  265. $messageSMS = sprintf(SMS_MESSAGE["MANNED_ALERT"], strtoupper($alertName) . " alert raised in " . $alert['regionAR'] . ' for ' . $alert['alertStatusAr'] . " event");
  266. try {
  267. $responseSMS = $this->richService->sendMannedAlertNotificationSMS($alertObject, $user, $purpose, $messageSMS);
  268. if (!$responseSMS) {
  269. $this->logger->error('Failed to send SMS to ' . SAUDI_CALL_CODE . $user->getPhoneNo());
  270. }
  271. } catch (\Exception $e) {
  272. $this->logger->error('Exception occurred while sending SMS: ' . $e->getMessage());
  273. }
  274. }
  275. }
  276. }
  277. }
  278. return $mailSent;
  279. }
  280. private function sendExcelEmailNotification($userGroupIds, $alert, $templating)
  281. {
  282. $mailSent = null;
  283. if ($alert) {
  284. $governatesArr = $alert['governorates'];
  285. $governates = [];
  286. if ($governatesArr) {
  287. foreach ($governatesArr as $gov) {
  288. $governates[] = $gov['nameEn'];
  289. }
  290. }
  291. $data = $alert;
  292. $data['sender'] = 'National Center for Meteorology';
  293. // $subject = $alert['searchEwsIdAr'] ??'Severe Weather Alert - ' . $gov['nameEn'];
  294. //$subject = $alert['alertStatusAr'] . ' - ' . $alert['regionAR'];
  295. // $subject = 'النظام الالي للإنذار المبكر :' . ' - ' . $alert['searchEwsIdAr'];
  296. $parts = explode('|', $alert['searchEwsIdAr']);
  297. $excluded = implode('|', array_slice($parts, 1));
  298. $subject = 'النظام الالي للإنذار المبكر :' . $excluded;
  299. if ($userGroupIds) {
  300. foreach ($userGroupIds as $userGroupId) {
  301. if ($userGroupId) {
  302. $userGroup = \Pimcore\Model\DataObject\EwsAndReportUserGroup::getById($userGroupId, true);
  303. if ($userGroup instanceof \Pimcore\Model\DataObject\EwsAndReportUserGroup) {
  304. foreach (json_decode($userGroup->getJsonData()) as $currentUser) {
  305. if (isset($currentUser->firstName) && isset($currentUser->lastName) && isset($currentUser->email)) {
  306. // extra param js
  307. $alertObj = \Pimcore\Model\DataObject\AlertType::getByAlertTypeId($alert['alertType'], true);
  308. $alertName = $alertObj->getColor() ? $alertObj->getColor() : '';
  309. $alert['user_name'] = $currentUser->firstName . ' ' . $currentUser->lastName;
  310. $alert['host'] = API_BASE_URL;
  311. $alert['alertColor'] = $alertName;
  312. $backgroundColor = '#fcb82526';
  313. $borderColor = '#000000';
  314. $textColor = '#000000';
  315. if (isset($alert['alertColor']) && !empty(trim($alert['alertColor']))) {
  316. $alertColorLower = strtolower(trim($alert['alertColor']));
  317. if ($alertColorLower === 'red') {
  318. $backgroundColor = '#f6000017';
  319. $borderColor = '#F60000';
  320. $textColor = '#F60000';
  321. } elseif ($alertColorLower === 'orange') {
  322. $backgroundColor = '#ff66001f';
  323. $borderColor = '#FF6600';
  324. $textColor = '#FF6600';
  325. } elseif ($alertColorLower === 'yellow') {
  326. $backgroundColor = '#fcb82526';
  327. $borderColor = '#FCB825';
  328. $textColor = '#FCB825';
  329. }
  330. }
  331. $alert['backgroundColor'] = $backgroundColor;
  332. $alert['borderColor'] = $borderColor;
  333. $alert['textColor'] = $textColor;
  334. $alert['tokenURL'] = null;
  335. $alert['mannedAlertDatailUrl'] = null;
  336. $html = $templating->render('web2print/_manned_alert_notification_ar.html.twig', $alert);
  337. // $email = $currentUser->email;
  338. // $param = ['user' => $currentUser, 'message' => $html, 'url' => null];
  339. // $mailSent = $emailService->sendMail($param, "abdul.muqeet@centric.ae", '/email/alert_notification', $subject);
  340. $purpose = EWS_MESSAGE;
  341. $mailSent = $this->c2Service->sendDefaultEmail($_ENV['EWS_MAIL_TEMPLATE'], $alert['id'], $currentUser->email, $html, $subject, $purpose);
  342. if ($mailSent) {
  343. $status = "sent";
  344. $this->saveEmailStatus($currentUser->email, $currentUser->firstName . ' ' . $currentUser->lastName, $alert, $status);
  345. } else {
  346. $status = "not sent";
  347. $this->saveEmailStatus($currentUser->email, $currentUser->firstName . ' ' . $currentUser->lastName, $alert, $status);
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }
  354. }
  355. }
  356. return $mailSent;
  357. }
  358. private function saveEmailStatus($userEmail, $userName, $alert, $emailStatus)
  359. {
  360. // Get all governorates names in string seprated by ,
  361. $nameEnArray = array_column($alert["governorates"], "nameEn");
  362. $nameEnString = implode(", ", $nameEnArray);
  363. $nameArArray = array_column($alert["governorates"], "nameAr");
  364. $nameArString = implode(", ", $nameArArray);
  365. $ewsEmailStatus = new FetchSentEwsEmail();
  366. $ewsEmailStatus->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath("/Other/FetchSentEwsEmail/"));
  367. $ewsEmailStatus->setKey(\Pimcore\Model\Element\Service::getValidKey($userEmail . "-" . strtotime("now") . "-" . rand(1, 10000), 'object'));
  368. $ewsEmailStatus->setEwsId($alert['id']);
  369. $ewsEmailStatus->setUserName($userName);
  370. $ewsEmailStatus->setEmail($userEmail);
  371. //$ewsEmailStatus->setLocationName($locationName);
  372. $ewsEmailStatus->setAlertType($alert['alertTypeEn'], 'en');
  373. $ewsEmailStatus->setAlertType($alert['alertTypeAr'], 'ar');
  374. $ewsEmailStatus->setAlertStatus($alert['alertStatusEn'], 'en');
  375. $ewsEmailStatus->setAlertStatus($alert['alertStatusAr'], 'ar');
  376. $ewsEmailStatus->setRegionName($alert['regionEn'], 'en');
  377. $ewsEmailStatus->setRegionName($alert['regionAR'], 'ar');
  378. $ewsEmailStatus->setGovernorateNames($nameEnString, 'en');
  379. $ewsEmailStatus->setGovernorateNames($nameArString, 'ar');
  380. $ewsEmailStatus->setStatus($emailStatus);
  381. $ewsEmailStatus->setPublished(true);
  382. $ewsEmailStatus->save();
  383. return $ewsEmailStatus;
  384. }
  385. private function createUpdateAlertNotification($alert, $output)
  386. {
  387. // Generate the hash for the current alert data
  388. $alertHash = md5(json_encode($alert));
  389. // Retrieve the existing EwsNotification object by alert ID
  390. $ewsNotification = \Pimcore\Model\DataObject\EwsNotification::getByAlertId($alert['id'], true);
  391. // Check if the ews alert already exists
  392. if ($ewsNotification) {
  393. // Compare the old hash with the new hash
  394. if ($ewsNotification->getHash() === $alertHash) {
  395. // If the hashes match, the alert has not changed; ignore it
  396. return null;
  397. }
  398. } else {
  399. // If the alert does not exist, create a new MannedAlertNotification object
  400. $ewsNotification = new DataObject\EwsNotification();
  401. $ewsNotification->setKey($alert['id']);
  402. $date = date('Y-m-d', strtotime($alert['fromDate']));
  403. $ewsNotification->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/EWSNotification/' . $date));
  404. $ewsNotification->setGuid(\App\Lib\Utility::getUUID()->toRfc4122());
  405. }
  406. $ewsNotification->setIsManned(true);
  407. // Update the MannedAlertNotification object with the new alert data
  408. $ewsNotification->setAlertId($alert['id']);
  409. $ewsNotification->setTitle($alert['title']);
  410. $ewsNotification->setAlertType($this->getAlertType($alert['alertType'], $alert['alertTypeAr'], $alert['alertTypeEn']));
  411. $fromDate = \Carbon\Carbon::createFromFormat('m/d/Y h:i:s A', $alert['fromDate']);
  412. $toDate = \Carbon\Carbon::createFromFormat('m/d/Y h:i:s A', $alert['toDate']);
  413. $lastModified = \Carbon\Carbon::createFromFormat('m/d/Y h:i:s A', $alert['lastModified']);
  414. $ewsNotification->setStartDate($fromDate);
  415. $ewsNotification->setEndDate($toDate);
  416. $ewsNotification->setLastModifiedDate($lastModified);
  417. $ewsNotification->setAlertStatusCategory($this->getAlertStatusCategory($alert['alertStatusCategory'])); // need to fix
  418. $ewsNotification->setStatus("active");
  419. $alertStatus = $this->getAlertStatus($alert['alertStatusID'], $alert['alertStatusAr'], $alert['alertStatusEn']);
  420. if ($alertStatus) {
  421. $ewsNotification->setAlertStatus($alertStatus);
  422. }
  423. $alertHazardArr = [];
  424. if ($alert['alertHazards']) {
  425. foreach ($alert['alertHazards'] as $alertHazard) {
  426. $alertHazardArr[] = $this->getAlertHazard($alertHazard['id'], $alertHazard['descriptionAr'], $alertHazard['descriptionEn']);
  427. }
  428. $ewsNotification->setAlertHazard(array_filter($alertHazardArr));
  429. }
  430. $ewsNotification->setRegion($this->getRegion($alert['regionID'], $alert['regionAR'], $alert['regionEn']));
  431. $alertGovernorateArr = [];
  432. if ($alert['governorates']) {
  433. foreach ($alert['governorates'] as $alertGovernorate) {
  434. $alertGovernorateArr[] = $this->getGovernorates($alertGovernorate['id'], $alertGovernorate['nameAr'], $alertGovernorate['nameEn'], $alertGovernorate['longitude'], $alertGovernorate['latitude']);
  435. }
  436. $ewsNotification->setGovernorate(array_filter($alertGovernorateArr));
  437. }
  438. $ewsNotification->setOtherLocation($alert['otherLocationsEn'], 'en');
  439. $ewsNotification->setOtherLocation($alert['otherLocationsAr'], 'ar');
  440. if ($alert['otherLocationsAr']) {
  441. $otherLocation = \Pimcore\Model\DataObject\EwsOtherLocation::getByName($alert['otherLocationsAr'], 'en', true);
  442. $ewsNotification->setEwsOtherLocations($otherLocation);
  443. }
  444. $ewsNotification->settweetID($alert['tweetID']);
  445. $alertAlertActionArr = [];
  446. if ($alert['alertActions']) {
  447. foreach ($alert['alertActions'] as $alertAlertAction) {
  448. $alertAlertActionArr[] = $this->getAlertAction($alertAlertAction['id'], $alertAlertAction['descriptionAr'], $alertAlertAction['descriptionEn']);
  449. }
  450. $ewsNotification->setAlertAction(array_filter($alertAlertActionArr));
  451. }
  452. // Set the hash for the alert
  453. $ewsNotification->setHash($alertHash);
  454. $ewsNotification->setRawText(json_encode($alert));
  455. $ewsNotification->setIsSent(false);
  456. $ewsNotification->setOmitMandatoryCheck(true);
  457. // saving here just to get the id
  458. $ewsNotification->save();
  459. $currentDate = new \DateTime();
  460. $formattedDate = $currentDate->format('dmY') . '-' . $ewsNotification->getId();
  461. $searchIdEn = 'Early Warning System | ' . $formattedDate . ' | ' . ucfirst($ewsNotification->getAlertType()?->getColor()) . ' Alert | ' . $ewsNotification->getAlertStatus()?->getName("en");
  462. $searchIdAr = $this->translator->trans('Early Warning System', [], null, "ar") . ' | ' . $formattedDate . ' | ' . $this->translator->trans(ucfirst($ewsNotification->getAlertType()?->getColor()) . ' Alert', [], null, "ar") . ' | ' . $ewsNotification->getAlertStatus()?->getName("ar");
  463. $ewsNotification->setEwsSearchId($searchIdEn, "en");
  464. $ewsNotification->setEwsSearchId($searchIdAr, "ar");
  465. // Save the updated MannedAlertNotification object
  466. $ewsNotification->setPublished(TRUE);
  467. $ewsNotification->save();
  468. return $ewsNotification;
  469. }
  470. private function getAlertType($id, $nameAr, $nameEn)
  471. {
  472. $alertType = \Pimcore\Model\DataObject\AlertType::getByAlertTypeId($id, true);
  473. if (!$alertType) {
  474. $alertType = new \Pimcore\Model\DataObject\AlertType();
  475. $alertType->setKey($id);
  476. $alertType->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/AlertType'));
  477. $alertType->setAlertTypeId($id);
  478. $alertType->setName($nameAr, "en");
  479. $alertType->setName($nameEn, "ar");
  480. $alertType->setPublished(true);
  481. $alertType->save();
  482. }
  483. return $alertType;
  484. }
  485. private function getAlertStatusCategory($name)
  486. {
  487. $alertStatusCategory = \Pimcore\Model\DataObject\AlertStatusCategory::getByName($name, true);
  488. if (!$alertStatusCategory) {
  489. $alertStatusCategory = new \Pimcore\Model\DataObject\AlertStatusCategory();
  490. $alertStatusCategory->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/AlertStatusCategory'));
  491. $alertStatusCategory->setKey($name);
  492. $alertStatusCategory->setName($name);
  493. $alertStatusCategory->setPublished(true);
  494. $alertStatusCategory->save();
  495. }
  496. return $alertStatusCategory;
  497. }
  498. private function getAlertStatus($id, $nameAr, $nameEn)
  499. {
  500. $alertStatus = \Pimcore\Model\DataObject\AlertStatus::getByAlertStatusId($id, true);
  501. if (!$alertStatus) {
  502. $alertStatus = new \Pimcore\Model\DataObject\AlertStatus();
  503. $alertStatus->setKey($id);
  504. $alertStatus->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/AlertStatus'));
  505. $alertStatus->setAlertStatusId($id);
  506. $alertStatus->setName($nameAr, 'ar');
  507. $alertStatus->setName($nameEn, 'en');
  508. $alertStatus->setPublished(true);
  509. $alertStatus->save();
  510. }
  511. return $alertStatus;
  512. }
  513. private function getAlertHazard($id, $descriptionAr, $descriptionEn)
  514. {
  515. $alertHazard = \Pimcore\Model\DataObject\AlertHazard::getByAlertHazardId($id, true);
  516. if (!$alertHazard) {
  517. $alertHazard = new \Pimcore\Model\DataObject\AlertHazard();
  518. $alertHazard->setKey($id);
  519. $alertHazard->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/AlertHazard'));
  520. $alertHazard->setAlertHazardId($id);
  521. $alertHazard->setName($descriptionAr, "ar");
  522. $alertHazard->setName($descriptionEn, "en");
  523. $alertHazard->setPublished(true);
  524. $alertHazard->save();
  525. } else {
  526. $alertHazard->setName($descriptionAr, "ar");
  527. $alertHazard->setName($descriptionEn, "en");
  528. $alertHazard->save();
  529. }
  530. return $alertHazard;
  531. }
  532. private function getRegion($id, $nameAr, $nameEn)
  533. {
  534. $region = \Pimcore\Model\DataObject\Region::getByRegionId($id, true);
  535. if (!$region) {
  536. $region = new \Pimcore\Model\DataObject\Region();
  537. $region->setKey($id);
  538. $region->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/Region'));
  539. $region->setRegionId($id);
  540. $region->setName($nameAr, 'ar');
  541. $region->setName($nameEn, 'en');
  542. $region->setPublished(true);
  543. $region->save();
  544. }
  545. return $region;
  546. }
  547. private function getGovernorates($id, $nameAr, $nameEn, $longitude, $latitude)
  548. {
  549. $governorate = \Pimcore\Model\DataObject\Governorate::getByGovernoteId($id, true);
  550. if (!$governorate) {
  551. $governorate = new \Pimcore\Model\DataObject\Governorate();
  552. $governorate->setKey($id);
  553. $governorate->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/Governorate'));
  554. $governorate->setGovernoteId($id);
  555. $governorate->setName($nameAr, "ar");
  556. $governorate->setName($nameEn, "en");
  557. $governorate->setLongitude($longitude);
  558. $governorate->setLatitude($latitude);
  559. $governorate->setPublished(true);
  560. $governorate->save();
  561. }
  562. return $governorate;
  563. }
  564. private function getAlertAction($id, $descriptionAr, $descriptionEn)
  565. {
  566. $alertAction = \Pimcore\Model\DataObject\AlertAction::getByAlertActionId($id, true);
  567. if (!$alertAction) {
  568. $alertAction = new \Pimcore\Model\DataObject\AlertAction();
  569. $alertAction->setKey($id);
  570. $alertAction->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath('/MannedAlert/Master/AlertAction'));
  571. $alertAction->setAlertActionId($id);
  572. $alertAction->setName($descriptionAr, "ar");
  573. $alertAction->setName($descriptionEn, "en");
  574. $alertAction->setPublished(true);
  575. $alertAction->save();
  576. }
  577. return $alertAction;
  578. }
  579. }