src/Leads/Controller/RequestController.php line 132

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Leads\Controller;
  3. use App\Base\Controller\BaseController;
  4. use App\Leads\Form\ApartmentRecallType;
  5. use App\Leads\Form\LayoutRecallType;
  6. use App\Leads\Form\MortgageType;
  7. use App\Leads\Form\PopupType;
  8. use App\Leads\Form\RecallType;
  9. use App\Leads\Form\TradeInApartmentRecallType;
  10. use App\Leads\Form\TradeInLayoutRecallType;
  11. use App\Leads\Service\LeadBuilder;
  12. use App\Leads\Service\LeadFormFillInterface;
  13. use App\Leads\Service\LeadManager;
  14. use App\Main\Entity\Popup;
  15. use App\Main\Repository\PopupRepository;
  16. use Doctrine\Persistence\ManagerRegistry;
  17. use Symfony\Component\Form\Form;
  18. use Symfony\Component\Form\FormInterface;
  19. use Symfony\Component\HttpFoundation\Exception\BadRequestException;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  25. class RequestController extends BaseController
  26. {
  27.     private LeadManager $leadManager;
  28.     private UrlGeneratorInterface $urlGenerator;
  29.     public function __construct(
  30.         ManagerRegistry $managerRegistry,
  31.         LeadManager $leadManager,
  32.         private PopupRepository $popupRepository,
  33.         UrlGeneratorInterface $urlGenerator,
  34.     ) {
  35.         parent::__construct($managerRegistry);
  36.         $this->leadManager $leadManager;
  37.         $this->urlGenerator $urlGenerator;
  38.     }
  39.     #[Route(path'/request/recall'name'request:recall')]
  40.     public function recall(Request $request): Response
  41.     {
  42.         $form $this->createForm(RecallType::class);
  43.         $form->handleRequest($request);
  44.         return $this->processInlineForm($form);
  45.     }
  46.     #[Route(path'/request/layout_recall'name'request:layout_recall')]
  47.     public function layoutRecall(Request $request): Response
  48.     {
  49.         $form $this->createForm(LayoutRecallType::class);
  50.         $form->handleRequest($request);
  51.         return $this->processInlineForm($form);
  52.     }
  53.     #[Route(path'/request/apartment_recall'name'request:apartment_recall')]
  54.     public function apartmentRecall(Request $request): Response
  55.     {
  56.         $form $this->createForm(ApartmentRecallType::class);
  57.         $form->handleRequest($request);
  58.         return $this->processInlineForm($form);
  59.     }
  60.     #[Route(path'/request/recall-modal'name'request:recall_modal')]
  61.     public function recallModal(Request $request): Response
  62.     {
  63.         $form $this->createForm(RecallType::class);
  64.         $form->handleRequest($request);
  65.         return $this->processModelForm(
  66.             $form,
  67.             'pages/modal/recall/index.html.twig',
  68.             'pages/modal/recall/success.html.twig',
  69.         );
  70.     }
  71.     #[Route(path'/request/look-modal'name'request:look_modal')]
  72.     public function recallFlatModal(Request $request): Response
  73.     {
  74.         $form $this->createForm(RecallType::class);
  75.         $form->handleRequest($request);
  76.         return $this->processModelForm(
  77.             $form,
  78.             'pages/modal/recall/index.html.twig',
  79.             'pages/modal/recall/success.html.twig',
  80.             [
  81.                 'title' => 'Записаться на просмотр',
  82.                 'subtitle' => 'Оставьте заявку, наш менеджер перезвонит вам и ответит на любые вопросы'
  83.             ]
  84.         );
  85.     }
  86.     #[Route(path'/request/mortgage-modal'name'request:mortgage_modal')]
  87.     public function mortgageModal(Request $request): Response
  88.     {
  89.         $form $this->createForm(MortgageType::class);
  90.         $form->handleRequest($request);
  91.         return $this->processModelForm(
  92.             $form,
  93.             'pages/modal/recall/index.html.twig',
  94.             'pages/modal/recall/success.html.twig',
  95.             [
  96.                 'title' => 'Рассчитать ипотеку',
  97.                 'subtitle' => 'Оставьте заявку, наш менеджер перезвонит вам и ответит на любые вопросы'
  98.             ]
  99.         );
  100.     }
  101.     #[Route(path'/request/trade-in-apartment-modal/{id}'name'request:trade_in_apartment_modal')]
  102.     public function tradeInApartmentModal(Request $requestint $id): Response
  103.     {
  104.         $link $request->getHttpHost();
  105.         $link .= $this->urlGenerator->generate('realty:apartment_modal', ['id' => $id]);
  106.         $form $this->createForm(TradeInApartmentRecallType::class);
  107.         $form->get('link')->setData($link);
  108.         $form->handleRequest($request);
  109.         return $this->processModelForm(
  110.             $form,
  111.             'pages/modal/recall/index.html.twig',
  112.             'pages/modal/recall/success.html.twig',
  113.             [
  114.                 'title' => 'Оставить заявку',
  115.                 'subtitle' => 'Наш менеджер перезвонит вам в ближайшее время и ответит на любые вопросы'
  116.             ]
  117.         );
  118.     }
  119.     #[Route(path'/request/trade-in-layout-modal/{id}'name'request:trade_in_layout_modal')]
  120.     public function tradeInLayoutModal(Request $requestint $id): Response
  121.     {
  122.         $link $request->getHttpHost();
  123.         $link .= $this->urlGenerator->generate('realty:layout_modal', ['id' => $id]);
  124.         $form $this->createForm(TradeInLayoutRecallType::class);
  125.         $form->get('link')->setData($link);
  126.         $form->handleRequest($request);
  127.         return $this->processModelForm(
  128.             $form,
  129.             'pages/modal/recall/index.html.twig',
  130.             'pages/modal/recall/success.html.twig',
  131.             [
  132.                 'title' => 'Оставить заявку',
  133.                 'subtitle' => 'Наш менеджер перезвонит вам в ближайшее время и ответит на любые вопросы'
  134.             ]
  135.         );
  136.     }
  137.     public function processModelForm(
  138.         FormInterface $form,
  139.         string $template,
  140.         string $successTemplate,
  141.         array $params = []
  142.     ): Response {
  143.         if ($form->isSubmitted() && $form->isValid()) {
  144.             $em $this->getEntityManager();
  145.             $em->persist($form->getData());
  146.             $em->flush();
  147.             $this->sendSuccessMessage($form);
  148.             return $this->render($successTemplate$params);
  149.         }
  150.         return $this->render($templatearray_merge([
  151.             'form' => $form->createView()
  152.         ], $params));
  153.     }
  154.     public function processInlineForm(FormInterface $form): JsonResponse
  155.     {
  156.         if (!$form->isSubmitted()) {
  157.             throw new BadRequestException("Form are not submitted");
  158.         }
  159.         if (!$form->isValid()) {
  160.             $errors = [];
  161.             /** @var Form $child */
  162.             foreach ($form as $child) {
  163.                 if (!$child->isValid()) {
  164.                     foreach ($child->getErrors() as $error) {
  165.                         $errors[$child->getName()][] = $error->getMessage();
  166.                     }
  167.                 }
  168.             }
  169.             $data = [
  170.                 'state' => 'error',
  171.                 'errors' => [
  172.                     $form->getName() => $errors
  173.                 ]
  174.             ];
  175.         } else {
  176.             $data = [
  177.                 'state' => 'success'
  178.             ];
  179.             $em $this->getEntityManager();
  180.             $em->persist($form->getData());
  181.             $em->flush();
  182.             $this->sendSuccessMessage($form);
  183.         }
  184.         return new JsonResponse($data);
  185.     }
  186.     public function sendSuccessMessage(FormInterface $form): void
  187.     {
  188.         $this->leadManager->processFormLead($form);
  189.     }
  190.     #[Route(path'/request/popup-modal/{id}'name'request:popup_modal')]
  191.     public function popupModal(Request $requestPopup $popup)
  192.     {
  193.         $form $this->createForm(PopupType::class);
  194.         $form->handleRequest($request);
  195.         return $this->processModelForm(
  196.             $form,
  197.             'pages/modal/popup/index.html.twig',
  198.             'pages/modal/popup/success.html.twig',
  199.             [
  200.                 'popup' => $popup,
  201.                 'successTitle' => 'Вы успешно оставили заявку',
  202.                 'successSubtitle' => 'Наш менеджер свяжется с вами в ближайшее время'
  203.             ]
  204.         );
  205.     }
  206. }