<?php
namespace App\Controller\Frontend;
use App\Misc\CaptchaHelper;
use App\Entity\Partner;
use App\Entity\OurStudent;
use Exception;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations as Rest;
use App\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class IndexController extends BaseFrontendController
{
/**
* @Route("/{_locale}", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="home")
*/
public function index(
TranslatorInterface $translator
): Response {
$siteName = $this->getParameter('site_name');
$metaTitle = $translator->trans(
'home.title',
[
'%siteName%' => $siteName
]
);
$this->addTitle($metaTitle);
$this->setDescription($translator->trans(
'home.desc',
[
'%siteName%' => $siteName
]
));
$data['jobs'] = $this->offerRepo->getListLasted();
$data['totalJob'] = $this->offerRepo->getTotalJobActive();
$data['campuses'] = $this->campusRepo->getAll(['filter_status' => 1, 'sort_asc' => 'ordering']);
$data['partners'] = $this->partnerRepo->getAll(['filter_status' => Partner::STATUS_ACTIVE], 'App\DTO\Partner\PartnerOutput');
$data['outStudentVideos'] = $this->ourStudentRepo->getAllOurStudentByType(OurStudent::TYPE_VIDEO, 3);
$data['jpos'] = $this->openDayRepo->getOpenDayLatest(4);
return $this->render('home/home.html.twig', $data);
}
/**
* @Route("/{_locale}/contact", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="contact_page")
*/
public function contact(
Request $request,
TranslatorInterface $translator,
CaptchaHelper $captchaHelper,
Session $session
) {
$siteName = $this->getParameter('site_name');
$this->addTitle($translator->trans(
'contact.title',
[
'%siteName%' => $siteName
]
));
$this->setDescription($translator->trans(
'contact.desc',
[
'%siteName%' => $siteName
]
));
$this->get('session')->getFlashBag()->clear();
$errorMessage = null;
$campuses = $this->campusRepo->getAll(['filter_status' => 1, 'sort_asc' => 'ordering']);
if ($request->isMethod('POST')) {
$session->getFlashBag()->set('messages', 'Message sent successfully');
try {
if ($this->getParameter('captcha_enabled') && !$captchaHelper->isCaptchaValid($request)) {
throw new Exception($translator->trans('message.captcha_is_not_valid'));
}
if ($this->contactService->sendContactEmail($request->request->all())) {
$session->getFlashBag()->set('messages', 'Message sent successfully');
}
} catch (Exception $exception) {
$errorMessage = $exception->getMessage();
}
}
return $this->render('pages/contact.html.twig', [
'errorMessage' => $errorMessage,
'campuses' => $campuses
]);
}
/**
* @Rest\Get("/404", name="404")
*/
public function notFound(): Response
{
return $this->render('pages/404.html.twig');
}
}