Fix saving PIREPs as draft

This commit is contained in:
Nabeel Shahzad 2018-05-29 11:04:28 -05:00
parent e02ae682a8
commit d76b264238
12 changed files with 125 additions and 56 deletions

View File

@ -139,6 +139,10 @@ class PirepController extends Controller
protected function saveFares(Pirep $pirep, Request $request)
{
$fares = [];
if (!$pirep->aircraft) {
return;
}
foreach ($pirep->aircraft->subfleet->fares as $fare) {
$field_name = 'fare_'.$fare->id;
if (!$request->filled($field_name)) {
@ -186,7 +190,6 @@ class PirepController extends Controller
$pirep = $this->pirepRepo->find($id);
if (empty($pirep)) {
Flash::error('Pirep not found');
return redirect(route('frontend.pirep.index'));
}
@ -244,40 +247,45 @@ class PirepController extends Controller
$pirep = new Pirep($request->post());
$pirep->user_id = Auth::user()->id;
# Are they allowed at this airport?
if (setting('pilots.only_flights_from_current')
&& Auth::user()->curr_airport_id !== $pirep->dpt_airport_id) {
return $this->flashError(
'You are currently not at the departure airport!',
'frontend.pireps.create'
);
}
$attrs = $request->all();
$attrs['submit'] = strtolower($attrs['submit']);
# Can they fly this aircraft?
if (setting('pireps.restrict_aircraft_to_rank', false)
&& !$this->userSvc->aircraftAllowed(Auth::user(), $pirep->aircraft_id)) {
return $this->flashError(
'You are not allowed to fly this aircraft!',
'frontend.pireps.create'
);
}
if($attrs['submit'] === 'submit') {
# Are they allowed at this airport?
if (setting('pilots.only_flights_from_current')
&& Auth::user()->curr_airport_id !== $pirep->dpt_airport_id) {
return $this->flashError(
'You are currently not at the departure airport!',
'frontend.pireps.create'
);
}
# is the aircraft in the right place?
if (setting('pireps.only_aircraft_at_dpt_airport')
&& $pirep->aircraft_id !== $pirep->dpt_airport_id) {
return $this->flashError(
'This aircraft is not positioned at the departure airport!',
'frontend.pireps.create'
);
}
# Can they fly this aircraft?
if (setting('pireps.restrict_aircraft_to_rank', false)
&& !$this->userSvc->aircraftAllowed(Auth::user(), $pirep->aircraft_id)) {
return $this->flashError(
'You are not allowed to fly this aircraft!',
'frontend.pireps.create'
);
}
# Make sure this isn't a duplicate
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
if ($dupe_pirep !== false) {
return $this->flashError(
'This PIREP has already been filed.',
'frontend.pireps.create'
);
# is the aircraft in the right place?
if (setting('pireps.only_aircraft_at_dpt_airport')
&& $pirep->aircraft_id !== $pirep->dpt_airport_id) {
return $this->flashError(
'This aircraft is not positioned at the departure airport!',
'frontend.pireps.create'
);
}
# Make sure this isn't a duplicate
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
if ($dupe_pirep !== false) {
return $this->flashError(
'This PIREP has already been filed.',
'frontend.pireps.create'
);
}
}
// Any special fields
@ -296,7 +304,7 @@ class PirepController extends Controller
// Depending on the button they selected, set an initial state
// Can be saved as a draft or just submitted
if ($attrs['submit'] === 'save') {
$pirep->status = PirepState::DRAFT;
$pirep->state = PirepState::DRAFT;
$pirep->save();
Flash::success('PIREP saved successfully.');
} else if ($attrs['submit'] === 'submit') {
@ -317,7 +325,6 @@ class PirepController extends Controller
$pirep = $this->pirepRepo->findWithoutFail($id);
if (empty($pirep)) {
Flash::error('Pirep not found');
return redirect(route('frontend.pireps.index'));
}
@ -363,6 +370,7 @@ class PirepController extends Controller
$orig_route = $pirep->route;
$attrs = $request->all();
$attrs['submit'] = strtolower($attrs['submit']);
# Fix the time
$attrs['flight_time'] = Time::init(
@ -384,7 +392,7 @@ class PirepController extends Controller
} else if($attrs['submit'] === 'submit') {
$this->pirepSvc->submit($pirep);
Flash::success('PIREP submitted!');
} else if($attrs['submit'] === 'cancel') {
} else if($attrs['submit'] === 'delete' || $attrs['submit'] === 'cancel') {
$this->pirepRepo->update([
'state' => PirepState::CANCELLED,
'status' => PirepStatus::CANCELLED,

View File

@ -26,6 +26,17 @@ class CreatePirepRequest extends FormRequest
*/
public function rules()
{
// Don't run validations if it's just being saved
$action = strtolower(request('submit', 'submit'));
if($action === 'save') {
return [
'airline_id' => 'required|exists:airlines,id',
'flight_number' => 'required',
'dpt_airport_id' => 'required',
'arr_airport_id' => 'required',
];
}
$field_rules = Pirep::$rules;
$field_rules['hours'] = 'nullable|integer';

View File

@ -3,7 +3,9 @@
namespace App\Http\Requests;
use App\Models\Pirep;
use App\Repositories\PirepFieldRepository;
use Illuminate\Foundation\Http\FormRequest;
use Log;
class UpdatePirepRequest extends FormRequest
{
@ -24,6 +26,31 @@ class UpdatePirepRequest extends FormRequest
*/
public function rules()
{
return Pirep::$rules;
// Don't run validations if it's just being saved
$action = strtolower(request('submit', 'submit'));
if ($action === 'save' || $action === 'cancel' || $action === 'delete') {
return [
'airline_id' => 'required|exists:airlines,id',
'flight_number' => 'required',
'dpt_airport_id' => 'required',
'arr_airport_id' => 'required',
];
}
$field_rules = Pirep::$rules;
$field_rules['hours'] = 'nullable|integer';
$field_rules['minutes'] = 'nullable|integer';
# Add the validation rules for the custom fields
$pirepFieldRepo = app(PirepFieldRepository::class);
$custom_fields = $pirepFieldRepo->all();
foreach ($custom_fields as $field) {
Log::info('field:', $field->toArray());
$field_rules[$field->slug] = $field->required ? 'required' : 'nullable';
}
return $field_rules;
}
}

View File

@ -40,10 +40,11 @@ use PhpUnitsOfMeasure\Exception\NonStringUnitName;
* @property User user
* @property Flight|null flight
* @property Collection fields
* @property int status
* @property bool state
* @property Carbon submitted_at
* @property Carbon created_at
* @property Carbon updated_at
* @property bool state
* @property Acars position
* @property Acars[] acars
* @package App\Models

View File

@ -3,6 +3,7 @@
namespace App\Widgets;
use App\Interfaces\Widget;
use App\Models\Enums\PirepState;
use App\Repositories\PirepRepository;
/**
@ -22,9 +23,17 @@ class LatestPireps extends Widget
{
$pirepRepo = app(PirepRepository::class);
$pireps = $pirepRepo
->whereNotInOrder('state', [
PirepState::CANCELLED,
PirepState::DRAFT,
PirepState::IN_PROGRESS
], 'created_at', 'desc')
->recent($this->config['count']);
return view('widgets.latest_pireps', [
'config' => $this->config,
'pireps' => $pirepRepo->recent($this->config['count']),
'pireps' => $pireps,
]);
}
}

View File

@ -21,6 +21,7 @@ return [
'download' => 'Download|Downloads',
'from' => 'from',
'to' => 'to',
'state' => 'State',
'status' => 'Status',
'departure' => 'Departure',
'arrival' => 'Arrival',

View File

@ -21,6 +21,7 @@ return [
'download' => 'Download|Downloads',
'from' => 'da',
'to' => 'a',
'state' => 'Stato',
'status' => 'Stato',
'departure' => 'Partenza',
'arrival' => 'Arrivo',

View File

@ -38,11 +38,13 @@
{{ Utils::minutesToTimeString($pirep->flight_time) }}
</span>
</div>
<div><span class="description"><b>Aircraft</b>&nbsp;
{{ $pirep->aircraft->registration }}
({{ $pirep->aircraft->name }})
</span>
</div>
@if($pirep->aircraft)
<div><span class="description"><b>Aircraft</b>&nbsp;
{{ $pirep->aircraft->registration }}
({{ $pirep->aircraft->name }})
</span>
</div>
@endif
@if(filled($pirep->level))
<div>
<span class="description"><b>Flight Level</b>&nbsp;

View File

@ -95,7 +95,7 @@
<li class="nav-item">
<a class="nav-link" href="{{ url('/logout') }}">
<i class="fas fa-sign-out-alt"></i>
<p>@lang('auth.logout')</p>
<p>@lang('common.logout')</p>
</a>
</li>
@endif

View File

@ -6,16 +6,7 @@
<div class="col-8">
<div class="row">
<div class="col-12">
<p>
<h2 style="margin-bottom: 5px;">{{$pirep->airline->code}}{{ $pirep->ident }}</h2>
<p>
@if($pirep->state === PirepState::IN_PROGRESS)
@else
@lang('pireps.arrived') {{$pirep->created_at->diffForHumans()}}
@endif
</p>
</p>
<h2 style="margin-bottom: 5px;">{{$pirep->airline->code}}{{ $pirep->ident }}</h2>
</div>
</div>
<div class="row">
@ -101,6 +92,16 @@
</div>
@endif
<table class="table table-striped">
<tr>
<td width="30%">@lang('common.state')</td>
<td>
<div class="badge badge-info">
{{ PirepState::label($pirep->state) }}
</div>
</td>
</tr>
<tr>
<td width="30%">@lang('common.status')</td>
<td>

View File

@ -33,7 +33,13 @@
'id' => $pirep->arr_airport->icao
])}}">{{$pirep->arr_airport->icao}}</a>)
</td>
<td>{{ $pirep->aircraft->name }}</td>
<td>
@if($pirep->aircraft)
{{ $pirep->aircraft->name }}
@else
-
@endif
</td>
<td class="text-center">
{{ (new \App\Support\Units\Time($pirep->flight_time)) }}
</td>

View File

@ -7,7 +7,9 @@
<td>
{{ $p->dpt_airport_id }}-
{{ $p->arr_airport_id }}&nbsp;
{{ $p->aircraft->name }}
@if($p->aircraft)
{{ $p->aircraft->name }}
@endif
</td>
</tr>
@endforeach