<?php
namespace App\Controller\Stoparea;
use App\Normalizer\Stoparea\ScheduleNormalizer;
use App\Service\Internal\HomePageService;
use App\Service\Internal\StopareaService;
use App\Validator\Schedule\StopAreaSchedulesValidator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use App\Normalizer\Stoparea\LineNormalizer as StopareaLineNormalizer;
use App\Service\Internal\MapService;
class ScheduleController extends AbstractController
{
/**
* Get the schedules of a stop area
*/
public function list(
Request $request,
StopAreaSchedulesValidator $stopAreaSchedulesValidator,
HomePageService $homePageService,
StopareaService $stopareaService,
MapService $mapService,
ScheduleNormalizer $scheduleNormalizer,
StopareaLineNormalizer $stopArealineNormalizer
): Response {
$parameters = [];
$parameters['activeTab'] = 'schedule';
$parameters['scheduleTemplate'] = 'stoparea-schedules';
$parameters['type'] = 'stoparea';
$parameters['isXmlHttpMode'] = false;
$get = $request->attributes->all();
$disruptions = [];
$homePageService->getDisruption($disruptions);
$parameters['disruptions'] = $disruptions;
// Validation
$parameters['validator'] = $stopAreaSchedulesValidator->validate($get);
// Display
if ($parameters['validator']['error'] === 0) {
$parameters = $this->getListRenderParameters(
$stopareaService,
$scheduleNormalizer,
$stopArealineNormalizer,
$homePageService,
$mapService,
$get,
$parameters
);
}
$parameters = $homePageService->getRenderParameters($get, $parameters);
$parameters = $parameters + $get;
return $this->render('pages/schedule/'.$parameters['scheduleTemplate'].'.html.twig', $parameters);
}
public function stopArea(string $id, MapService $mapService): JsonResponse
{
$response = $mapService->getStopAreaStopPoints($id);
return new JsonResponse(['stopPoints' => $response]);
}
public function getListRenderParameters(
StopareaService $stopareaService,
ScheduleNormalizer $scheduleNormalizer,
StopareaLineNormalizer $stopArealineNormalizer,
HomePageService $homePageService,
MapService $mapService,
array $get,
array $parameters
): array {
$stopAreaId = $get['issaid'] ?? '';
$stopAreaSchedules = $stopareaService->getStopAreaSchedules($stopAreaId);
$stopAreaSchedules = $scheduleNormalizer->normalize($stopAreaSchedules, ['groupBySubNetworks' => true]);
// Stop area lines
$stopAreaLines = $stopareaService->getStopAreaLines($stopAreaId);
$stopAreaLines = $stopArealineNormalizer->normalizer($stopAreaLines, ['groupBySubNetworks' => true]);
$name = $stopAreaSchedules['stopArea']['name'] ?? '';
$city = isset($stopAreaSchedules['stopArea']['city']) ? ' - ' . $stopAreaSchedules['stopArea']['city'] : '';
$parameters['groupBySubNetworks'] = true;
$parameters['stopArea'] = $stopAreaSchedules['stopArea'] ?? [];
$parameters['scheduleInputValue'] = $name . $city;
$parameters['stopPoints'] = $stopAreaSchedules['stopPoints'] ?? [];
$parameters['lines'] = $stopAreaSchedules['lines'] ?? [];
$lines = $parameters['lines'];
if (!empty($parameters['lines'])) {
if ($parameters['groupBySubNetworks']) {
$lines = [];
foreach ($parameters['lines'] as $value) {
foreach($value['line'] as $line) {
$lines[]['line'] = $line;
}
}
}
$lineInfos = array_column($lines, 'line');
$parameters['filters']['mode'] = array_unique(array_column($lineInfos, 'modeText'));
$subnetworkInfos = array_column($lineInfos, 'subNetwork');
$parameters['filters']['subnetwork'] = array_unique(array_column($subnetworkInfos, 'name'));
}
$parameters['departures'] = $stopAreaSchedules['departures'] ?? [];
if (!empty($parameters['departures'])) {
$departureLineInfos = array_column($parameters['departures'], 'line');
$parameters['trainFilters']['mode'] = array_unique(array_column($departureLineInfos, 'modeText'));
$departureSubnetworkInfos = array_column($departureLineInfos, 'subNetwork');
$parameters['trainFilters']['subnetwork'] = array_unique(array_column($departureSubnetworkInfos, 'name'));
}
$parameters['stopAreaLines'] = $stopAreaLines;
return $parameters;
}
}