<?php
namespace App\Controller\Front;
use App\Entity\Elearning;
use App\Entity\LiveSession;
use App\Entity\Post;
use App\Entity\PostCategory;
use App\Services\EmailManager;
use App\Services\PagesManager;
use App\Services\PdfManager;
use App\Services\StripeManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ElearningController extends AbstractController
{
/**
* @Route("/elearning", name="page_elearning")
* Method ({"GET", "POST"})
*/
public function index(Request $request, PagesManager $pagesManager): Response
{
$em = $this->getDoctrine()->getManager();
$page = $request->get('page', 1);
$limit = $request->get('limit', 9);
$category = $request->get('category', '');
$elearningPage = $em->getRepository(Post::class)->findOneBySlug('banner-elearning');
$categories = $em->getRepository(PostCategory::class)->findBy(['postType' => 'elearning']);
$videos = $em->getRepository(Elearning::class)->search($page, $limit, $category);
$formations = $em->getRepository(LiveSession::class)->search(1, 6, null, null);
return $this->render('front/elearning/index.html.twig', [
'blocs' => $pagesManager->getPageBlocs($elearningPage),
'categories' => $categories,
'formations' => $formations,
'videos' => $videos,
'page' => ($page+1),
'limit' => $limit
]);
}
/**
* @Route("/elearning-paginate", name="page_elearning_paginate", options={"expose"=true})
* Method ({"GET", "POST"})
*/
public function paginate(Request $request): Response
{
$em = $this->getDoctrine()->getManager();
$page = $request->get('page', 1);
$limit = $request->get('limit', 3);
$category = $request->get('category', '');
$videos = $em->getRepository(Elearning::class)->search($page, $limit, $category);
return $this->render('front/elearning/elearning_ajax.html.twig', [
'videos' => $videos
]);
}
/**
* @Route("/elearning/{slug}", name="page_elearning_single")
* Method ({"GET", "POST"})
*/
public function show($slug, PagesManager $pagesManager): Response
{
$em = $this->getDoctrine()->getManager();
$elearning = $em->getRepository(Elearning::class)->findOneBySlug($slug);
$toSee = $em->getRepository(Elearning::class)->getToSee($elearning->getCategory(), $slug);
$formations = $em->getRepository(LiveSession::class)->search(1, 3, null, null);
$elearningPage = $em->getRepository(Post::class)->findOneBySlug('banner-elearning');
return $this->render('front/elearning/show.html.twig', [
'blocs' => $pagesManager->getPageBlocs($elearningPage),
'video' => $elearning,
'toSee' => $toSee,
'formations' => $formations
]);
}
/**
* @Route("/abonnements-elearning", name="page_abonnnements_elearning")
* Method ({"GET", "POST"})
*/
public function abonnement(PagesManager $pagesManager): Response
{
$page = $this->getDoctrine()->getManager()->getRepository(Post::class)->findOneBySlug('abonnements-elearning');
return $this->render('front/elearning/abonnement.html.twig', [
'blocs' => $pagesManager->getPageBlocs($page)
]);
}
/**
* @Route("/abonnements/{type}", name="page_abonnnements_elearning_subscription")
* Method ({"GET", "POST"})
*/
public function abonnementSubscription(Request $request, $type): Response
{
if(!$this->getUser()) {
return new RedirectResponse($this->generateUrl('front_login'));
}
return $this->render('front/elearning/abonnement_subscription.html.twig', [
'type' => $type
]);
}
/**
* @Route("/abonnements/{type}/validation", name="page_abonnnements_elearning_subscription_validation")
* Method ({"GET", "POST"})
*/
public function abonnementSubscriptionValidation(Request $request, $type, StripeManager $stripeManager, EmailManager $emailManager, PdfManager $pdfManager): Response
{
$output = null;
if(!$this->getUser()) {
return new RedirectResponse($this->generateUrl('front_login'));
}
if($request->isMethod('POST')) {
if($request->request->get('newsletter', false)) {
$this->getUser()->setIsAcceptNewsletter(true);
}
$output = $stripeManager->addSubscriptionToUser($this->getUser(), $type, $request->request->get('stripeToken'), $request->request->get('code_reduction', null), $request->request->get('addoption', null), $request->request->get('modality', 'annually'));
if($output['status'] == 'success') {
$pdfOption = null;
$pdf = $pdfManager->generatePdfFacture($output['order']);
if($request->request->get('addoption', null)) {
$pdfOption = $pdfManager->generatePdfOptionFacture($output['order']);
}
$emailManager->sendEmailPaymentAbonnementSuccessToCandidate($output['order'], $pdf, $pdfOption);
}
}
return $this->render('front/elearning/abonnement_subscription_validation.html.twig', [
'type' => $type,
'output' => $output
]);
}
}