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

342 lines
9.9 KiB
PHP
Raw Normal View History

2017-08-04 23:04:00 +08:00
<?php
namespace App\Http\Controllers\Frontend;
use App\Facades\Utils;
2018-02-21 12:33:09 +08:00
use App\Http\Controllers\Controller;
use App\Http\Requests\CreatePirepRequest;
use App\Http\Requests\UpdatePirepRequest;
use App\Models\Enums\PirepSource;
use App\Models\Enums\PirepState;
2017-08-09 09:34:36 +08:00
use App\Models\Pirep;
use App\Repositories\AircraftRepository;
2017-08-17 07:18:47 +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\PirepFieldRepository;
2018-02-21 12:33:09 +08:00
use App\Repositories\PirepRepository;
use App\Services\FareService;
2018-02-21 12:33:09 +08:00
use App\Services\GeoService;
2018-03-07 07:36:06 +08:00
use App\Services\PirepService;
2018-02-21 12:33:09 +08:00
use App\Services\UserService;
use App\Support\Units\Time;
use Flash;
2018-02-21 12:33:09 +08:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Log;
2017-08-04 23:04:00 +08:00
2017-08-17 07:18:47 +08:00
class PirepController extends Controller
2017-08-04 23:04:00 +08:00
{
private $aircraftRepo,
$airlineRepo,
$fareSvc,
$geoSvc,
$pirepRepo,
$airportRepo,
2017-12-04 05:29:34 +08:00
$pirepFieldRepo,
$pirepSvc,
$userSvc;
2018-02-21 12:33:09 +08:00
/**
* PirepController constructor.
* @param AircraftRepository $aircraftRepo
2018-02-21 12:33:09 +08:00
* @param AirlineRepository $airlineRepo
* @param AirportRepository $airportRepo
* @param FareService $fareSvc
* @param GeoService $geoSvc
* @param PirepRepository $pirepRepo
2018-02-21 12:33:09 +08:00
* @param PirepFieldRepository $pirepFieldRepo
2018-03-07 07:36:06 +08:00
* @param PirepService $pirepSvc
2018-02-21 12:33:09 +08:00
* @param UserService $userSvc
*/
2017-08-17 07:18:47 +08:00
public function __construct(
AircraftRepository $aircraftRepo,
2017-08-17 07:18:47 +08:00
AirlineRepository $airlineRepo,
AirportRepository $airportRepo,
FareService $fareSvc,
GeoService $geoSvc,
PirepRepository $pirepRepo,
2017-12-04 05:29:34 +08:00
PirepFieldRepository $pirepFieldRepo,
2018-03-07 07:36:06 +08:00
PirepService $pirepSvc,
UserService $userSvc
) {
$this->aircraftRepo = $aircraftRepo;
2017-08-17 07:18:47 +08:00
$this->airlineRepo = $airlineRepo;
$this->pirepRepo = $pirepRepo;
$this->airportRepo = $airportRepo;
$this->pirepFieldRepo = $pirepFieldRepo;
$this->fareSvc = $fareSvc;
$this->geoSvc = $geoSvc;
2017-12-04 05:29:34 +08:00
$this->pirepSvc = $pirepSvc;
$this->userSvc = $userSvc;
2017-08-17 07:18:47 +08:00
}
/**
* Dropdown with aircraft grouped by subfleet
* @param null $user
* @return array
*/
public function aircraftList($user=null, $add_blank=false)
{
$aircraft = [];
$subfleets = $this->userSvc->getAllowableSubfleets($user);
if($add_blank) {
$aircraft[''] = '';
}
foreach ($subfleets as $subfleet) {
$tmp = [];
foreach ($subfleet->aircraft as $ac) {
$tmp[$ac->id] = $ac['name'] . ' - ' . $ac['registration'];
}
$aircraft[$subfleet->name] = $tmp;
}
return $aircraft;
}
/**
* Save any custom fields found
* @param Pirep $pirep
* @param Request $request
*/
protected function saveCustomFields(Pirep $pirep, Request $request)
{
$custom_fields = [];
$pirep_fields = $this->pirepFieldRepo->all();
foreach ($pirep_fields as $field) {
if (!$request->filled($field->slug)) {
continue;
}
$custom_fields[] = [
'name' => $field->name,
'value' => $request->input($field->slug),
'source' => PirepSource::MANUAL
];
}
Log::info('PIREP Custom Fields', $custom_fields);
$this->pirepSvc->updateCustomFields($pirep->id, $custom_fields);
}
/**
* Save the fares that have been specified/saved
* @param Pirep $pirep
* @param Request $request
* @throws \Exception
*/
protected function saveFares(Pirep $pirep, Request $request)
{
$fares = [];
foreach($pirep->aircraft->subfleet->fares as $fare) {
$field_name = 'fare_'.$fare->id;
if(!$request->filled($field_name)) {
$count = 0;
} else {
$count = $request->input($field_name);
}
$fares[] = [
'fare_id' => $fare->id,
'count' => $count,
];
}
$this->fareSvc->saveForPirep($pirep, $fares);
}
/**
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Prettus\Repository\Exceptions\RepositoryException
*/
2017-08-17 07:18:47 +08:00
public function index(Request $request)
2017-08-04 23:04:00 +08:00
{
2017-08-09 09:34:36 +08:00
$user = Auth::user();
2018-01-03 23:54:32 +08:00
$where = [['user_id', $user->id]];
2018-02-11 03:25:40 +08:00
$where[] = ['state', '<>', PirepState::CANCELLED];
2018-01-03 23:54:32 +08:00
$this->pirepRepo->pushCriteria(new WhereCriteria($request, $where));
$pireps = $this->pirepRepo->orderBy('created_at', 'desc')->paginate();
2017-08-09 09:34:36 +08:00
return view('pireps.index', [
2017-08-09 09:34:36 +08:00
'user' => $user,
'pireps' => $pireps,
2017-08-04 23:04:00 +08:00
]);
}
2017-08-09 09:34:36 +08:00
/**
* @param $id
* @return mixed
*/
public function show($id)
{
$pirep = $this->pirepRepo->find($id);
if (empty($pirep)) {
Flash::error('Pirep not found');
return redirect(route('frontend.pirep.index'));
}
$map_features = $this->geoSvc->pirepGeoJson($pirep);
return view('pireps.show', [
'pirep' => $pirep,
'map_features' => $map_features,
]);
}
/**
* Return the fares form for a given aircraft
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function fares(Request $request)
{
$aircraft_id = $request->input('aircraft_id');
$aircraft = $this->aircraftRepo->find($aircraft_id);
return view('pireps.fares', [
'aircraft' => $aircraft,
'read_only' => false,
]);
}
/**
* Create a new flight report
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
2017-08-09 09:34:36 +08:00
public function create()
{
$user = Auth::user();
return view('pireps.create', [
'aircraft' => null,
'read_only' => false,
'airline_list' => $this->airlineRepo->selectBoxList(true),
'aircraft_list' => $this->aircraftList($user, true),
'airport_list' => $this->airportRepo->selectBoxList(true),
'pirep_fields' => $this->pirepFieldRepo->all(),
'field_values' => [],
2017-08-17 07:18:47 +08:00
]);
2017-08-10 13:08:38 +08:00
}
/**
* @param CreatePirepRequest $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2018-02-21 12:33:09 +08:00
* @throws \Exception
*/
public function store(CreatePirepRequest $request)
2017-08-10 13:08:38 +08:00
{
// Create the main PIREP
$pirep = new Pirep($request->post());
$pirep->user_id = Auth::user()->id;
# Make sure this isn't a duplicate
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
if ($dupe_pirep !== false) {
flash()->error('This PIREP has already been filed.');
return redirect(route('frontend.pireps.create'))->withInput();
}
// Any special fields
$hours = (int) $request->input('hours', 0);
$minutes = (int) $request->input('minutes', 0);
$pirep->flight_time = Utils::hoursToMinutes($hours) + $minutes;
$pirep = $this->pirepSvc->create($pirep);
$this->saveCustomFields($pirep, $request);
$this->saveFares($pirep, $request);
$this->pirepSvc->saveRoute($pirep);
2017-08-10 13:08:38 +08:00
return redirect(route('frontend.pireps.show', ['id' => $pirep->id]));
2017-08-10 13:08:38 +08:00
}
2018-02-21 12:33:09 +08:00
/**
* Show the form for editing the specified Pirep.
* @param int $id
2018-02-21 12:33:09 +08:00
* @return mixed
*/
public function edit($id)
2017-08-10 13:08:38 +08:00
{
$pirep = $this->pirepRepo->findWithoutFail($id);
if (empty($pirep)) {
Flash::error('Pirep not found');
return redirect(route('frontend.pireps.index'));
}
$time = new Time($pirep->flight_time);
$pirep->hours = $time->hours;
$pirep->minutes = $time->minutes;
# Can we modify?
$read_only = $pirep->state !== PirepState::PENDING;
# set the custom fields
foreach($pirep->fields as $field) {
$pirep->{$field->slug} = $field->value;
}
# set the fares
foreach($pirep->fares as $fare) {
$field_name = 'fare_'.$fare->fare_id;
$pirep->{$field_name} = $fare->count;
}
return view('pireps.edit', [
2017-08-10 13:08:38 +08:00
'pirep' => $pirep,
'read_only' => $read_only,
'aircraft' => $pirep->aircraft,
'aircraft_list' => $this->aircraftList(),
'airline_list' => $this->airlineRepo->selectBoxList(),
'airport_list' => $this->airportRepo->selectBoxList(),
'pirep_fields' => $this->pirepFieldRepo->all(),
2017-08-10 13:08:38 +08:00
]);
2017-08-09 09:34:36 +08:00
}
/**
* @param $id
* @param UpdatePirepRequest $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Prettus\Validator\Exceptions\ValidatorException
* @throws \Exception
*/
public function update($id, UpdatePirepRequest $request)
{
$pirep = $this->pirepRepo->findWithoutFail($id);
if (empty($pirep)) {
Flash::error('Pirep not found');
return redirect(route('admin.pireps.index'));
}
$orig_route = $pirep->route;
$attrs = $request->all();
# Fix the time
$attrs['flight_time'] = Time::init(
$attrs['minutes'],
$attrs['hours'])->getMinutes();
$pirep = $this->pirepRepo->update($attrs, $id);
// A route change in the PIREP, so update the saved points in the ACARS table
if ($pirep->route !== $orig_route) {
$this->pirepSvc->saveRoute($pirep);
}
$this->saveCustomFields($pirep, $request);
$this->saveFares($pirep, $request);
Flash::success('Pirep updated successfully.');
return redirect(route('frontend.pireps.show', ['id' => $pirep->id]));
}
2017-08-04 23:04:00 +08:00
}