src/EventSubscriber/TwitterEventListner.php line 200

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Carbon\Carbon;
  4. use Pimcore\Mail;
  5. use Pimcore\Model\DataObject;
  6. use Pimcore\Event\Model\DataObjectEvent;
  7. use Pimcore\Model\DataObject\EwsNotification;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use App\Service\TwitterService;
  10. use App\C2IntegrationBundle\Service\C2Service;
  11. use Symfony\Component\Templating\EngineInterface;
  12. use Knp\Snappy\Pdf;
  13. use Knp\Snappy\Image;
  14. class TwitterEventListner
  15. {
  16.     private $twitterService;
  17.     private $c2Service;
  18.     public function __construct(private Pdf $snappy, private Image $snappyImage, private EngineInterface $twig)
  19.     {
  20.         $this->twitterService = new TwitterService();
  21.         $this->c2Service = new C2Service();
  22.     }
  23.     public function onObjectCreate(DataObjectEvent $ewsNotificationObject)
  24.     {
  25.         $ewsNotification $ewsNotificationObject->getObject();
  26.         
  27.         if (
  28.             ($ewsNotification instanceof EwsNotification) &&
  29.             ($ewsNotification->isPublished(true)) &&
  30.             empty($ewsNotification->getTwitterId())  &&
  31.             empty($ewsNotification->getTwitterLog()) &&
  32.             $ewsNotification->getEnableTwitterNotification() == true &&
  33.             $ewsNotification->getStatus() != "ended"
  34.         ) {
  35.             $governates $ewsNotification->getGovernorate();
  36.             $startDate $ewsNotification->getStartDate()->format('Y-m-d');
  37.             $currentDate Carbon::now();
  38.             $targetDate Carbon::parse($startDate);
  39.             $content '';
  40.             $parameters = [];
  41.             if (!empty($ewsNotification->getRegion())) {
  42.                 // $content .= 'Region : '. $ewsNotification->getRegion()->getName('en')  ." \n";
  43.                 $parameters['region'] = $ewsNotification->getRegion()->getName('ar');
  44.             }
  45.             if (!empty($ewsNotification->getAlertAction())) {
  46.                 $alertActions = [];
  47.                 //$content .= 'Alert Action: ';
  48.                 foreach ($ewsNotification->getAlertAction() as $alertAction) {
  49.                     // $content .= $alertAction->getName() .' , ';
  50.                     $alertActions[] = $alertAction->getName('ar');
  51.                 }
  52.                 // $content .= " \n";
  53.                 $parameters['alertAction'] = $alertActions;
  54.             }
  55.             if (!empty($ewsNotification->getAlertType())) {
  56.                 $parameters['alertType'] = $ewsNotification->getAlertType()->getName('en');
  57.                 
  58.                 // $content .= 'Alert Type: ' . $ewsNotification->getAlertType()->getName() ." \n";
  59.             }
  60.             if (!empty($ewsNotification->getWeatherPhenomenon())) {
  61.                 $parameters['weatherPhenomenon'] = $ewsNotification->getWeatherPhenomenon()->getTitle('en');
  62.                 // $parameters['alertStatusAr'] = $ewsNotification->getWeatherPhenomenon()->getTitle('ar');
  63.                 $parameters['alertStatusAr'] = '';
  64.                 //$content .= 'Weather Phenomenon: ' . $ewsNotification->getWeatherPhenomenon()->getTitle() ." \n";
  65.             }
  66.             
  67.             if (!empty($ewsNotification->getAlertHazard())) {
  68.                 $alertHazards = [];
  69.                 //$content .= 'Alert Hazards: ';
  70.                 foreach ($ewsNotification->getAlertHazard() as $hazard) {
  71.                     //$content .= $hazard->getName() .' , ';
  72.                     $alertHazards[] = $hazard->getName('ar');
  73.                 }
  74.                 $parameters['alertHazards'] = $alertHazards;
  75.                 //$content .= " \n";
  76.             }
  77.             if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  78.                 $governorates = [];
  79.                 //$content .= 'Governorate(s) Affected: ';
  80.                 foreach ($ewsNotification->getGovernorate() as $governorate) {
  81.                     $governorates[] = $governorate->getName('ar');
  82.                     //$content .= $governorate->getName() .' , ';
  83.                 }
  84.                 $parameters['governorateAffected'] = $governorates;
  85.                 //$content .= " \n";
  86.             }
  87.             //$content .= 'Alert Start Date: ' . (!empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  88.             //$content .= 'Alert End Date: ' . (!empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  89.             $parameters['alertStartDate'] = !empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown';
  90.             $parameters['alertEndDate'] = !empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown';
  91.             // $content .= 'Posted Today Date: ' . date('Y-m-d h:i:sa') ." \n";
  92.             // if (!empty($ewsNotification->getAttachment())) {
  93.             //     $attachmentFile = API_BASE_URL.'/'.$ewsNotification->getAttachment()->getFullPath();
  94.             //     $content .= 'Attachment : '. $attachmentFile ." \n";
  95.             // }
  96.             $HashTagcontent "";
  97.             if (!empty($ewsNotification->getRegion())) {
  98.                 $regionName $ewsNotification->getRegion()->getName('ar');
  99.                 $regionName str_replace(' ''_'$regionName);
  100.                 // $HashTagcontent .= "اسم المنطقة: #$regionName\n";
  101.             }
  102.             if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  103.                 // $HashTagcontent .= "المحافظات المتأثرة: ";
  104.                 foreach ($ewsNotification->getGovernorate() as $governorate) {
  105.                     $govName $governorate->getName('ar');
  106.                     // $HashTagcontent .= "#$govName, ";
  107.                 }
  108.                 // $HashTagcontent = rtrim($HashTagcontent, ', ');
  109.                 // $HashTagcontent .= "\n";
  110.             }
  111.             // $HashTagcontent .= "#الإنذار_المبكر #طقس_السعودية #المركز_الوطني_للأرصاد";
  112.             // $HashTagcontent .= "";
  113.             
  114.             $HashTagcontent $this->buildXPostCaption($ewsNotification);
  115.             
  116.             $parameters['furtherInformation'] = 'https://beta.ncm.gov.sa/en/early-warning/' $ewsNotification->getId();
  117.             // $content .= "For further information visit: https://beta.ncm.gov.sa/en/early-warning/" . $ewsNotification->getId() . " \n";
  118.             
  119.             // $html = $this->twig->render('image_templates/x-post-template.html.twig', $parameters);
  120.             // $this->snappyImage->setOption('enable-local-file-access', true);
  121.             // $this->snappyImage->setOption('width', '787');
  122.             // $this->snappyImage->setOption('height', '797');
  123.             // $this->snappyImage->setOption('format', 'png');
  124.             // $image = $this->snappyImage->getOutputFromHtml($html);
  125.             // $asset = \App\Lib\Utility::createAsset($image, rand(0, 10000) . '_' . time() . '_custom_weather_report.jpeg', 'EWS Twitter Images');
  126.             // $asset_path = PIMCORE_PROJECT_ROOT . '/public/var/assets' . $asset->getPath() . $asset->getFileName();
  127.             // $tweet = $this->twitterService->uploadMedia($asset_path,$HashTagcontent);
  128.             $xPostMedia $this->getXPostMedia($ewsNotification);
  129.             $tweet $this->twitterService->uploadMedia($xPostMedia['paths'], $HashTagcontent);
  130.             if ($tweet) {
  131.                 // $tweet = $this->twitterService->postTweet('', $media_id);
  132.                 // $twitterResponse = $this->c2Service->sendTwitterTweetResponse($_ENV['TWITTER_POST_TEMPLATE'], $ewsNotification->getId(), $html);
  133.                 if(isset($tweet['result']->data)){
  134.                     $tweet_text $tweet['result']?->data?->text;
  135.                     preg_match('/https?:\/\/\S+/'$tweet_text$matches);
  136.                     $url $matches[0] ?? null;
  137.                     $twitterResponse $this->c2Service->addAsset($xPostMedia['assets'], $url);
  138.                 }
  139.               
  140.                 // $tweet = $this->twitterService->postTweet('', $media_id);
  141.                 // $twitterResponse = $this->c2Service->sendTwitterTweetResponse($_ENV['TWITTER_POST_TEMPLATE'], $ewsNotification->getId(), $html, $url);
  142.                 // $asset->delete();
  143.                 if (!empty($tweet['tweetId'])) {
  144.                     $ewsNotification->setTwitterId($tweet['tweetId']);
  145.                 }
  146.     
  147.                 $ewsNotification->setTwitterLog($tweet['data']);
  148.                 $ewsNotification->save();
  149.     
  150.                 
  151.                 if ($tweet['httpCode'] === 200 || $tweet['httpCode'] === 201 || $tweet['httpCode'] === 204) {
  152.                     
  153.                 }
  154.                 elseif ($tweet['httpCode'] === 403) {
  155.                     // Forbidden - Duplicate content error
  156.                     $responseData json_decode($tweet['data'], true);
  157.                     $errorMessage $responseData['detail'];
  158.                     // echo $errorMessage;
  159.                 } else {
  160.                     // echo 'Unexpected HTTP status code: ' . $tweet['httpCode'];
  161.                 }
  162.             }
  163.             // $tweet = $this->twitterService->postTweet($content);
  164.             
  165.             
  166.             // echo 'POST_ID : '.$tweet['tweetId']. ' EWS_ID : '.$ewsNotification->getId(). ' , ';        
  167.         }
  168.     }
  169.     public function onObjectUpdate(DataObjectEvent $ewsNotificationObject)
  170.     {
  171.         $ewsNotification $ewsNotificationObject->getObject();
  172.         if (
  173.             ($ewsNotification instanceof EwsNotification) &&
  174.         ($ewsNotification->isPublished(true)) &&
  175.         empty($ewsNotification->getTwitterId())  &&
  176.             empty($ewsNotification->getTwitterLog()) &&
  177.             $ewsNotification->getEnableTwitterNotification() == true &&
  178.             $ewsNotification->getStatus() != "ended"
  179.         ) {
  180.             $governates $ewsNotification->getGovernorate();
  181.             $startDate $ewsNotification->getStartDate()->format('Y-m-d');
  182.             $currentDate Carbon::now();
  183.             $targetDate Carbon::parse($startDate);
  184.             $content '';
  185.             $parameters = [];
  186.             if (!empty($ewsNotification->getRegion())) {
  187.                // $content .= 'Region : '. $ewsNotification->getRegion()->getName('en')  ." \n";
  188.                 $parameters['region'] = $ewsNotification->getRegion()->getName('ar');
  189.             }
  190.             if (!empty($ewsNotification->getAlertAction())) {
  191.                // $content .= 'Alert Action: ';
  192.                 $alertActions = [];
  193.                 foreach ($ewsNotification->getAlertAction() as $alertAction) {
  194.                     $alertActions[] = $alertAction->getName('ar');
  195.                     //$content .= $alertAction->getName() .' , ';
  196.                 }
  197.                 $parameters['alertAction'] = $alertActions;
  198.                 //$content .= " \n";
  199.             }
  200.             if (!empty($ewsNotification->getAlertType())) {
  201.                 $parameters['alertType'] = $ewsNotification->getAlertType()->getName('en');
  202.                 //$content .= 'Alert Type: ' . $ewsNotification->getAlertType()->getColor() ." \n";
  203.             }
  204.             if (!empty($ewsNotification->getWeatherPhenomenon())) {
  205.                 $parameters['weatherPhenomenon'] = $ewsNotification->getWeatherPhenomenon()->getTitle('en');
  206.                 $parameters['alertStatusAr'] = $ewsNotification->getWeatherPhenomenon()->getTitle('ar');
  207.                 //$content .= 'Weather Phenomenon: ' . $ewsNotification->getWeatherPhenomenon()->getTitle() ." \n";
  208.             }
  209.             
  210.             if (!empty($ewsNotification->getAlertHazard())) {
  211.                 $alertHazards = [];
  212.                 //$content .= 'Alert Hazards: ';
  213.                 foreach ($ewsNotification->getAlertHazard() as $hazard) {
  214.                     $alertHazards[] = $hazard->getName('ar');
  215.                     //$content .= $hazard->getName() .' , ';
  216.                 }
  217.                 $parameters['alertHazards'] = $alertHazards;
  218.                 //$content .= " \n";
  219.             }
  220.             if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  221.                 //$content .= 'Governorate(s) Affected: ';
  222.                 $governorates = [];
  223.                 foreach ($ewsNotification->getGovernorate() as $governorate) {
  224.                     $governorates[] = $governorate->getName('ar');
  225.                     //$content .= $governorate->getName() .' , ';
  226.                 }
  227.                 $parameters['governorateAffected'] = $governorates;
  228.                 //$content .= " \n";
  229.             }
  230.             //$content .= 'Alert Start  Date: ' . (!empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  231.             //$content .= 'Alert End Date: ' . (!empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  232.             $parameters['alertStartDate'] = !empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown';
  233.             $parameters['alertEndDate'] = !empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown';
  234.             // $content .= 'Posted Today Date: ' . date('Y-m-d h:i:sa') ." \n";
  235.             // if (!empty($ewsNotification->getAttachment())) {
  236.             //     $attachmentFile = API_BASE_URL.'/'.$ewsNotification->getAttachment()->getFullPath();
  237.             //     $content .= 'Attachment : '. $attachmentFile ." \n";
  238.             // }
  239.             //$content .= "For further information visit: https://beta.ncm.gov.sa/en/early-warning/" . $ewsNotification->getId() . " \n";
  240.             $HashTagcontent "";
  241.             if (!empty($ewsNotification->getRegion())) {
  242.                 $regionName $ewsNotification->getRegion()->getName('ar');
  243.                 $regionName str_replace(' ''_'$regionName);
  244.                 // $HashTagcontent .= "اسم المنطقة: #$regionName\n";
  245.             }
  246.             if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  247.                 // $HashTagcontent .= "المحافظات المتأثرة: ";
  248.                 foreach ($ewsNotification->getGovernorate() as $governorate) {
  249.                     $govName $governorate->getName('ar');
  250.                     // $HashTagcontent .= "#$govName, ";
  251.                 }
  252.                 // $HashTagcontent = rtrim($HashTagcontent, ', ');
  253.                 // $HashTagcontent .= "\n";
  254.             }
  255.             // $HashTagcontent .= "#الإنذار_المبكر #طقس_السعودية #المركز_الوطني_للأرصاد";
  256.             // $HashTagcontent .= "";
  257.             $HashTagcontent $this->buildXPostCaption($ewsNotification);
  258.             $parameters['furtherInformation'] = 'https://beta.ncm.gov.sa/en/early-warning/' $ewsNotification->getId();
  259.             // $html = $this->twig->render('image_templates/x-post-template.html.twig', $parameters);
  260.             
  261.             // $this->snappyImage->setOption('enable-local-file-access', true);
  262.             // $this->snappyImage->setOption('width', '787');
  263.             // $this->snappyImage->setOption('height', '797');
  264.             // $this->snappyImage->setOption('format', 'png');
  265.             // $image = $this->snappyImage->getOutputFromHtml($html);
  266.             // $asset = \App\Lib\Utility::createAsset($image, rand(0, 10000) . '_' . time() . '_custom_weather_report.jpeg', 'EWS Twitter Images');
  267.             // $asset_path = PIMCORE_PROJECT_ROOT . '/public/var/assets' . $asset->getPath() . $asset->getFileName();
  268.             // $tweet = $this->twitterService->uploadMedia($asset_path, $HashTagcontent);
  269.             $xPostMedia $this->getXPostMedia($ewsNotification);
  270.             $tweet $this->twitterService->uploadMedia($xPostMedia['paths'], $HashTagcontent);
  271.             if ($tweet) {
  272.                 if(isset($tweet['result']->data)){
  273.                     $tweet_text $tweet['result']?->data?->text;
  274.                     preg_match('/https?:\/\/\S+/'$tweet_text$matches);
  275.                     $url $matches[0] ?? null;
  276.                     // $tweet = $this->twitterService->postTweet('', $media_id);
  277.                     // $twitterResponse = $this->c2Service->sendTwitterTweetResponse($_ENV['TWITTER_POST_TEMPLATE'], $ewsNotification->getId(), $html, $url);
  278.                     // $twitterResponse = $this->c2Service->addAsset([$asset],$url);
  279.                     // $asset->delete();
  280.                     $twitterResponse $this->c2Service->addAsset($xPostMedia['assets'], $url);
  281.                 }
  282.                 if (!empty($tweet['tweetId'])) {
  283.                     $ewsNotification->setTwitterId($tweet['tweetId']);
  284.                 }
  285.                 $ewsNotification->setTwitterLog($tweet['data']);
  286.                 $ewsNotification->save();
  287.                 if ($tweet['httpCode'] === 200 || $tweet['httpCode'] === 201 || $tweet['httpCode'] === 204) {
  288.                 }
  289.                 elseif ($tweet['httpCode'] === 403) {
  290.                     // Forbidden - Duplicate content error
  291.                     $responseData json_decode($tweet['data'], true);
  292.                     $errorMessage $responseData['detail'];
  293.                     // echo $errorMessage;
  294.                 } else {
  295.                     // echo 'Unexpected HTTP status code: ' . $tweet['httpCode'];
  296.                 }
  297.             }
  298.             //$tweet = $this->twitterService->postTweet($content);
  299.             
  300.             // echo 'POST_ID : '.$tweet['tweetId']. ' EWS_ID : '.$ewsNotification->getId(). ' , ';
  301.         }
  302.     }
  303.     private function getXPostMedia(EwsNotification $ewsNotification): array
  304.     {
  305.         $paths = [];
  306.         $assets = [];
  307.         $assetRoot PIMCORE_PROJECT_ROOT '/public/var/assets';
  308.         foreach ([
  309.             $ewsNotification->getXAttachment(),
  310.             $ewsNotification->getPolygonXAtachment(),
  311.             //$ewsNotification->getXHistoryAtachment(),
  312.         ] as $asset) {
  313.             if ($asset) {
  314.                 $paths[] = $assetRoot $asset->getPath() . $asset->getFileName();
  315.                 $assets[] = $asset;
  316.             }
  317.         }
  318.         return ['paths' => $paths'assets' => $assets];
  319.     }
  320.     /**
  321.      * Replace old NCM alert page URLs in the X caption with the public portal early-warning link.
  322.      */
  323.     private function buildXPostCaption(EwsNotification $ewsNotification): string
  324.     {
  325.         $caption = (string) $ewsNotification->getXPost();
  326.         $earlyWarningUrl 'https://beta.ncm.gov.sa/en/early-warning/' $ewsNotification->getId();
  327.         $replaced preg_replace(
  328.             '#https?://(?:www\.)?ncm\.gov\.sa/[Aa]r/alert/[Pp]ages\S*#u',
  329.             $earlyWarningUrl,
  330.             $caption
  331.         );
  332.         return is_string($replaced) ? $replaced $caption;
  333.     }
  334. }