phpvms/app/Http/Controllers/Frontend/FlightController.php
B.Fatih KOZ 4c60e5da69
Eager loading update for frontend controllers and widgets (#1348)
* Update AirportController.php

Eager load `files`

* Update DashboardController.php

 Eager load `journal` for user , and `aircraft, arr_airport, comments, dpt_airport` for last_pirep.

* Update FlightController.php

Eager load + `alt_airport, subfleets` (with their airlines) for flights and `current_airport` for user 
(though current_airport can be removed and we can base the in blade checks on `$user->curr_airport_id` to reduce db reads and model loading)

* Update HomeController.php

Eager load `home_airport` for users

* Update PirepController.php

Eager load `airline, aircraft, fares, transactions, dpt_airport, arr_airport, comments, simbrief, user with rank` for pirep details

* Update ProfileController.php

Eager load `airline, awards, current_airport, fields.field, home_airport, last_pirep, rank` for user

* Update UserController.php

Eager load `airline, current_airport, fields.field, home_airport, rank` for users and count `awards`

* Update AirportController.php

* Update DashboardController.php

* Update PirepController.php

* Update ProfileController.php

* Update LatestNews.php

Eager load `user` for news

* Update LatestPilots.php

Eager load `home_airport` for latest users

* StyleFix 1

* StlyeFix 1.5

* Update SimBriefController.php

Eager load airline with flight

* Update SimBriefController.php

* Update SimBriefController.php
2021-11-09 09:51:02 -05:00

252 lines
7.9 KiB
PHP

<?php
namespace App\Http\Controllers\Frontend;
use App\Contracts\Controller;
use App\Models\Bid;
use App\Models\Enums\FlightType;
use App\Models\Flight;
use App\Repositories\AirlineRepository;
use App\Repositories\AirportRepository;
use App\Repositories\Criteria\WhereCriteria;
use App\Repositories\FlightRepository;
use App\Repositories\SubfleetRepository;
use App\Repositories\UserRepository;
use App\Services\GeoService;
use App\Services\ModuleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Laracasts\Flash\Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Exceptions\RepositoryException;
class FlightController extends Controller
{
private $airlineRepo;
private $airportRepo;
private $flightRepo;
private $moduleSvc;
private $subfleetRepo;
private $geoSvc;
private $userRepo;
/**
* @param AirlineRepository $airlineRepo
* @param AirportRepository $airportRepo
* @param FlightRepository $flightRepo
* @param GeoService $geoSvc
* @param ModuleService $moduleSvc
* @param SubfleetRepository $subfleetRepo
* @param UserRepository $userRepo
*/
public function __construct(
AirlineRepository $airlineRepo,
AirportRepository $airportRepo,
FlightRepository $flightRepo,
GeoService $geoSvc,
ModuleService $moduleSvc,
SubfleetRepository $subfleetRepo,
UserRepository $userRepo
) {
$this->airlineRepo = $airlineRepo;
$this->airportRepo = $airportRepo;
$this->flightRepo = $flightRepo;
$this->geoSvc = $geoSvc;
$this->moduleSvc = $moduleSvc;
$this->subfleetRepo = $subfleetRepo;
$this->userRepo = $userRepo;
}
/**
* @param Request $request
*
* @throws \Prettus\Repository\Exceptions\RepositoryException
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(Request $request)
{
return $this->search($request);
}
/**
* Make a search request using the Repository search
*
* @param Request $request
*
* @throws \Prettus\Repository\Exceptions\RepositoryException
*
* @return mixed
*/
public function search(Request $request)
{
$where = [
'active' => true,
'visible' => true,
];
/** @var \App\Models\User $user */
$user = Auth::user();
$user->loadMissing('current_airport');
if (setting('pilots.restrict_to_company')) {
$where['airline_id'] = $user->airline_id;
}
// default restrictions on the flights shown. Handle search differently
if (setting('pilots.only_flights_from_current')) {
$where['dpt_airport_id'] = $user->curr_airport_id;
}
$this->flightRepo->resetCriteria();
try {
$this->flightRepo->searchCriteria($request);
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where, [
'airline' => ['active' => true],
]));
$this->flightRepo->pushCriteria(new RequestCriteria($request));
} catch (RepositoryException $e) {
Log::emergency($e);
}
// Get only used Flight Types for the search form
// And filter according to settings
$usedtypes = Flight::select('flight_type')
->where($where)
->groupby('flight_type')
->orderby('flight_type')
->get();
// Build collection with type codes and labels
$flight_types = collect('', '');
foreach ($usedtypes as $ftype) {
$flight_types->put($ftype->flight_type, FlightType::label($ftype->flight_type));
}
$flights = $this->flightRepo->searchCriteria($request)
->with([
'airline',
'alt_airport',
'arr_airport',
'dpt_airport',
'subfleets.airline',
'simbrief' => function ($query) use ($user) {
$query->where('user_id', $user->id);
}, ])
->orderBy('flight_number')
->orderBy('route_leg')
->paginate();
$saved_flights = [];
$bids = Bid::where('user_id', Auth::id())->get();
foreach ($bids as $bid) {
if (!$bid->flight) {
$bid->delete();
continue;
}
$saved_flights[$bid->flight_id] = $bid->id;
}
return view('flights.index', [
'user' => $user,
'airlines' => $this->airlineRepo->selectBoxList(true),
'airports' => $this->airportRepo->selectBoxList(true),
'flights' => $flights,
'saved' => $saved_flights,
'subfleets' => $this->subfleetRepo->selectBoxList(true),
'flight_number' => $request->input('flight_number'),
'flight_types' => $flight_types,
'flight_type' => $request->input('flight_type'),
'arr_icao' => $request->input('arr_icao'),
'dep_icao' => $request->input('dep_icao'),
'subfleet_id' => $request->input('subfleet_id'),
'simbrief' => !empty(setting('simbrief.api_key')),
'simbrief_bids' => setting('simbrief.only_bids'),
'acars_plugin' => $this->moduleSvc->isModuleActive('VMSAcars'),
]);
}
/**
* Find the user's bids and display them
*
* @param Request $request
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function bids(Request $request)
{
$user = $this->userRepo
->with(['bids', 'bids.flight'])
->find(Auth::user()->id);
$flights = collect();
$saved_flights = [];
foreach ($user->bids as $bid) {
// Remove any invalid bids (flight doesn't exist or something)
if (!$bid->flight) {
$bid->delete();
continue;
}
$flights->add($bid->flight);
$saved_flights[$bid->flight_id] = $bid->id;
}
return view('flights.bids', [
'user' => $user,
'airlines' => $this->airlineRepo->selectBoxList(true),
'airports' => $this->airportRepo->selectBoxList(true),
'flights' => $flights,
'saved' => $saved_flights,
'subfleets' => $this->subfleetRepo->selectBoxList(true),
'simbrief' => !empty(setting('simbrief.api_key')),
'simbrief_bids' => setting('simbrief.only_bids'),
'acars_plugin' => $this->moduleSvc->isModuleActive('VMSAcars'),
]);
}
/**
* Show the flight information page
*
* @param $id
*
* @return mixed
*/
public function show($id)
{
$user_id = Auth::id();
$with_flight = [
'airline',
'alt_airport',
'arr_airport',
'dpt_airport',
'subfleets.airline',
'simbrief' => function ($query) use ($user_id) {
$query->where('user_id', $user_id);
},
];
$flight = $this->flightRepo->with($with_flight)->find($id);
if (empty($flight)) {
Flash::error('Flight not found!');
return redirect(route('frontend.dashboard.index'));
}
$map_features = $this->geoSvc->flightGeoJson($flight);
// See if the user has a bid for this flight
$bid = Bid::where(['user_id' => $user_id, 'flight_id' => $flight->id])->first();
return view('flights.show', [
'flight' => $flight,
'map_features' => $map_features,
'bid' => $bid,
'acars_plugin' => $this->moduleSvc->isModuleActive('VMSAcars'),
]);
}
}