phpvms/app/Http/Controllers/Frontend/FlightController.php

190 lines
5.5 KiB
PHP
Raw Normal View History

2017-08-03 04:12:26 +08:00
<?php
namespace App\Http\Controllers\Frontend;
use App\Interfaces\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;
use App\Services\GeoService;
use Flash;
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;
2018-02-21 12:33:09 +08:00
use Log;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Exceptions\RepositoryException;
2017-08-03 04:12:26 +08:00
/**
* Class FlightController
*/
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;
private $geoSvc;
2017-12-05 04:21:46 +08:00
2018-02-21 12:33:09 +08:00
/**
* FlightController constructor.
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @param AirlineRepository $airlineRepo
* @param AirportRepository $airportRepo
* @param FlightRepository $flightRepo
* @param GeoService $geoSvc
2018-02-21 12:33:09 +08:00
*/
2017-12-05 04:21:46 +08:00
public function __construct(
AirlineRepository $airlineRepo,
AirportRepository $airportRepo,
FlightRepository $flightRepo,
GeoService $geoSvc
) {
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;
$this->geoSvc = $geoSvc;
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
*
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)
{
$where = [
'active' => true,
'visible' => true,
];
2018-08-27 00:40:04 +08:00
if (setting('pilots.restrict_to_company')) {
$where['airline_id'] = Auth::user()->airline_id;
}
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
if (setting('pilots.only_flights_from_current')) {
2017-08-03 04:29:04 +08:00
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
}
try {
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
$this->flightRepo->pushCriteria(new RequestCriteria($request));
} catch (RepositoryException $e) {
Log::emergency($e);
}
2018-03-23 11:50:41 +08:00
$flights = $this->flightRepo
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')
->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())
->pluck('flight_id')->toArray();
2017-08-04 10:02:02 +08:00
return view('flights.index', [
2017-12-05 04:21:46 +08:00
'airlines' => $this->airlineRepo->selectBoxList(true),
'airports' => $this->airportRepo->selectBoxList(true),
'flights' => $flights,
'saved' => $saved_flights,
2017-08-03 04:12:26 +08:00
]);
}
/**
* Find the user's bids and display them
2018-08-27 00:40:04 +08:00
*
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function bids(Request $request)
{
$user = Auth::user();
$flights = $user->flights()->paginate();
$saved_flights = $flights->pluck('id')->toArray();
return view('flights.index', [
2018-05-22 01:05:22 +08:00
'title' => trans_choice('flights.mybid', 2),
'airlines' => $this->airlineRepo->selectBoxList(true),
'airports' => $this->airportRepo->selectBoxList(true),
'flights' => $flights,
'saved' => $saved_flights,
]);
}
/**
* Make a search request using the Repository search
2018-08-27 00:40:04 +08:00
*
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Repository\Exceptions\RepositoryException
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
2017-08-04 10:02:02 +08:00
public function search(Request $request)
2017-08-03 04:12:26 +08:00
{
2018-09-02 22:20:52 +08:00
$where = [
'active' => true,
'visible' => true,
];
2018-08-27 00:40:04 +08:00
if (setting('pilots.restrict_to_company')) {
2018-09-02 22:20:52 +08:00
$where['airline_id'] = Auth::user()->airline_id;
}
// default restrictions on the flights shown. Handle search differently
if (setting('pilots.only_flights_from_current')) {
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
}
2019-05-12 00:37:06 +08:00
$this->flightRepo->resetCriteria();
2018-09-02 22:20:52 +08:00
try {
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
} catch (RepositoryException $e) {
Log::emergency($e);
}
2018-05-19 06:28:34 +08:00
$flights = $this->flightRepo->searchCriteria($request)
2018-09-02 22:20:52 +08:00
->with(['dpt_airport', 'arr_airport', 'airline'])
->orderBy('flight_number', 'asc')
->orderBy('route_leg', 'asc')
->paginate();
2018-02-28 03:25:32 +08:00
$saved_flights = Bid::where('user_id', Auth::id())
->pluck('flight_id')->toArray();
2017-08-04 10:02:02 +08:00
return view('flights.index', [
2017-12-05 04:21:46 +08:00
'airlines' => $this->airlineRepo->selectBoxList(true),
'airports' => $this->airportRepo->selectBoxList(true),
'flights' => $flights,
'saved' => $saved_flights,
2017-08-03 04:29:04 +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
*
2018-02-21 12:33:09 +08:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
*/
2017-08-04 10:02:02 +08:00
public function show($id)
{
$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
$map_features = $this->geoSvc->flightGeoJson($flight);
return view('flights.show', [
'flight' => $flight,
'map_features' => $map_features,
]);
2017-08-04 10:02:02 +08:00
}
2017-08-03 04:12:26 +08:00
}