<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-04-15
* Time: 12:36
*/
namespace App\Controller;
use App\Entity\Location\City;
use App\Repository\CityRepository;
use App\Repository\ProfileRepository;
use App\Service\DefaultCityProvider;
use Flagception\Bundle\FlagceptionBundle\Annotations\Feature;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Intl\Intl;
use Symfony\Contracts\Translation\TranslatorInterface;
class CityListController extends AbstractController
{
public function __construct(
private CityRepository $cityRepository,
private ProfileRepository $profileRepository,
private TranslatorInterface $translator
) {}
public function page(): Response
{
$countByCity = $this->profileRepository->countByCity();
$cities = [
'megalopolis' => [],
'moscow_region' => [],
];
foreach ($this->cityRepository->findAll() as $city) {
/** @var City $city */
$label = sprintf('%s (%d)', $this->translator->trans($city->getName()), $countByCity[$city->getId()] ?? 0);
if (City::GROUP_MEGALOPOLIS === $city->getCityGroup()) {
$cities['megalopolis'][$label] = $city;
} elseif (City::GROUP_MOSCOW_REGION === $city->getCityGroup()) {
$cities['moscow_region'][$label] = $city;
} else {
$cities[$city->getCountryCode()][$label] = $city;
}
}
return $this->render('CityList/list.html.twig', [
'cities' => $cities,
]);
}
/**
* @Feature("has_city_list_page")
*/
public function listByCountry(Request $request, DefaultCityProvider $defaultCityProvider): Response
{
$locale = $request->getLocale() ?? 'ru';
$countries = Countries::getNames($locale);
$countByCity = $this->profileRepository->countByCity();
$cities = [];
foreach ($this->cityRepository->findAll() as $city) {
/** @var City $city */
$countryCode = $city->getCountryCode();
$country = $countries[$countryCode];
if (!isset($cities[$country]))
$cities[$country] = [];
$route = $city->equals($defaultCityProvider->getDefaultCity()) ? 'homepage' : 'profile_list.list_by_city';
$cities[$country][$city->getId()] = [
'city' => $city,
'count' => $countByCity[$city->getId()] ?? 0,
'uri' => $this->generateUrl($route, ['city' => $city->getUriIdentity()]),
];
}
return $this->render('CityList/list_by_country.html.twig', [
'cities' => $cities,
]);
}
}