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] = "$url";
$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] = "" . $mentions[0][$i] . "";
$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] = "" . $hashtags[0][$i] . "";
$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] = "$profile_name";
$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] = "$hero_name";
$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');
}
}