src/Controller/Front/NetworkController.php line 31

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\Routing\Annotation\Route;
  15. class NetworkController extends AbstractController
  16. {
  17.     protected array $locals = [];
  18.     public function __construct()
  19.     {
  20.         $this->locals['menu_active'] = '';
  21.     }
  22.     /**
  23.      * @Route("/red", name="network")
  24.      * @Template("front/network/network.html.twig")
  25.      */
  26.     public function network(NetworkManager $networkManagerDepartmentManager $departmentManagerEstablishmentCategoryManager $establishmentCategoryManagerEstablishmentManager $establishmentManager): array
  27.     {
  28.         $this->locals['departments'] = $departmentManager->valid();
  29.         $this->locals['categories'] = $establishmentCategoryManager->valid();
  30.         $this->locals['establishments'] = $establishmentManager->valid();
  31.         $this->locals['sd'] = $networkManager->find(1);
  32.         return $this->locals;
  33.     }
  34.     /**
  35.      * @Route("/network-establishments", name="network_establishments")
  36.      */
  37.     public function networkEstablishments(Request $requestEstablishmentManager $establishmentManager): JsonResponse
  38.     {
  39.         $establishments $establishmentManager->valid();
  40.         $content $this->renderView('front/network/establishment.html.twig', ['establishments' => $establishments]);
  41.         $response = new JsonResponse();
  42.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  43.         $response->headers->set('Access-Control-Allow-Headers''*');
  44.         $response->headers->set('Access-Control-Allow-Origin''*');
  45.         $response->setData($content);
  46.         return $response;
  47.     }
  48.     /**
  49.      * @Route("/network-departments", name="network_departments")
  50.      */
  51.     public function networkDepartments(Request $requestDepartmentManager $departmentManager): JsonResponse
  52.     {
  53.         $departments $departmentManager->valid();
  54.         $departmentsArray array_map(function (Department $department) { return $department->toArray(); }, $departments);
  55.         $response = new JsonResponse();
  56.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  57.         $response->headers->set('Access-Control-Allow-Headers''*');
  58.         $response->headers->set('Access-Control-Allow-Origin''*');
  59.         $response->setData($departmentsArray);
  60.         return $response;
  61.     }
  62.     /**
  63.      * @Route("/cities-by-department", name="cities_by_department")
  64.      */
  65.     public function citiesByDepartment(Request $requestDepartmentManager $departmentManagerCityManager $cityManager): JsonResponse
  66.     {
  67.         $response = new JsonResponse();
  68.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  69.         $response->headers->set('Access-Control-Allow-Headers''*');
  70.         $response->headers->set('Access-Control-Allow-Origin''*');
  71.         $department $request->request->get('department''');
  72.         if (!$department) {
  73.             return $response;
  74.         }
  75.         $department $departmentManager->bySlug($department);
  76.         if (is_null($department)) {
  77.             return $response;
  78.         }
  79.         $cities $cityManager->byDepartment($department);
  80.         $citiesArray array_map(function (City $city) { return $city->toArray(); }, $cities);
  81.         $response->setData($citiesArray);
  82.         return $response;
  83.     }
  84.     /**
  85.      * @Route("/establishments-by-department", name="establishments_by_department")
  86.      */
  87.     public function establishmentsByDepartment(Request $requestDepartmentManager $departmentManagerEstablishmentManager $establishmentManager): JsonResponse
  88.     {
  89.         $response = new JsonResponse();
  90.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  91.         $response->headers->set('Access-Control-Allow-Headers''*');
  92.         $response->headers->set('Access-Control-Allow-Origin''*');
  93.         $departmentSlug $request->request->get('department''');
  94.         if (!$departmentSlug) {
  95.             return $response->setData([
  96.                 'status' => false,
  97.                 'message' => null,
  98.             ]);
  99.         }
  100.         $department $departmentManager->bySlug($departmentSlug);
  101.         $departmentNoValid $departmentManager->bySlug($departmentSlugfalse);
  102.         $messageNoValid is_null($departmentNoValid) ? null $departmentNoValid->getMessage();
  103.         if (is_null($department)) {
  104.             return $response->setData([
  105.                 'status' => false,
  106.                 'message' => $messageNoValid,
  107.             ]);
  108.         }
  109.         $establishments $establishmentManager->byDepartment($department);
  110.         $content $this->renderView('front/network/establishment.html.twig', ['establishments' => $establishments]);
  111.         $status count($establishments) > 0;
  112.         $data = [
  113.             'status' => $status,
  114.             'message' => $status null $messageNoValid,
  115.             'content' => $content,
  116.         ];
  117.         $response->setData($data);
  118.         return $response;
  119.     }
  120.     /**
  121.      * @Route("/establishments-by-city", name="establishments_by_city")
  122.      */
  123.     public function establishmentsByCity(Request $requestDepartmentManager $departmentManagerCityManager $cityManagerEstablishmentManager $establishmentManager): JsonResponse
  124.     {
  125.         $response = new JsonResponse();
  126.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  127.         $response->headers->set('Access-Control-Allow-Headers''*');
  128.         $response->headers->set('Access-Control-Allow-Origin''*');
  129.         $departmentSlug $request->request->get('department''');
  130.         $city $request->request->get('city''');
  131.         if (!$departmentSlug or !$city) {
  132.             return $response->setData([
  133.                 'status' => false,
  134.                 'message' => null,
  135.             ]);
  136.         }
  137.         $department $departmentManager->bySlug($departmentSlug);
  138.         $departmentNoValid $departmentManager->bySlug($departmentSlugfalse);
  139.         $messageNoValid is_null($departmentNoValid) ? null $departmentNoValid->getMessage();
  140.         if (is_null($department)) {
  141.             return $response->setData([
  142.                 'status' => false,
  143.                 'message' => $messageNoValid,
  144.             ]);
  145.         }
  146.         $city $cityManager->bySlug($city);
  147.         if (is_null($city)) {
  148.             return $response->setData([
  149.                 'status' => false,
  150.                 'message' => null,
  151.             ]);
  152.         }
  153.         if ($department != $city->getDepartment()) {
  154.             return $response->setData([
  155.                 'status' => false,
  156.                 'message' => null,
  157.             ]);
  158.         }
  159.         $establishments $establishmentManager->byCity($city);
  160.         $content $this->renderView('front/network/establishment.html.twig', ['establishments' => $establishments]);
  161.         $status count($establishments) > 0;
  162.         $data = [
  163.             'status' => $status,
  164.             'message' => $status null $messageNoValid,
  165.             'content' => $content,
  166.         ];
  167.         $response->setData($data);
  168.         return $response;
  169.     }
  170. }