commit | author | age
|
bcd661
|
1 |
<?php |
JK |
2 |
class Output { |
413654
|
3 |
private $db; |
JK |
4 |
private $mapper; |
|
5 |
private $vehicleTypes; |
|
6 |
|
|
7 |
function __construct(Database $db, Mapper $mapper, VehicleTypes $vehicleTypes) { |
|
8 |
$this->db = $db; |
|
9 |
$this->mapper = $mapper; |
|
10 |
$this->vehicleTypes = $vehicleTypes; |
|
11 |
} |
|
12 |
|
|
13 |
function createMapping($saveConfig = FALSE) { |
bcd661
|
14 |
$mapping = []; |
413654
|
15 |
foreach($this->db->getAllById() as $vehicle) { |
JK |
16 |
$mapping[$vehicle['id']] = $this->vehicleTypes->getByNumber($vehicle['num']); |
bcd661
|
17 |
} |
JK |
18 |
|
|
19 |
if($saveConfig) { |
|
20 |
$json = json_encode($mapping); |
|
21 |
if(!file_put_contents($saveConfig['result_temp'], $json)) { |
|
22 |
throw new Exception('Result save failed'); |
|
23 |
} |
|
24 |
rename($saveConfig['result_temp'], $saveConfig['result']); |
|
25 |
} |
|
26 |
|
|
27 |
return $mapping; |
|
28 |
} |
|
29 |
|
413654
|
30 |
function createVehiclesList($fullMapping, $saveConfig = FALSE) { |
JK |
31 |
$trips = $this->mapper->getTTSSTrips(); |
|
32 |
|
bcd661
|
33 |
$lines = []; |
JK |
34 |
$vehicles = []; |
|
35 |
foreach($trips as $trip) { |
413654
|
36 |
$vehicle = $fullMapping[$trip['id']] ?? []; |
bcd661
|
37 |
$vehicle += ['trip' => $trip['id']]; |
JK |
38 |
$lines[$trip['line']][] = [ |
|
39 |
'trip' => $trip, |
|
40 |
'vehicle' => $vehicle, |
|
41 |
]; |
|
42 |
$vehicles[$vehicle['type'] ?? '?'][] = $vehicle; |
|
43 |
} |
413654
|
44 |
|
bcd661
|
45 |
foreach($lines as &$line) { |
JK |
46 |
usort($line, function($a, $b) { |
|
47 |
return (substr($a['vehicle']['num'] ?? '', 2) <=> substr($b['vehicle']['num'] ?? '', 2)); |
|
48 |
}); |
|
49 |
} |
|
50 |
unset($line); |
|
51 |
ksort($lines); |
413654
|
52 |
|
bcd661
|
53 |
foreach($vehicles as &$vehicle) { |
JK |
54 |
usort($vehicle, function($a, $b) { |
|
55 |
return (substr($a['num'] ?? '', 2) <=> substr($b['num'] ?? '', 2)); |
|
56 |
}); |
|
57 |
} |
|
58 |
unset($vehicle); |
|
59 |
ksort($vehicles); |
|
60 |
|
413654
|
61 |
$dbMapping = $this->db->getAllByNum(); |
JK |
62 |
ksort($dbMapping); |
|
63 |
|
bcd661
|
64 |
if($saveConfig) { |
JK |
65 |
$twigLoader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../templates'); |
|
66 |
$twig = new \Twig\Environment($twigLoader); |
413654
|
67 |
$twig->addExtension(new Twig_Extensions_Extension_Date()); |
bcd661
|
68 |
|
JK |
69 |
$vehiclesHtml = $twig->render('vehicles.html', [ |
|
70 |
'lines' => $lines, |
|
71 |
'vehicles' => $vehicles, |
|
72 |
'prefix' => $saveConfig['prefix'], |
413654
|
73 |
'mapping' => $dbMapping, |
bcd661
|
74 |
]); |
JK |
75 |
if(!file_put_contents($saveConfig['result_vehicles_temp'], $vehiclesHtml)) { |
|
76 |
throw new Exception('Vehicles save failed'); |
|
77 |
} |
|
78 |
rename($saveConfig['result_vehicles_temp'], $saveConfig['result_vehicles']); |
|
79 |
} |
|
80 |
|
|
81 |
return $lines; |
|
82 |
} |
|
83 |
} |