src/EventSubscriber/TwitterEventListner.php line 201

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. if (
  27. ($ewsNotification instanceof EwsNotification) &&
  28. ($ewsNotification->isPublished(true)) &&
  29. empty($ewsNotification->getTwitterId()) &&
  30. empty($ewsNotification->getTwitterLog()) &&
  31. $ewsNotification->getEnableTwitterNotification() == true &&
  32. $ewsNotification->getStatus() != "ended"
  33. ) {
  34. $governates = $ewsNotification->getGovernorate();
  35. $startDate = $ewsNotification->getStartDate()->format('Y-m-d');
  36. $currentDate = Carbon::now();
  37. $targetDate = Carbon::parse($startDate);
  38. $content = '';
  39. $parameters = [];
  40. if (!empty($ewsNotification->getRegion())) {
  41. // $content .= 'Region : '. $ewsNotification->getRegion()->getName('en') ." \n";
  42. $parameters['region'] = $ewsNotification->getRegion()->getName('ar');
  43. }
  44. if (!empty($ewsNotification->getAlertAction())) {
  45. $alertActions = [];
  46. //$content .= 'Alert Action: ';
  47. foreach ($ewsNotification->getAlertAction() as $alertAction) {
  48. // $content .= $alertAction->getName() .' , ';
  49. $alertActions[] = $alertAction->getName('ar');
  50. }
  51. // $content .= " \n";
  52. $parameters['alertAction'] = $alertActions;
  53. }
  54. if (!empty($ewsNotification->getAlertType())) {
  55. $parameters['alertType'] = $ewsNotification->getAlertType()->getName('en');
  56. // $content .= 'Alert Type: ' . $ewsNotification->getAlertType()->getName() ." \n";
  57. }
  58. if (!empty($ewsNotification->getWeatherPhenomenon())) {
  59. $parameters['weatherPhenomenon'] = $ewsNotification->getWeatherPhenomenon()->getTitle('en');
  60. // $parameters['alertStatusAr'] = $ewsNotification->getWeatherPhenomenon()->getTitle('ar');
  61. $parameters['alertStatusAr'] = '';
  62. //$content .= 'Weather Phenomenon: ' . $ewsNotification->getWeatherPhenomenon()->getTitle() ." \n";
  63. }
  64. if (!empty($ewsNotification->getAlertHazard())) {
  65. $alertHazards = [];
  66. //$content .= 'Alert Hazards: ';
  67. foreach ($ewsNotification->getAlertHazard() as $hazard) {
  68. //$content .= $hazard->getName() .' , ';
  69. $alertHazards[] = $hazard->getName('ar');
  70. }
  71. $parameters['alertHazards'] = $alertHazards;
  72. //$content .= " \n";
  73. }
  74. if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  75. $governorates = [];
  76. //$content .= 'Governorate(s) Affected: ';
  77. foreach ($ewsNotification->getGovernorate() as $governorate) {
  78. $governorates[] = $governorate->getName('ar');
  79. //$content .= $governorate->getName() .' , ';
  80. }
  81. $parameters['governorateAffected'] = $governorates;
  82. //$content .= " \n";
  83. }
  84. //$content .= 'Alert Start Date: ' . (!empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  85. //$content .= 'Alert End Date: ' . (!empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  86. $parameters['alertStartDate'] = !empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown';
  87. $parameters['alertEndDate'] = !empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown';
  88. // $content .= 'Posted Today Date: ' . date('Y-m-d h:i:sa') ." \n";
  89. // if (!empty($ewsNotification->getAttachment())) {
  90. // $attachmentFile = API_BASE_URL.'/'.$ewsNotification->getAttachment()->getFullPath();
  91. // $content .= 'Attachment : '. $attachmentFile ." \n";
  92. // }
  93. $HashTagcontent = "";
  94. if (!empty($ewsNotification->getRegion())) {
  95. $regionName = $ewsNotification->getRegion()->getName('ar');
  96. $regionName = str_replace(' ', '_', $regionName);
  97. // $HashTagcontent .= "اسم المنطقة: #$regionName\n";
  98. }
  99. if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  100. // $HashTagcontent .= "المحافظات المتأثرة: ";
  101. foreach ($ewsNotification->getGovernorate() as $governorate) {
  102. $govName = $governorate->getName('ar');
  103. // $HashTagcontent .= "#$govName, ";
  104. }
  105. // $HashTagcontent = rtrim($HashTagcontent, ', ');
  106. // $HashTagcontent .= "\n";
  107. }
  108. // $HashTagcontent .= "#الإنذار_المبكر #طقس_السعودية #المركز_الوطني_للأرصاد";
  109. // $HashTagcontent .= "";
  110. $HashTagcontent = $ewsNotification->getXPost();
  111. $parameters['furtherInformation'] = "https://ncm.gov.sa/Ar/alert/Pages/default.aspx";
  112. // $content .= "For further information visit: https://ncm.gov.sa/Ar/alert/Pages/default.aspx \n";
  113. // $html = $this->twig->render('image_templates/x-post-template.html.twig', $parameters);
  114. // $this->snappyImage->setOption('enable-local-file-access', true);
  115. // $this->snappyImage->setOption('width', '787');
  116. // $this->snappyImage->setOption('height', '797');
  117. // $this->snappyImage->setOption('format', 'png');
  118. // $image = $this->snappyImage->getOutputFromHtml($html);
  119. // $asset = \App\Lib\Utility::createAsset($image, rand(0, 10000) . '_' . time() . '_custom_weather_report.jpeg', 'EWS Twitter Images');
  120. // $asset_path = PIMCORE_PROJECT_ROOT . '/public/var/assets' . $asset->getPath() . $asset->getFileName();
  121. // $tweet = $this->twitterService->uploadMedia($asset_path,$HashTagcontent);
  122. $xPost = PIMCORE_PROJECT_ROOT . '/public/var/assets' .$ewsNotification->getXAttachment()->getPath() . $ewsNotification->getXAttachment()->getFileName();
  123. $xPostAssets = $ewsNotification->getXAttachment();
  124. $tweet = $this->twitterService->uploadMedia($xPost,$HashTagcontent);
  125. if ($tweet) {
  126. // $tweet = $this->twitterService->postTweet('', $media_id);
  127. // $twitterResponse = $this->c2Service->sendTwitterTweetResponse($_ENV['TWITTER_POST_TEMPLATE'], $ewsNotification->getId(), $html);
  128. if(isset($tweet['result']->data)){
  129. $tweet_text = $tweet['result']?->data?->text;
  130. preg_match('/https?:\/\/\S+/', $tweet_text, $matches);
  131. $url = $matches[0] ?? null;
  132. $twitterResponse = $this->c2Service->addAsset([$xPostAssets],$url);
  133. }
  134. // $tweet = $this->twitterService->postTweet('', $media_id);
  135. // $twitterResponse = $this->c2Service->sendTwitterTweetResponse($_ENV['TWITTER_POST_TEMPLATE'], $ewsNotification->getId(), $html, $url);
  136. // $asset->delete();
  137. if (!empty($tweet['tweetId'])) {
  138. $ewsNotification->setTwitterId($tweet['tweetId']);
  139. }
  140. $ewsNotification->setTwitterLog($tweet['data']);
  141. $ewsNotification->save();
  142. if ($tweet['httpCode'] === 200 || $tweet['httpCode'] === 201 || $tweet['httpCode'] === 204) {
  143. }
  144. elseif ($tweet['httpCode'] === 403) {
  145. // Forbidden - Duplicate content error
  146. $responseData = json_decode($tweet['data'], true);
  147. $errorMessage = $responseData['detail'];
  148. // echo $errorMessage;
  149. } else {
  150. // echo 'Unexpected HTTP status code: ' . $tweet['httpCode'];
  151. }
  152. }
  153. // $tweet = $this->twitterService->postTweet($content);
  154. // echo 'POST_ID : '.$tweet['tweetId']. ' EWS_ID : '.$ewsNotification->getId(). ' , ';
  155. }
  156. }
  157. public function onObjectUpdate(DataObjectEvent $ewsNotificationObject)
  158. {
  159. $ewsNotification = $ewsNotificationObject->getObject();
  160. if (
  161. ($ewsNotification instanceof EwsNotification) &&
  162. ($ewsNotification->isPublished(true)) &&
  163. $ewsNotification->getEnableTwitterNotification() == true &&
  164. $ewsNotification->getStatus() != "ended"
  165. ) {
  166. $governates = $ewsNotification->getGovernorate();
  167. $startDate = $ewsNotification->getStartDate()->format('Y-m-d');
  168. $currentDate = Carbon::now();
  169. $targetDate = Carbon::parse($startDate);
  170. $content = '';
  171. $parameters = [];
  172. if (!empty($ewsNotification->getRegion())) {
  173. // $content .= 'Region : '. $ewsNotification->getRegion()->getName('en') ." \n";
  174. $parameters['region'] = $ewsNotification->getRegion()->getName('ar');
  175. }
  176. if (!empty($ewsNotification->getAlertAction())) {
  177. // $content .= 'Alert Action: ';
  178. $alertActions = [];
  179. foreach ($ewsNotification->getAlertAction() as $alertAction) {
  180. $alertActions[] = $alertAction->getName('ar');
  181. //$content .= $alertAction->getName() .' , ';
  182. }
  183. $parameters['alertAction'] = $alertActions;
  184. //$content .= " \n";
  185. }
  186. if (!empty($ewsNotification->getAlertType())) {
  187. $parameters['alertType'] = $ewsNotification->getAlertType()->getName('en');
  188. //$content .= 'Alert Type: ' . $ewsNotification->getAlertType()->getColor() ." \n";
  189. }
  190. if (!empty($ewsNotification->getWeatherPhenomenon())) {
  191. $parameters['weatherPhenomenon'] = $ewsNotification->getWeatherPhenomenon()->getTitle('en');
  192. $parameters['alertStatusAr'] = $ewsNotification->getWeatherPhenomenon()->getTitle('ar');
  193. //$content .= 'Weather Phenomenon: ' . $ewsNotification->getWeatherPhenomenon()->getTitle() ." \n";
  194. }
  195. if (!empty($ewsNotification->getAlertHazard())) {
  196. $alertHazards = [];
  197. //$content .= 'Alert Hazards: ';
  198. foreach ($ewsNotification->getAlertHazard() as $hazard) {
  199. $alertHazards[] = $hazard->getName('ar');
  200. //$content .= $hazard->getName() .' , ';
  201. }
  202. $parameters['alertHazards'] = $alertHazards;
  203. //$content .= " \n";
  204. }
  205. if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  206. //$content .= 'Governorate(s) Affected: ';
  207. $governorates = [];
  208. foreach ($ewsNotification->getGovernorate() as $governorate) {
  209. $governorates[] = $governorate->getName('ar');
  210. //$content .= $governorate->getName() .' , ';
  211. }
  212. $parameters['governorateAffected'] = $governorates;
  213. //$content .= " \n";
  214. }
  215. //$content .= 'Alert Start Date: ' . (!empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  216. //$content .= 'Alert End Date: ' . (!empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  217. $parameters['alertStartDate'] = !empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown';
  218. $parameters['alertEndDate'] = !empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown';
  219. // $content .= 'Posted Today Date: ' . date('Y-m-d h:i:sa') ." \n";
  220. // if (!empty($ewsNotification->getAttachment())) {
  221. // $attachmentFile = API_BASE_URL.'/'.$ewsNotification->getAttachment()->getFullPath();
  222. // $content .= 'Attachment : '. $attachmentFile ." \n";
  223. // }
  224. //$content .= "For further information visit: https://ncm.gov.sa/Ar/alert/Pages/default.aspx \n";
  225. $HashTagcontent = "";
  226. if (!empty($ewsNotification->getRegion())) {
  227. $regionName = $ewsNotification->getRegion()->getName('ar');
  228. $regionName = str_replace(' ', '_', $regionName);
  229. // $HashTagcontent .= "اسم المنطقة: #$regionName\n";
  230. }
  231. if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  232. // $HashTagcontent .= "المحافظات المتأثرة: ";
  233. foreach ($ewsNotification->getGovernorate() as $governorate) {
  234. $govName = $governorate->getName('ar');
  235. // $HashTagcontent .= "#$govName, ";
  236. }
  237. // $HashTagcontent = rtrim($HashTagcontent, ', ');
  238. // $HashTagcontent .= "\n";
  239. }
  240. // $HashTagcontent .= "#الإنذار_المبكر #طقس_السعودية #المركز_الوطني_للأرصاد";
  241. // $HashTagcontent .= "";
  242. $HashTagcontent = $ewsNotification->getXPost();
  243. $parameters['furtherInformation'] = "https://ncm.gov.sa/Ar/alert/Pages/default.aspx";
  244. // $html = $this->twig->render('image_templates/x-post-template.html.twig', $parameters);
  245. // $this->snappyImage->setOption('enable-local-file-access', true);
  246. // $this->snappyImage->setOption('width', '787');
  247. // $this->snappyImage->setOption('height', '797');
  248. // $this->snappyImage->setOption('format', 'png');
  249. // $image = $this->snappyImage->getOutputFromHtml($html);
  250. // $asset = \App\Lib\Utility::createAsset($image, rand(0, 10000) . '_' . time() . '_custom_weather_report.jpeg', 'EWS Twitter Images');
  251. // $asset_path = PIMCORE_PROJECT_ROOT . '/public/var/assets' . $asset->getPath() . $asset->getFileName();
  252. // $tweet = $this->twitterService->uploadMedia($asset_path, $HashTagcontent);
  253. $xPost = PIMCORE_PROJECT_ROOT . '/public/var/assets' .$ewsNotification->getXAttachment()->getPath() . $ewsNotification->getXAttachment()->getFileName();
  254. $xPostAssets = $ewsNotification->getXAttachment();
  255. $tweet = $this->twitterService->uploadMedia($xPost, $HashTagcontent);
  256. if ($tweet) {
  257. if(isset($tweet['result']->data)){
  258. $tweet_text = $tweet['result']?->data?->text;
  259. preg_match('/https?:\/\/\S+/', $tweet_text, $matches);
  260. $url = $matches[0] ?? null;
  261. // $tweet = $this->twitterService->postTweet('', $media_id);
  262. // $twitterResponse = $this->c2Service->sendTwitterTweetResponse($_ENV['TWITTER_POST_TEMPLATE'], $ewsNotification->getId(), $html, $url);
  263. // $twitterResponse = $this->c2Service->addAsset([$asset],$url);
  264. // $asset->delete();
  265. $twitterResponse = $this->c2Service->addAsset([$xPostAssets],$url);
  266. }
  267. if (!empty($tweet['tweetId'])) {
  268. $ewsNotification->setTwitterId($tweet['tweetId']);
  269. }
  270. $ewsNotification->setTwitterLog($tweet['data']);
  271. $ewsNotification->save();
  272. if ($tweet['httpCode'] === 200 || $tweet['httpCode'] === 201 || $tweet['httpCode'] === 204) {
  273. }
  274. elseif ($tweet['httpCode'] === 403) {
  275. // Forbidden - Duplicate content error
  276. $responseData = json_decode($tweet['data'], true);
  277. $errorMessage = $responseData['detail'];
  278. // echo $errorMessage;
  279. } else {
  280. // echo 'Unexpected HTTP status code: ' . $tweet['httpCode'];
  281. }
  282. }
  283. //$tweet = $this->twitterService->postTweet($content);
  284. // echo 'POST_ID : '.$tweet['tweetId']. ' EWS_ID : '.$ewsNotification->getId(). ' , ';
  285. }
  286. }
  287. }