<?php declare(strict_types=1);
namespace App\Leads\Controller;
use App\Base\Controller\BaseController;
use App\Leads\Form\ApartmentRecallType;
use App\Leads\Form\LayoutRecallType;
use App\Leads\Form\MortgageType;
use App\Leads\Form\PopupType;
use App\Leads\Form\RecallType;
use App\Leads\Form\TradeInApartmentRecallType;
use App\Leads\Form\TradeInLayoutRecallType;
use App\Leads\Service\LeadBuilder;
use App\Leads\Service\LeadFormFillInterface;
use App\Leads\Service\LeadManager;
use App\Main\Entity\Popup;
use App\Main\Repository\PopupRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class RequestController extends BaseController
{
private LeadManager $leadManager;
private UrlGeneratorInterface $urlGenerator;
public function __construct(
ManagerRegistry $managerRegistry,
LeadManager $leadManager,
private PopupRepository $popupRepository,
UrlGeneratorInterface $urlGenerator,
) {
parent::__construct($managerRegistry);
$this->leadManager = $leadManager;
$this->urlGenerator = $urlGenerator;
}
#[Route(path: '/request/recall', name: 'request:recall')]
public function recall(Request $request): Response
{
$form = $this->createForm(RecallType::class);
$form->handleRequest($request);
return $this->processInlineForm($form);
}
#[Route(path: '/request/layout_recall', name: 'request:layout_recall')]
public function layoutRecall(Request $request): Response
{
$form = $this->createForm(LayoutRecallType::class);
$form->handleRequest($request);
return $this->processInlineForm($form);
}
#[Route(path: '/request/apartment_recall', name: 'request:apartment_recall')]
public function apartmentRecall(Request $request): Response
{
$form = $this->createForm(ApartmentRecallType::class);
$form->handleRequest($request);
return $this->processInlineForm($form);
}
#[Route(path: '/request/recall-modal', name: 'request:recall_modal')]
public function recallModal(Request $request): Response
{
$form = $this->createForm(RecallType::class);
$form->handleRequest($request);
return $this->processModelForm(
$form,
'pages/modal/recall/index.html.twig',
'pages/modal/recall/success.html.twig',
);
}
#[Route(path: '/request/look-modal', name: 'request:look_modal')]
public function recallFlatModal(Request $request): Response
{
$form = $this->createForm(RecallType::class);
$form->handleRequest($request);
return $this->processModelForm(
$form,
'pages/modal/recall/index.html.twig',
'pages/modal/recall/success.html.twig',
[
'title' => 'Записаться на просмотр',
'subtitle' => 'Оставьте заявку, наш менеджер перезвонит вам и ответит на любые вопросы'
]
);
}
#[Route(path: '/request/mortgage-modal', name: 'request:mortgage_modal')]
public function mortgageModal(Request $request): Response
{
$form = $this->createForm(MortgageType::class);
$form->handleRequest($request);
return $this->processModelForm(
$form,
'pages/modal/recall/index.html.twig',
'pages/modal/recall/success.html.twig',
[
'title' => 'Рассчитать ипотеку',
'subtitle' => 'Оставьте заявку, наш менеджер перезвонит вам и ответит на любые вопросы'
]
);
}
#[Route(path: '/request/trade-in-apartment-modal/{id}', name: 'request:trade_in_apartment_modal')]
public function tradeInApartmentModal(Request $request, int $id): Response
{
$link = $request->getHttpHost();
$link .= $this->urlGenerator->generate('realty:apartment_modal', ['id' => $id]);
$form = $this->createForm(TradeInApartmentRecallType::class);
$form->get('link')->setData($link);
$form->handleRequest($request);
return $this->processModelForm(
$form,
'pages/modal/recall/index.html.twig',
'pages/modal/recall/success.html.twig',
[
'title' => 'Оставить заявку',
'subtitle' => 'Наш менеджер перезвонит вам в ближайшее время и ответит на любые вопросы'
]
);
}
#[Route(path: '/request/trade-in-layout-modal/{id}', name: 'request:trade_in_layout_modal')]
public function tradeInLayoutModal(Request $request, int $id): Response
{
$link = $request->getHttpHost();
$link .= $this->urlGenerator->generate('realty:layout_modal', ['id' => $id]);
$form = $this->createForm(TradeInLayoutRecallType::class);
$form->get('link')->setData($link);
$form->handleRequest($request);
return $this->processModelForm(
$form,
'pages/modal/recall/index.html.twig',
'pages/modal/recall/success.html.twig',
[
'title' => 'Оставить заявку',
'subtitle' => 'Наш менеджер перезвонит вам в ближайшее время и ответит на любые вопросы'
]
);
}
public function processModelForm(
FormInterface $form,
string $template,
string $successTemplate,
array $params = []
): Response {
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getEntityManager();
$em->persist($form->getData());
$em->flush();
$this->sendSuccessMessage($form);
return $this->render($successTemplate, $params);
}
return $this->render($template, array_merge([
'form' => $form->createView()
], $params));
}
public function processInlineForm(FormInterface $form): JsonResponse
{
if (!$form->isSubmitted()) {
throw new BadRequestException("Form are not submitted");
}
if (!$form->isValid()) {
$errors = [];
/** @var Form $child */
foreach ($form as $child) {
if (!$child->isValid()) {
foreach ($child->getErrors() as $error) {
$errors[$child->getName()][] = $error->getMessage();
}
}
}
$data = [
'state' => 'error',
'errors' => [
$form->getName() => $errors
]
];
} else {
$data = [
'state' => 'success'
];
$em = $this->getEntityManager();
$em->persist($form->getData());
$em->flush();
$this->sendSuccessMessage($form);
}
return new JsonResponse($data);
}
public function sendSuccessMessage(FormInterface $form): void
{
$this->leadManager->processFormLead($form);
}
#[Route(path: '/request/popup-modal/{id}', name: 'request:popup_modal')]
public function popupModal(Request $request, Popup $popup)
{
$form = $this->createForm(PopupType::class);
$form->handleRequest($request);
return $this->processModelForm(
$form,
'pages/modal/popup/index.html.twig',
'pages/modal/popup/success.html.twig',
[
'popup' => $popup,
'successTitle' => 'Вы успешно оставили заявку',
'successSubtitle' => 'Наш менеджер свяжется с вами в ближайшее время'
]
);
}
}