From eb0777cc3cc2dda0fc023e0b252ff3c38b3ac320 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 9 Mar 2020 13:24:49 -0400 Subject: [PATCH] Account ICAO for subfleet, airline/location if columns exist #628 --- .../Services/Importers/AircraftImporter.php | 66 ++++++++++++++----- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/modules/Importer/Services/Importers/AircraftImporter.php b/modules/Importer/Services/Importers/AircraftImporter.php index a00ac7af1..82307e1d7 100644 --- a/modules/Importer/Services/Importers/AircraftImporter.php +++ b/modules/Importer/Services/Importers/AircraftImporter.php @@ -11,32 +11,57 @@ class AircraftImporter extends BaseImporter { protected $table = 'aircraft'; - /** - * CONSTANTS - */ - public const SUBFLEET_NAME = 'Imported Aircraft'; - public function run($start = 0) { $this->comment('--- AIRCRAFT IMPORT ---'); - $subfleet = $this->getSubfleet(); + $fields = [ + 'id', + 'icao', + 'name', + 'fullname', + 'registration', + 'enabled', + ]; + + // See if there is an airline column + $columns = $this->db->getColumns($this->table); + if (in_array('airline', $columns)) { + $fields[] = 'airline'; + } - $this->info('Subfleet ID is '.$subfleet->id); + if (in_array('location', $columns)) { + $fields[] = 'location'; + } $count = 0; - $rows = $this->db->readRows($this->table, $this->idField, $start); + $rows = $this->db->readRows($this->table, $this->idField, $start, $fields); foreach ($rows as $row) { + + $airline_id = null; + if (!empty($row->airline)) { + $airline_id = $this->idMapper->getMapping('airlines', $row->airline_id); + } + + $subfleet = $this->getSubfleet($row->icao, $airline_id); + $this->info('Subfleet ID is '.$subfleet->id); + $where = [ - 'name' => $row->fullname, 'registration' => $row->registration, ]; - $aircraft = Aircraft::firstOrCreate($where, [ + $cols = [ 'icao' => $row->icao, + 'name' => $row->fullname, 'subfleet_id' => $subfleet->id, 'active' => $row->enabled, - ]); + ]; + + if (!empty($row->location)) { + $cols['airport_id'] = $row->location; + } + + $aircraft = Aircraft::firstOrCreate($where, $cols); $this->idMapper->addMapping('aircraft', $row->id, $aircraft->id); @@ -51,16 +76,21 @@ public function run($start = 0) /** * Return the subfleet * + * @param string $icao ICAO of the subfleet + * @param int $airline_id + * * @return mixed */ - protected function getSubfleet() + protected function getSubfleet($icao, $airline_id = null) { - $airline = Airline::first(); - $subfleet = Subfleet::firstOrCreate([ - 'airline_id' => $airline->id, - 'name' => self::SUBFLEET_NAME, - ], ['type' => 'PHPVMS']); + if ($airline_id === null) { + $airline = Airline::first(); + $airline_id = $airline->id; + } - return $subfleet; + return Subfleet::firstOrCreate([ + 'airline_id' => $airline_id, + 'name' => $icao, + ], ['type' => $icao]); } }