2017-08-03 04:12:26 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Frontend;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Controller;
|
2018-02-28 03:25:32 +08:00
|
|
|
use App\Models\Bid;
|
2017-12-05 04:21:46 +08:00
|
|
|
use App\Repositories\AirlineRepository;
|
|
|
|
use App\Repositories\AirportRepository;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Repositories\Criteria\WhereCriteria;
|
|
|
|
use App\Repositories\FlightRepository;
|
2020-01-17 06:36:03 +08:00
|
|
|
use App\Repositories\SubfleetRepository;
|
2020-03-27 23:49:19 +08:00
|
|
|
use App\Repositories\UserRepository;
|
2017-12-21 09:12:39 +08:00
|
|
|
use App\Services\GeoService;
|
2017-08-03 04:12:26 +08:00
|
|
|
use Illuminate\Http\Request;
|
2017-08-03 04:29:04 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2020-01-17 06:36:03 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-03-27 23:49:19 +08:00
|
|
|
use Laracasts\Flash\Flash;
|
2019-05-11 05:03:04 +08:00
|
|
|
use Prettus\Repository\Criteria\RequestCriteria;
|
2017-12-05 00:59:25 +08:00
|
|
|
use Prettus\Repository\Exceptions\RepositoryException;
|
2017-08-03 04:12:26 +08:00
|
|
|
|
2018-01-12 11:35:03 +08:00
|
|
|
class FlightController extends Controller
|
2017-08-03 04:12:26 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
private $airlineRepo;
|
|
|
|
private $airportRepo;
|
|
|
|
private $flightRepo;
|
2020-01-17 06:36:03 +08:00
|
|
|
private $subfleetRepo;
|
2018-08-27 00:40:04 +08:00
|
|
|
private $geoSvc;
|
2020-03-27 23:49:19 +08:00
|
|
|
private $userRepo;
|
2017-12-05 04:21:46 +08:00
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
2020-01-17 06:36:03 +08:00
|
|
|
* @param AirlineRepository $airlineRepo
|
|
|
|
* @param AirportRepository $airportRepo
|
|
|
|
* @param FlightRepository $flightRepo
|
|
|
|
* @param GeoService $geoSvc
|
|
|
|
* @param SubfleetRepository $subfleetRepo
|
2020-03-27 23:49:19 +08:00
|
|
|
* @param UserRepository $userRepo
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2017-12-05 04:21:46 +08:00
|
|
|
public function __construct(
|
|
|
|
AirlineRepository $airlineRepo,
|
|
|
|
AirportRepository $airportRepo,
|
2017-12-21 09:12:39 +08:00
|
|
|
FlightRepository $flightRepo,
|
2020-01-17 06:36:03 +08:00
|
|
|
GeoService $geoSvc,
|
2020-03-27 23:49:19 +08:00
|
|
|
SubfleetRepository $subfleetRepo,
|
|
|
|
UserRepository $userRepo
|
2018-03-21 00:28:06 +08:00
|
|
|
) {
|
2017-12-05 04:21:46 +08:00
|
|
|
$this->airlineRepo = $airlineRepo;
|
|
|
|
$this->airportRepo = $airportRepo;
|
2017-08-03 04:12:26 +08:00
|
|
|
$this->flightRepo = $flightRepo;
|
2017-12-21 09:12:39 +08:00
|
|
|
$this->geoSvc = $geoSvc;
|
2020-01-17 06:36:03 +08:00
|
|
|
$this->subfleetRepo = $subfleetRepo;
|
2020-03-27 23:49:19 +08:00
|
|
|
$this->userRepo = $userRepo;
|
2017-08-03 04:12:26 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2020-01-17 06:36:03 +08:00
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
2017-08-03 04:12:26 +08:00
|
|
|
public function index(Request $request)
|
2020-01-17 06:36:03 +08:00
|
|
|
{
|
|
|
|
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)
|
2017-08-03 04:12:26 +08:00
|
|
|
{
|
2018-03-21 00:28:06 +08:00
|
|
|
$where = [
|
2020-05-23 23:48:48 +08:00
|
|
|
'active' => true,
|
|
|
|
'visible' => true,
|
2018-03-21 00:28:06 +08:00
|
|
|
];
|
2018-07-13 11:29:50 +08:00
|
|
|
|
2020-05-23 23:48:48 +08:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = Auth::user();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if (setting('pilots.restrict_to_company')) {
|
2020-05-23 23:48:48 +08:00
|
|
|
$where['airline_id'] = $user->airline_id;
|
2018-05-19 08:03:06 +08:00
|
|
|
}
|
2017-08-03 04:12:26 +08:00
|
|
|
|
2017-08-03 04:29:04 +08:00
|
|
|
// default restrictions on the flights shown. Handle search differently
|
2018-01-04 10:56:46 +08:00
|
|
|
if (setting('pilots.only_flights_from_current')) {
|
2020-05-23 23:48:48 +08:00
|
|
|
$where['dpt_airport_id'] = $user->curr_airport_id;
|
2017-08-03 04:29:04 +08:00
|
|
|
}
|
|
|
|
|
2020-01-17 06:36:03 +08:00
|
|
|
$this->flightRepo->resetCriteria();
|
|
|
|
|
2017-12-05 00:59:25 +08:00
|
|
|
try {
|
2020-01-17 06:36:03 +08:00
|
|
|
$this->flightRepo->searchCriteria($request);
|
2020-05-23 23:48:48 +08:00
|
|
|
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where, [
|
|
|
|
'airline' => ['active' => true],
|
|
|
|
]));
|
|
|
|
|
2019-05-11 05:03:04 +08:00
|
|
|
$this->flightRepo->pushCriteria(new RequestCriteria($request));
|
2017-12-05 00:59:25 +08:00
|
|
|
} catch (RepositoryException $e) {
|
|
|
|
Log::emergency($e);
|
|
|
|
}
|
|
|
|
|
2020-01-17 06:36:03 +08:00
|
|
|
$flights = $this->flightRepo->searchCriteria($request)
|
2018-08-26 21:43:47 +08:00
|
|
|
->with(['dpt_airport', 'arr_airport', 'airline'])
|
2018-03-23 11:50:41 +08:00
|
|
|
->orderBy('flight_number', 'asc')
|
2018-05-19 06:28:34 +08:00
|
|
|
->orderBy('route_leg', 'asc')
|
2018-04-04 11:45:45 +08:00
|
|
|
->paginate();
|
2017-08-03 04:29:04 +08:00
|
|
|
|
2018-02-28 03:25:32 +08:00
|
|
|
$saved_flights = Bid::where('user_id', Auth::id())
|
2018-03-20 09:50:40 +08:00
|
|
|
->pluck('flight_id')->toArray();
|
2017-08-04 10:02:02 +08:00
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('flights.index', [
|
2020-01-17 06:36:03 +08:00
|
|
|
'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'),
|
|
|
|
'arr_icao' => $request->input('arr_icao'),
|
|
|
|
'dep_icao' => $request->input('dep_icao'),
|
|
|
|
'subfleet_id' => $request->input('subfleet_id'),
|
2020-03-23 21:31:35 +08:00
|
|
|
'simbrief' => !empty(setting('simbrief.api_key')),
|
2020-03-27 23:49:19 +08:00
|
|
|
'simbrief_bids' => setting('simbrief.only_bids'),
|
2017-08-03 04:12:26 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:28:06 +08:00
|
|
|
/**
|
|
|
|
* Find the user's bids and display them
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 00:28:06 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 00:28:06 +08:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function bids(Request $request)
|
|
|
|
{
|
2020-03-27 23:49:19 +08:00
|
|
|
$user = $this->userRepo
|
|
|
|
->with(['bids', 'bids.flight'])
|
|
|
|
->find(Auth::user()->id);
|
|
|
|
|
|
|
|
$flights = collect();
|
|
|
|
$saved_flights = [];
|
|
|
|
foreach ($user->bids as $bid) {
|
|
|
|
$flights->add($bid->flight);
|
|
|
|
$saved_flights[] = $bid->flight->id;
|
|
|
|
}
|
2018-03-21 00:28:06 +08:00
|
|
|
|
2020-03-27 23:49:19 +08:00
|
|
|
return view('flights.bids', [
|
|
|
|
'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'),
|
2018-03-21 00:28:06 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-21 09:12:39 +08:00
|
|
|
/**
|
|
|
|
* Show the flight information page
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2020-01-17 06:36:03 +08:00
|
|
|
* @return mixed
|
2017-12-21 09:12:39 +08:00
|
|
|
*/
|
2017-08-04 10:02:02 +08:00
|
|
|
public function show($id)
|
|
|
|
{
|
2017-12-21 09:12:39 +08:00
|
|
|
$flight = $this->flightRepo->find($id);
|
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found!');
|
|
|
|
return redirect(route('frontend.dashboard.index'));
|
|
|
|
}
|
2017-08-04 10:02:02 +08:00
|
|
|
|
2017-12-25 07:38:38 +08:00
|
|
|
$map_features = $this->geoSvc->flightGeoJson($flight);
|
2017-12-21 09:12:39 +08:00
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('flights.show', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'flight' => $flight,
|
2017-12-25 07:38:38 +08:00
|
|
|
'map_features' => $map_features,
|
2017-12-21 09:12:39 +08:00
|
|
|
]);
|
2017-08-04 10:02:02 +08:00
|
|
|
}
|
2017-08-03 04:12:26 +08:00
|
|
|
}
|