| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace App\Controller;
- use App\Entity\Hashtag;
- use App\Entity\Message;
- use App\Entity\Retweets;
- use App\Entity\User;
- use Doctrine\DBAL\Types\TextType;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpFoundation\Session\Session;
- use Symfony\Component\Routing\Annotation\Route;
- use Symfony\Component\Validator\Constraints\Date;
- class HomeController extends AbstractController
- {
- private function cmp_array($a, $b): int
- {
- if ($a->getDate() == $b->getDate()) {
- return 0;
- }
- return ($a->getDate() > $b->getDate()) ? -1 : 1;
- }
- #[Route('/home', name: 'home')]
- public function index(Request $request): Response
- {
- $session = $this->get('session');
- if (null === $session->get('user')) {
- return $this->redirectToRoute('home');
- }
- $formBuilder = $this->createFormBuilder();
- $formBuilder->add('text', \Symfony\Component\Form\Extension\Core\Type\TextType::class,['label' => 'Envoyer un message'])
- ->setAction($this->generateUrl('home'));
- $form = $formBuilder->getForm();
- if ($request->getMethod() == 'POST') {
- $form->handleRequest($request);
- if ($form->isValid()) {
- $session = $this->get('session');
- $em = $this->getDoctrine()->getManager();
- $repository_profile = $em->getRepository(User::class);
- $profile = $repository_profile->findOneBy(array('username' => $session->get('user')));
- $message = new Message();
- $text = $form->get('text')->getData();
- $mentions = array();
- $hashtags = array();
- $dp = array();
- $dh = array();
- $urls = array();
- preg_match_all("~@([a-zA-Z0-9_]*)~", $text, $mentions);
- preg_match_all("~#([a-zA-Z0-9_]*)~", $text, $hashtags);
- preg_match_all("!DP %(.+)(?:\s|$)!U", $text, $dp);
- preg_match_all("!DH %(.+)(?:\s|$)!U", $text, $dh);
- preg_match_all("~[a-z]+://\S+~", $text, $urls);
- for($i = 0; $i < sizeof($urls[0]); $i++) {
- $url = $urls[0][$i];
- $urls[1][$i] = "<a href='$url'>$url</a>";
- $u = explode("/", $urls[0][$i]);
- $formatted_url = "";
- foreach ($u as $u_part) {
- $formatted_url .= $u_part . "\/";
- }
- $urls[0][$i] = "~[a-z]+://\S+~";
- }
- $text = preg_replace($urls[0], $urls[1], $text);
- for($i=0; $i < sizeof($mentions[0]); $i++) {
- $user = $repository_profile->findOneBy(array('username' => $mentions[1][$i]));
- if ($user) {
- $message->addMention($user);
- $mentions[1][$i] = "<a href='/profile/" . $mentions[1][$i] . "'>" . $mentions[0][$i] . "</a>";
- $mentions[0][$i] = "~" . $mentions[0][$i] . "~";
- } else {
- unset($mentions[1][$i]);
- unset($mentions[0][$i]);
- }
- }
- $repository_hashtag = $em->getRepository(Hashtag::class);
- for($i=0; $i < sizeof($hashtags[0]); $i++) {
- $hashtag = $repository_hashtag->findOneBy(array('name' => $hashtags[1][$i]));
- if (!$hashtag) {
- $hashtag = new Hashtag();
- $hashtag->setName($hashtags[1][$i]);
- }
- $hashtag->addMessage($message);
- $hashtags[1][$i] = "<a href='/hashtag/" . $hashtags[1][$i] . "'>" . $hashtags[0][$i] . "</a>";
- $hashtags[0][$i] = "~" . $hashtags[0][$i] . "~";
- $em->persist($hashtag);
- }
- for($i=0; $i < sizeof($dp[0]); $i++) {
- $ch = curl_init("https://richie.u-strasbg.fr/~virgile/sf4/public/index.php/cours/diablo/");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($ch);
- curl_close($ch);
- $json_response = json_decode($response, true);
- $battletag = $json_response["battleTag"];
- if (str_replace("-", "#", $dp[1][$i]) == $battletag) {
- $parangon = $json_response["paragonLevel"];
- $parangon_season = $json_response["paragonLevelSeason"];
- $kills = $json_response["kills"]["monsters"];
- $kills_elites = $json_response["kills"]["elites"];
- $profile_name = $dp[1][$i];
- $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>";
- $dp[0][$i] = "~" . $dp[0][$i] . "~";
- $text = preg_replace($dp[0], $dp[1], $text);
- }
- }
- for($i=0; $i < sizeof($dh[0]); $i++) {
- $values = explode("/", $dh[1][$i]);
- $ch = curl_init("https://richie.u-strasbg.fr/~virgile/sf4/public/index.php/cours/diablo/");
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($ch);
- curl_close($ch);
- $json_response = json_decode($response, true);
- $battletag = $json_response["battleTag"];
- $found_hero = null;
- if (str_replace("-", "#", $values[0]) == $battletag) {
- foreach ($json_response["heroes"] as $hero) {
- if ($hero["name"] == $values[1]) {
- $found_hero = $hero;
- break;
- }
- }
- if ($found_hero) {
- $hero_name = $found_hero["name"];
- $hero_class = $found_hero["class"];
- $hero_level = $found_hero["level"];
- $dh[1][$i] = "<a href='#' data-toggle='popover' data-original-title='$hero_name' data-content='Classe : $hero_class Niveau : $hero_level'>$hero_name</a>";
- $dh[0][$i] = "~" . $dh[0][$i] . "~";
- $text = preg_replace($dh[0], $dh[1], $text);
- }
- }
- }
- $text = preg_replace($mentions[0], $mentions[1], $text);
- $message->setText(preg_replace($hashtags[0], $hashtags[1], $text));
- $message->setSender($profile);
- $message->setDate(new \DateTime("now"));
- $em->persist($message);
- $em->flush();
- }
- }
- $em = $this->getDoctrine()->getManager();
- $repository_profile = $em->getRepository(User::class);
- $profile = $repository_profile->findOneBy(array('username' => $session->get('user')));
- $mentioned_messages = $profile->getMentionedMessages();
- $subscriptions = $profile->getSubscriptions();
- $rt = $profile->getRetweets();
- $sub_msg = array();
- foreach ($rt as $r) {
- $og_message = $r->getOgMessage();
- $rt_message = new Message();
- $rt_message->setText($og_message->getText());
- $rt_message->setSender($og_message->getSender());
- $rt_message->setId($og_message->getId());
- $rt_message->setDate($r->getRtDate());
- array_push($sub_msg, $rt_message);
- }
- foreach ($mentioned_messages as $msg_m) {
- if (!in_array($msg_m->getSender(), $subscriptions->getValues())) {
- array_push($sub_msg, $msg_m);
- }
- }
- foreach ($profile->getMessages() as $msg_u) {
- array_push($sub_msg, $msg_u);
- }
- foreach ($subscriptions as $user) {
- foreach ($user->getMessages() as $msg) {
- array_push($sub_msg, $msg);
- }
- }
- usort($sub_msg, array($this, "cmp_array"));
- return $this->render('home/index.html.twig', [
- 'controller_name' => 'HomeController',
- 'messages' => $sub_msg,
- 'form' => $form->createView(),
- 'username' => $session->get('user'),
- ]);
- }
- #[Route('/rt/{msgId}', name: 'rt_message')]
- public function retweet($msgId) {
- $session = $this->get('session');
- if (null === $session->get('user')) {
- return $this->redirectToRoute('home');
- }
- $em = $this->getDoctrine()->getManager();
- $repository_profile = $em->getRepository(User::class);
- $profile = $repository_profile->findOneBy(array('username' => $session->get('user')));
- $repository_message = $em->getRepository(Message::class);
- $rt = new Retweets();
- $rt->setRtDate(new \DateTime("now"));
- $rt->setRetweeter($profile);
- $rt->setOgMessage($repository_message->find($msgId));
- $em->persist($rt);
- $em->flush();
- return $this->redirectToRoute('home');
- }
- }
|