HomeController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Hashtag;
  4. use App\Entity\Message;
  5. use App\Entity\Retweets;
  6. use App\Entity\User;
  7. use Doctrine\DBAL\Types\TextType;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpFoundation\Session\Session;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Validator\Constraints\Date;
  14. class HomeController extends AbstractController
  15. {
  16. private function cmp_array($a, $b): int
  17. {
  18. if ($a->getDate() == $b->getDate()) {
  19. return 0;
  20. }
  21. return ($a->getDate() > $b->getDate()) ? -1 : 1;
  22. }
  23. #[Route('/home', name: 'home')]
  24. public function index(Request $request): Response
  25. {
  26. $session = $this->get('session');
  27. if (null === $session->get('user')) {
  28. return $this->redirectToRoute('home');
  29. }
  30. $formBuilder = $this->createFormBuilder();
  31. $formBuilder->add('text', \Symfony\Component\Form\Extension\Core\Type\TextType::class,['label' => 'Envoyer un message'])
  32. ->setAction($this->generateUrl('home'));
  33. $form = $formBuilder->getForm();
  34. if ($request->getMethod() == 'POST') {
  35. $form->handleRequest($request);
  36. if ($form->isValid()) {
  37. $session = $this->get('session');
  38. $em = $this->getDoctrine()->getManager();
  39. $repository_profile = $em->getRepository(User::class);
  40. $profile = $repository_profile->findOneBy(array('username' => $session->get('user')));
  41. $message = new Message();
  42. $text = $form->get('text')->getData();
  43. $mentions = array();
  44. $hashtags = array();
  45. $dp = array();
  46. $dh = array();
  47. $urls = array();
  48. preg_match_all("~@([a-zA-Z0-9_]*)~", $text, $mentions);
  49. preg_match_all("~#([a-zA-Z0-9_]*)~", $text, $hashtags);
  50. preg_match_all("!DP %(.+)(?:\s|$)!U", $text, $dp);
  51. preg_match_all("!DH %(.+)(?:\s|$)!U", $text, $dh);
  52. preg_match_all("~[a-z]+://\S+~", $text, $urls);
  53. for($i = 0; $i < sizeof($urls[0]); $i++) {
  54. $url = $urls[0][$i];
  55. $urls[1][$i] = "<a href='$url'>$url</a>";
  56. $u = explode("/", $urls[0][$i]);
  57. $formatted_url = "";
  58. foreach ($u as $u_part) {
  59. $formatted_url .= $u_part . "\/";
  60. }
  61. $urls[0][$i] = "~[a-z]+://\S+~";
  62. }
  63. $text = preg_replace($urls[0], $urls[1], $text);
  64. for($i=0; $i < sizeof($mentions[0]); $i++) {
  65. $user = $repository_profile->findOneBy(array('username' => $mentions[1][$i]));
  66. if ($user) {
  67. $message->addMention($user);
  68. $mentions[1][$i] = "<a href='/profile/" . $mentions[1][$i] . "'>" . $mentions[0][$i] . "</a>";
  69. $mentions[0][$i] = "~" . $mentions[0][$i] . "~";
  70. } else {
  71. unset($mentions[1][$i]);
  72. unset($mentions[0][$i]);
  73. }
  74. }
  75. $repository_hashtag = $em->getRepository(Hashtag::class);
  76. for($i=0; $i < sizeof($hashtags[0]); $i++) {
  77. $hashtag = $repository_hashtag->findOneBy(array('name' => $hashtags[1][$i]));
  78. if (!$hashtag) {
  79. $hashtag = new Hashtag();
  80. $hashtag->setName($hashtags[1][$i]);
  81. }
  82. $hashtag->addMessage($message);
  83. $hashtags[1][$i] = "<a href='/hashtag/" . $hashtags[1][$i] . "'>" . $hashtags[0][$i] . "</a>";
  84. $hashtags[0][$i] = "~" . $hashtags[0][$i] . "~";
  85. $em->persist($hashtag);
  86. }
  87. for($i=0; $i < sizeof($dp[0]); $i++) {
  88. $ch = curl_init("https://richie.u-strasbg.fr/~virgile/sf4/public/index.php/cours/diablo/");
  89. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  90. $response = curl_exec($ch);
  91. curl_close($ch);
  92. $json_response = json_decode($response, true);
  93. $battletag = $json_response["battleTag"];
  94. if (str_replace("-", "#", $dp[1][$i]) == $battletag) {
  95. $parangon = $json_response["paragonLevel"];
  96. $parangon_season = $json_response["paragonLevelSeason"];
  97. $kills = $json_response["kills"]["monsters"];
  98. $kills_elites = $json_response["kills"]["elites"];
  99. $profile_name = $dp[1][$i];
  100. $dp[1][$i] = "<a href='#' data-toggle='popover' data-original-title='$profile_name' data-content='Parangon : $parangon ; Parangon saisonnier : $parangon_season ; Nombre de tués : $kills ; Nombre d élites tués : $kills_elites'>$profile_name</a>";
  101. $dp[0][$i] = "~" . $dp[0][$i] . "~";
  102. $text = preg_replace($dp[0], $dp[1], $text);
  103. }
  104. }
  105. for($i=0; $i < sizeof($dh[0]); $i++) {
  106. $values = explode("/", $dh[1][$i]);
  107. $ch = curl_init("https://richie.u-strasbg.fr/~virgile/sf4/public/index.php/cours/diablo/");
  108. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  109. $response = curl_exec($ch);
  110. curl_close($ch);
  111. $json_response = json_decode($response, true);
  112. $battletag = $json_response["battleTag"];
  113. $found_hero = null;
  114. if (str_replace("-", "#", $values[0]) == $battletag) {
  115. foreach ($json_response["heroes"] as $hero) {
  116. if ($hero["name"] == $values[1]) {
  117. $found_hero = $hero;
  118. break;
  119. }
  120. }
  121. if ($found_hero) {
  122. $hero_name = $found_hero["name"];
  123. $hero_class = $found_hero["class"];
  124. $hero_level = $found_hero["level"];
  125. $dh[1][$i] = "<a href='#' data-toggle='popover' data-original-title='$hero_name' data-content='Classe : $hero_class Niveau : $hero_level'>$hero_name</a>";
  126. $dh[0][$i] = "~" . $dh[0][$i] . "~";
  127. $text = preg_replace($dh[0], $dh[1], $text);
  128. }
  129. }
  130. }
  131. $text = preg_replace($mentions[0], $mentions[1], $text);
  132. $message->setText(preg_replace($hashtags[0], $hashtags[1], $text));
  133. $message->setSender($profile);
  134. $message->setDate(new \DateTime("now"));
  135. $em->persist($message);
  136. $em->flush();
  137. }
  138. }
  139. $em = $this->getDoctrine()->getManager();
  140. $repository_profile = $em->getRepository(User::class);
  141. $profile = $repository_profile->findOneBy(array('username' => $session->get('user')));
  142. $mentioned_messages = $profile->getMentionedMessages();
  143. $subscriptions = $profile->getSubscriptions();
  144. $rt = $profile->getRetweets();
  145. $sub_msg = array();
  146. foreach ($rt as $r) {
  147. $og_message = $r->getOgMessage();
  148. $rt_message = new Message();
  149. $rt_message->setText($og_message->getText());
  150. $rt_message->setSender($og_message->getSender());
  151. $rt_message->setId($og_message->getId());
  152. $rt_message->setDate($r->getRtDate());
  153. array_push($sub_msg, $rt_message);
  154. }
  155. foreach ($mentioned_messages as $msg_m) {
  156. if (!in_array($msg_m->getSender(), $subscriptions->getValues())) {
  157. array_push($sub_msg, $msg_m);
  158. }
  159. }
  160. foreach ($profile->getMessages() as $msg_u) {
  161. array_push($sub_msg, $msg_u);
  162. }
  163. foreach ($subscriptions as $user) {
  164. foreach ($user->getMessages() as $msg) {
  165. array_push($sub_msg, $msg);
  166. }
  167. }
  168. usort($sub_msg, array($this, "cmp_array"));
  169. return $this->render('home/index.html.twig', [
  170. 'controller_name' => 'HomeController',
  171. 'messages' => $sub_msg,
  172. 'form' => $form->createView(),
  173. 'username' => $session->get('user'),
  174. ]);
  175. }
  176. #[Route('/rt/{msgId}', name: 'rt_message')]
  177. public function retweet($msgId) {
  178. $session = $this->get('session');
  179. if (null === $session->get('user')) {
  180. return $this->redirectToRoute('home');
  181. }
  182. $em = $this->getDoctrine()->getManager();
  183. $repository_profile = $em->getRepository(User::class);
  184. $profile = $repository_profile->findOneBy(array('username' => $session->get('user')));
  185. $repository_message = $em->getRepository(Message::class);
  186. $rt = new Retweets();
  187. $rt->setRtDate(new \DateTime("now"));
  188. $rt->setRetweeter($profile);
  189. $rt->setOgMessage($repository_message->find($msgId));
  190. $em->persist($rt);
  191. $em->flush();
  192. return $this->redirectToRoute('home');
  193. }
  194. }