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. empty($ewsNotification->getTwitterId()) &&
  164. empty($ewsNotification->getTwitterLog()) &&
  165. $ewsNotification->getEnableTwitterNotification() == true &&
  166. $ewsNotification->getStatus() != "ended"
  167. ) {
  168. $governates = $ewsNotification->getGovernorate();
  169. $startDate = $ewsNotification->getStartDate()->format('Y-m-d');
  170. $currentDate = Carbon::now();
  171. $targetDate = Carbon::parse($startDate);
  172. $content = '';
  173. $parameters = [];
  174. if (!empty($ewsNotification->getRegion())) {
  175. // $content .= 'Region : '. $ewsNotification->getRegion()->getName('en') ." \n";
  176. $parameters['region'] = $ewsNotification->getRegion()->getName('ar');
  177. }
  178. if (!empty($ewsNotification->getAlertAction())) {
  179. // $content .= 'Alert Action: ';
  180. $alertActions = [];
  181. foreach ($ewsNotification->getAlertAction() as $alertAction) {
  182. $alertActions[] = $alertAction->getName('ar');
  183. //$content .= $alertAction->getName() .' , ';
  184. }
  185. $parameters['alertAction'] = $alertActions;
  186. //$content .= " \n";
  187. }
  188. if (!empty($ewsNotification->getAlertType())) {
  189. $parameters['alertType'] = $ewsNotification->getAlertType()->getName('en');
  190. //$content .= 'Alert Type: ' . $ewsNotification->getAlertType()->getColor() ." \n";
  191. }
  192. if (!empty($ewsNotification->getWeatherPhenomenon())) {
  193. $parameters['weatherPhenomenon'] = $ewsNotification->getWeatherPhenomenon()->getTitle('en');
  194. $parameters['alertStatusAr'] = $ewsNotification->getWeatherPhenomenon()->getTitle('ar');
  195. //$content .= 'Weather Phenomenon: ' . $ewsNotification->getWeatherPhenomenon()->getTitle() ." \n";
  196. }
  197. if (!empty($ewsNotification->getAlertHazard())) {
  198. $alertHazards = [];
  199. //$content .= 'Alert Hazards: ';
  200. foreach ($ewsNotification->getAlertHazard() as $hazard) {
  201. $alertHazards[] = $hazard->getName('ar');
  202. //$content .= $hazard->getName() .' , ';
  203. }
  204. $parameters['alertHazards'] = $alertHazards;
  205. //$content .= " \n";
  206. }
  207. if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  208. //$content .= 'Governorate(s) Affected: ';
  209. $governorates = [];
  210. foreach ($ewsNotification->getGovernorate() as $governorate) {
  211. $governorates[] = $governorate->getName('ar');
  212. //$content .= $governorate->getName() .' , ';
  213. }
  214. $parameters['governorateAffected'] = $governorates;
  215. //$content .= " \n";
  216. }
  217. //$content .= 'Alert Start Date: ' . (!empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  218. //$content .= 'Alert End Date: ' . (!empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown') ." \n";
  219. $parameters['alertStartDate'] = !empty($ewsNotification->getStartDate()) ? $ewsNotification->getStartDate()->format('Y-m-d h:i:sa') : 'Unknown';
  220. $parameters['alertEndDate'] = !empty($ewsNotification->getEndDate()) ? $ewsNotification->getEndDate()->format('Y-m-d h:i:sa') : 'Unknown';
  221. // $content .= 'Posted Today Date: ' . date('Y-m-d h:i:sa') ." \n";
  222. // if (!empty($ewsNotification->getAttachment())) {
  223. // $attachmentFile = API_BASE_URL.'/'.$ewsNotification->getAttachment()->getFullPath();
  224. // $content .= 'Attachment : '. $attachmentFile ." \n";
  225. // }
  226. //$content .= "For further information visit: https://ncm.gov.sa/Ar/alert/Pages/default.aspx \n";
  227. $HashTagcontent = "";
  228. if (!empty($ewsNotification->getRegion())) {
  229. $regionName = $ewsNotification->getRegion()->getName('ar');
  230. $regionName = str_replace(' ', '_', $regionName);
  231. // $HashTagcontent .= "اسم المنطقة: #$regionName\n";
  232. }
  233. if ($ewsNotification->getGovernorate() && !empty($ewsNotification->getGovernorate())) {
  234. // $HashTagcontent .= "المحافظات المتأثرة: ";
  235. foreach ($ewsNotification->getGovernorate() as $governorate) {
  236. $govName = $governorate->getName('ar');
  237. // $HashTagcontent .= "#$govName, ";
  238. }
  239. // $HashTagcontent = rtrim($HashTagcontent, ', ');
  240. // $HashTagcontent .= "\n";
  241. }
  242. // $HashTagcontent .= "#الإنذار_المبكر #طقس_السعودية #المركز_الوطني_للأرصاد";
  243. // $HashTagcontent .= "";
  244. $HashTagcontent = $ewsNotification->getXPost();
  245. $parameters['furtherInformation'] = "https://ncm.gov.sa/Ar/alert/Pages/default.aspx";
  246. // $html = $this->twig->render('image_templates/x-post-template.html.twig', $parameters);
  247. // $this->snappyImage->setOption('enable-local-file-access', true);
  248. // $this->snappyImage->setOption('width', '787');
  249. // $this->snappyImage->setOption('height', '797');
  250. // $this->snappyImage->setOption('format', 'png');
  251. // $image = $this->snappyImage->getOutputFromHtml($html);
  252. // $asset = \App\Lib\Utility::createAsset($image, rand(0, 10000) . '_' . time() . '_custom_weather_report.jpeg', 'EWS Twitter Images');
  253. // $asset_path = PIMCORE_PROJECT_ROOT . '/public/var/assets' . $asset->getPath() . $asset->getFileName();
  254. // $tweet = $this->twitterService->uploadMedia($asset_path, $HashTagcontent);
  255. $xPost = PIMCORE_PROJECT_ROOT . '/public/var/assets' .$ewsNotification->getXAttachment()->getPath() . $ewsNotification->getXAttachment()->getFileName();
  256. $xPostAssets = $ewsNotification->getXAttachment();
  257. $tweet = $this->twitterService->uploadMedia($xPost, $HashTagcontent);
  258. if ($tweet) {
  259. if(isset($tweet['result']->data)){
  260. $tweet_text = $tweet['result']?->data?->text;
  261. preg_match('/https?:\/\/\S+/', $tweet_text, $matches);
  262. $url = $matches[0] ?? null;
  263. // $tweet = $this->twitterService->postTweet('', $media_id);
  264. // $twitterResponse = $this->c2Service->sendTwitterTweetResponse($_ENV['TWITTER_POST_TEMPLATE'], $ewsNotification->getId(), $html, $url);
  265. // $twitterResponse = $this->c2Service->addAsset([$asset],$url);
  266. // $asset->delete();
  267. $twitterResponse = $this->c2Service->addAsset([$xPostAssets],$url);
  268. }
  269. if (!empty($tweet['tweetId'])) {
  270. $ewsNotification->setTwitterId($tweet['tweetId']);
  271. }
  272. $ewsNotification->setTwitterLog($tweet['data']);
  273. $ewsNotification->save();
  274. if ($tweet['httpCode'] === 200 || $tweet['httpCode'] === 201 || $tweet['httpCode'] === 204) {
  275. }
  276. elseif ($tweet['httpCode'] === 403) {
  277. // Forbidden - Duplicate content error
  278. $responseData = json_decode($tweet['data'], true);
  279. $errorMessage = $responseData['detail'];
  280. // echo $errorMessage;
  281. } else {
  282. // echo 'Unexpected HTTP status code: ' . $tweet['httpCode'];
  283. }
  284. }
  285. //$tweet = $this->twitterService->postTweet($content);
  286. // echo 'POST_ID : '.$tweet['tweetId']. ' EWS_ID : '.$ewsNotification->getId(). ' , ';
  287. }
  288. }
  289. }