<?php
namespace App\Controller\Frontend;
use App\Entity\Campaign;
use App\Misc\CaptchaHelper;
use App\Utilities\ServiceUtil;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations as Rest;
use App\Exception\BadRequestException;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\Annotation\Route;
use App\Annotation\Log;
class DocumentController extends BaseFrontendController
{
/**
* @Route("/{_locale}/telechargez-la-brochure-estiam", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="demande")
*/
public function demande(Request $request, TranslatorInterface $translator)
{
$siteName = $translator->trans('siteName');
$this->addTitle($translator->trans(
'demande.title',
[
'%siteName%' => $siteName
]
));
$this->setDescription($translator->trans(
'demande.desc',
[
'%siteName%' => $siteName
]
));
$data = [];
$data['campaign'] = $this->campaignRepo->findOneBy(['id' => Campaign::DOCUMENT]);
$data['programId'] = $request->query->get('id') ? $request->query->get('id') : null;
$data['campuses'] = $this->campusRepo->findBy(['status' => 1]);
$data['programs'] = $this->programRepo->findBy(['isRegister' => 1], ['ordering' => 'ASC']);
return $this->render('demande/index.html.twig', $data);
}
/**
* @Rest\Post("/save-document", name="document.save_apply")
* @Log
*/
public function saveDocument(
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')
// ]));
// }
$information['previousSchool'] = '';
if ($request->get('schoolName')) {
$information['previousSchool'] = $request->get('schoolName');
if ($request->get('schoolPostal')) {
$information['previousSchool'] = $information['previousSchool'] . ' / ' . $request->get('schoolPostal');
}
if ($request->get('schoolCity')) {
$information['previousSchool'] = $information['previousSchool'] . ' / ' . $request->get('schoolCity');
}
$request->query->set('information', $information);
}
//check email
$userRequest = $request->get('user');
$user = $this->userRepo->findOneBy(['email' => $userRequest['email']]);
if ($user) {
$this->userService->updateProfileUser($userRequest, $user);
$profile = $this->profileRepo->findOneBy(['user' => $user]);
$request->query->set('profileId', $profile->getId());
$programs = $request->get('programs');
foreach ($profile->getPrograms() as $program) {
if (!in_array($program->getId(), $programs)) {
$programs[] = $program->getId();
}
}
$request->query->set('programs', $programs);
$this->profileService->updateProfile($request);
} else {
$this->profileService->addProfile($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));
}
}