<?php
namespace App\Controller\Front;
use App\Entity\City;
use App\Entity\Department;
use App\Manager\CityManager;
use App\Manager\DepartmentManager;
use App\Manager\EstablishmentCategoryManager;
use App\Manager\EstablishmentManager;
use App\Manager\NetworkManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class NetworkController extends AbstractController
{
protected array $locals = [];
public function __construct()
{
$this->locals['menu_active'] = '';
}
/**
* @Route("/red-camiones", name="network_trucks")
* @Template("front/network/trucks.html.twig")
*/
public function networkTrucks(NetworkManager $networkManager, DepartmentManager $departmentManager, EstablishmentCategoryManager $establishmentCategoryManager, EstablishmentManager $establishmentManager): array
{
$this->locals['departments'] = $departmentManager->valid();
$this->locals['categories'] = $establishmentCategoryManager->valid();
$this->locals['establishments'] = $establishmentManager->valid();
$this->locals['sd'] = $networkManager->find(1);
return $this->locals;
}
/**
* @Route("/red-buses", name="network_buses")
* @Template("front/network/buses.html.twig")
*/
public function networkBuses(NetworkManager $networkManager, DepartmentManager $departmentManager, EstablishmentCategoryManager $establishmentCategoryManager, EstablishmentManager $establishmentManager): array
{
$this->locals['departments'] = $departmentManager->valid();
$this->locals['categories'] = $establishmentCategoryManager->valid();
$this->locals['establishments'] = $establishmentManager->valid();
$this->locals['sd'] = $networkManager->find(1);
return $this->locals;
}
/**
* @Route("/network-establishments", name="network_establishments")
*/
public function networkDepartments(Request $request, EstablishmentManager $establishmentManager): Response
{
$establishments = $establishmentManager->valid();
$content = $this->renderView('front/network/establishment.html.twig', ['establishments' => $establishments]);
$response = new Response();
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
$response->setContent($content);
return $response;
}
/**
* @Route("/cities-by-department", name="cities_by_department")
*/
public function citiesByDepartment(Request $request, DepartmentManager $departmentManager, CityManager $cityManager): JsonResponse
{
$department = $request->request->get('department', '');
if (!$department) {
return new JsonResponse([]);
}
$department = $departmentManager->bySlug($department);
if (is_null($department)) {
return new JsonResponse([]);
}
$cities = $cityManager->byDepartment($department);
$citiesArray = array_map(function (City $city) { return $city->toArray(); }, $cities);
$response = new JsonResponse($citiesArray);
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
return $response;
}
/**
* @Route("/establishments-by-department", name="establishments_by_department")
*/
public function establishmentsByDepartment(Request $request, DepartmentManager $departmentManager, EstablishmentManager $establishmentManager): Response
{
$departmentSlug = $request->request->get('department', '');
if (!$departmentSlug) {
return new JsonResponse([
'status' => false,
'message' => null,
]);
}
$department = $departmentManager->bySlug($departmentSlug);
$departmentNoValid = $departmentManager->bySlug($departmentSlug, false);
$messageNoValid = is_null($departmentNoValid) ? null : $departmentNoValid->getMessage();
if (is_null($department)) {
return new JsonResponse([
'status' => false,
'message' => $messageNoValid,
]);
}
$establishments = $establishmentManager->byDepartment($department);
$content = $this->renderView('front/network/establishment.html.twig', ['establishments' => $establishments]);
$status = count($establishments) > 0;
$data = [
'status' => $status,
'message' => $status ? null : $messageNoValid,
'content' => $content,
];
$response = new JsonResponse();
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
$response->setData($data);
return $response;
}
/**
* @Route("/establishments-by-city", name="establishments_by_city")
*/
public function establishmentsByCity(Request $request, DepartmentManager $departmentManager, CityManager $cityManager, EstablishmentManager $establishmentManager): JsonResponse
{
$departmentSlug = $request->request->get('department', '');
$city = $request->request->get('city', '');
if (!$departmentSlug or !$city) {
return new JsonResponse([
'status' => false,
'message' => null,
]);
}
$department = $departmentManager->bySlug($departmentSlug);
$departmentNoValid = $departmentManager->bySlug($departmentSlug, false);
$messageNoValid = is_null($departmentNoValid) ? null : $departmentNoValid->getMessage();
if (is_null($department)) {
return new JsonResponse([
'status' => false,
'message' => $messageNoValid,
]);
}
$city = $cityManager->bySlug($city);
if (is_null($city)) {
return new JsonResponse([
'status' => false,
'message' => null,
]);
}
if ($department != $city->getDepartment()) {
return new JsonResponse([
'status' => false,
'message' => null,
]);
}
$establishments = $establishmentManager->byCity($city);
$content = $this->renderView('front/network/establishment.html.twig', ['establishments' => $establishments]);
$status = count($establishments) > 0;
$data = [
'status' => $status,
'message' => $status ? null : $messageNoValid,
'content' => $content,
];
$response = new JsonResponse();
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
$response->setData($data);
return $response;
}
}