From f6f79198b6467435fd7caccd2067eb0a29c97787 Mon Sep 17 00:00:00 2001 From: Jacek Kowalski <Jacek@jacekk.info> Date: Fri, 21 Jun 2019 22:58:16 +0000 Subject: [PATCH] Move mapping creation to a function in lib/output.php --- lib/output.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 50 insertions(+), 0 deletions(-) diff --git a/lib/output.php b/lib/output.php new file mode 100644 index 0000000..4d05b40 --- /dev/null +++ b/lib/output.php @@ -0,0 +1,50 @@ +<?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 = []; + foreach($trips as $trip) { + $lines[$trip['line']][] = [ + 'trip' => $trip, + 'vehicle' => $mapping[$trip['id']] ?? [], + ]; + } + foreach($lines as &$line) { + usort($line, function($a, $b) { + return (substr($a['vehicle']['num'] ?? '', 2) <=> substr($b['vehicle']['num'] ?? '', 2)); + }); + } + unset($line); + ksort($lines); + + if($saveConfig) { + $twigLoader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../templates'); + $twig = new \Twig\Environment($twigLoader); + + $vehiclesHtml = $twig->render('vehicles.html', [ + 'lines' => $lines, + 'prefix' => $saveConfig['prefix'], + ]); + if(!file_put_contents($saveConfig['result_vehicles_temp'], $vehiclesHtml)) { + throw new Exception('Vehicles save failed'); + } + rename($saveConfig['result_vehicles_temp'], $saveConfig['result_vehicles']); + } + + return $lines; +} -- Gitblit v1.9.1