src/Controller/CatalogController.php line 116

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use App\Services\OrderDetailManager;
  14. use App\Services\CourseManager;
  15. use App\Repository\UserRepository;
  16. use App\Entity\Certificate;
  17. use App\Entity\Competence;
  18. use App\Entity\Course;
  19. use App\Entity\CourseSession;
  20. use App\Entity\CustomerSession;
  21. use App\Entity\User;
  22. use App\Entity\UserSession;
  23. use App\Form\ParticipantFormType;
  24. use App\Services\CourseSessionManager;
  25. use App\Services\UserManager;
  26. use Symfony\Component\Mailer\MailerInterface;
  27. use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
  28. use Symfony\Component\Mime\Email;
  29. use App\Services\CustomerManager;
  30. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  31. use App\Config\Config;
  32. use App\Services\CustomerSessionManager;
  33. use DateTime;
  34. class CatalogController extends AbstractController
  35. {
  36.     public function __construct(
  37.         EntityManagerInterface $entityManager,
  38.         Security $security,
  39.         SessionInterface $session,
  40.         UserManager $um,
  41.         /*, OrderDetailManager $odm*/
  42.         CourseManager $cm,
  43.         MailerInterface $mailer,
  44.         CourseSessionManager $csm,
  45.         CustomerManager $customerManager,
  46.         Config $config,
  47.         CustomerSessionManager $custsesm
  48.     ) {
  49.         $this->entityManager $entityManager;
  50.         $this->session $session;
  51.         $this->security $security;
  52.         $this->config $config;
  53.         $this->um $um;
  54.         $this->mailer $mailer;
  55.         $this->cm $cm;
  56.         $this->csm $csm;
  57.         $this->customerManager $customerManager;
  58.         $this->custsesm $custsesm;
  59.     }
  60.     #[Route("/catalog/professionnel"name"catalog_professionnel")]
  61.     public function catalogProfessionnel(Request $request): Response
  62.     {
  63.         $filtersSelected = [];
  64.         $filters = [];
  65.         $filtersSelected = [];
  66.         if ($request->isMethod("post")) {
  67.             $filtersSelected["location"] = $request->request->get("location");
  68.             $filtersSelected["duration"] = $request->request->get("duration");
  69.             $filtersSelected["year_of_exp"] = $request->request->get(
  70.                 "year_of_exp"
  71.             );
  72.             $filtersSelected["certificate"] = $request->request->get(
  73.                 "certificate"
  74.             );
  75.             $filtersSelected["competence"] = $request->request->get(
  76.                 "competence"
  77.             );
  78.             $filtersSelected["price_min"] = $request->request->get("price_min");
  79.             $filtersSelected["price_max"] = $request->request->get("price_max");
  80.             $filtersSelected["date_begin"] = $request->request->get(
  81.                 "date_begin"
  82.             );
  83.             $this->session->set("filtersSelected"$filtersSelected);
  84.             $courses $this->cm->getFilteredCoursesWithDateByType(
  85.                 "inter",
  86.                 $filtersSelected
  87.             );
  88.         } else {
  89.             $courses $this->cm->getCoursesWithDateByType("inter");
  90.         }
  91.         $filters["locations"] = $this->cm->getCourseLocationByType("inter");
  92.         $filters["durations"] = $this->cm->getCourseDurationByType("inter");
  93.         $filters["certificates"] = $this->entityManager
  94.             ->getRepository(Certificate::class)
  95.             ->findBy([], ["level" => "ASC""title" => "ASC"]);
  96.         $filters["competences"] = $this->entityManager
  97.             ->getRepository(Competence::class)
  98.             ->findBy([], ["id" => "ASC"]);
  99.         //"courses" => $this->cm->getLastCourseForHomeIntra(), //courses
  100.         return $this->render("acoa/catalog/catalog_professionnel.html.twig", [
  101.             "controller_name" => "CatalogController",
  102.             "courses" => $courses,
  103.             "filters" => $filters,
  104.             "filtersSelected" => $filtersSelected,
  105.         ]);
  106.     }
  107.     #[Route("/catalog/entreprise"name"catalog_entreprise")]
  108.     public function catalogEntreprise(Request $request): Response
  109.     {
  110.         $filtersSelected = [];
  111.         if ($request->isMethod("post")) {
  112.             $filtersSelected["location"] = $request->request->get("location");
  113.             $filtersSelected["duration"] = $request->request->get("duration");
  114.             $filtersSelected["year_of_exp"] = $request->request->get(
  115.                 "year_of_exp"
  116.             );
  117.             $filtersSelected["competence"] = $request->request->get(
  118.                 "competence"
  119.             );
  120.             $filtersSelected["price_min"] = $request->request->get("price_min");
  121.             $filtersSelected["price_max"] = $request->request->get("price_max");
  122.             $filtersSelected["date_begin"] = $request->request->get(
  123.                 "date_begin"
  124.             );
  125.             $this->session->set("filtersSelected"$filtersSelected);
  126.             $courses $this->cm->getFilteredCoursesWithDateByType(
  127.                 "intra",
  128.                 $filtersSelected
  129.             );
  130.         } else {
  131.             $courses $this->cm->getCoursesWithDateByType("intra");
  132.         }
  133.         //On prĂ©pare les donnĂ©es pour les SELECT du filtre
  134.         $filters = [];
  135.         $filters["locations"] = $this->cm->getCourseLocationByType("intra");
  136.         $filters["durations"] = $this->cm->getCourseDurationByType("intra");
  137.         $filters["competences"] = $this->entityManager
  138.             ->getRepository(Competence::class)
  139.             ->findBy([], ["id" => "ASC"]);
  140.         return $this->render("acoa/catalog/catalog_entreprise.html.twig", [
  141.             "controller_name" => "CatalogController",
  142.             "courses" => $courses,
  143.             "filters" => $filters,
  144.             "filtersSelected" => $filtersSelected,
  145.         ]);
  146.     }
  147.     #[
  148.         Route(
  149.             "/catalog/detail/{idCourse}",
  150.             name"catalog_detail",
  151.             requirements: ["idCourse" => "\d+"]
  152.         )
  153.     ]
  154.     public function detailFormation(Request $requestint $idCourse): Response
  155.     {
  156.         if ($request->request->get("idCourseSession")) {
  157.             $this->session->set(
  158.                 "idCourseSession",
  159.                 $request->request->get("idCourseSession")
  160.             );
  161.             return $this->redirectToRoute("catalog_participant");
  162.         }
  163.         if ($this->session->get("filtesSelected") !== null) {
  164.             $course $this->cm->getFilteredCoursesAndCourseSessionsByIdCourse(
  165.                 $idCourse,
  166.                 $this->session->get("filtesSelected")
  167.             );
  168.             $filtersSelected $this->session->get("filtesSelected");
  169.         } else {
  170.             $course $this->cm->getCourseByIdCourse($idCourse);
  171.             $filtersSelected null;
  172.         }
  173.         if (sizeof($course)) {
  174.             if ($course[0]["categorie_id"] != 3) {
  175.                 $courseSession $this->cm->getCourseSessionsByIdCourse($idCourse);
  176.                 //dd($courseSession);
  177.                 foreach ($courseSession as $i => $data) {
  178.                     $courseSession[$i]["trainer"] = $this->um->getTeachersForCourseSessionCatalogDetail(
  179.                         $courseSession[$i]["idCourseSession"]
  180.                     );
  181.                 }
  182.                 $course[0]["courseSession"] = $courseSession;
  183.             } else {
  184.                 $course[0]["courseSession"] = array();
  185.             }
  186.         }
  187.         return $this->render("acoa/catalog/catalog_detail.html.twig", [
  188.             "controller_name" => "CatalogController",
  189.             "course" => $course,
  190.             "filtersSelected" => $filtersSelected,
  191.         ]);
  192.     }
  193.     #[
  194.         Route(
  195.             "/upd_list_paticipants/{email}",
  196.             name"upd_list_paticipants_email"
  197.         )
  198.     ]
  199.     public function upd_list_paticipants()
  200.     {
  201.         $oldEmail substr($_SERVER["REQUEST_URI"], 31);
  202.         if (!str_contains(substr($_SERVER["REQUEST_URI"], 22), "del_to_eb")) {
  203.             if ($_POST) {
  204.                 $this->um->updateUserParticipant($_POST["email"], substr($_SERVER["REQUEST_URI"], 22));
  205.                 return $this->redirectToRoute("home");
  206.             } else {
  207.                 return $this->renderForm(
  208.                     "acoa/catalog/catalog_upd_list_participants.html.twig",
  209.                     ["email" => substr($_SERVER["REQUEST_URI"], 22)]
  210.                 );
  211.             }
  212.         } else {
  213.             $this->um->updateUserParticipantDelete($oldEmail);
  214.             return $this->redirectToRoute("home");
  215.         }
  216.     }
  217.     #[Route("/catalog/participant/{origin}"name"catalog_participant")]
  218.     public function participant(
  219.         Request $request,
  220.         ManagerRegistry $doctrine,
  221.         UserPasswordHasherInterface $userPasswordHasher,
  222.         UserRepository $userRepository,
  223.         $origin null
  224.     ): Response {
  225.         $new $this->session->get('new');
  226.         $ics $this->session->get('ics');
  227.         $sd $this->session->get('sd');
  228.         $f $this->session->get('f');
  229.         if ((int)$new == 1) {
  230.             $this->session->set('new_data', ["origin" => "?=0""f" => $f"sd" => $sd"ics" => $ics]);
  231.             return $this->redirectToRoute('register_information');
  232.         }
  233.         $internal false;
  234.         if (isset($_GET["origin"]) && urldecode($_GET["origin"]) == "?=0") {
  235.             $internal true;
  236.             $this->session->set("origin"0);
  237.         }
  238.         if (isset($_GET["f"])) {
  239.             $formation base64_decode($_GET["f"]);
  240.         }
  241.         if (isset($_GET["sd"])) {
  242.             $startDate base64_decode($_GET["sd"]);
  243.         }
  244.         if ($this->session->get("ics")) {
  245.             $ics $this->session->get("ics");
  246.         }
  247.         $users = new ArrayCollection();
  248.         $form $this->createForm(ParticipantFormType::class, $users);
  249.         $form->handleRequest($request);
  250.         if ($form->isSubmitted() && $form->isValid()) {
  251.             $users $form->getData();
  252.             $i 0;
  253.             $error false;
  254.             $emailExist false;
  255.             $this->session->set(
  256.                 "users",
  257.                 $request->request->get("participant_form")["users"]
  258.             );
  259.             foreach ($request->request->get("participant_form")["users"] as $user) {
  260.                 if (
  261.                     $user["firstName"] == null ||
  262.                     $user["lastName"] == null ||
  263.                     $user["email"] == null ||
  264.                     $user["schoolDegree1"] == null
  265.                 ) {
  266.                     $this->session
  267.                         ->getFlashBag()
  268.                         ->add(
  269.                             "info",
  270.                             "Veuillez saisir toutes les informations."
  271.                         );
  272.                     $error true;
  273.                     break;
  274.                 } else {
  275.                     if (!filter_var($user["email"], FILTER_VALIDATE_EMAIL)) {
  276.                         $this->session
  277.                             ->getFlashBag()
  278.                             ->add("info""Veuillez saisir un email valide.");
  279.                         $error true;
  280.                         break;
  281.                     } else {
  282.                         $newUser $this->getDoctrine()
  283.                             ->getRepository(User::class)
  284.                             ->findOneBy(["email" => $user["email"]]);
  285.                         if ($newUser === null) {
  286.                             $newUser = new User();
  287.                             $newUser->setFirstName($user["firstName"]);
  288.                             $newUser->setLastName($user["lastName"]);
  289.                             $newUser->setEmail($user["email"]);
  290.                             $newUser->setSchoolDegree1($user["schoolDegree1"]);
  291.                             $newUser->setPassword(
  292.                                 password_hash(
  293.                                     $this->generateRandomString(10),
  294.                                     PASSWORD_DEFAULT
  295.                                 )
  296.                             );
  297.                             $newUser->setRoles(["ROLE_STUDENT"]);
  298.                             $newUser->setIsActive(1);
  299.                             $newUser->setIdUserProfil(2);
  300.                             $this->entityManager->persist($newUser);
  301.                             ///////_______
  302.                             // PLI dans le cas des formations INTER (en passant par le catalogue)
  303.                             // on renseigne  les particiapants avant l'inscription ou l'authentification du customer
  304.                             // c'est correct pour les formations INTRA.
  305.                             if ($this->getUser() && $internal) {
  306.                                 // PL$ - setIdCustomer on  
  307.                                 $newUser->setIdCustomer((int) $this->getUser()->getIdCustomer());
  308.                                 if (!$this->custsesm->isCustomerSessionExists($this->getUser()->getIdCustomer(), base64_decode($ics))) {
  309.                                     $customerSession = new CustomerSession();
  310.                                     $customerSession->setIdCustomer((int) $this->getUser()->getIdCustomer());
  311.                                     if (isset($ics)) {
  312.                                         $customerSession->setIdCourseSession(base64_decode($ics));
  313.                                     } else
  314.                                         $customerSession->setIdCourseSession(0);
  315.                                     $customerSession->setDateEvenement(new \DateTime());
  316.                                     $this->entityManager->persist($customerSession);
  317.                                     $this->entityManager->flush();
  318.                                 }
  319.                             }
  320.                             ///////_______
  321.                             $this->entityManager->flush();
  322.                             $emailTo[] = [
  323.                                 "email" => $user["email"],
  324.                                 "name" =>
  325.                                 $user["firstName"] .
  326.                                     " " .
  327.                                     $user["lastName"],
  328.                             ];
  329.                         } else {
  330.                             $emailExist true;
  331.                             $emailTo[] = [
  332.                                 "email" => $user["email"],
  333.                                 "name" =>
  334.                                 $user["lastName"] .
  335.                                     " " .
  336.                                     $user["firstName"],
  337.                             ];
  338.                         }
  339.                         $newUser->setTypeCourseSession($user["type"]);
  340.                         $this->session->set("user" $i$newUser);
  341.                         $i++;
  342.                     }
  343.                 }
  344.             }
  345.             $s $this->session->get("origin");
  346.             if ($error === false && !$s && !isset($_GET["origin"])) {
  347.                 $this->entityManager->flush();
  348.                 $this->session->set("nbUser"$i);
  349.                 return $this->redirectToRoute("catalog_panier", [
  350.                     "idCourseSession" => $this->session->get("idCourseSession"),
  351.                 ]);
  352.             } elseif (($s || isset($_GET["origin"])) && $error === false) {
  353.                 $this->session->set(
  354.                     "data_origin_i_formation",
  355.                     isset($formation) ? $formation ""
  356.                 );
  357.                 $this->session->set(
  358.                     "data_origin_i_startDate",
  359.                     isset($startDate) ? $startDate ""
  360.                 );
  361.                 $formation = isset($formation) ? $formation "";
  362.                 $startDate = isset($startDate) ? $startDate "";
  363.                 if (!$emailExist) {
  364.                     if (isset($emailTo) && $emailTo) {
  365.                         foreach ($emailTo as $key) {
  366.                             if (isset($ics)) {
  367.                                 $u_s = new UserSession();
  368.                                 $u_s->setIdCourseSession(base64_decode($ics));
  369.                                 $u_s->setIdUser(
  370.                                     $this->um
  371.                                         ->getUserByEmail($key["email"])[0]
  372.                                         ->getId()
  373.                                 );
  374.                                 $u_s->setMark("");
  375.                                 $this->entityManager->persist($u_s);
  376.                                 $this->entityManager->flush();
  377.                             }
  378.                             $url =
  379.                                 $this->config->getDomain() .
  380.                                 "/confirm/registration/" .
  381.                                 base64_encode("iokljhko!$#pkY@787NK---LN47") .
  382.                                 "?sun=" .
  383.                                 base64_encode($key["email"]) .
  384.                                 "&f=" .
  385.                                 base64_encode($formation);
  386.                             $em = (new TemplatedEmail())
  387.                                 ->from($this->config->getMailSender())
  388.                                 ->priority(Email::PRIORITY_HIGH)
  389.                                 ->to($key["email"])
  390.                                 ->cc(
  391.                                     "kalidougattaba@gmail.com",
  392.                                     "pascal.liatard@abware.fr"
  393.                                 )
  394.                                 ->subject(
  395.                                     $formation .
  396.                                         ": Finalisation de votre inscription"
  397.                                 )
  398.                                 ->htmlTemplate(
  399.                                     "acoa/email/mailto_student.html.twig"
  400.                                 )
  401.                                 ->context([
  402.                                     "name" => $key["name"],
  403.                                     "formation" => $formation,
  404.                                     "link" => $url,
  405.                                     "startDate" => $startDate,
  406.                                 ]);
  407.                             $this->mailer->send($em);
  408.                         }
  409.                     }
  410.                 } else {
  411.                     if (isset($emailTo) && $emailTo) {
  412.                         foreach ($emailTo as $key) {
  413.                             if (isset($ics)) {
  414.                                 $u_s = new UserSession();
  415.                                 $u_s->setIdCourseSession(base64_decode($ics));
  416.                                 $u_s->setIdUser(
  417.                                     $this->um
  418.                                         ->getUserByEmail($key["email"])[0]
  419.                                         ->getId()
  420.                                 );
  421.                                 $u_s->setMark("");
  422.                                 $this->um->updateUserIdCustomer($key["email"], $this->getUser()->getIdCustomer());
  423.                                 $this->entityManager->persist($u_s);
  424.                                 $this->entityManager->flush();
  425.                             }
  426.                             $em = (new TemplatedEmail())
  427.                                 ->from($this->config->getMailSender())
  428.                                 ->priority(Email::PRIORITY_HIGH)
  429.                                 ->to($key["email"])
  430.                                 ->cc(
  431.                                     "kalidougattaba@gmail.com",
  432.                                     "pascal.liatard@abware.fr"
  433.                                 )
  434.                                 ->subject($formation ": Inscription")
  435.                                 ->htmlTemplate(
  436.                                     "acoa/email/mailto_student.html.twig"
  437.                                 )
  438.                                 ->context([
  439.                                     "name" => $key["name"],
  440.                                     "formation" => $formation,
  441.                                     "startDate" => $startDate,
  442.                                 ]);
  443.                             $this->mailer->send($em);
  444.                         }
  445.                     }
  446.                 }
  447.                 return $this->redirectToRoute("register_student_confirm");
  448.             } else {
  449.                 for ($j 0$j <= $i$j++) {
  450.                     $this->session->set("user" $jnull);
  451.                 }
  452.             }
  453.         }
  454.         if (!$this->getUser() && $internal) {
  455.             return $this->redirectToRoute("login");
  456.         }
  457.         /*$participants = array();
  458.         if ($internal) {
  459.             $participants = $this->um->getAllParticipantsByCustomer($this->getUser()->getIdCustomer()) ;
  460.         }*/
  461.         return $this->renderForm("acoa/catalog/catalog_participant.html.twig", [
  462.             "form" => $form,
  463.             "internal" => $internal,
  464.             "formation" => isset($formation) ? $formation "",
  465.             "startDate" => isset($startDate) ? $startDate "",
  466.             "participants" => $internal $this->um->getAllParticipantsByCustomer($this->getUser()->getIdCustomer()) : []
  467.         ]);
  468.     }
  469.     #[
  470.         Route(
  471.             "/catalog/panier/{idCourseSession}",
  472.             name"catalog_panier",
  473.             requirements: ["idCourseSession" => "\d+"]
  474.         )
  475.     ]
  476.     public function panier(
  477.         ManagerRegistry $doctrine,
  478.         Request $request,
  479.         int $idCourseSession,
  480.         UserRepository $userRepository
  481.     ): Response {
  482.         if ($request->isMethod("post")) {
  483.             $j 0;
  484.             for ($i 0$i $this->session->get("nbUser"); $i++) {
  485.                 if (
  486.                     !in_array(
  487.                         $this->session->get("user" $i)->getId(),
  488.                         $request->request->get("users")
  489.                     )
  490.                 ) {
  491.                     $user $userRepository->find(
  492.                         $this->session->get("user" $i)->getId()
  493.                     );
  494.                     $doctrine->getManager()->remove($user);
  495.                     $this->session->set("user" $inull);
  496.                     $j++;
  497.                 }
  498.             }
  499.             $this->session->set("finalNbUser"$i $j);
  500.             $doctrine->getManager()->flush();
  501.             return $this->redirectToRoute("login");
  502.         }
  503.         $courseSession $doctrine
  504.             ->getRepository(CourseSession::class)
  505.             ->find($idCourseSession);
  506.         $this->session->set("courseSession"$courseSession);
  507.         $course $doctrine
  508.             ->getRepository(Course::class)
  509.             ->find($courseSession->getIdCourse());
  510.         // PL$ -  dans le cas des formations INTER (en passant par le catalogue)  
  511.         if (
  512.             $this->getUser() &&
  513.             (!$this->custsesm->isCustomerSessionExists($this->getUser()->getIdCustomer(), $idCourseSession))
  514.         ) {
  515.             $customerSession = new CustomerSession();
  516.             $customerSession->setIdCustomer((int) $this->getUser()->getIdCustomer());
  517.             if (isset($idCourseSession)) {
  518.                 $customerSession->setIdCourseSession($idCourseSession);
  519.             } else
  520.                 $customerSession->setIdCourseSession(0);
  521.             $customerSession->setDateEvenement(new \DateTime());
  522.             $this->entityManager->persist($customerSession);
  523.             $this->entityManager->flush();
  524.         }
  525.         $users = [];
  526.         for ($i 0$i $this->session->get("nbUser"); $i++) {
  527.             $users[] = $this->session->get("user" $i);
  528.         }
  529.         return $this->render("acoa/catalog/catalog_panier.html.twig", [
  530.             "controller_name" => "CatalogController",
  531.             "courseSession" => $courseSession,
  532.             "users" => $users,
  533.             "course" => $course,
  534.         ]);
  535.     }
  536.     private function generateRandomString($length 10)
  537.     {
  538.         $characters =
  539.             "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  540.         $charactersLength strlen($characters);
  541.         $randomString "";
  542.         for ($i 0$i $length$i++) {
  543.             $randomString .= $characters[rand(0$charactersLength 1)];
  544.         }
  545.         return $randomString;
  546.     }
  547.     #[Route("/catalog/check-mail"name"catalog_check_mail")]
  548.     public function verifMail()
  549.     {
  550.         $idCourseSession $this->session->get("idCourseSession");
  551.         $mail $_GET["mail"];
  552.         $user $this->getDoctrine()
  553.             ->getRepository(User::class)
  554.             ->findOneBy(["email" => $mail]);
  555.         if ($user) {
  556.             $userSession $this->csm->findUserBySession(
  557.                 $user->getId(),
  558.                 $idCourseSession
  559.             );
  560.             if ($userSession) {
  561.                 return $this->json(true200);
  562.             }
  563.         }
  564.         return $this->json(false200);
  565.     }
  566. }