2017-06-18 06:25:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2017-07-12 06:44:12 +08:00
|
|
|
use App\Models\Airline;
|
|
|
|
use App\Models\FlightFields;
|
|
|
|
use App\Models\Airport;
|
2017-06-18 06:25:36 +08:00
|
|
|
use App\Http\Requests\CreateFlightRequest;
|
|
|
|
use App\Http\Requests\UpdateFlightRequest;
|
2017-12-01 10:28:45 +08:00
|
|
|
use App\Repositories\AirlineRepository;
|
|
|
|
use App\Repositories\AirportRepository;
|
2017-06-18 06:25:36 +08:00
|
|
|
use App\Repositories\FlightRepository;
|
2017-06-24 06:33:18 +08:00
|
|
|
use App\Repositories\SubfleetRepository;
|
2017-12-05 02:05:31 +08:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2017-06-18 06:25:36 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Flash;
|
|
|
|
use Prettus\Repository\Criteria\RequestCriteria;
|
|
|
|
use Response;
|
|
|
|
|
|
|
|
class FlightController extends BaseController
|
|
|
|
{
|
2017-12-02 01:11:03 +08:00
|
|
|
private $airlineRepo,
|
|
|
|
$airportRepo,
|
|
|
|
$flightRepo,
|
|
|
|
$subfleetRepo;
|
|
|
|
|
2017-06-20 03:29:10 +08:00
|
|
|
public function __construct(
|
2017-12-01 10:28:45 +08:00
|
|
|
AirlineRepository $airlineRepo,
|
|
|
|
AirportRepository $airportRepo,
|
2017-06-20 03:29:10 +08:00
|
|
|
FlightRepository $flightRepo,
|
2017-06-24 06:33:18 +08:00
|
|
|
SubfleetRepository $subfleetRepo
|
2017-12-01 10:28:45 +08:00
|
|
|
) {
|
|
|
|
$this->airlineRepo = $airlineRepo;
|
|
|
|
$this->airportRepo = $airportRepo;
|
|
|
|
$this->flightRepo = $flightRepo;
|
2017-06-24 06:33:18 +08:00
|
|
|
$this->subfleetRepo = $subfleetRepo;
|
2017-06-20 03:29:10 +08:00
|
|
|
}
|
|
|
|
|
2017-06-24 06:33:18 +08:00
|
|
|
protected function getAvailSubfleets($flight)
|
2017-06-20 03:29:10 +08:00
|
|
|
{
|
|
|
|
$retval = [];
|
|
|
|
|
|
|
|
$flight->refresh();
|
2017-06-24 06:33:18 +08:00
|
|
|
$all_aircraft = $this->subfleetRepo->all();
|
|
|
|
$avail_fleets = $all_aircraft->except($flight->subfleets->modelKeys());
|
2017-06-20 03:29:10 +08:00
|
|
|
|
2017-06-24 06:33:18 +08:00
|
|
|
foreach ($avail_fleets as $ac) {
|
|
|
|
$retval[$ac->id] = $ac->type.' - '.$ac->name;
|
2017-06-20 03:29:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $retval;
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
2017-12-02 01:11:03 +08:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2017-12-05 02:05:31 +08:00
|
|
|
$flights = $this->flightRepo->searchCriteria($request)->paginate();
|
2017-07-02 10:06:55 +08:00
|
|
|
return view('admin.flights.index', [
|
|
|
|
'flights' => $flights,
|
2017-12-05 02:05:31 +08:00
|
|
|
'airlines' => $this->airlineRepo->selectBoxList(true),
|
|
|
|
'airports' => $this->airportRepo->selectBoxList(true),
|
2017-07-02 10:06:55 +08:00
|
|
|
]);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new Flight.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2017-12-02 01:11:03 +08:00
|
|
|
return view('admin.flights.create', [
|
|
|
|
'flight' => null,
|
|
|
|
'airlines' => $this->airlineRepo->selectBoxList(),
|
|
|
|
'airports' => $this->airportRepo->selectBoxList(),
|
|
|
|
]);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CreateFlightRequest $request
|
2017-12-02 01:11:03 +08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
public function store(CreateFlightRequest $request)
|
|
|
|
{
|
|
|
|
$input = $request->all();
|
|
|
|
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->create($input);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
Flash::success('Flight saved successfully.');
|
2017-12-02 01:11:03 +08:00
|
|
|
return redirect(route('admin.flights.edit', $flight->id));
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-02 01:11:03 +08:00
|
|
|
* @param $id
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($id);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
2017-06-24 06:33:18 +08:00
|
|
|
$avail_subfleets = $this->getAvailSubfleets($flight);
|
2017-07-02 10:06:55 +08:00
|
|
|
return view('admin.flights.show', [
|
|
|
|
'flight' => $flight,
|
|
|
|
'avail_subfleets' => $avail_subfleets,
|
|
|
|
]);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-02 01:11:03 +08:00
|
|
|
* @param $id
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($id);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
2017-07-02 10:06:55 +08:00
|
|
|
$avail_subfleets = $this->getAvailSubfleets($flight);
|
|
|
|
return view('admin.flights.edit', [
|
|
|
|
'flight' => $flight,
|
2017-12-02 01:11:03 +08:00
|
|
|
'airlines' => $this->airlineRepo->selectBoxList(),
|
|
|
|
'airports' => $this->airportRepo->selectBoxList(),
|
2017-07-02 10:06:55 +08:00
|
|
|
'avail_subfleets' => $avail_subfleets,
|
|
|
|
]);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-02 01:11:03 +08:00
|
|
|
* @param $id
|
2017-06-18 06:25:36 +08:00
|
|
|
* @param UpdateFlightRequest $request
|
2017-12-02 01:11:03 +08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
public function update($id, UpdateFlightRequest $request)
|
|
|
|
{
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($id);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->update($request->all(), $id);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
Flash::success('Flight updated successfully.');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-02 01:11:03 +08:00
|
|
|
* @param $id
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2017-06-18 06:25:36 +08:00
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($id);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
2017-12-01 10:28:45 +08:00
|
|
|
$this->flightRepo->delete($id);
|
2017-06-18 06:25:36 +08:00
|
|
|
|
|
|
|
Flash::success('Flight deleted successfully.');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:11:03 +08:00
|
|
|
/**
|
|
|
|
* @param $flight
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
2017-07-12 06:44:12 +08:00
|
|
|
protected function return_fields_view($flight)
|
|
|
|
{
|
|
|
|
$flight->refresh();
|
|
|
|
return view('admin.flights.flight_fields', [
|
|
|
|
'flight' => $flight,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:11:03 +08:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
|
|
|
*/
|
2017-07-12 06:44:12 +08:00
|
|
|
public function fields(Request $request)
|
|
|
|
{
|
|
|
|
$id = $request->id;
|
|
|
|
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($id);
|
2017-07-12 06:44:12 +08:00
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// add custom field to flight
|
|
|
|
if ($request->isMethod('post')) {
|
|
|
|
$field = new FlightFields;
|
|
|
|
$field->flight_id = $id;
|
|
|
|
$field->name = $request->name;
|
|
|
|
$field->value = $request->value;
|
|
|
|
$field->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
elseif ($request->isMethod('put')) {
|
|
|
|
$field = FlightFields::where('id', $request->field_id)->first();
|
|
|
|
$field->value = $request->value;
|
|
|
|
$field->save();
|
|
|
|
// update the field value
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove custom field from flight
|
|
|
|
elseif ($request->isMethod('delete')) {
|
|
|
|
FlightFields::destroy($request->field_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->return_fields_view($flight);
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:11:03 +08:00
|
|
|
/**
|
|
|
|
* @param $flight
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
2017-06-24 06:33:18 +08:00
|
|
|
protected function return_subfleet_view($flight)
|
2017-06-20 03:29:10 +08:00
|
|
|
{
|
2017-06-24 06:33:18 +08:00
|
|
|
$avail_subfleets = $this->getAvailSubfleets($flight);
|
2017-07-02 10:06:55 +08:00
|
|
|
return view('admin.flights.subfleets', [
|
|
|
|
'flight' => $flight,
|
|
|
|
'avail_subfleets' => $avail_subfleets,
|
|
|
|
]);
|
2017-06-20 03:29:10 +08:00
|
|
|
}
|
|
|
|
|
2017-12-02 01:11:03 +08:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
|
|
|
*/
|
2017-06-24 06:33:18 +08:00
|
|
|
public function subfleets(Request $request)
|
2017-06-18 06:25:36 +08:00
|
|
|
{
|
|
|
|
$id = $request->id;
|
|
|
|
|
2017-12-01 10:28:45 +08:00
|
|
|
$flight = $this->flightRepo->findWithoutFail($id);
|
2017-06-18 06:25:36 +08:00
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found');
|
|
|
|
return redirect(route('admin.flights.index'));
|
|
|
|
}
|
|
|
|
|
2017-06-21 01:14:10 +08:00
|
|
|
// add aircraft to flight
|
2017-06-18 06:25:36 +08:00
|
|
|
if ($request->isMethod('post')) {
|
2017-06-24 06:33:18 +08:00
|
|
|
$flight->subfleets()->syncWithoutDetaching([$request->subfleet_id]);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
|
2017-06-21 01:14:10 +08:00
|
|
|
// remove aircraft from flight
|
2017-06-18 06:25:36 +08:00
|
|
|
elseif ($request->isMethod('delete')) {
|
2017-06-24 06:33:18 +08:00
|
|
|
$flight->subfleets()->detach($request->subfleet_id);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
2017-06-20 03:29:10 +08:00
|
|
|
|
2017-06-24 06:33:18 +08:00
|
|
|
return $this->return_subfleet_view($flight);
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|
|
|
|
}
|