src/Controller/Front/HomepageController.php line 142

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use DateTime;
  4. use Exception;
  5. use DateInterval;
  6. use App\Entity\Post;
  7. use App\Entity\User;
  8. use App\Form\PostType;
  9. use App\Form\RegisterType;
  10. use App\Entity\DebanRequest;
  11. use App\Form\DebanRequestType;
  12. use App\EmailNotification\ToUser;
  13. use App\Repository\PostRepository;
  14. use App\Repository\UserRepository;
  15. use Symfony\Component\Form\FormError;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\Mercure\Discovery;
  18. use App\Repository\NotificationRepository;
  19. use Symfony\Component\HttpFoundation\Cookie;
  20. use Symfony\Component\Mercure\Authorization;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. use Symfony\Component\HttpFoundation\Session\Session;
  26. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  27. class HomepageController extends AbstractController
  28. {
  29.     #[Route('/coming-soon'name'coming_soon')]
  30.     public function comingSoon(Request $request): Response
  31.     {
  32.         $session $request->getSession();
  33.         if ($request->request->get('accessPassword')) {
  34.             
  35.             $truePassword "P2lhNLVt1y";
  36.             if ($request->request->get('accessPassword') == $truePassword) {
  37.                 
  38.                 $expire = new DateTime();
  39.                 $expire->add(new DateInterval("P5D"));
  40.         
  41.                 $cookie Cookie::create('politicly-production-access')
  42.                         ->withValue(true)
  43.                         ->withExpires($expire)
  44.                         ->withSecure(true);
  45.         
  46.                 $response $this->redirectToRoute('feed');
  47.                 $session->remove('wrong-password');
  48.         
  49.                 $response->headers->setCookie($cookie);
  50.         
  51.                 return $response;
  52.             }
  53.             else {
  54.                 $session->set('wrong-password'true);
  55.                 return $this->redirectToRoute('coming_soon');
  56.             }
  57.         }
  58.         return $this->render('front/coming-soon.html.twig', [
  59.             'coming' => true
  60.         ]);
  61.     }
  62.     
  63.     #[Route('/bienvenue'name'user_welcome')]
  64.     public function welcome(Request $requestUserRepository $userRepositorySession $session): Response
  65.     {
  66.         $form $this->createForm(RegisterType::class);
  67.         $form->handleRequest($request);
  68.         if ($form->isSubmitted() && $form->isValid())
  69.         {
  70.             $data $form->getData();
  71.             /* Check if user already exist */
  72.             $existUser $userRepository->findOneBy(['email' => $data['email']]);
  73.             if ($existUser)
  74.             {
  75.                 $form->get('email')->addError(new FormError("Un compte existe avec cet email"));
  76.             }
  77.             else
  78.             {
  79.                 try {
  80.                     $date date_create_from_format('d/m/Y'$data['birthDate']);
  81.                 } catch (Exception) {
  82.                     $form->get('birthDate')->addError(new FormError("Veuillez renseigner une date valide"));
  83.                     return $this->render('front/welcome.html.twig', [
  84.                         'menu' => 'welcome',
  85.                         'form' => $form->createView()
  86.                     ]);
  87.                 }
  88.                 if ($date > new DateTime("-13 years")) {
  89.                     $form->get('birthDate')->addError(new FormError("Vous devez avoir plus de 13 ans pour vous inscrire"));
  90.                     return $this->render('front/welcome.html.twig', [
  91.                         'menu' => 'welcome',
  92.                         'form' => $form->createView()
  93.                     ]);
  94.                 }
  95.                 if ($date < new DateTime("-130 years")) {
  96.                     $form->get('birthDate')->addError(new FormError("Veuillez renseigner une date valide"));
  97.                     return $this->render('front/welcome.html.twig', [
  98.                         'menu' => 'welcome',
  99.                         'form' => $form->createView()
  100.                     ]);
  101.                 }
  102.                 /* Set info user */
  103.                 $session->set('email'$data['email']);
  104.                 $session->set('password'$data['password']);
  105.                 $session->set('firstname'$data['firstname']);
  106.                 $session->set('lastname'$data['lastname']);
  107.                 $session->set('genre'$data['genre']);
  108.                 $session->set('phone'$data['phone']);
  109.                 $session->set('birthDate'$data['birthDate']);
  110.                 return $this->render('front/registration/info-type-registration.html.twig');
  111.             }
  112.         }
  113.         return $this->render('front/welcome.html.twig', [
  114.             'menu' => 'welcome',
  115.             'form' => $form->createView()
  116.         ]);
  117.     }
  118.     #[Route('/'name'feed')]
  119.     public function feed(PostRepository $postRepoRequest $request): Response
  120.     {   
  121.         /** @var User $user */
  122.         $user $this->getUser();
  123.         $post = new Post();
  124.         $post->setHandleRedirect('feed');
  125.         $form $this->createForm(PostType::class, $post, [
  126.             'action' => $this->generateUrl('handle_post_submit')
  127.         ]);
  128.         $form->handleRequest($request);
  129.         $posts $postRepo->feedByUser($user);
  130.         if (count($posts) < 10) {
  131.             foreach ($postRepo->feedByUserRandom($user) as $post) { $posts[] = $post; }
  132.         }
  133.         return $this->render('front/feed.html.twig', [
  134.             'menu' => 'feed',
  135.             'user' => $user,
  136.             'showing' => 'owner',
  137.             'type' => $user->getType(),
  138.             'posts' => $posts,
  139.             'form' => $form->createView(),
  140.             'totalPost' => $postRepo->getTotalPostVisible()
  141.         ]);
  142.     }
  143.     #[Route('/discover-notifications'name'discover_notifications')]
  144.     public function discover(Request $requestDiscovery $discoveryAuthorization $authorization): JsonResponse
  145.     {
  146.         // Link: <https://hub.example.com/.well-known/mercure>; rel="mercure"
  147.         $discovery->addLink($request);
  148.         $authorization->setCookie($request, ['*']);
  149.         return $this->json([true]);
  150.     }
  151.     #[Route('/mes-notifications'name'responsive_notifications')]
  152.     public function responsiveNotifications(EntityManagerInterface $managerNotificationRepository $notificationRepo)
  153.     {
  154.         /** @var User $user */
  155.         $user $this->getUser();
  156.         $user->setNotificationsOpen(true)
  157.              ->setLastNotificationsOpening(new DateTime());
  158.         $manager->persist($user);
  159.         $manager->flush();
  160.         $lastNotifications $notificationRepo->findBy(['owner' => $user], ['id' => 'DESC'], 30);
  161.         $notifications = [];
  162.         $date = new DateTime();
  163.         foreach ($lastNotifications as $notification) {
  164.             $notificationDate $notification->getCreatedAt();
  165.             $diff $notificationDate->diff($date);
  166.             $daysInSecs $diff->format('%r%a') * 24 60 60;
  167.             $hoursInSecs $diff->60 60;
  168.             $minsInSecs $diff->60;
  169.             $seconds $daysInSecs $hoursInSecs $minsInSecs $diff->s;
  170.             
  171.             
  172.             if ($seconds 60 ) {
  173.             
  174.                 $notificationInterval '1 min';
  175.     
  176.             }
  177.     
  178.             else if ($seconds >= 60 && $seconds 3600 ) {
  179.             
  180.                 $notificationInterval intval($seconds 60) . ' min';
  181.     
  182.             }
  183.     
  184.             else if ($seconds >= 3600 && $seconds 86400) {
  185.     
  186.                 $notificationInterval intval($seconds 3600) . ' h';
  187.     
  188.             }
  189.             else if ($seconds >= 86400 && $seconds 604800) {
  190.     
  191.                 $notificationInterval intval($seconds 86400) . ' j';
  192.     
  193.             }
  194.             else if ($seconds >= 604800 && $seconds 604800) {
  195.                 $notificationInterval intval($seconds 604800) . ' semaine';
  196.             }
  197.             else {
  198.                 $notificationInterval intval($seconds 604800) . ' semaines';
  199.             }
  200.             
  201.             $formattedNotification = [
  202.                 'id' => $notification->getId(),
  203.                 'avatar' => $notification->getRelatedUser()->getAvatar(),
  204.                 'fullname' => $notification->getRelatedUser()->getFullname(),
  205.                 'content' =>  $notification->getContent(),
  206.                 'date' => $notificationInterval,
  207.                 'status' => !$notification->getIsOpened() ? 'new' ''
  208.             ];
  209.             $notifications[] = $formattedNotification;
  210.         }
  211.         return $this->render('front/shared/responsive-notifications.html.twig', [
  212.             'menu' => 'notification',
  213.             'user' => $user,
  214.             'showing' => 'owner',
  215.             'type' => $user->getType(),
  216.             'notifications' => $notifications
  217.         ]);
  218.     }
  219.     #[Route('/retrieve-post-video'name'retrieve_post_video')]
  220.     public function retrievePostVideo(Request $requestPostRepository $postRepo): Response
  221.     {
  222.         $data json_decode($request->getContent(), true);
  223.         $postId $data["postId"];
  224.         $post $postRepo->find($postId);
  225.         $videoPath $this->generateUrl('feed') . 'images/medias/' $post->getVideo();
  226.         return new JsonResponse(['videoPath' => $videoPath]);
  227.     }
  228.     #[Route('/email'name'email')]
  229.     public function email(ToUser $toUser): Response
  230.     {
  231.         $toUser->testEmail();
  232.         return $this->render('email/test-email.html.twig');
  233.     }
  234.     #[Route('/report-contact/{token}'name'report_contact')]
  235.     public function reportContact(User $userRequest $requestEntityManagerInterface $manager): Response
  236.     {
  237.         $debanRequest = new DebanRequest();
  238.         $form $this->createForm(DebanRequestType::class, $debanRequest);
  239.         $form->handleRequest($request);
  240.         if ($form->isSubmitted() && $form->isValid()) {
  241.             $debanRequest->setUser($user);
  242.             $manager->persist($debanRequest);
  243.             $manager->flush();
  244.             $this->addFlash("success""Votre demande a bien été pris en compte");
  245.             return $this->redirectToRoute('user_welcome');
  246.         }
  247.         return $this->render('/front/citizen/deban-request.html.twig', [
  248.             'form' => $form->createView()
  249.         ]);
  250.     }
  251. }