2017-06-29 08:54:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Http\Requests\CreatePirepRequest;
|
|
|
|
use App\Http\Requests\UpdatePirepRequest;
|
2017-07-14 11:09:38 +08:00
|
|
|
use App\Repositories\AircraftRepository;
|
2017-12-04 05:29:34 +08:00
|
|
|
use App\Repositories\AirlineRepository;
|
|
|
|
use App\Repositories\AirportRepository;
|
2017-06-29 08:54:05 +08:00
|
|
|
use App\Repositories\PirepRepository;
|
2017-12-03 14:48:33 +08:00
|
|
|
use App\Services\PIREPService;
|
2017-06-29 08:54:05 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Flash;
|
2017-12-03 14:48:33 +08:00
|
|
|
use Log;
|
2017-06-29 08:54:05 +08:00
|
|
|
use Prettus\Repository\Criteria\RequestCriteria;
|
|
|
|
use Response;
|
2017-12-04 05:29:34 +08:00
|
|
|
use App\Facades\Utils;
|
|
|
|
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
class PirepController extends BaseController
|
|
|
|
{
|
2017-12-04 05:29:34 +08:00
|
|
|
private $airportRepo,
|
|
|
|
$airlineRepo,
|
|
|
|
$pirepRepo,
|
|
|
|
$aircraftRepo,
|
|
|
|
$pirepSvc;
|
2017-06-29 08:54:05 +08:00
|
|
|
|
2017-12-03 14:48:33 +08:00
|
|
|
public function __construct(
|
2017-12-04 05:29:34 +08:00
|
|
|
AirportRepository $airportRepo,
|
|
|
|
AirlineRepository $airlineRepo,
|
2017-12-03 14:48:33 +08:00
|
|
|
AircraftRepository $aircraftRepo,
|
|
|
|
PirepRepository $pirepRepo,
|
|
|
|
PIREPService $pirepSvc
|
|
|
|
) {
|
2017-12-04 05:29:34 +08:00
|
|
|
$this->airportRepo = $airportRepo;
|
|
|
|
$this->airlineRepo = $airlineRepo;
|
2017-07-14 11:09:38 +08:00
|
|
|
$this->aircraftRepo = $aircraftRepo;
|
|
|
|
$this->pirepRepo = $pirepRepo;
|
2017-12-03 14:48:33 +08:00
|
|
|
$this->pirepSvc = $pirepSvc;
|
2017-07-14 11:09:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function aircraftList()
|
|
|
|
{
|
|
|
|
$retval = [];
|
|
|
|
$all_aircraft = $this->aircraftRepo->all();
|
|
|
|
|
|
|
|
foreach ($all_aircraft as $ac) {
|
|
|
|
$retval[$ac->id] = $ac->subfleet->name.' - '.$ac->name.' ('.$ac->registration.')';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $retval;
|
2017-06-29 08:54:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
2017-12-02 00:53:33 +08:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
2017-06-29 08:54:05 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2017-08-25 02:37:54 +08:00
|
|
|
$criterea = new RequestCriteria($request);
|
|
|
|
$this->pirepRepo->pushCriteria($criterea);
|
|
|
|
|
2017-12-03 14:48:33 +08:00
|
|
|
$pireps = $this->pirepRepo
|
|
|
|
->orderBy('created_at', 'desc')
|
2017-12-02 00:44:35 +08:00
|
|
|
->paginate();
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
return view('admin.pireps.index', [
|
|
|
|
'pireps' => $pireps
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-04 00:55:01 +08:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
|
|
|
*/
|
|
|
|
public function pending(Request $request)
|
|
|
|
{
|
|
|
|
$criterea = new RequestCriteria($request);
|
|
|
|
$this->pirepRepo->pushCriteria($criterea);
|
|
|
|
|
|
|
|
$pireps = $this->pirepRepo
|
|
|
|
->findWhere(['status' => config('enums.pirep_status.PENDING')])
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
->paginate();
|
|
|
|
|
|
|
|
return view('admin.pireps.index', [
|
|
|
|
'pireps' => $pireps
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-06-29 08:54:05 +08:00
|
|
|
/**
|
|
|
|
* Show the form for creating a new Pirep.
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return view('admin.pireps.create');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CreatePirepRequest $request
|
2017-12-02 00:53:33 +08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2017-06-29 08:54:05 +08:00
|
|
|
*/
|
|
|
|
public function store(CreatePirepRequest $request)
|
|
|
|
{
|
|
|
|
$input = $request->all();
|
2017-07-14 11:09:38 +08:00
|
|
|
$pirep = $this->pirepRepo->create($input);
|
2017-06-29 08:54:05 +08:00
|
|
|
|
2017-12-14 00:56:26 +08:00
|
|
|
$pirep->flight_time = ((int) Utils::hoursToMinutes($request['hours']))
|
|
|
|
+ ((int) $request['minutes']);
|
|
|
|
|
2017-06-29 08:54:05 +08:00
|
|
|
Flash::success('Pirep saved successfully.');
|
|
|
|
return redirect(route('admin.pireps.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified Pirep.
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2017-08-25 02:37:54 +08:00
|
|
|
$pirep = $this->pirepRepo->find($id);
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
if (empty($pirep)) {
|
|
|
|
Flash::error('Pirep not found');
|
|
|
|
return redirect(route('admin.pireps.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.pireps.show', [
|
|
|
|
'pirep' => $pirep,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified Pirep.
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
2017-07-14 11:09:38 +08:00
|
|
|
$pirep = $this->pirepRepo->findWithoutFail($id);
|
2017-06-29 08:54:05 +08:00
|
|
|
if (empty($pirep)) {
|
|
|
|
Flash::error('Pirep not found');
|
|
|
|
return redirect(route('admin.pireps.index'));
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:56:26 +08:00
|
|
|
$hms = Utils::minutesToTimeParts($pirep->flight_time);
|
2017-12-04 05:29:34 +08:00
|
|
|
$pirep->hours = $hms['h'];
|
|
|
|
$pirep->minutes = $hms['m'];
|
|
|
|
|
2017-06-29 08:54:05 +08:00
|
|
|
return view('admin.pireps.edit', [
|
|
|
|
'pirep' => $pirep,
|
2017-12-04 05:29:34 +08:00
|
|
|
'airports' => $this->airportRepo->selectBoxList(),
|
|
|
|
'airlines' => $this->airlineRepo->selectBoxList(),
|
|
|
|
'aircraft' => $this->aircraftRepo->selectBoxList(),
|
2017-06-29 08:54:05 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-02 00:53:33 +08:00
|
|
|
* @param $id
|
2017-06-29 08:54:05 +08:00
|
|
|
* @param UpdatePirepRequest $request
|
2017-12-02 00:53:33 +08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2017-06-29 08:54:05 +08:00
|
|
|
*/
|
|
|
|
public function update($id, UpdatePirepRequest $request)
|
|
|
|
{
|
2017-07-14 11:09:38 +08:00
|
|
|
$pirep = $this->pirepRepo->findWithoutFail($id);
|
2017-06-29 08:54:05 +08:00
|
|
|
|
2017-12-14 00:56:26 +08:00
|
|
|
$pirep->flight_time = ((int) Utils::hoursToMinutes($request['hours']))
|
|
|
|
+ ((int) $request['minutes']);
|
2017-12-04 05:29:34 +08:00
|
|
|
|
2017-06-29 08:54:05 +08:00
|
|
|
if (empty($pirep)) {
|
|
|
|
Flash::error('Pirep not found');
|
|
|
|
return redirect(route('admin.pireps.index'));
|
|
|
|
}
|
|
|
|
|
2017-07-14 11:09:38 +08:00
|
|
|
$pirep = $this->pirepRepo->update($request->all(), $id);
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
Flash::success('Pirep updated successfully.');
|
|
|
|
return redirect(route('admin.pireps.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified Pirep from storage.
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2017-07-14 11:09:38 +08:00
|
|
|
$pirep = $this->pirepRepo->findWithoutFail($id);
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
if (empty($pirep)) {
|
|
|
|
Flash::error('Pirep not found');
|
|
|
|
return redirect(route('admin.pireps.index'));
|
|
|
|
}
|
|
|
|
|
2017-07-14 11:09:38 +08:00
|
|
|
$this->pirepRepo->delete($id);
|
2017-06-29 08:54:05 +08:00
|
|
|
|
|
|
|
Flash::success('Pirep deleted successfully.');
|
|
|
|
return redirect(route('admin.pireps.index'));
|
|
|
|
}
|
2017-12-03 14:48:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Change or update the PIREP status
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function status(Request $request)
|
|
|
|
{
|
|
|
|
Log::info('PIREP status update call', [$request->toArray()]);
|
|
|
|
|
|
|
|
$pirep = $this->pirepRepo->findWithoutFail($request->id);
|
|
|
|
if($request->isMethod('post')) {
|
|
|
|
$new_status = (int) $request->new_status;
|
|
|
|
$pirep = $this->pirepSvc->changeStatus($pirep, $new_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pirep->refresh();
|
|
|
|
return view('admin.pireps.pirep_card', ['pirep' => $pirep]);
|
|
|
|
}
|
2017-06-29 08:54:05 +08:00
|
|
|
}
|