Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Flights By Type Rating and Aircraft ICAO Type Code #1881

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions app/Http/Controllers/Frontend/FlightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace App\Http\Controllers\Frontend;

use App\Contracts\Controller;
use App\Models\Aircraft;
use App\Models\Bid;
use App\Models\Enums\FlightType;
use App\Models\Flight;
use App\Models\Typerating;
use App\Repositories\AirlineRepository;
use App\Repositories\AirportRepository;
use App\Repositories\Criteria\WhereCriteria;
Expand Down Expand Up @@ -78,7 +80,7 @@ public function search(Request $request): View

/** @var \App\Models\User $user */
$user = Auth::user();
$user->loadMissing('current_airport');
$user->loadMissing(['current_airport', 'typeratings']);

if (setting('pilots.restrict_to_company')) {
$where['airline_id'] = $user->airline_id;
Expand Down Expand Up @@ -117,10 +119,17 @@ public function search(Request $request): View
->toArray();
// Get flight_id's of open (non restricted) flights
$open_flights = Flight::withCount('subfleets')->whereNull('user_id')->having('subfleets_count', 0)->pluck('id')->toArray();
// Merge results
$allowed_flights = array_merge($user_flights, $open_flights);
// Build aircraft icao codes by considering allowed subfleets
$icao_codes = Aircraft::whereIn('subfleet_id', $user_subfleets)->groupBy('icao')->orderBy('icao')->pluck('icao')->toArray();
// Build type ratings collection by considering user's capabilities
$type_ratings = $user->typeratings;
} else {
$allowed_flights = [];
// Build aircraft icao codes array from complete fleet
$icao_codes = Aircraft::groupBy('icao')->orderBy('icao')->pluck('icao')->toArray();
// Build type ratings collection from all active ratings
$type_ratings = Typerating::where('active', 1)->select('id', 'name', 'type')->orderBy('type')->get();
}

// Get only used Flight Types for the search form
Expand Down Expand Up @@ -181,6 +190,8 @@ public function search(Request $request): View
'simbrief' => !empty(setting('simbrief.api_key')),
'simbrief_bids' => setting('simbrief.only_bids'),
'acars_plugin' => $this->moduleSvc->isModuleActive('VMSAcars'),
'icao_codes' => $icao_codes,
'type_ratings' => $type_ratings,
]);
}

Expand Down
12 changes: 11 additions & 1 deletion app/Repositories/Criteria/WhereCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ public function apply($model, RepositoryInterface $repository)
$model = $model
->with($relation)
->whereHas($relation, function (Builder $query) use ($criterea) {
$query->where($criterea);
// By Taylor Broad
if (!isset($criterea['method'])) {
$query->where($criterea);
} else {
if ($criterea['method'] == 'where') {
$query->where($criterea['query']);
}
if ($criterea['method'] == 'whereIn') {
$query->whereIn($criterea['query']['key'], $criterea['query']['values']);
}
}
});
}
}
Expand Down
30 changes: 30 additions & 0 deletions app/Repositories/FlightRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Repositories;

use App\Contracts\Repository;
use App\Models\Aircraft;
use App\Models\Flight;
use App\Models\Typerating;
use App\Repositories\Criteria\WhereCriteria;
use Illuminate\Http\Request;
use Prettus\Repository\Contracts\CacheableInterface;
Expand Down Expand Up @@ -151,6 +153,34 @@ public function searchCriteria(Request $request, bool $only_active = true): self
];
}

// Search by TypeRating (based on SubFleet relationships)
if ($request->filled('type_rating_id')) {
$type_rating = Typerating::with(['subfleets'])->where('id', $request->input('type_rating_id'))->first();
$subfleet_ids = filled(optional($type_rating)->subfleets) ? $type_rating->subfleets->pluck('id')->toArray() : [];

$relations['subfleets'] = [
'method' => 'whereIn',
'query' => [
'key' => 'subfleets.id',
'values' => $subfleet_ids,
],
];
}

// Search By ICAO type code (based on Aircraft definitions)
if ($request->filled('icao_type')) {
$icao = $request->input('icao_type');
$subfleet_ids = Aircraft::where('icao', $icao)->groupBy('subfleet_id')->pluck('subfleet_id')->toArray();

$relations['subfleets'] = [
'method' => 'whereIn',
'query' => [
'key' => 'subfleets.id',
'values' => $subfleet_ids,
],
];
}

$this->pushCriteria(new WhereCriteria($request, $where, $relations));

return $this;
Expand Down
24 changes: 24 additions & 0 deletions resources/views/layouts/default/flights/search.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@
</select>
</div>

@if(filled($type_ratings))
<div class="mt-3">
<div>Type Rating</div>
<select name="type_rating_id" id="type_rating_id" class="form-control select2">
<option value=""></option>
@foreach($type_ratings as $tr)
<option value="{{ $tr->id }}" @if(request()->get('type_rating_id') == $tr->id) selected @endif>{{ $tr->type.' | '.$tr->name }}</option>
@endforeach
</select>
</div>
@endif

@if(filled($icao_codes))
<div class="mt-3">
<div>ICAO Type</div>
<select name="icao_type" id="icao_type" class="form-control select2">
<option value=""></option>
@foreach($icao_codes as $icao)
<option value="{{ $icao }}" @if(request()->get('icao_type') == $icao) selected @endif>{{ $icao }}</option>
@endforeach
</select>
</div>
@endif

<div class="clear mt-3" style="margin-top: 10px;">
<button type="submit" class="btn btn-outline-primary">@lang('common.find')</button>
<a href="{{ route('frontend.flights.index') }}">@lang('common.reset')</a>
Expand Down
Loading