Jacek Kowalski
2019-06-21 f6f79198b6467435fd7caccd2067eb0a29c97787
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 = [];
21     foreach($trips as $trip) {
22         $lines[$trip['line']][] = [
23             'trip' => $trip,
24             'vehicle' => $mapping[$trip['id']] ?? [],
25         ];
26     }
27     foreach($lines as &$line) {
28         usort($line, function($a, $b) {
29             return (substr($a['vehicle']['num'] ?? '', 2) <=> substr($b['vehicle']['num'] ?? '', 2)); 
30         });
31     }
32     unset($line);
33     ksort($lines);
34     
35     if($saveConfig) {
36         $twigLoader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../templates');
37         $twig = new \Twig\Environment($twigLoader);
38         
39         $vehiclesHtml = $twig->render('vehicles.html', [
40             'lines' => $lines,
41             'prefix' => $saveConfig['prefix'],
42         ]);
43         if(!file_put_contents($saveConfig['result_vehicles_temp'], $vehiclesHtml)) {
44             throw new Exception('Vehicles save failed');
45         }
46         rename($saveConfig['result_vehicles_temp'], $saveConfig['result_vehicles']);
47     }
48     
49     return $lines;
50 }