Skip to content

Commit 85158ae

Browse files
committed
Add Multiple Aircraft per Route in Import
1 parent a972c51 commit 85158ae

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

classes/controllers/admin/AdminRoutesController.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,15 @@ private function import_choose()
203203
"dep" => $segments[2],
204204
"arr" => $segments[3],
205205
"duration" => Time::strToSecs(str_replace('.', ':', $segments[10])),
206-
"aircraftid" => $segments[5]
206+
"aircraftids" => array_map('trim', explode(',', $segments[5])),
207207
);
208208
}, array_filter($routelines[0], function ($l) {
209209
return strlen(trim($l)) > 0;
210210
}));
211211

212212
foreach ($data->routes as $r) {
213213
foreach ($r as $k => $v) {
214-
if (strlen($v) < 1) {
214+
if (gettype($v) === 'string' && strlen($v) < 1) {
215215
Session::flash('error', 'Invalid CSV File');
216216
$this->get_import();
217217
}
@@ -231,11 +231,13 @@ private function import_import()
231231
$db->query("DELETE FROM route_aircraft WHERE NOT routeid IN (SELECT id FROM routes)");
232232

233233
$allaircraft = Aircraft::fetchActiveAircraft()->results();
234+
$acdict = [];
234235
$firstRank = $db->query("SELECT * FROM ranks ORDER BY timereq ASC LIMIT 1")->first()->id;
235236

236237
for ($i = 0; $i < $count; $i++) {
237238
$item = Input::get('livery' . $i);
238239
if (empty($item)) continue;
240+
239241
$aircraft = false;
240242
foreach ($allaircraft as $a) {
241243
if ($a->ifliveryid == $item) $aircraft = $a;
@@ -247,15 +249,7 @@ private function import_import()
247249
array_push($allaircraft, $aircraft);
248250
}
249251

250-
251-
252-
$routes = array_map(function ($r) use ($i, $aircraft) {
253-
if ($r['aircraftid'] == Input::get('rego' . $i)) {
254-
$r['aircraftid'] = $aircraft->id;
255-
}
256-
257-
return $r;
258-
}, $routes);
252+
$acdict[Input::get('rego' . $i)] = $aircraft;
259253
}
260254

261255
$nextId = intval(Route::nextId());
@@ -270,7 +264,19 @@ private function import_import()
270264
array_push($params, $item["dep"]);
271265
array_push($params, $item["arr"]);
272266
array_push($params, $item["duration"]);
273-
Route::addAircraft($nextId + $j, $item["aircraftid"]);
267+
268+
$aircraft = array_map(function ($a) use ($acdict) {
269+
if (isset($acdict[$a])) return $acdict[$a]->id;
270+
271+
return null;
272+
}, $item["aircraftids"]);
273+
$aircraft = array_filter($aircraft, function ($a) {
274+
return $a !== null;
275+
});
276+
277+
foreach ($aircraft as $a) {
278+
Route::addAircraft($nextId + $j, $a);
279+
}
274280

275281
$j++;
276282
}

themes/default/views/admin/import_choose.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@
5252
$aircraftOptions .= '<option value="' . $id . '">' . $name . '</option>';
5353
}
5454

55+
$aircraftgroups = array_column(Page::$pageData->routes, 'aircraftids');
56+
$uniqueaircraft = array_unique(array_merge(...$aircraftgroups));
57+
5558
echo '<form action="/admin/routes/import" method="post">';
5659
echo '<input hidden name="action" value="import" />';
5760
echo "<input hidden name='rJson' value='$routesJson' />";
5861
echo '<table class="w-100 mb-2">';
5962
$i = 0;
60-
$doneAircraft = [];
61-
foreach (Page::$pageData->routes as $r) {
62-
if (in_array($r['aircraftid'], $doneAircraft)) continue;
63-
63+
foreach ($uniqueaircraft as $aircraftid) {
6464
echo '<tr class="border-bottom border-top"><td class="align-middle p-2"><b>';
65-
echo $r['aircraftid'];
65+
echo $aircraftid;
6666
echo '</b></td><td class="align-middle py-2">';
6767
echo '<input hidden name="rego' . $i . '" value="' . $r["aircraftid"] . '" />';
6868
echo '<select required class="form-control mb-2 aircraftSelect" name="aircraft' . $i . '" id="' . $i . '">';
@@ -73,7 +73,6 @@
7373
echo '<option value>Select an Aircraft to Get Liveries</option>';
7474
echo '</select>';
7575
echo '</td></tr>';
76-
array_push($doneAircraft, $r['aircraftid']);
7776
$i++;
7877
}
7978
echo '</table>';

themes/tailwind/views/admin/import_choose.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22
Page::setTitle('Import Routes - ' . Page::$pageData->va_name);
33
require_once __DIR__ . '/../../includes/header.php';
4-
$uniqueaircraft = array_unique(array_column(Page::$pageData->routes, 'aircraftid'));
4+
$aircraftgroups = array_column(Page::$pageData->routes, 'aircraftids');
5+
$uniqueaircraft = array_unique(array_merge(...$aircraftgroups));
56
$aircraftoptions = [];
67
foreach (Page::$pageData->aircraft as $id => $name) {
78
$aircraftoptions[] = '<option value="' . $id . '">' . $name . '</option>';

0 commit comments

Comments
 (0)