Jacek Kowalski
2019-06-22 51c6b4f563d195a634039cf5b4d9ba83004e1a52
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);
51c6b4 38     ksort($vehicles);
c077c7 39     
JK 40     if($saveConfig) {
41         $twigLoader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../templates');
42         $twig = new \Twig\Environment($twigLoader);
43         
44         $vehiclesHtml = $twig->render('vehicles.html', [
45             'lines' => $lines,
51c6b4 46             'vehicles' => $vehicles,
c077c7 47             'prefix' => $saveConfig['prefix'],
JK 48         ]);
49         if(!file_put_contents($saveConfig['result_vehicles_temp'], $vehiclesHtml)) {
50             throw new Exception('Vehicles save failed');
51         }
52         rename($saveConfig['result_vehicles_temp'], $saveConfig['result_vehicles']);
53     }
54     
55     return $lines;
56 }