<?php
namespace App\Controller\Front;
use App\Controller\Core\BaseFrontController;
use App\Entity\FormTruck;
use App\Entity\Model;
use App\Entity\Models;
use App\Form\Front\FormTruckType;
use App\Manager\BusFilterModelManager;
use App\Manager\BusFilterPBVManager;
use App\Manager\BusFilterPowerManager;
use App\Manager\BusFilterTonnageManager;
use App\Manager\BusManager;
use App\Manager\FormTruckManager;
use App\Manager\ModelManager;
use App\Manager\ModelsManager;
use App\Manager\TruckFilterModelManager;
use App\Manager\TruckFilterPowerManager;
use App\Manager\TruckFilterTonnageManager;
use App\Manager\TruckFilterTractionManager;
use App\Manager\TruckFilterTypeManager;
use App\Manager\TruckManager;
use App\Service\Mail\ModelMail;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ModelsController extends BaseFrontController
{
protected array $locals = [];
const FORM_TRUCK = 'sd_form_truck_entity';
const FORM_BUS = 'sd_form_bus_entity';
/**
* @Route("/volkswagen", name="model_list")
* @Template("front/models/list.html.twig")
*/
public function home(
Request $request,
TruckFilterModelManager $truckFilterModelManager,
TruckFilterTractionManager $truckFilterTractionManager,
TruckFilterTypeManager $truckFilterTypeManager,
TruckFilterPowerManager $truckFilterPowerManager,
TruckFilterTonnageManager $truckFilterTonnageManager,
BusFilterModelManager $busFilterModelManager,
BusFilterPBVManager $busFilterPBVManager,
BusFilterPowerManager $busFilterPowerManager,
TruckManager $truckManager,
BusManager $busManager,
ModelManager $modelManager,
ModelsManager $modelsManager,
PaginatorInterface $paginator
): array
{
$params = $request->query->all();
$page = $request->query->getInt('page', 1);
$trucks = $truckManager->filter($params);
$buses = $busManager->filter($params);
$models = $modelManager->byTrucksAndBuses($trucks, $buses);
$models = $paginator->paginate($models, $page, 12);
/** @var Models $info */
$info = $modelsManager->find(1);
$this->locals['truck_filter_model'] = $truckFilterModelManager->valid();
$this->locals['truck_filter_traction'] = $truckFilterTractionManager->valid();
$this->locals['truck_filter_type'] = $truckFilterTypeManager->valid();
$this->locals['truck_filter_power'] = $truckFilterPowerManager->valid();
$this->locals['truck_filter_tonnage'] = $truckFilterTonnageManager->valid();
$this->locals['bus_filter_model'] = $busFilterModelManager->valid();
$this->locals['bus_filter_pbv'] = $busFilterPBVManager->valid();
$this->locals['bus_filter_power'] = $busFilterPowerManager->valid();
$this->locals['models'] = $models;
$this->locals['type'] = 'both';
$this->locals['base_url'] = $this->generateUrl('model_list');
$this->locals['sd'] = $info;
$this->locals['sd_title'] = $info->getBothTitle();
$this->locals['sd_description'] = $info->getBothDescription();
return $this->locals;
}
/**
* @Route("/volkswagen-camiones", name="model_trucks")
* @Template("front/models/list.html.twig")
*/
public function trucks(
Request $request,
TruckFilterModelManager $truckFilterModelManager,
TruckFilterTractionManager $truckFilterTractionManager,
TruckFilterTypeManager $truckFilterTypeManager,
TruckFilterPowerManager $truckFilterPowerManager,
TruckFilterTonnageManager $truckFilterTonnageManager,
TruckManager $truckManager,
ModelsManager $modelsManager,
PaginatorInterface $paginator
): array
{
$params = $request->query->all();
$page = $request->query->getInt('page', 1);
$trucks = $truckManager->filter($params);
$trucks = $paginator->paginate($trucks, $page, 12);
/** @var Models $info */
$info = $modelsManager->find(1);
$this->locals['truck_filter_model'] = $truckFilterModelManager->valid();
$this->locals['truck_filter_traction'] = $truckFilterTractionManager->valid();
$this->locals['truck_filter_type'] = $truckFilterTypeManager->valid();
$this->locals['truck_filter_power'] = $truckFilterPowerManager->valid();
$this->locals['truck_filter_tonnage'] = $truckFilterTonnageManager->valid();
$this->locals['models'] = $trucks;
$this->locals['type'] = Model::TYPE_TRUCK;
$this->locals['base_url'] = $this->generateUrl('model_trucks');
$this->locals['sd'] = $info;
$this->locals['sd_title'] = $info->getTrucksTitle();
$this->locals['sd_description'] = $info->getTrucksDescription();
return $this->locals;
}
/**
* @Route("/volkswagen-buses", name="model_buses")
* @Template("front/models/list.html.twig")
*/
public function buses(
Request $request,
BusFilterModelManager $busFilterModelManager,
BusFilterPBVManager $busFilterPBVManager,
BusFilterPowerManager $busFilterPowerManager,
BusFilterTonnageManager $busFilterTonnageManager,
BusManager $busManager,
ModelsManager $modelsManager,
PaginatorInterface $paginator
): array
{
$params = $request->query->all();
$page = $request->query->getInt('page', 1);
$buses = $busManager->filter($params);
$buses = $paginator->paginate($buses, $page, 12);
/** @var Models $info */
$info = $modelsManager->find(1);
$this->locals['bus_filter_model'] = $busFilterModelManager->valid();
$this->locals['bus_filter_pbv'] = $busFilterPBVManager->valid();
$this->locals['bus_filter_power'] = $busFilterPowerManager->valid();
$this->locals['bus_filter_tonnage'] = $busFilterTonnageManager->valid();
$this->locals['models'] = $buses;
$this->locals['type'] = Model::TYPE_BUS;
$this->locals['base_url'] = $this->generateUrl('model_buses');
$this->locals['sd'] = $info;
$this->locals['sd_title'] = $info->getBusesTitle();
$this->locals['sd_description'] = $info->getBusesDescription();
return $this->locals;
}
/**
* @Route("/camion/{slug}", name="model_detail", methods={"GET"})
* @Template("front/models/detail_truck.html.twig")
*/
public function detailTruck(string $slug, Request $request, TruckManager $truckManager, ModelsManager $modelsManager): array
{
$truck = $truckManager->bySlug($slug);
if (is_null($truck)) {
throw new NotFoundHttpException('Camión no encontrado');
}
$entity = $this->getFormSession(self::FORM_TRUCK, new FormTruck());
$form = $this->getFormView(FormTruckType::class, $entity, 'model_detail_action', ['slug' => $slug]);
$referer = $request->headers->get('referer');
$sd = $modelsManager->find(1);
$this->locals['back_url'] = ($referer and $referer != $this->generateUrl('model_detail', ['slug' => $slug], UrlGeneratorInterface::ABSOLUTE_URL)) ? $referer : $this->generateUrl('model_list');
$this->locals['truck'] = $truck;
$this->locals['form'] = $form;
$this->locals['sd'] = $sd;
return $this->locals;
}
/**
* @Route("/camion/{slug}", name="model_detail_action", methods={"POST"})
*/
public function detailTruckAction(string $slug, Request $request, FormTruckManager $manager): RedirectResponse
{
$entity = new FormTruck();
$form = $this->getForm(FormTruckType::class, $entity, 'model_detail_action', ['slug' => $slug]);
$form->handleRequest($request);
if ($form->isValid()) {
$manager->save($entity);
return $this->redirectToRoute('thanks_slug', ['slug' => 'cotizar-camion']);
}
$this->saveFormSession(self::FORM_TRUCK, $entity);
return $this->redirectToRoute('model_detail', ['slug' => $slug]);
}
/**
* @Route("/bus/{slug}", name="modelbus_detail", methods={"GET"})
* @Template("front/models/detail_bus.html.twig")
*/
public function detailBus(string $slug, Request $request, BusManager $busManager, ModelsManager $modelsManager): array
{
$truck = $busManager->bySlug($slug);
if (is_null($truck)) {
throw new NotFoundHttpException('Camión no encontrado');
}
$entity = $this->getFormSession(self::FORM_BUS, new FormTruck());
$form = $this->getFormView(FormTruckType::class, $entity, 'modelbus_detail_action', ['slug' => $slug]);
$referer = $request->headers->get('referer');
$sd = $modelsManager->find(1);
$this->locals['back_url'] = ($referer and $referer != $this->generateUrl('modelbus_detail', ['slug' => $slug], UrlGeneratorInterface::ABSOLUTE_URL)) ? $referer : $this->generateUrl('model_list');
$this->locals['truck'] = $truck;
$this->locals['form'] = $form;
$this->locals['sd'] = $sd;
return $this->locals;
}
/**
* @Route("/bus/{slug}", name="modelbus_detail_action", methods={"POST"})
*/
public function detailBusAction(string $slug, Request $request, FormTruckManager $manager, ModelMail $mail): RedirectResponse
{
$entity = new FormTruck();
$form = $this->getForm(FormTruckType::class, $entity, 'modelbus_detail_action', ['slug' => $slug]);
$form->handleRequest($request);
if ($form->isValid()) {
$manager->save($entity);
$mail->load($entity, ['is_bus' => true])->send();
return $this->redirectToRoute('thanks_slug', ['slug' => 'cotizar-camion']);
}
$this->saveFormSession(self::FORM_BUS, $entity);
return $this->redirectToRoute('model_detail', ['slug' => $slug]);
}
}