Skip to content

Commit

Permalink
Add Multiple Aircraft per Route in Import
Browse files Browse the repository at this point in the history
  • Loading branch information
velocity23 committed Jul 31, 2022
1 parent a972c51 commit 85158ae
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
30 changes: 18 additions & 12 deletions classes/controllers/admin/AdminRoutesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ private function import_choose()
"dep" => $segments[2],
"arr" => $segments[3],
"duration" => Time::strToSecs(str_replace('.', ':', $segments[10])),
"aircraftid" => $segments[5]
"aircraftids" => array_map('trim', explode(',', $segments[5])),
);
}, array_filter($routelines[0], function ($l) {
return strlen(trim($l)) > 0;
}));

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

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

for ($i = 0; $i < $count; $i++) {
$item = Input::get('livery' . $i);
if (empty($item)) continue;

$aircraft = false;
foreach ($allaircraft as $a) {
if ($a->ifliveryid == $item) $aircraft = $a;
Expand All @@ -247,15 +249,7 @@ private function import_import()
array_push($allaircraft, $aircraft);
}



$routes = array_map(function ($r) use ($i, $aircraft) {
if ($r['aircraftid'] == Input::get('rego' . $i)) {
$r['aircraftid'] = $aircraft->id;
}

return $r;
}, $routes);
$acdict[Input::get('rego' . $i)] = $aircraft;
}

$nextId = intval(Route::nextId());
Expand All @@ -270,7 +264,19 @@ private function import_import()
array_push($params, $item["dep"]);
array_push($params, $item["arr"]);
array_push($params, $item["duration"]);
Route::addAircraft($nextId + $j, $item["aircraftid"]);

$aircraft = array_map(function ($a) use ($acdict) {
if (isset($acdict[$a])) return $acdict[$a]->id;

return null;
}, $item["aircraftids"]);
$aircraft = array_filter($aircraft, function ($a) {
return $a !== null;
});

foreach ($aircraft as $a) {
Route::addAircraft($nextId + $j, $a);
}

$j++;
}
Expand Down
11 changes: 5 additions & 6 deletions themes/default/views/admin/import_choose.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@
$aircraftOptions .= '<option value="' . $id . '">' . $name . '</option>';
}

$aircraftgroups = array_column(Page::$pageData->routes, 'aircraftids');
$uniqueaircraft = array_unique(array_merge(...$aircraftgroups));

echo '<form action="/admin/routes/import" method="post">';
echo '<input hidden name="action" value="import" />';
echo "<input hidden name='rJson' value='$routesJson' />";
echo '<table class="w-100 mb-2">';
$i = 0;
$doneAircraft = [];
foreach (Page::$pageData->routes as $r) {
if (in_array($r['aircraftid'], $doneAircraft)) continue;

foreach ($uniqueaircraft as $aircraftid) {
echo '<tr class="border-bottom border-top"><td class="align-middle p-2"><b>';
echo $r['aircraftid'];
echo $aircraftid;
echo '</b></td><td class="align-middle py-2">';
echo '<input hidden name="rego' . $i . '" value="' . $r["aircraftid"] . '" />';
echo '<select required class="form-control mb-2 aircraftSelect" name="aircraft' . $i . '" id="' . $i . '">';
Expand All @@ -73,7 +73,6 @@
echo '<option value>Select an Aircraft to Get Liveries</option>';
echo '</select>';
echo '</td></tr>';
array_push($doneAircraft, $r['aircraftid']);
$i++;
}
echo '</table>';
Expand Down
3 changes: 2 additions & 1 deletion themes/tailwind/views/admin/import_choose.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
Page::setTitle('Import Routes - ' . Page::$pageData->va_name);
require_once __DIR__ . '/../../includes/header.php';
$uniqueaircraft = array_unique(array_column(Page::$pageData->routes, 'aircraftid'));
$aircraftgroups = array_column(Page::$pageData->routes, 'aircraftids');
$uniqueaircraft = array_unique(array_merge(...$aircraftgroups));
$aircraftoptions = [];
foreach (Page::$pageData->aircraft as $id => $name) {
$aircraftoptions[] = '<option value="' . $id . '">' . $name . '</option>';
Expand Down

0 comments on commit 85158ae

Please sign in to comment.