From 51c6b4f563d195a634039cf5b4d9ba83004e1a52 Mon Sep 17 00:00:00 2001 From: Jacek Kowalski <Jacek@jacekk.info> Date: Sat, 22 Jun 2019 20:31:10 +0000 Subject: [PATCH] Add table of vehicles by type --- lib/output.php | 25 ++++++++++++++++++++++++- 1 files changed, 24 insertions(+), 1 deletions(-) diff --git a/lib/output.php b/lib/output.php index d3fd6d9..0743017 100644 --- a/lib/output.php +++ b/lib/output.php @@ -1,11 +1,32 @@ <?php +function createMapping($db, $mapFunction, $saveConfig = FALSE) { + $mapping = []; + foreach($db->getAll() as $vehicle) { + $mapping[$vehicle['id']] = $mapFunction($vehicle['num']); + } + + if($saveConfig) { + $json = json_encode($mapping); + if(!file_put_contents($saveConfig['result_temp'], $json)) { + throw new Exception('Result save failed'); + } + rename($saveConfig['result_temp'], $saveConfig['result']); + } + + return $mapping; +} + function createVehiclesList($trips, $mapping, $saveConfig = FALSE) { $lines = []; + $vehicles = []; foreach($trips as $trip) { + $vehicle = $mapping[$trip['id']] ?? []; + $vehicle += ['trip' => $trip['id']]; $lines[$trip['line']][] = [ 'trip' => $trip, - 'vehicle' => $mapping[$trip['id']] ?? [], + 'vehicle' => $vehicle, ]; + $vehicles[$vehicle['type'] ?? '?'][] = $vehicle; } foreach($lines as &$line) { usort($line, function($a, $b) { @@ -14,6 +35,7 @@ } unset($line); ksort($lines); + ksort($vehicles); if($saveConfig) { $twigLoader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../templates'); @@ -21,6 +43,7 @@ $vehiclesHtml = $twig->render('vehicles.html', [ 'lines' => $lines, + 'vehicles' => $vehicles, 'prefix' => $saveConfig['prefix'], ]); if(!file_put_contents($saveConfig['result_vehicles_temp'], $vehiclesHtml)) { -- Gitblit v1.9.1