src/Controller/HomeController.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CBRCAGCADocument;
  4. use App\Entity\CBRCArticle;
  5. use App\Entity\CBRCArticleCategory;
  6. use App\Entity\CBRCUser;
  7. use App\Form\Type\CBRCUserDescriptionType;
  8. use App\Form\Type\CBRCUserType;
  9. use App\Form\Type\ContactType;
  10. use App\Form\Type\ModifyEmailType;
  11. use App\Form\Type\ModifyPasswordType;
  12. use App\Utilities\Mail;
  13. use Knp\Component\Pager\PaginatorInterface;
  14. use Symfony\Component\Form\FormError;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  21. use Symfony\Component\HttpFoundation\Request;
  22. class HomeController extends AbstractController
  23. {
  24.     /**
  25.      * @Route("", name="home")
  26.      */
  27.     public function ShowHome(Request $requestPaginatorInterface $paginator): \Symfony\Component\HttpFoundation\Response
  28.     {
  29.         $articles $this->getDoctrine()->getRepository(CBRCArticle::class)->findHome();
  30.         $pagination $paginator->paginate(
  31.             $articles/* query NOT result */
  32.             $request->query->getInt('page'1), /*page number*/
  33.             10 /*limit per page*/
  34.         );
  35.         return $this->render('home.html.twig', [
  36.             'articles' => $articles,
  37.             'pagination' => $pagination,
  38.         ]);
  39.     }
  40.     /**
  41.      * @Route("/categorie/{categoryname}", name="home_by_category")
  42.      */
  43.     public function ShowHomeByCategory(string $categorynameRequest $requestPaginatorInterface $paginator): \Symfony\Component\HttpFoundation\Response
  44.     {
  45.         $isnone=false;
  46.         $category$this->getDoctrine()->getRepository(CBRCArticleCategory::class)->findOneBy(['name'=>$categoryname]);
  47.         if(!$category && $categoryname!="none")
  48.             throw new NotFoundHttpException();
  49.         if($categoryname=="none") {
  50.             $articles =$this->getDoctrine()->getRepository(CBRCArticle::class)->findHomeNoneCategory();
  51.             $isnone=true;
  52.         }
  53.         else{
  54.             $articles $this->getDoctrine()->getRepository(CBRCArticle::class)->findHomeByCategory($category);
  55.         }
  56.         $pagination $paginator->paginate(
  57.             $articles/* query NOT result */
  58.             $request->query->getInt('page'1), /*page number*/
  59.             10 /*limit per page*/
  60.         );
  61.         return $this->render('home.html.twig', [
  62.             'articles' => $articles,
  63.             'pagination' => $pagination,
  64.             'activecategory' => $category,
  65.             'isnone' => $isnone,
  66.         ]);
  67.     }
  68.     /**
  69.      * @Route ("creeruncompte", name="signup")
  70.      */
  71.     public function ShowSignUp(Request $requestUserPasswordEncoderInterface $passwordEncoder): \Symfony\Component\HttpFoundation\Response
  72.     {
  73.         $user = new CBRCUser();
  74.         $form $this->createForm(CBRCUserType::class, $user);
  75.         $form->handleRequest($request);
  76.         if ($form->isSubmitted() && $form->isValid()) {
  77.             $password $passwordEncoder->encodePassword($user$user->getPlainPassword());
  78.             $user->setPassword($password);
  79.             $entityManager $this->getDoctrine()->getManager();
  80.             $entityManager->persist($user);
  81.             $entityManager->flush();
  82.             $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  83.             $this->container->get('security.token_storage')->setToken($token);
  84.             $this->container->get('session')->set('_security_main'serialize($token));
  85.             return $this->redirectToRoute('account');
  86.             //test modif 5
  87.         }
  88.         return $this->render('homesignup.html.twig', [
  89.             'registerform' => $form->createView(),
  90.         ]);
  91.     }
  92.     /**
  93.      * @Route("/contact", name="contact")
  94.      */
  95.     public function ShowContact(Request $request)
  96.     {
  97.         //form contact
  98.         $formcontact $this->createForm(ContactType::class,null,[
  99.             'attr' => [
  100.                 'onsubmit' => 'return oncontactsubmit(event);'
  101.             ]
  102.         ]);
  103.         $formcontact->handleRequest($request);
  104.         if($formcontact->isSubmitted() && $formcontact->isValid()){
  105.             $name $formcontact->getData()['name'];
  106.             $email $formcontact->getData()['email'];
  107.             $message $formcontact->getData()['message'];
  108.             $recaptchatoken $formcontact['recaptchatoken']->getData();
  109.             if(!in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1''::1')))
  110.             {
  111.                 $recaptcha = new \ReCaptcha\ReCaptcha("6Ldhw70gAAAAAAefuTIeH2S7hu8LH6f2n8Mw_UCN");
  112.                 $resp $recaptcha->setExpectedAction('contact')
  113.                     ->verify($recaptchatoken$_SERVER['REMOTE_ADDR']);
  114.                 //->setExpectedHostname('dev.cbrc51.fr')
  115.                 if (!$resp->isSuccess()) {
  116.                     $this->addFlash('danger''Etes vous un robot?');
  117.                     return $this->redirect($request->getUri());
  118.                 }
  119.             }
  120.             $txtmessage "Demande de ".$name." dont l'adresse mail est ".$email." et le message est: ".$message;
  121.             $subject "Contact";
  122.             $htmlmessage $this->renderView('mail/askinformationmail.html.twig', [
  123.                 'subject' => $subject,
  124.                 'name' => $name,
  125.                 'email' => $email,
  126.                 'message' => $message,
  127.             ]);
  128.             $return Mail::SendMailToAdmin($email$subject$htmlmessage$txtmessage);
  129.             if($return ==false)
  130.             {
  131.                 $this->addFlash('danger''Erreur lors de l\'envoi du mail');
  132.             }
  133.             else
  134.             {
  135.                 $this->addFlash('success''Le message a été envoyé');
  136.             }
  137.             return $this->redirect($request->getUri());
  138.         }
  139.         if($formcontact->isSubmitted() && !$formcontact->isValid())
  140.         {
  141.             foreach($formcontact->getErrors(true) as $error)
  142.                 $this->addFlash('danger'"(".$error->getOrigin()->getName().") ".$error->getMessage());
  143.             return $this->redirect($request->getUri());
  144.         }
  145.         return $this->render('homecontact.html.twig', [
  146.             'formcontact' => $formcontact->createView(),
  147.         ]);
  148.     }
  149.     /**
  150.      * @Route("/informations", name="about")
  151.      */
  152.     public function ShowAbout(Request $request)
  153.     {
  154.         $documents $this->getDoctrine()->getRepository(CBRCAGCADocument::class)->findAll();
  155.         return $this->render('homeabout.html.twig', [
  156.             'documents' => $documents,
  157.         ]);
  158.     }
  159.     /**
  160.      * @Route("/mentionslegales", name="legal")
  161.      */
  162.     public function ShowLegal(Request $request)
  163.     {
  164.         return $this->render('homelegal.html.twig', [
  165.         ]);
  166.     }
  167.     /**
  168.      * @Route ("/moncompte", name="account")
  169.      */
  170.     public function ShowAccount(Request $requestUserPasswordEncoderInterface $passwordEncoder)
  171.     {
  172.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  173.         $user $this->getUser();
  174.         $formuserdescription $this->createForm(CBRCUserDescriptionType::class, $user->getDescription());
  175.         $formuserdescription->handleRequest($request);
  176.         if ($formuserdescription->isSubmitted() && $formuserdescription->isValid()) {
  177.             $em $this->getDoctrine()->getManager();
  178.             $em->persist($user);
  179.             $em->flush();
  180.             $this->addFlash('success''Modification effetuée');
  181.             return $this->redirect($request->getUri());
  182.         }
  183.         if ($formuserdescription->isSubmitted() && !$formuserdescription->isValid()) {
  184.             foreach ($formuserdescription->getErrors(true) as $error)
  185.                 $this->addFlash('danger'"(" $error->getOrigin()->getName() . ") " $error->getMessage());
  186.             $formuserdescription->clearErrors(true);
  187.             return $this->redirect($request->getUri());
  188.         }
  189.         //formmodifypassword
  190.         $formuserpassword $this->createForm(ModifyPasswordType::class);
  191.         $formuserpassword->handleRequest($request);
  192.         if ($formuserpassword->isSubmitted() && $formuserpassword->isValid()) {
  193.             //Check actual password
  194.             $password $formuserpassword->getData()['actualpassword'];
  195.             //check password
  196.             $validpassword $passwordEncoder->isPasswordValid($user$password);
  197.             if ($validpassword) {
  198.                 $password $passwordEncoder->encodePassword($user$formuserpassword->getData()['plainPassword']);
  199.                 $user->setPassword($password);
  200.                 // 4) save the User!
  201.                 $entityManager $this->getDoctrine()->getManager();
  202.                 $entityManager->persist($user);
  203.                 $entityManager->flush();
  204.                 $this->addFlash('primary''Le mot de passe a été modifié');
  205.                 return $this->redirect($request->getUri());
  206.             } else {
  207.                 $formuserpassword->get('actualpassword')->addError(new FormError('Mauvais mot de passe'));
  208.             }
  209.         }
  210.         if ($formuserpassword->isSubmitted() && !$formuserpassword->isValid()) {
  211.             foreach ($formuserpassword->getErrors(true) as $error)
  212.                 $this->addFlash('danger'"(" $error->getOrigin()->getName() . ") " $error->getMessage());
  213.             return $this->redirect($request->getUri());
  214.         }
  215.         //formmodifyemail
  216.         $formuseremail $this->createForm(ModifyEmailType::class, null, ['email_placeholder' => $user->getEmail()]);
  217.         $formuseremail->handleRequest($request);
  218.         if ($formuseremail->isSubmitted() && $formuseremail->isValid()) {
  219.             //Check actual password
  220.             $password $formuseremail->getData()['password'];
  221.             //check password
  222.             $validpassword $passwordEncoder->isPasswordValid($user$password);
  223.             if ($validpassword) {
  224.                 $usermail $formuseremail->getData()['email'];
  225.                 $user->setEmail($usermail);
  226.                 $entityManager $this->getDoctrine()->getManager();
  227.                 $entityManager->persist($user);
  228.                 $entityManager->flush();
  229.                 $this->addFlash('primary''L\'adresse email a été modifée');
  230.                 return $this->redirect($request->getUri());
  231.             } else {
  232.                 $formuseremail->get('password')->addError(new FormError('Mauvais mot de passe'));
  233.             }
  234.         }
  235.         if ($formuseremail->isSubmitted() && !$formuseremail->isValid()) {
  236.             foreach ($formuseremail->getErrors(true) as $error)
  237.                 $this->addFlash('danger'"(" $error->getOrigin()->getName() . ") " $error->getMessage());
  238.             return $this->redirect($request->getUri());
  239.         }
  240.         return $this->render('homeaccount.html.twig', [
  241.                 'userdescriptionform' => $formuserdescription->createView(),
  242.                 'modifypasswordform' => $formuserpassword->createView(),
  243.                 'modifyemailform' => $formuseremail->createView(),
  244.                 'idactive'=>0,
  245.             ]
  246.         );
  247.     }
  248.     public function RenderAccountMenu(int $idactive=0)
  249.     {
  250.         return $this->render('menu/accountmenu.html.twig', [
  251.                 'idactive' => $idactive
  252.             ]
  253.         );
  254.     }
  255. }