Jacek Kowalski
2019-06-22 a255d6af5519895fa22d592dcff06944111abba3
commit | author | age
c077c7 1 <?php
f6f791 2 function createMapping($db, $mapFunction, $saveConfig = FALSE) {
JK 3     $mapping = [];
4     foreach($db->getAll() as $vehicle) {
5         $mapping[$vehicle['id']] = $mapFunction($vehicle['num']);
6     }
7     
8     if($saveConfig) {
9         $json = json_encode($mapping);
10         if(!file_put_contents($saveConfig['result_temp'], $json)) {
11             throw new Exception('Result save failed');
12         }
13         rename($saveConfig['result_temp'], $saveConfig['result']);
14     }
15     
16     return $mapping;
17 }
18
c077c7 19 function createVehiclesList($trips, $mapping, $saveConfig = FALSE) {
JK 20     $lines = [];
51c6b4 21     $vehicles = [];
c077c7 22     foreach($trips as $trip) {
51c6b4 23         $vehicle = $mapping[$trip['id']] ?? [];
JK 24         $vehicle += ['trip' => $trip['id']];
c077c7 25         $lines[$trip['line']][] = [
JK 26             'trip' => $trip,
51c6b4 27             'vehicle' => $vehicle,
c077c7 28         ];
51c6b4 29         $vehicles[$vehicle['type'] ?? '?'][] = $vehicle;
c077c7 30     }
JK 31     foreach($lines as &$line) {
32         usort($line, function($a, $b) {
33             return (substr($a['vehicle']['num'] ?? '', 2) <=> substr($b['vehicle']['num'] ?? '', 2)); 
34         });
35     }
36     unset($line);
37     ksort($lines);
3c8681 38     foreach($vehicles as &$vehicle) {
JK 39         usort($vehicle, function($a, $b) {
40             return (substr($a['num'] ?? '', 2) <=> substr($b['num'] ?? '', 2));
41         });
42     }
43     unset($vehicle);
51c6b4 44     ksort($vehicles);
c077c7 45     
JK 46     if($saveConfig) {
47         $twigLoader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../templates');
48         $twig = new \Twig\Environment($twigLoader);
49         
50         $vehiclesHtml = $twig->render('vehicles.html', [
51             'lines' => $lines,
51c6b4 52             'vehicles' => $vehicles,
c077c7 53             'prefix' => $saveConfig['prefix'],
JK 54         ]);
55         if(!file_put_contents($saveConfig['result_vehicles_temp'], $vehiclesHtml)) {
56             throw new Exception('Vehicles save failed');
57         }
58         rename($saveConfig['result_vehicles_temp'], $saveConfig['result_vehicles']);
59     }
60     
61     return $lines;
62 }