<?php
namespace App\Controller;
use App\Entity\Actualite;
use App\Entity\Matiere;
use App\Entity\MatierePedagoOpt;
use App\Entity\Message;
use App\Entity\Paragraphe;
use App\Entity\Pedagogie;
use App\Entity\Tag;
use App\Service\ActualiteService;
use App\Service\FileUploader;
use App\Service\LogService;
use App\Service\OngletService;
use App\Service\Validator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Serializer\SerializerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use \utilphp\util;
class ActualiteController extends AbstractController
{
/**
* @Route("/actualite/view/{title}", name="actualite_view",
* requirements={"title"="^[a-z0-9]+[a-z0-9-]*[a-z0-9-]+$"})
*/
public function viewActualite($title, Request $request, OngletService $ongletService, SessionInterface $session,
LogService $logService,ActualiteService $actualiteService)
{
$repository = $this->getDoctrine()->getRepository(Actualite::class);
if (!($actualite = $repository->findOneBy(['link' => $title])))
throw $this->createNotFoundException("La actualite demandée n'existe pas ou à été supprimée");
if (!$actualite->getIsPublic() && !$this->getUser()) {
throw new AccessDeniedException('Vous devez être connecté pour accéder à cet article');
}
if ($request->isMethod('GET')) {
if (!$session->get($title, false)) {
$session->set($title, true);
$em = $this->getDoctrine()->getManager();
$em->persist($actualite);
$em->flush();
}
if ($session->get('has_been_viewed', false) !== $actualite->getId()) {
$logService->log('view', $actualite);
$session->set('has_been_viewed', $actualite->getId());
$this->getDoctrine()->getManager()->flush();
}
}
if ($request->isMethod('POST')) {
$em = $this->getDoctrine()->getManager();
$req = $request->request;
switch ($req->get('data_post')) {
case 'message':
$data = $req->get('message');
if ($data != null) {
$message = new Message();
$message->setText($data);
$message->setPublishDate(new \DateTime("now"));
$message->setLikes([]);
$message->setApplys([]);
$message->setReports([]);
$message->setActive(true);
$message->setActualite($actualite);
$message->setUser($this->getUser() ?? null);
$em->persist($message);
$em->flush();
return $this->redirectToRoute('actualite_view', ['title' => $actualite->getLink()]);
}
break;
case 'like':
$likeArray = $actualite->getLikes();
$currentUser = $this->getUser();
if (array_search($currentUser->getId(), $likeArray) === false) {
$likeArray[] = $currentUser->getId();
$actualite->setLikes($likeArray);
$em->persist($actualite);
$em->flush();
return $this->redirectToRoute('actualite_view', ['title' => $actualite->getLink()]);
}
break;
case 'apply':
$applyArray = $actualite->getApplys();
$currentUser = $this->getUser();
if (array_search($currentUser->getId(), $applyArray) === false) {
$applyArray[] = $currentUser->getId();
$actualite->setApplys($applyArray);
$em->persist($actualite);
$em->flush();
return $this->redirectToRoute('actualite_view', ['title' => $actualite->getLink()]);
}
break;
case 'favorite':
$currentUser = $this->getUser();
$currentUser->addFavoriteActualite($actualite);
return $this->redirectToRoute('actualite_view', ['title' => $actualite->getLink()]);
break;
}
}
$user = $this->getUser();
$autres = $actualiteService->getAutresActualites(!$user ? true : null, $actualite);
return $this->render('actualite/view.html.twig', [
'onglets' => $ongletService->getOnglets(),
'actualite' => $actualite,
'paragraphes' => $actualite->getParagraphes(),
'autres' => $autres,
]);
}
/**
* @Route("/evenement/view/{title}", name="evenement_view",
* requirements={"title"="^[a-z0-9]+[a-z0-9-]*[a-z0-9-]+$"})
*/
public function viewEvenement($title, Request $request, OngletService $ongletService, SessionInterface $session,
LogService $logService,ActualiteService $actualiteService)
{
$repository = $this->getDoctrine()->getRepository(Actualite::class);
if (!($actualite = $repository->findOneBy(['link' => $title])))
throw $this->createNotFoundException("La actualite demandée n'existe pas ou à été supprimée");
if (!$actualite->getIsPublic() && !$this->getUser()) {
throw new AccessDeniedException('Vous devez être connecté pour accéder à cet article');
}
if ($request->isMethod('GET')) {
if (!$session->get($title, false)) {
$session->set($title, true);
$em = $this->getDoctrine()->getManager();
$em->persist($actualite);
$em->flush();
}
if ($session->get('has_been_viewed', false) !== $actualite->getId()) {
$logService->log('view', $actualite);
$session->set('has_been_viewed', $actualite->getId());
$this->getDoctrine()->getManager()->flush();
}
}
if ($request->isMethod('POST')) {
$em = $this->getDoctrine()->getManager();
$req = $request->request;
switch ($req->get('data_post')) {
case 'message':
$data = $req->get('message');
if ($data != null) {
$message = new Message();
$message->setText($data);
$message->setPublishDate(new \DateTime("now"));
$message->setLikes([]);
$message->setApplys([]);
$message->setReports([]);
$message->setActive(true);
$message->setActualite($actualite);
$message->setUser($this->getUser() ?? null);
$em->persist($message);
$em->flush();
return $this->redirectToRoute('evenement_view', ['title' => $actualite->getLink()]);
}
break;
case 'like':
$likeArray = $actualite->getLikes();
$currentUser = $this->getUser();
if (array_search($currentUser->getId(), $likeArray) === false) {
$likeArray[] = $currentUser->getId();
$actualite->setLikes($likeArray);
$em->persist($actualite);
$em->flush();
return $this->redirectToRoute('evenement_view', ['title' => $actualite->getLink()]);
}
break;
case 'apply':
$applyArray = $actualite->getApplys();
$currentUser = $this->getUser();
if (array_search($currentUser->getId(), $applyArray) === false) {
$applyArray[] = $currentUser->getId();
$actualite->setApplys($applyArray);
$em->persist($actualite);
$em->flush();
return $this->redirectToRoute('evenement_view', ['title' => $actualite->getLink()]);
}
break;
case 'favorite':
$currentUser = $this->getUser();
$currentUser->addFavoriteActualite($actualite);
return $this->redirectToRoute('evenement_view', ['title' => $actualite->getLink()]);
break;
}
}
$autres = $actualiteService->getAutresEvenements($actualite->getIsPublic() ? true : null, $actualite);
return $this->render('evenement/view.html.twig', [
'onglets' => $ongletService->getOnglets(),
'actualite' => $actualite,
'paragraphes' => $actualite->getParagraphes(),
'autres' => $autres,
]);
}
/**
* @Route("/actualites", name="actualites")
*/
public function index(Request $request, OngletService $ongletService)
{
return $this->render('actualite/index.html.twig', [
'onglets' => $ongletService->getOnglets(),
]);
}
/**
* @Route("/evenements", name="evenements")
*/
public function evenements(Request $request, OngletService $ongletService)
{
return $this->render('evenement/index.html.twig', [
'onglets' => $ongletService->getOnglets(),
]);
}
/**
* @Route("/ajax/actualites/{page}/{filtres}", name="ajax_actualites", defaults={"page"="", "filtres"=""} )
*/
public function ajax_actualites($page, $filtres, Request $request, ActualiteService $actualiteService, SerializerInterface $serializer)
{
$user = $this->getUser();
$public = $user ? null : true;
$filtres = $filtres ? explode(',', $filtres) : [];
// $actualites = $actualiteService->getRepository()->findBy(['isPublic' => $public], ['publishDate' => 'DESC'], 8, (int)$page);
$actualites = $actualiteService->getRepository()->findbyFiltre($filtres, $public, 8, (int)$page, ['evenement']);
return new Response($serializer->serialize($actualites, 'json', ['groups' => 'actualite_total']));
}
/**
* @Route("/ajax/evenements/{page}/{filtres}", name="ajax_evenements", defaults={"page"="", "filtres"=""} )
*/
public function ajax_evenements($page, $filtres, Request $request, ActualiteService $actualiteService, SerializerInterface $serializer)
{
$user = $this->getUser();
$public = $user ? null : true;
$filtres = $filtres ? explode(',', $filtres) : [];
// $actualites = $actualiteService->getRepository()->findBy(['isPublic' => $public], ['publishDate' => 'DESC'], 8, (int)$page);
$actualites = $actualiteService->getRepository()->findbyFiltre($filtres, $public, 8, (int)$page, [], 'debut');
return new Response($serializer->serialize($actualites, 'json', ['groups' => 'actualite_total']));
}
}