<?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\Routing\Annotation\Route;
class NetworkController extends AbstractController
{
protected array $locals = [];
public function __construct()
{
$this->locals['menu_active'] = '';
}
/**
* @Route("/red", name="network")
* @Template("front/network/network.html.twig")
*/
public function network(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 networkEstablishments(Request $request, EstablishmentManager $establishmentManager): JsonResponse
{
$establishments = $establishmentManager->valid();
$content = $this->renderView('front/network/establishment.html.twig', ['establishments' => $establishments]);
$response = new JsonResponse();
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
$response->headers->set('Access-Control-Allow-Headers', '*');
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->setData($content);
return $response;
}
/**
* @Route("/network-departments", name="network_departments")
*/
public function networkDepartments(Request $request, DepartmentManager $departmentManager): JsonResponse
{
$departments = $departmentManager->valid();
$departmentsArray = array_map(function (Department $department) { return $department->toArray(); }, $departments);
$response = new JsonResponse();
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
$response->headers->set('Access-Control-Allow-Headers', '*');
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->setData($departmentsArray);
return $response;
}
/**
* @Route("/cities-by-department", name="cities_by_department")
*/
public function citiesByDepartment(Request $request, DepartmentManager $departmentManager, CityManager $cityManager): JsonResponse
{
$response = new JsonResponse();
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
$response->headers->set('Access-Control-Allow-Headers', '*');
$response->headers->set('Access-Control-Allow-Origin', '*');
$department = $request->request->get('department', '');
if (!$department) {
return $response;
}
$department = $departmentManager->bySlug($department);
if (is_null($department)) {
return $response;
}
$cities = $cityManager->byDepartment($department);
$citiesArray = array_map(function (City $city) { return $city->toArray(); }, $cities);
$response->setData($citiesArray);
return $response;
}
/**
* @Route("/establishments-by-department", name="establishments_by_department")
*/
public function establishmentsByDepartment(Request $request, DepartmentManager $departmentManager, EstablishmentManager $establishmentManager): JsonResponse
{
$response = new JsonResponse();
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
$response->headers->set('Access-Control-Allow-Headers', '*');
$response->headers->set('Access-Control-Allow-Origin', '*');
$departmentSlug = $request->request->get('department', '');
if (!$departmentSlug) {
return $response->setData([
'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 $response->setData([
'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->setData($data);
return $response;
}
/**
* @Route("/establishments-by-city", name="establishments_by_city")
*/
public function establishmentsByCity(Request $request, DepartmentManager $departmentManager, CityManager $cityManager, EstablishmentManager $establishmentManager): JsonResponse
{
$response = new JsonResponse();
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
$response->headers->set('Access-Control-Allow-Headers', '*');
$response->headers->set('Access-Control-Allow-Origin', '*');
$departmentSlug = $request->request->get('department', '');
$city = $request->request->get('city', '');
if (!$departmentSlug or !$city) {
return $response->setData([
'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 $response->setData([
'status' => false,
'message' => $messageNoValid,
]);
}
$city = $cityManager->bySlug($city);
if (is_null($city)) {
return $response->setData([
'status' => false,
'message' => null,
]);
}
if ($department != $city->getDepartment()) {
return $response->setData([
'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->setData($data);
return $response;
}
}