<?php
namespace App\Controller\Frontend;
use App\Misc\CaptchaHelper;
use App\Utilities\ServiceUtil;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Exception\BadRequestException;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Annotation\Log;
class OpenDayController extends BaseFrontendController
{
/**
* @Route("/{_locale}/portes-ouvertes-et-evenements", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="open_day")
*/
public function openDay(
TranslatorInterface $translator,
Request $request
) {
$siteName = $translator->trans('siteName');
$metaTitle = $translator->trans(
'openDay.title'
);
$this->addTitle($metaTitle);
$this->setDescription($translator->trans(
'openDay.desc'
));
$data['campuses'] = $this->campusRepo->getAll(['filter_status' => 1, 'sort_asc' => 'ordering']);
$data['items'] = $this->openDayRepo->getOpendayInMonth($request);
return $this->render('open_day/index.html.twig', $data);
}
/**
* @Route("/filter-open-day", name="open_day.filter")
*/
public function filterData(
Request $request
) {
$items = $this->openDayService->filter($request->request->all());
$campuses = $this->campusRepo->findBy(['status' => 1], ['name' => 'desc']);
$view = $this->renderView('open_day/_filter.html.twig', [
'items' => $items,
'campuses' => $campuses
]);
return $this->json([
'data' => $view
]);
}
/**
* @Route("/{_locale}/inscription/{id}-{slug}", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="open_day.inscription")
*/
public function inscription(
$slug,
$id,
TranslatorInterface $translator
) {
$data['studyLevels'] = $this->studyLevelRepository->findAll();
$data['campuses'] = $this->campusRepo->findBy(['status' => 1], ['ordering' => 'DESC']);
$data['openDay'] = $this->openDayService->getById($id);
$this->addTitle($data['openDay']['name']);
$this->setDescription($data['openDay']['name']);
return $this->render('open_day/inscription.html.twig', $data);
}
/**
* @Route("/save-open-day", name="open_day.save")
* @Log
*/
public function saveOpenDay(
Request $request,
CaptchaHelper $captchaHelper,
TranslatorInterface $translator
) {
try {
// Check captcha is valid
if ($this->getParameter('captcha_enabled') && !$captchaHelper->isCaptchaValid($request)) {
return $this->json(ServiceUtil::processFailed([
'errorMessage' => $translator->trans('message.captcha_is_not_valid')
]));
}
$this->openDayPersonService->addPerson($request);
return $this->json(ServiceUtil::processSuccessful());
} catch (BadRequestException $badRequestException) {
$messagesArr = $badRequestException->getMessages();
foreach ($messagesArr['data'] as $field => $message) {
$errorMessage = $message;
break;
}
}
$data['errorMessage'] = $errorMessage;
return $this->json(ServiceUtil::processFailed($data));
}
}