src/Controller/DefaultController.php line 109

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Game;
  4. use App\Entity\Page;
  5. use App\Entity\User;
  6. use App\Entity\Word;
  7. use App\Entity\Category;
  8. use Symfony\Component\Mime\Email;
  9. use App\Controller\CachedController;
  10. use App\Entity\UserSubscribeActivity;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Karser\Recaptcha3Bundle\Form\Recaptcha3Type;
  17. use Symfony\Component\Validator\Constraints\Length;
  18. use Symfony\Component\Form\Extension\Core\Type\TextType;
  19. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  20. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  21. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  22. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  23. use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3;
  24. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  25. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  26. #[Route('/{_locale}')]
  27. class DefaultController extends AbstractController
  28. {
  29.     private $doctrine;
  30.     private $entityManager;
  31.     
  32.     public function __construct(ManagerRegistry $doctrine)
  33.     {
  34.         $this->doctrine $doctrine;
  35.         $this->entityManager $this->doctrine->getManager();
  36.     }
  37.     
  38.     #[Route('/default'name'app_default')]
  39.     public function index(): Response
  40.     {
  41.         return $this->render('default/index.html.twig', [
  42.             'controller_name' => 'DefaultController',
  43.         ]);
  44.     }
  45.     
  46.     #[Route('/restricted-access.html'name'front_restricted_access')]
  47.     public function restrictedAccessAction(){
  48.         
  49.         $response = new Response();
  50.         $response->setCache(['max_age' => CachedController::ONE_WEEK_CACHE,  's_maxage' => CachedController::ONE_WEEK_CACHE]);
  51.         return $this->render('default/restricted_access.html.twig', array());
  52.     }
  53.     #[Route('/page/{url}'name'static_page')]
  54.     public function staticPage(Request $request$url): Response
  55.     {
  56.         $locale $request->get('_locale','fr');
  57.         $pageRepository $this->doctrine->getRepository(Page::class);
  58.         $content $pageRepository->findOneBy(['url' => $url'locale' => $locale]);
  59.         if( $content )
  60.         {
  61.             return $this->render('default/static_page.html.twig', [
  62.                 'content' => $content ,
  63.             ]);
  64.         }else{
  65.              throw $this->createNotFoundException();
  66.         }
  67.     }
  68.     #[Route('/mentions-legales.html'name'front_mention_legale')]
  69.     public function mentionLegales(Request $request): Response
  70.     {
  71.         return $this->render('default/mention-legales.html.twig', []);
  72.     }
  73.     #[Route('/plan-du-site.html'name'front_sitemap')]
  74.     public function siteMap(Request $request): Response
  75.     {
  76.         $locale $request->get('_locale','fr');
  77.         $repository $this->doctrine->getRepository(Page::class);
  78.         $link $repository->getI18nPageByRoute('cgv'$locale);
  79.         $categories $this->doctrine->getRepository(Category::class)->getFrontCategories();
  80.         $themes $this->doctrine->getRepository(Category::class)->getFrontThemes();
  81.         $games $this->doctrine->getRepository(Game::class)->getPublishedGamesByTitle($request->getLocale());
  82.         $response = new Response();
  83.         $response->setCache(['max_age' => CachedController::ONE_WEEK_CACHE,  's_maxage' => CachedController::ONE_WEEK_CACHE]);
  84.         return $this->render('default/sitemap.html.twig', [
  85.             'cgv' => $link
  86.             'categories' => $categories
  87.             'themes' => $themes
  88.             'games' => $games
  89.         ],$response);
  90.     }
  91.     
  92.     #[Route('/foire-aux-questions.html'name'front_faqs')]
  93.     public function faqs(Request $request): Response
  94.     {
  95.         die();
  96.     }
  97.     #[Route('/qui-sommes-nous.html'name'front_whoarewe')]
  98.     public function whoarewe(Request $request): Response
  99.     {
  100.         $tDebut = new \DateTime();
  101.         $tDebut->setTimestamp(1323212400);
  102.         $intervalle $tDebut->diff(new \DateTime());
  103.         $nbMois $intervalle->$intervalle->12;
  104.         $nbFans 120547;
  105.         
  106.         $nbInscrit $this->doctrine->getRepository(User::class)->countUsers();
  107.         $nbAbonne $this->doctrine->getRepository(User::class)->countSubscriber();
  108.         
  109.         $nbJeux $this->doctrine->getRepository(Game::class)->countGames();
  110.         
  111.         $nbMots $this->doctrine->getRepository(Word::class)->getRoundedCount(1);
  112.         $response = new Response();
  113.         $response->setCache(['max_age' => CachedController::ONE_WEEK_CACHE,  's_maxage' => CachedController::ONE_WEEK_CACHE]);
  114.         
  115.         $locale $request->get('_locale','fr');
  116.         $locales = array('fr');
  117.         if(array_search($locale$locales) === false)
  118.             $locale 'fr';
  119.         
  120.         return $this->render('whoarewe/whoarewe-'.$locale.'.html.twig', [
  121.             'nbFan' => $nbFans,
  122.             'nbMois' => $nbMois,
  123.             'nbInscrit' => $nbInscrit,
  124.             'nbAbonne' => $nbAbonne,
  125.             'nbJeux' => $nbJeux,
  126.             'nbMots' => $nbMots
  127.         ], $response);
  128.     }
  129.     #[Route('/charte.html'name'front_charte')]
  130.     public function charte(Request $request): Response
  131.     {
  132.         $response = new Response();
  133.         $response->setCache(['max_age' => CachedController::ONE_WEEK_CACHE,  's_maxage' => CachedController::ONE_WEEK_CACHE]);
  134.         
  135.         $locale $request->get('_locale','fr');
  136.         $locales = array('fr','de');
  137.         if(array_search($locale$locales) === false)
  138.             $locale 'fr';
  139.             
  140.         return $this->render('charte/charteV3-'.$locale.'.html.twig', [
  141.         ], $response);
  142.     }
  143.     
  144.     #[Route('/contact-questions.html'name'front_contact_new')]
  145.     public function contactNew(Request $request): Response
  146.     {
  147.         $locale $request->get('_locale','fr');
  148.         $locales = array('fr','de');
  149.         if(array_search($locale$locales) === false) {
  150.             return $this->redirect($this->generateUrl('front_contact'));
  151.             //$locale = 'fr';
  152.         }
  153.         $response = new Response();
  154.         $response->setCache(['max_age' => CachedController::ONE_WEEK_CACHE,  's_maxage' => CachedController::ONE_WEEK_CACHE]);
  155.         return $this->render('faq/faqs-'.$locale.'.html.twig', ['locale' => $locale], $response);
  156.     }
  157.     const MAIL_CONTACT "contact@speakyplanet.com";
  158.     const MAIL_CONTACT_DE "kontakt-de@speakyplanet.com";
  159.     #[Route('/contact.html'name'front_contact')]
  160.     public function contact(Request $requestMailerInterface $mailer): Response
  161.     {
  162.         $defaultData = [];
  163.         $isUser false;
  164.         $subscribedUser false;
  165.         /** @var User $user */
  166.         $user $this->getUser();
  167.         if($user)
  168.         {
  169.             $defaultData = [
  170.                 'firstName' => $user->getFirstName(),
  171.                 'lastName' => $user->getLastName(),
  172.                 'email' => $user->getEmail(),
  173.             ];
  174.             $isUser true;
  175.             $subscribedUser $user->getIsSubscriber();
  176.         }
  177.         $form $this->createFormBuilder($defaultData, ['translation_domain' => 'contact'])
  178.                 ->add('firstName'TextType::class, [
  179.                     'constraints' => new Length(['min' => 3]),
  180.                     'label' => "Votre nom :"
  181.                 ])
  182.                 ->add('lastName'TextType::class, [
  183.                     'constraints' => new Length(['min' => 3]),
  184.                     'label' => "Votre prénom :"
  185.                 ])
  186.                 ->add('email'EmailType::class, [
  187.                     'label' => "Votre adresse e-mail :"
  188.                 ])
  189.                 ->add('reason'ChoiceType::class, [
  190.                     'choices'  => [
  191.                         'une demande de renseignement' => 'renseignement'
  192.                         'un problème technique' => 'problème technique'
  193.                         'votre avis sur le site' => 'votre avis'
  194.                         'une proposition de partenariat' => 'partenariat'
  195.                         'une demande de désabonnement' => 'désabonnement'
  196.                         'Satisfait ou remboursé sous 48h' => 'remboursement'
  197.                     ],
  198.                     'label' => "Votre question concerne :",
  199.                 ])
  200.                 ->add('message'TextareaType::class, [
  201.                     'label' => "Votre message :",
  202.                 ])
  203.                 ->add('envoyer'SubmitType::class, [
  204.                     'label' => "Envoyer",
  205.                 ])
  206.                 ->add('captcha'Recaptcha3Type::class, [
  207.                     'constraints' => new Recaptcha3(),
  208.                     'action_name' => 'contact',
  209.                     'locale' => $request->getLocale(),
  210.                 ])
  211.                 ->getForm();
  212.         $form->handleRequest($request);
  213.         if ($form->isSubmitted() && $form->isValid()) {
  214.             $data $form->getData();
  215.             $isAutomaticUnsubscribe $user != null && $data['reason'] == "désabonnement";
  216.             if(!$isAutomaticUnsubscribe)
  217.             {
  218.                 switch ($request->getLocale())
  219.                 {
  220.                     case 'fr':
  221.                         $contactEmail self::MAIL_CONTACT;
  222.                         break;
  223.                     case 'de':
  224.                         $contactEmail self::MAIL_CONTACT_DE;
  225.                         break;
  226.                     default:
  227.                         $contactEmail self::MAIL_CONTACT;
  228.                         break;
  229.                 }
  230.                 $browser $_SERVER['HTTP_USER_AGENT'];
  231.                 $html $this->renderView('mail/mail_contact.html.twig', [
  232.                     'nom'            => $data['lastName'],
  233.                     'prenom'    => $data['firstName'],
  234.                     'reason'        => $data['reason'],
  235.                     'message'    => $data['message'],
  236.                     'browser' => $browser
  237.                 ]);
  238.                 $text preg_replace"/\n\s+/""\n"rtrim(html_entity_decode(strip_tags($html))) );
  239.                 $email = (new Email())
  240.                 ->from($contactEmail)
  241.                 ->to($contactEmail)
  242.                 ->replyTo($data['email'])
  243.                 ->subject('[Contact] ' $data['lastName'] . ' ' $data['firstName'])
  244.                 ->text($text)
  245.                 ->html($html);
  246.                 $mailer->send($email);
  247.                 return $this->redirectToRoute('front_contact_sent');
  248.             } else {
  249.                 $repository $this->doctrine->getRepository(UserSubscribeActivity::class);
  250.                 $activity $repository->findOneBy(array('user' => $user->getId(), 'statut' => UserSubscribeActivity::STATUT_PENDING));
  251.                 
  252.                 if($activity == null)
  253.                 {
  254.                     $activity = new UserSubscribeActivity();
  255.                     $activity->setUser($user);
  256.                     $activity->setStatut(UserSubscribeActivity::STATUT_PENDING);
  257.                     $activity->setType(UserSubscribeActivity::TYPE_UNSUBSCRIBE);
  258.                     $activity->setIsAuto(true);
  259.                     
  260.                     $em $this->entityManager;
  261.                     $em->persist($activity);
  262.                     $em->flush();
  263.                     
  264.                 }
  265.                 return $this->redirectToRoute('front_unsubscribe_sent');
  266.             }
  267.         }
  268.         
  269.         return $this->renderForm('default/contact.html.twig', [
  270.             'form' => $form,
  271.             'subscribedUser' => $subscribedUser,
  272.         ]);
  273.     }
  274.     #[Route('/email')]
  275.     public function sendEmail(MailerInterface $mailer): Response
  276.     {
  277.         $email = (new Email())
  278.             ->from(self::MAIL_CONTACT)
  279.             ->to('anthony@speakyplanet.com')
  280.             //->cc('cc@example.com')
  281.             //->bcc('bcc@example.com')
  282.             //->replyTo('fabien@example.com')
  283.             //->priority(Email::PRIORITY_HIGH)
  284.             ->subject('Time for Symfony Mailer!')
  285.             ->text('Sending emails is fun again!')
  286.             ->html('<p>See Twig integration for better HTML integration!</p>');
  287.         try {
  288.             $mailer->send($email);
  289.         } catch (TransportExceptionInterface $e) {
  290.             dump($e);
  291.             // some error prevented the email sending; display an
  292.             // error message or try to resend the message
  293.         }
  294.         return $this->render('default/contact_sent.html.twig', []);
  295.     }
  296.     #[Route('/contact-envoye.html'name'front_contact_sent')]
  297.     public function contactSent(Request $request): Response
  298.     {
  299.         return $this->render('default/contact_sent.html.twig', []);
  300.     }
  301.     #[Route('/desabonnement-envoye.html'name'front_unsubscribe_sent')]
  302.     public function unsubscribeSent(Request $request): Response
  303.     {
  304.         return $this->render('default/unsubscribe_sent.html.twig', []);
  305.     }
  306.     
  307. }