src/Controller/FrontController.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Alma\API\Request as APIRequest;
  4. use App\Entity\Answer;
  5. use stdClass;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use App\Entity\ExamDate;
  12. use App\Entity\Pack;
  13. use App\Entity\Question;
  14. use App\Entity\Subscription;
  15. use App\Entity\Course;
  16. use App\Entity\Settings;
  17. use App\Entity\Booking;
  18. use App\Entity\User;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use Symfony\Contracts\HttpClient\HttpClientInterface;
  21. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  22. use App\Security\LoginFormAuthenticator;
  23. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  24. use Symfony\Component\Security\Guard\GuardAuthenticationProvider;
  25. class FrontController extends AbstractController
  26. {
  27.     private $client;
  28.     public function __construct(HttpClientInterface $client)
  29.     {
  30.         $this->client $client;
  31.     }
  32.     /**
  33.      * @Route(
  34.      *     "/{_locale}",
  35.      *     name="front",
  36.      *     requirements={
  37.      *         "_locale": "en|fr|cn",
  38.      *     },
  39.      *     defaults = {"_locale" = "fr"}
  40.      * )
  41.      */
  42.     public function index(): Response
  43.     {
  44.         $packs $this->getDoctrine()->getRepository(Pack::class)->findBy(array('status'=>1));
  45.         // $today = date('Y-m-d');
  46.         // $em = $this->getDoctrine()->getManager();
  47.         // $query = $em->createQuery("SELECT e FROM App\Entity\ExamDate e WHERE e.status = 1 AND e.date >= :today"
  48.         // )->setParameter('today', $today);
  49.         // $examDates = $query->getResult();
  50.         
  51.         $examMonths = array();
  52.         $begin = new \DateTime(date('Y-m').'-01');
  53.         $end = new \DateTime(date('Y-m-d'));
  54.         $end $end->modify'+2 years' );
  55.         $interval = \DateInterval::createFromDateString('+1 month');
  56.         $period = new \DatePeriod($begin$interval$end);
  57.         
  58.         $formatter = new \IntlDateFormatter('fr_FR', \IntlDateFormatter::SHORT, \IntlDateFormatter::SHORT);
  59.         $formatter->setPattern('MMMM Y');
  60.         foreach ($period as $dt) {
  61.             $examMonths[] = array(
  62.                 // 'readable' => $formatter->format($dt),
  63.                 'readable' => $dt->format('m/Y'),
  64.                 'date' => $dt->format('Y-m')
  65.             );
  66.         }
  67.         return $this->render('front/index.html.twig', [
  68.             'controller_name' => 'FrontController',
  69.             'packs' => $packs,
  70.             // 'examDates' => $examDates,
  71.             'examMonths' => $examMonths
  72.         ]);
  73.     }
  74.     public function loginUser($user$request,  $loginAuthenticator$guard){
  75.         // public function authenticateUserAndHandleSuccess(UserInterface $user, Request $request, AuthenticatorInterface $authenticator, string $providerKey): ?Response
  76.         return $guard->authenticateUserAndHandleSuccess(
  77.             $user,
  78.             $request,
  79.             $loginAuthenticator,
  80.             'main'
  81.         );
  82.     }
  83.     // methods={"POST"}
  84.     /**
  85.      * @Route("/checkout_login", name="checkout_login")
  86.      */
  87.     public function checkout_login(Request $requestUserPasswordHasherInterface $userPasswordHasherGuardAuthenticationProvider $authenticatorGuardAuthenticatorHandler $guard): Response
  88.     {
  89.         $isPartner $user false;
  90.         $checkEmailExists $this->getDoctrine()->getRepository(User::class)->findOneBy(array('email' => $request->get('email')));
  91.         if($checkEmailExists){
  92.             $match $userPasswordHasher->isPasswordValid($checkEmailExists$request->get('password'));
  93.             $logged $this->loginUser($checkEmailExists$request$authenticator$guard);
  94.             if($match){
  95.                 $user $checkEmailExists;
  96.             }
  97.             if($this->getUser() && in_array('ROLE_PARTNER'$this->getUser()->getRoles())){
  98.                 $isPartner true;
  99.             }
  100.         }
  101.         return $this->json([
  102.             // 'user' => $this->getUser() ? $this->getUser()->getId(): null,
  103.             'isPartner' => $isPartner,
  104.             'emx' => $request->get('email'),
  105.             'user' => $user
  106.         ]);
  107.     }
  108.     /**
  109.      * @Route("/checkout/{id}", name="checkout")
  110.      */
  111.     public function checkout($idRequest $requestUserPasswordHasherInterface $userPasswordHasherLoginFormAuthenticator $loginAuthenticatorGuardAuthenticatorHandler $guard): Response
  112.     {
  113.         $user null;
  114.         if(!$this->getUser()){
  115.             if($request->get('login')){
  116.                 return $this->render('front/checkout-login.html.twig', [
  117.                     'controller_name' => 'FrontController',
  118.                     'orderID' => $id,
  119.                     'login' => $request->get('login'),
  120.                     'password' => $request->get('password'),
  121.                     'isIframe' => true
  122.                 ]);
  123.             //     $checkEmailExists = $this->getDoctrine()->getRepository(User::class)->findOneBy(array('email' => $request->get('login')));
  124.             //     if($checkEmailExists){
  125.             //         $match = $userPasswordHasher->isPasswordValid($checkEmailExists, $request->get('password'));
  126.             //         $logged = $this->loginUser($checkEmailExists, $request, $loginAuthenticator, $guard);
  127.             //         var_dump($logged);
  128.                     
  129.             //         if($match){
  130.             //             $user = $checkEmailExists;
  131.             //         }
  132.             //     }
  133.             }
  134.             // if(!$user) return $this->redirectToRoute('login', array('redirect_to'=>'/checkout/'.$id));
  135.         } else {
  136.             $user $this->getUser();
  137.         }
  138.         $subscription $this->getDoctrine()->getRepository(Subscription::class)->findOneBy(array('id'=>(int)$id));        
  139.         if(!$subscription || $subscription->getUser() != $user){
  140.             return $this->redirectToRoute('dashboard_front');
  141.         } 
  142.         // else if($subscription->getStatus() != 0){
  143.         //     return $this->json(['error']);
  144.         // }
  145.         $taken false;
  146.         $errors null;
  147.         $checkBookings $this->getDoctrine()->getRepository(Booking::class)->findByTakenBookings($subscription->getBookings());
  148.         if($checkBookings){
  149.             if(isset($checkBookings['taken'])){
  150.                 $paypalKey null;
  151.                 $stripeKey null;
  152.                 $almaPaymentUrl null;
  153.                 $edofEnabled null;
  154.                 $taken true;
  155.             }
  156.         }
  157.         if(!$taken) {
  158.             $edofEnabled true;
  159.             require_once $this->getParameter('kernel.project_dir') . "/vendor/alma-php-client/vendor/autoload.php";
  160.             if($this->getParameter('kernel.environment') == 'prod'){
  161.                 $almaApiKey $this->getDoctrine()->getRepository(Settings::class)->findOneByName('alma_token');
  162.                 $alma = new \Alma\API\Client($almaApiKey, ['mode' => \Alma\API\Client::LIVE_MODE]);
  163.             } else {
  164.                 $almaApiKey 'sk_test_nq8s0vsTyaKEQSGA8oy4c4JL';
  165.                 $alma = new \Alma\API\Client($almaApiKey, ['mode' => \Alma\API\Client::TEST_MODE]);
  166.             }
  167.             $email $user->getEmail();
  168.             $almaArgs = array(
  169.                 'origin'   => 'online',
  170.                 'payment'  =>
  171.                     [
  172.                         'return_url'         => 'https://esrautomoto.fr/',
  173.                         'ipn_callback_url'   => 'https://esrautomoto.fr/api/paymentOKAlma',
  174.                         'purchase_amount'    => (int)(round($subscription->getPack()->getPrice() * 100)),
  175.                         // 'installments_count' => 4,
  176.                         'custom_data'        =>
  177.                             [
  178.                                 // 'my_very_important_key' => 'the_context_custom_value',
  179.                             ],
  180.                         'locale'             => 'fr',
  181.                         'billing_address'    =>
  182.                             [
  183.                             //     'first_name'  => 'John',
  184.                             //     'last_name'   => 'Doe',
  185.                             //     'email'       => $email,
  186.                             //     'line1'       => '1 rue de Rome',
  187.                             //     'postal_code' => '75001',
  188.                             //     'city'        => 'Paris',
  189.                             //     'country'     => 'FR',
  190.                             ],
  191.                         'shipping_address'   =>
  192.                             [
  193.                                 // 'first_name'  => 'John',
  194.                                 // 'last_name'   => 'Doe',
  195.                                 // 'email'       => $email,
  196.                                 // 'line1'       => '1 rue de Rome',
  197.                                 // 'postal_code' => '75001',
  198.                                 // 'city'        => 'Paris',
  199.                                 // 'country'     => 'FR',
  200.                             ],
  201.                     ],
  202.                 'customer' =>
  203.                     [
  204.                         // 'first_name' => 'John',
  205.                         // 'last_name'  => 'Doe',
  206.                         'email'      => $email,
  207.                         // 'phone'      => '06 12 34 56 78',
  208.                         // 'addresses'  =>
  209.                         //     [
  210.                         //         [
  211.                         //             'first_name' => 'John',
  212.                         //             'last_name'  => 'Doe',
  213.                         //             'email'      => $email,
  214.                         //             'phone'      => '06 12 34 56 78',
  215.                         //         ],
  216.                         //     ],
  217.                     ],
  218.             );
  219.             try {
  220.                 $almaPayment $alma->payments->create($almaArgs);
  221.                 $almaPaymentUrl $almaPayment->url;
  222.             } catch (\Exception $th) {
  223.                 $errors[] = "Erreur Alma: ".$th->response->json['errors'][0]['message'];
  224.                 $almaPaymentUrl null;
  225.             }
  226.     
  227.             $paypalKey null;//$this->getDoctrine()->getRepository(Settings::class)->findOneByName('paypal_key');
  228.             $stripeKey $this->getDoctrine()->getRepository(Settings::class)->findOneByName('stripe_key');
  229.         }
  230.         if($subscription->getPack()->getType() == 'online'){
  231.             $examCenter $examDate '';
  232.             $examDateStatus null;
  233.         } elseif($subscription->getExamDate()){
  234.             $examDate $subscription->getExamDate()->format('d-m-Y');
  235.             $examDateExpl explode('-'$examDate);
  236.             $week floor($examDateExpl[0] / 7);
  237.             if($week 1$week 1;
  238.             $examDate $examDateExpl[1].'-'.$examDateExpl[2].' semaine '.$week;
  239.             $examDateStatus 2;
  240.             $examCenter '';
  241.         } else {
  242.             $examDate $subscription->getExam();
  243.             $examCenter $examDate->getExamCenter()->getName();
  244.             $examDateStatus $examDate->getStatus();
  245.             $examDate $examDate->getDate()->format('d-m-Y').' à '.$examDate->getTime();
  246.         }
  247.         return $this->render('front/checkout.html.twig', [
  248.             'controller_name' => 'FrontController',
  249.             'subscription' => $subscription,
  250.             'paypalKey' => $paypalKey,
  251.             'stripeKey' => $stripeKey,
  252.             'examDate' => $examDate,
  253.             'examDateStatus' => $examDateStatus,
  254.             'examCenter' => $examCenter,
  255.             'almaPaymentUrl' => $almaPaymentUrl,
  256.             'edofEnabled' => $edofEnabled,
  257.             'checkBookings' => $checkBookings,
  258.             'isIframe' => isset($_GET['isIframe']) ? true false,
  259.             'errors' => $errors
  260.         ]);
  261.     }
  262.     /**
  263.      * @Route("/test", name="test")
  264.      */
  265.     public function test(Request $request): Response
  266.     {
  267.         die;
  268.         $em $this->getDoctrine()->getManager();
  269.         $query1 "SELECT q.text, a.text as a_text, q.file FROM tts q LEFT JOIN tts a on a.type LIKE 'R' and a.file = q.file where q.type like 'Q' order by q.file2 asc";
  270.         $stmt $em->getConnection()->prepare($query1);
  271.         $stmt->execute();
  272.         $tts $stmt->fetchAll();
  273.         $course $this->getDoctrine()->getRepository(Course::class)->findOneBy(array('id'=>1));
  274.         $qDate = new \DateTimeImmutable();
  275.         $posi 1;
  276.         $alphaArray = array(
  277.             'A' => 1,
  278.             'B' => 2,
  279.             'C' => 3,
  280.             'D' => 4,
  281.             'E' => 5,
  282.             'F' => 6,
  283.             'G' => 7
  284.         );
  285.         foreach($tts as $ttsElem){
  286.             $ttsElemExpl explode('|'$ttsElem['text']);
  287.             // var_dump($ttsElemExpl);
  288.             // echo '<br />';
  289.             // if()
  290.             $qTitle trim(str_replace(':'''$ttsElemExpl[0]));
  291.             $question = new Question();
  292.             $question->setTitle($qTitle);
  293.             $question->setContent(str_replace('|''<br />'$ttsElem['text']));
  294.             $question->setActive(1);
  295.             $question->setCourse($course);
  296.             $question->setFile($ttsElem['file'].'.jpg');
  297.             $question->setCreatedAt($qDate);
  298.             $question->setUpdateAt($qDate);
  299.             $question->setPosition($ttsElem['file']);
  300.             $em->persist($question);
  301.     
  302.             $em->flush();
  303.             $goodAnswer explode('|'$ttsElem['a_text']);
  304.             $goodAnswer str_replace(array('Bonne réponse'':'' ''.''et'), ''$goodAnswer[0]);
  305.             $goodAnswerExpl str_split($goodAnswer);
  306.             $qGoodAnswers = array();
  307.             if(sizeof($goodAnswerExpl) > 0){
  308.                 foreach($goodAnswerExpl as $goodAnswerExplElem){
  309.                     if(isset($alphaArray[$goodAnswerExplElem])) $qGoodAnswers[] = $alphaArray[$goodAnswerExplElem];
  310.                 }
  311.             }
  312.             // var_dump($qGoodAnswers);
  313.             // echo '<hr />';
  314.             $ansPosi 1;
  315.             foreach($ttsElemExpl as $ttsElemExplElemKey => $ttsElemExplElem){
  316.                 if(strpos($ttsElemExplElem'Réponse') !== false && strpos($ttsElemExplElem'Réponse') == 0){
  317.                     $trimTtsElemExplElem trim($ttsElemExplElem);
  318.                     // var_dump(strpos($trimTtsElemExplElem, 'Réponse'));
  319.                     // echo '<br />'.$trimTtsElemExplElem.'<hr />';
  320.                     $answer = new Answer();
  321.                     $answer->setTitle($trimTtsElemExplElem);
  322.                     $answer->setQuestion($question);
  323.                     $correct in_array($ansPosi$qGoodAnswers) ? true false;
  324.                     $answer->setCorrect($correct);
  325.         
  326.                     $em->persist($answer);
  327.                     $em->flush();
  328.                     $ansPosi++;
  329.                 }
  330.             }
  331.             
  332.             $posi++;
  333.             
  334.             // if($posi >= 10) break;
  335.         }
  336.         echo '<h1>Finished!</h1>';
  337.         // var_dump($posi);
  338.         die;
  339.         $partnerCode $request->get('partnerCode');
  340.         $partnerCode str_replace('BC'''$partnerCode);
  341.         $partnerCode substr($partnerCode0, -2);
  342.         $em $this->getDoctrine()->getManager();
  343.         $conn $em->getConnection();
  344.         $sql "SELECT * FROM user u WHERE u.id = :partnerCode AND u.roles LIKE '%ROLE_PARTNER%' LIMIT 0, 1";
  345.         $stmt $conn->prepare($sql);
  346.         $stmt->execute(array('partnerCode' => $partnerCode));
  347.         $partner $stmt->fetch();
  348.         // $partner = $this->getDoctrine()->getRepository(User::class)->findOneBy(array('id' => $partnerCode));
  349.         
  350.     }
  351.     /**
  352.      * @Route("/faq", name="faq")
  353.      */
  354.     public function faq(): Response
  355.     {
  356.         return $this->render('front/faq.html.twig', [
  357.             'controller_name' => 'FrontController'
  358.         ]);
  359.     }
  360.     /**
  361.      * @Route("/paiement-effectue/{subscriptionID}", name="thanks")
  362.      */
  363.     public function thanks($subscriptionID): Response
  364.     {
  365.         return $this->render('front/thanks.html.twig', [
  366.             'controller_name' => 'FrontController',
  367.             'errors' => null,
  368.             'subscriptionID' => $subscriptionID
  369.         ]);
  370.     }
  371.     /**
  372.      * @Route("/dates-examen", name="list_exam_dates")
  373.      */
  374.     public function list_exam_dates(): Response
  375.     {
  376.         $today date('Y-m-d');
  377.         $em $this->getDoctrine()->getManager();
  378.         $query $em->createQuery("SELECT e FROM App\Entity\ExamDate e WHERE e.status = 1 AND e.date >= :today"
  379.         )->setParameter('today'$today);
  380.         $examDates $query->getResult();
  381.         return $this->render('front/examdates.html.twig', [
  382.             'controller_name' => 'FrontController',
  383.             'errors' => null,
  384.             'examDates' => $examDates
  385.         ]);
  386.     }
  387.     /**
  388.      * @Route("/dates-examen/{id}", name="exam_date")
  389.      */
  390.     public function exam_date($id): Response
  391.     {
  392.         $today date('Y-m-d');
  393.         $em $this->getDoctrine()->getManager();
  394.         $query $em->createQuery("SELECT e FROM App\Entity\ExamDate e WHERE e.id = :id AND e.status = 1 AND e.date >= :today"
  395.         )->setParameter('today'$today)->setParameter('id'$id);
  396.         $examDate $query->getOneOrNullResult();
  397.         $startDate strtotime($examDate->getDate()->format('d-m-Y').' '.$examDate->getTime());
  398.         $endDate date('c'strtotime("+1 day"$startDate));
  399.         $validFrom date('c'strtotime("-10 days"$startDate));
  400.         $startDate date('c'$startDate);
  401.         return $this->render('front/examdate.html.twig', [
  402.             'controller_name' => 'FrontController',
  403.             'errors' => null,
  404.             'examDate' => $examDate,
  405.             'startDate' => $startDate,
  406.             'endDate' => $endDate,
  407.             'validFrom' => $validFrom
  408.         ]);
  409.     }
  410.     /**
  411.      * @Route("/code-de-la-route", name="landing_code")
  412.      */
  413.     public function landing_code(): Response
  414.     {
  415.         return $this->render('front/landing-code.html.twig', [
  416.             'controller_name' => 'FrontController'
  417.         ]);
  418.     }
  419. }