From 6e1dac584a72cb048fa1314fabb24df9411796f2 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Fri, 30 Jul 2021 14:49:33 +0000
Subject: [PATCH] Change tram types: RF304 -> GT8N, RF312 -> GT8C

---
 lib/Output.php |   33 ++++++++++++++++++++++++++++-----
 1 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/lib/Output.php b/lib/Output.php
index 2944625..be23ebb 100644
--- a/lib/Output.php
+++ b/lib/Output.php
@@ -1,9 +1,19 @@
 <?php
 class Output {
-	static function createMapping($db, VehicleTypes $vehicleTypes, $saveConfig = FALSE) {
+	private $db;
+	private $mapper;
+	private $vehicleTypes;
+	
+	function __construct(Database $db, Mapper $mapper, VehicleTypes $vehicleTypes) {
+		$this->db = $db;
+		$this->mapper = $mapper;
+		$this->vehicleTypes = $vehicleTypes;
+	}
+	
+	function createMapping($saveConfig = FALSE) {
 		$mapping = [];
-		foreach($db->getAll() as $vehicle) {
-			$mapping[$vehicle['id']] = $vehicleTypes->getByNumber($vehicle['num']);
+		foreach($this->db->getAllById() as $vehicle) {
+			$mapping[$vehicle['id']] = $this->vehicleTypes->getByNumber($vehicle['num']);
 		}
 		
 		if($saveConfig) {
@@ -17,11 +27,13 @@
 		return $mapping;
 	}
 	
-	function createVehiclesList($trips, $mapping, $saveConfig = FALSE) {
+	function createVehiclesList($fullMapping, $saveConfig = FALSE) {
+		$trips = $this->mapper->getTTSSTrips();
+		
 		$lines = [];
 		$vehicles = [];
 		foreach($trips as $trip) {
-			$vehicle = $mapping[$trip['id']] ?? [];
+			$vehicle = $fullMapping[$trip['id']] ?? [];
 			$vehicle += ['trip' => $trip['id']];
 			$lines[$trip['line']][] = [
 				'trip' => $trip,
@@ -29,6 +41,7 @@
 			];
 			$vehicles[$vehicle['type'] ?? '?'][] = $vehicle;
 		}
+		
 		foreach($lines as &$line) {
 			usort($line, function($a, $b) {
 				return (substr($a['vehicle']['num'] ?? '', 2) <=> substr($b['vehicle']['num'] ?? '', 2)); 
@@ -36,6 +49,7 @@
 		}
 		unset($line);
 		ksort($lines);
+		
 		foreach($vehicles as &$vehicle) {
 			usort($vehicle, function($a, $b) {
 				return (substr($a['num'] ?? '', 2) <=> substr($b['num'] ?? '', 2));
@@ -44,14 +58,23 @@
 		unset($vehicle);
 		ksort($vehicles);
 		
+		$dbMapping = $this->db->getAllByNum();
+		foreach($dbMapping as &$vehicle) {
+			$vehicle['vehicle'] = $this->vehicleTypes->getByNumber($vehicle['num']);
+		}
+		unset($vehicle);
+		ksort($dbMapping);
+		
 		if($saveConfig) {
 			$twigLoader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../templates');
 			$twig = new \Twig\Environment($twigLoader);
+			$twig->addExtension(new Twig_Extensions_Extension_Date());
 			
 			$vehiclesHtml = $twig->render('vehicles.html', [
 				'lines' => $lines,
 				'vehicles' => $vehicles,
 				'prefix' => $saveConfig['prefix'],
+				'mapping' => $dbMapping,
 			]);
 			if(!file_put_contents($saveConfig['result_vehicles_temp'], $vehiclesHtml)) {
 				throw new Exception('Vehicles save failed');

--
Gitblit v1.9.1