src/Controller/ActualiteController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Actualite;
  4. use App\Entity\Matiere;
  5. use App\Entity\MatierePedagoOpt;
  6. use App\Entity\Message;
  7. use App\Entity\Paragraphe;
  8. use App\Entity\Pedagogie;
  9. use App\Entity\Tag;
  10. use App\Service\ActualiteService;
  11. use App\Service\FileUploader;
  12. use App\Service\LogService;
  13. use App\Service\OngletService;
  14. use App\Service\Validator;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  20. use Symfony\Component\HttpKernel\Exception\HttpException;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  23. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  24. use Symfony\Component\Serializer\SerializerInterface;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  26. use \utilphp\util;
  27. class ActualiteController extends AbstractController
  28. {
  29.   /**
  30.    * @Route("/actualite/view/{title}", name="actualite_view",
  31.    *   requirements={"title"="^[a-z0-9]+[a-z0-9-]*[a-z0-9-]+$"})
  32.    */
  33.   public function viewActualite($titleRequest $requestOngletService $ongletServiceSessionInterface $session,
  34.                                    LogService $logService,ActualiteService $actualiteService)
  35.   {
  36.     $repository $this->getDoctrine()->getRepository(Actualite::class);
  37.     if (!($actualite $repository->findOneBy(['link' => $title])))
  38.       throw $this->createNotFoundException("La actualite demandée n'existe pas ou à été supprimée");
  39.     if (!$actualite->getIsPublic() && !$this->getUser()) {
  40.         throw new AccessDeniedException('Vous devez être connecté pour accéder à cet article');
  41.     }
  42.     if ($request->isMethod('GET')) {
  43.       if (!$session->get($titlefalse)) {
  44.         $session->set($titletrue);
  45.         $em $this->getDoctrine()->getManager();
  46.         $em->persist($actualite);
  47.         $em->flush();
  48.       }
  49.       if ($session->get('has_been_viewed'false) !== $actualite->getId()) {
  50.         $logService->log('view'$actualite);
  51.         $session->set('has_been_viewed'$actualite->getId());
  52.         $this->getDoctrine()->getManager()->flush();
  53.       }
  54.     }
  55.     if ($request->isMethod('POST')) {
  56.       $em $this->getDoctrine()->getManager();
  57.       $req $request->request;
  58.       switch ($req->get('data_post')) {
  59.         case 'message':
  60.           $data $req->get('message');
  61.           if ($data != null) {
  62.             $message = new Message();
  63.             $message->setText($data);
  64.             $message->setPublishDate(new \DateTime("now"));
  65.             $message->setLikes([]);
  66.             $message->setApplys([]);
  67.             $message->setReports([]);
  68.             $message->setActive(true);
  69.             $message->setActualite($actualite);
  70.             $message->setUser($this->getUser() ?? null);
  71.             $em->persist($message);
  72.             $em->flush();
  73.             return $this->redirectToRoute('actualite_view', ['title' => $actualite->getLink()]);
  74.           }
  75.           break;
  76.         case 'like':
  77.           $likeArray $actualite->getLikes();
  78.           $currentUser $this->getUser();
  79.           if (array_search($currentUser->getId(), $likeArray) === false) {
  80.             $likeArray[] = $currentUser->getId();
  81.             $actualite->setLikes($likeArray);
  82.             $em->persist($actualite);
  83.             $em->flush();
  84.             return $this->redirectToRoute('actualite_view', ['title' => $actualite->getLink()]);
  85.           }
  86.           break;
  87.         case 'apply':
  88.           $applyArray $actualite->getApplys();
  89.           $currentUser $this->getUser();
  90.           if (array_search($currentUser->getId(), $applyArray) === false) {
  91.             $applyArray[] = $currentUser->getId();
  92.             $actualite->setApplys($applyArray);
  93.             $em->persist($actualite);
  94.             $em->flush();
  95.             return $this->redirectToRoute('actualite_view', ['title' => $actualite->getLink()]);
  96.           }
  97.           break;
  98.         case 'favorite':
  99.           $currentUser $this->getUser();
  100.           $currentUser->addFavoriteActualite($actualite);
  101.           return $this->redirectToRoute('actualite_view', ['title' => $actualite->getLink()]);
  102.           break;
  103.       }
  104.     }
  105.     $user $this->getUser();
  106.     $autres $actualiteService->getAutresActualites(!$user true null$actualite);
  107.     return $this->render('actualite/view.html.twig', [
  108.       'onglets' => $ongletService->getOnglets(),
  109.       'actualite' => $actualite,
  110.       'paragraphes' => $actualite->getParagraphes(),
  111.       'autres' => $autres,
  112.     ]);
  113.   }
  114.   /**
  115.    * @Route("/evenement/view/{title}", name="evenement_view",
  116.    *   requirements={"title"="^[a-z0-9]+[a-z0-9-]*[a-z0-9-]+$"})
  117.    */
  118.   public function viewEvenement($titleRequest $requestOngletService $ongletServiceSessionInterface $session,
  119.                                    LogService $logService,ActualiteService $actualiteService)
  120.   {
  121.     $repository $this->getDoctrine()->getRepository(Actualite::class);
  122.     if (!($actualite $repository->findOneBy(['link' => $title])))
  123.       throw $this->createNotFoundException("La actualite demandée n'existe pas ou à été supprimée");
  124.     if (!$actualite->getIsPublic() && !$this->getUser()) {
  125.         throw new AccessDeniedException('Vous devez être connecté pour accéder à cet article');
  126.     }
  127.     if ($request->isMethod('GET')) {
  128.       if (!$session->get($titlefalse)) {
  129.         $session->set($titletrue);
  130.         $em $this->getDoctrine()->getManager();
  131.         $em->persist($actualite);
  132.         $em->flush();
  133.       }
  134.       if ($session->get('has_been_viewed'false) !== $actualite->getId()) {
  135.         $logService->log('view'$actualite);
  136.         $session->set('has_been_viewed'$actualite->getId());
  137.         $this->getDoctrine()->getManager()->flush();
  138.       }
  139.     }
  140.     if ($request->isMethod('POST')) {
  141.       $em $this->getDoctrine()->getManager();
  142.       $req $request->request;
  143.       switch ($req->get('data_post')) {
  144.         case 'message':
  145.           $data $req->get('message');
  146.           if ($data != null) {
  147.             $message = new Message();
  148.             $message->setText($data);
  149.             $message->setPublishDate(new \DateTime("now"));
  150.             $message->setLikes([]);
  151.             $message->setApplys([]);
  152.             $message->setReports([]);
  153.             $message->setActive(true);
  154.             $message->setActualite($actualite);
  155.             $message->setUser($this->getUser() ?? null);
  156.             $em->persist($message);
  157.             $em->flush();
  158.             return $this->redirectToRoute('evenement_view', ['title' => $actualite->getLink()]);
  159.           }
  160.           break;
  161.         case 'like':
  162.           $likeArray $actualite->getLikes();
  163.           $currentUser $this->getUser();
  164.           if (array_search($currentUser->getId(), $likeArray) === false) {
  165.             $likeArray[] = $currentUser->getId();
  166.             $actualite->setLikes($likeArray);
  167.             $em->persist($actualite);
  168.             $em->flush();
  169.             return $this->redirectToRoute('evenement_view', ['title' => $actualite->getLink()]);
  170.           }
  171.           break;
  172.         case 'apply':
  173.           $applyArray $actualite->getApplys();
  174.           $currentUser $this->getUser();
  175.           if (array_search($currentUser->getId(), $applyArray) === false) {
  176.             $applyArray[] = $currentUser->getId();
  177.             $actualite->setApplys($applyArray);
  178.             $em->persist($actualite);
  179.             $em->flush();
  180.             return $this->redirectToRoute('evenement_view', ['title' => $actualite->getLink()]);
  181.           }
  182.           break;
  183.         case 'favorite':
  184.           $currentUser $this->getUser();
  185.           $currentUser->addFavoriteActualite($actualite);
  186.           return $this->redirectToRoute('evenement_view', ['title' => $actualite->getLink()]);
  187.           break;
  188.       }
  189.     }
  190.     $autres $actualiteService->getAutresEvenements($actualite->getIsPublic() ? true null$actualite);
  191.     return $this->render('evenement/view.html.twig', [
  192.       'onglets' => $ongletService->getOnglets(),
  193.       'actualite' => $actualite,
  194.       'paragraphes' => $actualite->getParagraphes(),
  195.       'autres' => $autres,
  196.     ]);
  197.   }
  198.   /**
  199.    * @Route("/actualites", name="actualites")
  200.    */
  201.   public function index(Request $requestOngletService $ongletService)
  202.   {
  203.     return $this->render('actualite/index.html.twig', [
  204.       'onglets' => $ongletService->getOnglets(),
  205.     ]);
  206.   }
  207.   /**
  208.    * @Route("/evenements", name="evenements")
  209.    */
  210.   public function evenements(Request $requestOngletService $ongletService)
  211.   {
  212.     return $this->render('evenement/index.html.twig', [
  213.       'onglets' => $ongletService->getOnglets(),
  214.     ]);
  215.   }
  216.   /**
  217.    * @Route("/ajax/actualites/{page}/{filtres}", name="ajax_actualites", defaults={"page"="", "filtres"=""} )
  218.    */
  219.   public function ajax_actualites($page$filtresRequest $requestActualiteService $actualiteServiceSerializerInterface $serializer)
  220.   {
  221.     $user $this->getUser();
  222.     $public $user null true;
  223.     $filtres $filtres explode(','$filtres) : [];
  224. //    $actualites = $actualiteService->getRepository()->findBy(['isPublic' => $public], ['publishDate' => 'DESC'], 8, (int)$page);
  225.     $actualites $actualiteService->getRepository()->findbyFiltre($filtres$public8, (int)$page, ['evenement']);
  226.     return new Response($serializer->serialize($actualites'json', ['groups' => 'actualite_total']));
  227.   }
  228.   /**
  229.    * @Route("/ajax/evenements/{page}/{filtres}", name="ajax_evenements", defaults={"page"="", "filtres"=""} )
  230.    */
  231.   public function ajax_evenements($page$filtresRequest $requestActualiteService $actualiteServiceSerializerInterface $serializer)
  232.   {
  233.     $user $this->getUser();
  234.     $public $user null true;
  235.     $filtres $filtres explode(','$filtres) : [];
  236. //    $actualites = $actualiteService->getRepository()->findBy(['isPublic' => $public], ['publishDate' => 'DESC'], 8, (int)$page);
  237.     $actualites $actualiteService->getRepository()->findbyFiltre($filtres$public8, (int)$page, [], 'debut');
  238.     return new Response($serializer->serialize($actualites'json', ['groups' => 'actualite_total']));
  239.   }
  240.   
  241. }