<?php
namespace App\Controller\Frontend;
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 EntreprisesController extends BaseFrontendController
{
/**
* @Route("/{_locale}/forum-des-entreprises", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="forum_entreprises")
*/
public function forumEntreprises(TranslatorInterface $translator)
{
$this->addTitle($translator->trans(
'forum_entreprises.title'
));
$this->setDescription($translator->trans(
'forum_entreprises.desc'
));
$data = [];
$timezone = $this->requestService->getTimezone('fallback_timezone');
$data['timezone'] = $timezone;
$data['forums'] = $this->forumRepo->getActiveDate($timezone);
$data['campuses'] = $this->campusRepo->findBy(['status' => 1], ['ordering' => 'DESC']);
return $this->render('entreprises/forum_entreprises.html.twig', $data);
}
/**
* @Rest\Post("/save-entreprises", name="entreprises.save_apply")
* @Log
*/
public function saveEntreprises(
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')
]));
}
$contact = $request->request->get('contact');
if (isset($contact)) {
if (count($contact) > 1) {
$request->request->set('contact', 2);
} else {
$request->request->set('contact', $contact[0]);
}
}
$this->companyService->add($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));
}
/**
* @Rest\Post("/save-candidate", name="candidate.save_apply")
* @Log
*/
public function saveCandidate(
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->candidateService->add($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));
}
/**
* @Rest\Post("/get-forum", name="get.forum")
*/
public function getForum(
Request $request
) {
$timezone = $this->requestService->getTimezone('fallback_timezone');
$data['timezone'] = $timezone;
$data['forums'] = $this->forumRepo->getActiveDate($timezone);
$select = $this->container->get('twig')->render('entreprises/select_forum.html.twig', $data);
return $this->json(ServiceUtil::processSuccessful(['html' => $select]));
}
}