src/Controller/Front/NetworkController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\City;
  4. use App\Entity\Department;
  5. use App\Manager\CityManager;
  6. use App\Manager\DepartmentManager;
  7. use App\Manager\EstablishmentCategoryManager;
  8. use App\Manager\EstablishmentManager;
  9. use App\Manager\NetworkManager;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class NetworkController extends AbstractController
  17. {
  18.     protected array $locals = [];
  19.     public function __construct()
  20.     {
  21.         $this->locals['menu_active'] = '';
  22.     }
  23.     /**
  24.      * @Route("/red-camiones", name="network_trucks")
  25.      * @Template("front/network/trucks.html.twig")
  26.      */
  27.     public function networkTrucks(NetworkManager $networkManagerDepartmentManager $departmentManagerEstablishmentCategoryManager $establishmentCategoryManagerEstablishmentManager $establishmentManager): array
  28.     {
  29.         $this->locals['departments'] = $departmentManager->valid();
  30.         $this->locals['categories'] = $establishmentCategoryManager->valid();
  31.         $this->locals['establishments'] = $establishmentManager->valid();
  32.         $this->locals['sd'] = $networkManager->find(1);
  33.         return $this->locals;
  34.     }
  35.     /**
  36.      * @Route("/red-buses", name="network_buses")
  37.      * @Template("front/network/buses.html.twig")
  38.      */
  39.     public function networkBuses(NetworkManager $networkManagerDepartmentManager $departmentManagerEstablishmentCategoryManager $establishmentCategoryManagerEstablishmentManager $establishmentManager): array
  40.     {
  41.         $this->locals['departments'] = $departmentManager->valid();
  42.         $this->locals['categories'] = $establishmentCategoryManager->valid();
  43.         $this->locals['establishments'] = $establishmentManager->valid();
  44.         $this->locals['sd'] = $networkManager->find(1);
  45.         return $this->locals;
  46.     }
  47.     /**
  48.      * @Route("/network-establishments", name="network_establishments")
  49.      */
  50.     public function networkDepartments(Request $requestEstablishmentManager $establishmentManager): Response
  51.     {
  52.         $establishments $establishmentManager->valid();
  53.         $content $this->renderView('front/network/establishment.html.twig', ['establishments' => $establishments]);
  54.         $response = new Response();
  55.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  56.         $response->setContent($content);
  57.         return $response;
  58.     }
  59.     /**
  60.      * @Route("/cities-by-department", name="cities_by_department")
  61.      */
  62.     public function citiesByDepartment(Request $requestDepartmentManager $departmentManagerCityManager $cityManager): JsonResponse
  63.     {
  64.         $department $request->request->get('department''');
  65.         if (!$department) {
  66.             return new JsonResponse([]);
  67.         }
  68.         $department $departmentManager->bySlug($department);
  69.         if (is_null($department)) {
  70.             return new JsonResponse([]);
  71.         }
  72.         $cities $cityManager->byDepartment($department);
  73.         $citiesArray array_map(function (City $city) { return $city->toArray(); }, $cities);
  74.         $response = new JsonResponse($citiesArray);
  75.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  76.         return $response;
  77.     }
  78.     /**
  79.      * @Route("/establishments-by-department", name="establishments_by_department")
  80.      */
  81.     public function establishmentsByDepartment(Request $requestDepartmentManager $departmentManagerEstablishmentManager $establishmentManager): Response
  82.     {
  83.         $departmentSlug $request->request->get('department''');
  84.         if (!$departmentSlug) {
  85.             return new JsonResponse([
  86.                 'status' => false,
  87.                 'message' => null,
  88.             ]);
  89.         }
  90.         $department $departmentManager->bySlug($departmentSlug);
  91.         $departmentNoValid $departmentManager->bySlug($departmentSlugfalse);
  92.         $messageNoValid is_null($departmentNoValid) ? null $departmentNoValid->getMessage();
  93.         if (is_null($department)) {
  94.             return new JsonResponse([
  95.                 'status' => false,
  96.                 'message' => $messageNoValid,
  97.             ]);
  98.         }
  99.         $establishments $establishmentManager->byDepartment($department);
  100.         $content $this->renderView('front/network/establishment.html.twig', ['establishments' => $establishments]);
  101.         $status count($establishments) > 0;
  102.         $data = [
  103.             'status' => $status,
  104.             'message' => $status null $messageNoValid,
  105.             'content' => $content,
  106.         ];
  107.         $response = new JsonResponse();
  108.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  109.         $response->setData($data);
  110.         return $response;
  111.     }
  112.     /**
  113.      * @Route("/establishments-by-city", name="establishments_by_city")
  114.      */
  115.     public function establishmentsByCity(Request $requestDepartmentManager $departmentManagerCityManager $cityManagerEstablishmentManager $establishmentManager): JsonResponse
  116.     {
  117.         $departmentSlug $request->request->get('department''');
  118.         $city $request->request->get('city''');
  119.         if (!$departmentSlug or !$city) {
  120.             return new JsonResponse([
  121.                 'status' => false,
  122.                 'message' => null,
  123.             ]);
  124.         }
  125.         $department $departmentManager->bySlug($departmentSlug);
  126.         $departmentNoValid $departmentManager->bySlug($departmentSlugfalse);
  127.         $messageNoValid is_null($departmentNoValid) ? null $departmentNoValid->getMessage();
  128.         if (is_null($department)) {
  129.             return new JsonResponse([
  130.                 'status' => false,
  131.                 'message' => $messageNoValid,
  132.             ]);
  133.         }
  134.         $city $cityManager->bySlug($city);
  135.         if (is_null($city)) {
  136.             return new JsonResponse([
  137.                 'status' => false,
  138.                 'message' => null,
  139.             ]);
  140.         }
  141.         if ($department != $city->getDepartment()) {
  142.             return new JsonResponse([
  143.                 'status' => false,
  144.                 'message' => null,
  145.             ]);
  146.         }
  147.         $establishments $establishmentManager->byCity($city);
  148.         $content $this->renderView('front/network/establishment.html.twig', ['establishments' => $establishments]);
  149.         $status count($establishments) > 0;
  150.         $data = [
  151.             'status' => $status,
  152.             'message' => $status null $messageNoValid,
  153.             'content' => $content,
  154.         ];
  155.         $response = new JsonResponse();
  156.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  157.         $response->setData($data);
  158.         return $response;
  159.     }
  160. }