<?php
namespace App\Controller\Frontend;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use App\Services\MailerService;
use App\Services\MessageService;
use App\Services\OptionService;
use App\Entity\{
Page,
Block,
Contact,
Categorypage,
Actualites,
Um6p\Event,
Um6p\MediaEvent,
Um6p\Speaker
};
use App\Form\Frontend\ContactpublicType;
use App\Form\Frontend\CandidaturepublicType;
use App\Form\Frontend\Sothema\PharmapublicType;
class DefaultController extends AbstractController
{
/**
* @var OptionService
*/
private $configRepo;
public function __construct(OptionService $configRepo)
{
$this->configRepo = $configRepo;
}
/**
* Change the locale for the current user
*
* @param String $langue
* @return array
*
* @Route("/setlocale/{langue}", name="select_lang", defaults={"langue":"en"}, requirements={"_locale":"en|fr"})
*/
public function setLocaleAction($langue = "en", Request $request)
{
if ($langue != null) {
$request->getSession()->set('_locale', $langue);
$this->get('session')->set('_locale', $langue);
$request->setLocale($langue);
}
$url = $this->generateUrl('frontend_home', array('_locale' => $langue));
return new RedirectResponse($url);
}
/**
* @Route("/{_locale}", name="frontend_home", defaults={"_locale":"en"}, requirements={"_locale":"en|fr"})
*/
public function index(): Response
{
$em = $this->getDoctrine()->getManager();
$page_selected = $em->getRepository(Page::class)->findOneBySlug('home');
// $list_inspirations = $em->getRepository(inspiration::class)->getAllHomePublic();
return $this->render('Frontend/Page/index.html.twig', [
'page_selected' => $page_selected
]);
}
/**
* @Route("/_header", name="_header")
*/
public function _header(): Response
{
$em = $this->getDoctrine()->getManager();
$categorier_pages = $em->getRepository(Categorypage::class)->getAllpublic();
return $this->render('Frontend/_header.html.twig', [
'categorier_pages' => $categorier_pages
]);
}
/**
* @Route("/_footer", name="_footer")
*/
public function _footer(Request $request): Response
{
return $this->render('Frontend/_footer.html.twig');
}
/* Actualités */
/**
* @Route("/{_locale}/actualites", name="actualites", defaults={"_locale":"en"}, requirements={"_locale":"en|fr"})
*/
public function actualites(Request $request): Response
{
$em = $this->getDoctrine()->getManager();
$nbrmax_article = $this->configRepo->getValue('article_nbr_page');
$page = $request->query->getInt('page', 1);
$page_selected = $em->getRepository(Page::class)->findOneBySlug('actualites');
$list_actualites = $em->getRepository(Actualites::class)->findAllPagination($page, $nbrmax_article);
$pagination = array(
'page' => $page,
'nbPages' => ceil(count($list_actualites) / $nbrmax_article),
'nomRoute' => 'actualites',
'paramsRoute' => array()
);
if ($page_selected) {
return $this->render('Frontend/Actualites/actualites.html.twig', [
'page_selected' => $page_selected,
'list_actualites' => $list_actualites,
'pagination' => $pagination
]);
}
throw new NotFoundHttpException();
}
/**
* @Route("/{_locale}/actualites/{slug}", name="actu_selected", defaults={"_locale":"en"}, requirements={"_locale":"en|fr"})
*/
public function actuSelected($slug, Request $request): Response
{
$em = $this->getDoctrine()->getManager();
$page_selected = $em->getRepository(Page::class)->findOneBySlug('actualites');
$actu_selected = $em->getRepository(Actualites::class)->findOneBySlug($slug);
if ($actu_selected) {
return $this->render('Frontend/Actualites/actu_selected.html.twig', [
'page_selected' => $page_selected,
'actu_selected' => $actu_selected
]);
}
throw new NotFoundHttpException();
}
//Events
/**
* @Route("/{_locale}/seminars-conferences/{slug}", name="event_selected", defaults={"_locale":"en"}, requirements={"_locale":"en|fr"})
*/
public function eventSelected($slug, Request $request): Response
{
$em = $this->getDoctrine()->getManager();
// $page_selected = $em->getRepository(Page::class)->findOneBySlug('seminars-conferences');
$event_selected = $em->getRepository(Event::class)->findOneBySlug($slug);
$list_galerie = $em->getRepository(MediaEvent::class)->getAllPublic($event_selected);
$list_videos = $em->getRepository(MediaEvent::class)->getAllPublicByOpen($event_selected);
$list_speakers = $em->getRepository(Speaker::class)->getAllPublic($event_selected);
if ($event_selected) {
return $this->render('Frontend/Events/event_selected.html.twig', [
'event_selected' => $event_selected,
'list_galerie' => $list_galerie,
'list_videos' => $list_videos,
'list_speakers' => $list_speakers,
]);
}
throw new NotFoundHttpException();
}
/**
* @Route("/{_locale}/contact", name="contact", defaults={"_locale":"en"}, requirements={"_locale":"en|fr"})
*/
public function contact(Request $request, MailerInterface $mailer): Response
{
$contact = new Contact();
$em = $this->getDoctrine()->getManager();
$page_selected = $em->getRepository(Page::class)->findOneBySlug('contact');
$form = $this->createForm(ContactpublicType::class, $contact, array(
'action' => $this->generateUrl('contact'),
'method' => 'POST'
));
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid()) {
$em->persist($contact);
$em->flush();
/* $datamail = $this->sendEmail($contact,'Contact',"Frontend/Email/_contact.html.twig");
$mailer->send($datamail); */
$request->getSession()->getFlashBag()->add('notice_success', 'Votre message a bien été envoyé');
return $this->redirectToRoute('contact');
} else {
$request->getSession()->getFlashBag()->add('notice_error', 'Votre formulaire est invalide');
}
}
return $this->render('Frontend/Contact/contact.html.twig', [
'page_selected' => $page_selected,
'form' => $form->createView()
]);
}
/**
* @Route("/{_locale}/{slug}", name="page_single_simple", defaults={"_locale":"en"}, requirements={"_locale":"en|fr"})
*/
public function pageSingleSimple($slug, Request $request): Response
{
$em = $this->getDoctrine()->getManager();
$page_selected = $em->getRepository(Page::class)->findOneBySlug($slug);
if ($page_selected) {
return $this->render('Frontend/Page/page_single.html.twig', [
'page_selected' => $page_selected
]);
}
throw $this->createNotFoundException('Page not found');
}
/**
* @Route("/{_locale}/{catslug}/{slug}", name="page_single", defaults={"_locale":"en"}, requirements={"_locale":"en|fr"})
*/
public function pageSingle($catslug, $slug, Request $request): Response
{
$em = $this->getDoctrine()->getManager();
$cat_selected = $em->getRepository(Categorypage::class)->findOneBySlug($catslug);
$page_selected = $em->getRepository(Page::class)->findOneBySlug($slug);
if ($page_selected) {
if ($page_selected->getSlug() == 'seminars-conferences') {
$nbrmax_article = $this->configRepo->getValue('articles_nbr_page');
$page_current = $request->query->getInt('page', 1);
$list_events = $em->getRepository(Event::class)->findAllPagineEtTrie($page_current, $nbrmax_article);
$pagination = array(
'page' => $page_current,
'nbPages' => ceil(count($list_events) / $nbrmax_article),
'nomRoute' => 'page_single',
'paramsRoute' => array(
'catslug' => $catslug,
'slug' => $slug
)
);
return $this->render('Frontend/Events/list_events.html.twig', [
'page_selected' => $page_selected,
'list_events' => $list_events,
'pagination' => $pagination,
]);
}
return $this->render('Frontend/Page/page_single.html.twig', [
'cat_selected' => $cat_selected,
'page_selected' => $page_selected
]);
}
throw $this->createNotFoundException('Page not found');
}
private function sendEmail($data, $object, $template)
{
$dataview = $this->renderView($template, array('contact' => $data));
$email = (new Email())
->from($this->configRepo->getValue('mail_envoie'))
->to($this->configRepo->getValue('mail_reception'))
->subject($object)
->html($dataview);
return $email;
}
private function sendEmailCC($data, $object, $template)
{
$dataview = $this->renderView($template, array('visiteur' => $data));
$email = (new Email())
->from($this->configRepo->getValue('mail_envoie'))
->to($data->getMailadresse())
->subject($object)
->html($dataview);
return $email;
}
}