<?php
namespace App\Controller;
use App\Form\VacancyFormType;
use App\Services\CheckwebsitesettingService;
use App\Services\GeneralSendMailService;
use App\Services\ReCaptchaService;
use Knp\Component\Pager\PaginatorInterface;
use MultilingualBundle\Service\DocumentLookupService;
use Pimcore\Model\DataObject\Vacancy;
use Pimcore\Model\Site;
use Pimcore\Model\DataObject;
use Pimcore\Translation\Translator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class VacancyController extends BaseController
{
protected $recaptcha;
protected $recaptchaVersion;
protected $recaptchaPublicKey;
protected $checkwebsitesettingService;
protected $inotherlang;
protected $translator;
protected $sendmail;
protected $currentLanguage;
public function __construct(CheckwebsitesettingService $checkwebsitesettingService, DocumentLookupService $inotherlang, Translator $translator, ReCaptchaService $recaptcha, GeneralSendMailService $sendmail){
$this->checkwebsitesettingService = $checkwebsitesettingService;
$this->inotherlang = $inotherlang;
$this->translator = $translator;
$this->recaptcha = $recaptcha;
$this->recaptchaVersion = $recaptcha->getVersion();
$this->recaptchaPublicKey = $recaptcha->getPublicKey();
$this->sendmail = $sendmail;
}
/**
* @param Request $request
*
* @return Response
*
* @throws \Exception
*/
public function overviewAction(Request $request, PaginatorInterface $paginator)
{
$subSiteKey = '*';
if (Site::isSiteRequest()) {
$subSiteKey = Site::getCurrentSite()->getRootDocument()->getKey();
}
$vacancyList = new DataObject\Vacancy\Listing();
$vacancyList->setOrderKey('date');
$vacancyList->setOrder('DESC');
if (Site::isSiteRequest()) {
$vacancyList->setCondition('subsites LIKE ? AND title != ""', '%' . $subSiteKey . '%');
}
$paginator = $paginator->paginate(
$vacancyList,
$request->get('page', 1),
6
);
return $this->render('vacancy/overview.html.twig', [
'vacancies' => $paginator,
'paginationVariables' => $paginator->getPaginationData()
]);
}
/**
* @Route("{_locale}/{path}/{key}~v{id}", name="vacancy-detail", defaults={"path"=""}, requirements={"path"=".*?", "key"="[\w-]+", "id"="\d+"})
*
* @param Request $request
*
* @return Response
*/
public function detailAction(Request $request)
{
$vacancy = Vacancy::getById($request->get('id'));
if (!($vacancy instanceof Vacancy && ($vacancy->isPublished() || $this->verifyPreviewRequest($request, $vacancy)))) {
throw new NotFoundHttpException('Vacancy not found.');
}
//
$formName = VacancyFormType::class;
$emailTemplate = 'email_vacancy_contact'; // Email template contact
$emailTemplateConfirm = 'email_vacancy_customer'; // Email template contact confirm
///////// EDIT VARIABLES ABOVE
$showApplyForm = true;
// $this->currentLanguage = $request->getLocale();
// initialize form and handle request data
if($formName){
$form = $this->createForm($formName);
$form->handleRequest($request);
}
// handle form
if ($form->isSubmitted()) {
if ($form->isValid()) {
$params = $request->request->all();
$message = "Bedankt voor uw bericht. We nemen zo snel mogelijk contact met u op!";
if($this->recaptchaVersion){
if($this->recaptcha->captchaverify($params)){
$data = $form->getData();
$this->sendmail->_sendMailDefaultForm($data, "$emailTemplate", "$emailTemplateConfirm", "$message", $vacancy);
$showApplyForm = false;
}else{
$message = "Captcha code is niet correct!";
$this->addFlash("warning", $message);
}
}else{
$data = $form->getData();
$this->sendmail->_sendMailDefaultForm($data, "$emailTemplate", "$emailTemplateConfirm", "$message", $vacancy);
$showApplyForm = false;
}
}
}
return $this->render('vacancy/detail.html.twig', [
'vacancy' => $vacancy,
'form' => $form->createView(),
'recaptcha' => $this->recaptchaVersion,
'recaptchaPublic' => $this->recaptchaPublicKey,
'showApplyForm' => $showApplyForm
]);
}
}