<?php
namespace App\Controller\Front;
use App\Manager\InfoManager;
use App\Service\CRM\ApiClient;
use App\Traits\RecaptchaTrait;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RequestStack;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Service\Mail\{PromoMail, PartsMail, MachineMail, ServiceMail};
use App\Manager\{PromoManager, MachineManager, ServiceManager, CategoryManager};
use App\Form\Front\{FormPromoType, FormPartsType, FormMachineType, FormServiceType};
use App\Manager\{FormPromoManager, FormPartsManager, FormMachineManager, FormServiceManager};
use App\Manager\{InfoPromoManager, InfoPartsManager, InfoMachineManager, InfoServiceManager};
class MachineController extends AbstractController
{
use RecaptchaTrait;
protected array $locals = [];
public function __construct(RequestStack $requestStack, InfoManager $infoManager)
{
$this->requestStack = $requestStack;
$this->infoManager = $infoManager;
$this->locals['menu_active'] = '';
}
/**
* @Route("/camiones/{slug}/", name="trucks_cat")
*/
public function trucksCat(Request $request, MachineManager $mm, CategoryManager $cm, InfoMachineManager $imm, PaginatorInterface $paginator, $slug)
{
$this->locals['machines'] = $mm->listByCategory($slug);
$this->locals['machines'] = $paginator->paginate(
$this->locals['machines'],
$request->query->getInt('page', 1),
12,
);
if ($request->query->get('ajax', false) == true) {
$response = new Response($this->renderView('front/ajax/trucks.html.twig', $this->locals), 200);
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
return $response;
}
$this->locals['menu_active'] = 'trucks';
$this->locals['category'] = $cm->bySlug($slug);
$this->locals['home'] = $imm->find(1);
$this->locals['slug'] = $slug;
$response = new Response($this->renderView('front/machine/trucks.html.twig', $this->locals), 200);
return $response;
}
/**
* @Route("/camiones/", name="trucks")
*/
public function trucks(Request $request, MachineManager $mm, CategoryManager $cm, InfoMachineManager $imm, PaginatorInterface $paginator)
{
$this->locals['machines'] = $mm->listByCategory(null);
$this->locals['machines'] = $paginator->paginate(
$this->locals['machines'],
$request->query->getInt('page', 1),
12,
);
if ($request->query->get('ajax', false) == true) {
$response = new Response($this->renderView('front/ajax/trucks.html.twig', $this->locals), 200);
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
return $response;
}
$this->locals['menu_active'] = 'trucks';
$this->locals['category'] = null; //$cm->bySlug($slug);
$this->locals['home'] = $imm->find(1);
$this->locals['slug'] = ''; //$slug;
$response = new Response($this->renderView('front/machine/trucks.html.twig', $this->locals), 200);
return $response;
}
/**
* @Route("/camion/{slug}/", name="truck")
* @Template("front/machine/detail.html.twig")
*/
public function truck(Request $request, String $slug, MachineManager $mm, FormMachineManager $fmm, InfoMachineManager $imm, MachineMail $mail, ApiClient $apiclient)
{
$entity = $fmm->create();
$detail = $mm->bySlug($slug);
$form = $this->createForm(FormMachineType::class, $entity, [
'action' => $this->generateUrl('truck', ['slug' => $detail->getSlug()]),
'method' => 'POST',
]);
if ($request->isMethod('POST')) {
$entity->setTypeMachine($detail->getCategory()->getTitleCrm());
$entity->setTypeModel($detail->getCodeCrm());
$form->handleRequest($request);
if ($form->isValid() && $this->validateGoogleCaptcha()) {
$fmm->save($entity);
//$mail->load($entity)->send();
//send to CRM
$result = $apiclient->sendLead($entity);
return $this->redirectToRoute('thanks_machine');
} else {
//dump($form->getErrors(true, true)); exit;
}
}
$this->locals['form'] = $form->createView();
$this->locals['home'] = $imm->find(1);
$this->locals['detail'] = $detail;
$this->locals['menu_active'] = 'trucks';
return $this->locals;
}
/**
* @Route("/promociones/", name="promos", methods={"POST", "GET"})
* @Template("front/machine/promos.html.twig")
*/
public function promos(Request $request, PromoManager $pm, InfoPromoManager $ipm, FormPromoManager $fpm, PromoMail $mail)
{
$entity = $fpm->create();
$form = $this->createForm(FormPromoType::class, $entity, [
'action' => $this->generateUrl('promos'),
'method' => 'POST',
]);
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid() && $this->validateGoogleCaptcha()) {
$fpm->save($entity);
$mail->load($entity)->send();
return $this->redirectToRoute('thanks_promo');
} else {
//dump($form->getErrors(true, true)); exit;
}
}
$this->locals['form'] = $form->createView();
$this->locals['home'] = $ipm->find(1);
$this->locals['promos'] = $pm->listActive();
$this->locals['menu_active'] = 'promos';
return $this->locals;
}
/**
* @Route("/repuestos/", name="parts", methods={"POST", "GET"})
* @Template("front/machine/parts.html.twig")
*/
public function parts(Request $request, InfoPartsManager $ipm, FormPartsManager $fpm, PartsMail $mail)
{
$entity = $fpm->create();
$form = $this->createForm(FormPartsType::class, $entity, [
'action' => $this->generateUrl('parts'),
'method' => 'POST',
]);
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid() && $this->validateGoogleCaptcha()) {
$fpm->save($entity);
$mail->load($entity)->send();
return $this->redirectToRoute('thanks_parts');
} else {
//dump($form->getErrors(true, true)); exit;
}
}
$this->locals['form'] = $form->createView();
$this->locals['home'] = $ipm->find(1);
$this->locals['menu_active'] = 'parts';
return $this->locals;
}
/**
* @Route("/servicios/", name="services")
* @Template("front/machine/services.html.twig")
*/
public function services(ServiceManager $sm, InfoServiceManager $ism): array
{
$this->locals['home'] = $ism->find(1);
$this->locals['services'] = $sm->listActive();
$this->locals['menu_active'] = 'services';
return $this->locals;
}
/**
* @Route("/servicio/{slug}/", name="service", methods={"POST", "GET"})
* @Template("front/machine/service.html.twig")
*/
public function service(Request $request, String $slug, ServiceManager $sm, FormServiceManager $fsm, ServiceMail $mail)
{
$entity = $fsm->create();
$form = $this->createForm(FormServiceType::class, $entity, [
'action' => $this->generateUrl('service', ['slug' => $slug]),
'method' => 'POST',
]);
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid() && $this->validateGoogleCaptcha()) {
$fsm->save($entity);
$mail->load($entity)->send();
return $this->redirectToRoute('thanks_service');
} else {
//dump($form->getErrors(true, true)); exit;
}
}
$this->locals['form'] = $form->createView();
$this->locals['detail'] = $sm->bySlug($slug);
$this->locals['menu_active'] = 'services';
return $this->locals;
}
}