Fix PIREP field and non-existant airport errors when editing PIREPs

This commit is contained in:
Nabeel Shahzad 2018-07-12 21:20:10 -05:00
parent 85615300e0
commit 9cc640b866
18 changed files with 138 additions and 65 deletions

View File

@ -84,7 +84,7 @@ class CreatePirepTables extends Migration
$table->string('name', 50);
$table->string('slug', 50)->nullable();
$table->string('value')->nullable();
$table->string('source')->nullable();
$table->unsignedTinyInteger('source');
$table->timestamps();
$table->index('pirep_id');

View File

@ -393,10 +393,10 @@ flights:
updated_at: NOW
flight_fields:
- name: Departure Gate
slug: departure-gate
- name: Arrival Gate
slug: arrival-gate
- name: Departure Terminal
slug: departure-terminal
- name: Arrival Terminal
slug: arrival-terminal
flight_field_values:
- id: 1
@ -540,27 +540,33 @@ pirep_fares:
pirep_fields:
- id: 1
name: departure gate
slug: departure-gate
name: departure terminal
slug: departure-terminal
required: 1
- id: 2
name: arrival gate
slug: arrival-gate
name: arrival terminal
slug: arrival-terminal
required: 0
pirep_field_values:
- id: 1
pirep_id: pirepid_1
name: arrival gate
slug: arrival-gate
name: arrival terminal
slug: arrival-terminal
value: 10
source: manual
source: 0
- id: 2
pirep_id: pirepid_1
name: departure gate
slug: departure-gate
value: B32
source: manual
name: departure terminal
slug: departure-terminal
value: 4
source: 0
- id: 3
pirep_id: pirepid_1
name: Landing Rate
slug: landing-rate
value: -225.3
source: 1
pirep_comments:
- id: 1

View File

@ -298,7 +298,8 @@ class PirepController extends Controller
# set the custom fields
foreach ($pirep->fields as $field) {
$pirep->{$field->slug} = $field->value;
$field_name = 'field_'.$field->slug;
$pirep->{$field_name} = $field->value;
}
# set the fares

View File

@ -20,6 +20,7 @@ use App\Http\Resources\PirepFieldCollection;
use App\Interfaces\Controller;
use App\Models\Acars;
use App\Models\Enums\AcarsType;
use App\Models\Enums\PirepFieldSource;
use App\Models\Enums\PirepSource;
use App\Models\Enums\PirepState;
use App\Models\Enums\PirepStatus;
@ -122,7 +123,7 @@ class PirepController extends Controller
$pirep_fields[] = [
'name' => $field_name,
'value' => $field_value,
'source' => $pirep->source,
'source' => PirepFieldSource::ACARS,
];
}

View File

@ -332,6 +332,9 @@ class PirepController extends Controller
return redirect(route('frontend.pireps.index'));
}
# Eager load the subfleet and fares under it
$pirep->aircraft->load('subfleet.fares');
$time = new Time($pirep->flight_time);
$pirep->hours = $time->hours;
$pirep->minutes = $time->minutes;
@ -342,7 +345,8 @@ class PirepController extends Controller
$field->slug = str_slug($field->name);
}
$pirep->{$field->slug} = $field->value;
$field_name = 'field_'.$field->slug;
$pirep->{$field_name} = $field->value;
}
# set the fares

View File

@ -0,0 +1,15 @@
<?php
namespace App\Models\Enums;
use App\Interfaces\Enum;
/**
* Class AcarsType
* @package App\Models\Enums
*/
class PirepFieldSource extends Enum
{
public const MANUAL = 0;
public const ACARS = 1;
}

View File

@ -4,6 +4,7 @@ namespace App\Models;
use App\Interfaces\Model;
use App\Models\Enums\AcarsType;
use App\Models\Enums\PirepFieldSource;
use App\Models\Enums\PirepState;
use App\Models\Traits\HashIdTrait;
use App\Support\Units\Distance;
@ -128,7 +129,7 @@ class Pirep extends Model
* If a PIREP is in these states, then it can't be changed.
*/
public static $read_only_states = [
PirepState::PENDING,
//PirepState::PENDING,
PirepState::ACCEPTED,
PirepState::REJECTED,
PirepState::CANCELLED,
@ -301,6 +302,32 @@ class Pirep extends Model
return round(($this->distance['nmi'] / $upper_bound) * 100, 0);
}
/**
* Get the pirep_fields and then the pirep_field_values and
* merge them together. If a field value doesn't exist then add in a fake one
*/
public function getFieldsAttribute()
{
$custom_fields = PirepField::all();
$field_values = PirepFieldValue::where('pirep_id', $this->id)->get();
# Merge the field values into $fields
foreach($custom_fields as $field) {
$has_value = $field_values->firstWhere('slug', $field->slug);
if(!$has_value) {
$field_values->push(new PirepFieldValue([
'pirep_id' => $this->id,
'name' => $field->name,
'slug' => $field->slug,
'value' => '',
'source' => PirepFieldSource::MANUAL
]));
}
}
return $field_values->sortBy('source');
}
/**
* Look up the flight, based on the PIREP flight info
* @return Flight|null
@ -454,9 +481,9 @@ class Pirep extends Model
return $this->hasMany(PirepFare::class, 'pirep_id');
}
public function fields()
public function field_values()
{
return $this->hasMany(PirepFieldValues::class, 'pirep_id');
return $this->hasMany(PirepFieldValue::class, 'pirep_id');
}
public function pilot()

View File

@ -3,12 +3,13 @@
namespace App\Models;
use App\Interfaces\Model;
use App\Models\Enums\PirepFieldSource;
/**
* Class PirepFieldValues
* Class PirepFieldValue
* @package App\Models
*/
class PirepFieldValues extends Model
class PirepFieldValue extends Model
{
public $table = 'pirep_field_values';
@ -24,6 +25,19 @@ class PirepFieldValues extends Model
'name' => 'required',
];
protected $casts = [
'source' => 'integer',
];
/**
* If it was filled in from ACARS, then it's read only
* @return bool
*/
public function getReadOnlyAttribute()
{
return $this->source === PirepFieldSource::ACARS;
}
/**
* @param $name
*/

View File

@ -16,7 +16,7 @@ use App\Models\Observers\Sluggable;
use App\Models\Observers\SettingObserver;
use App\Models\Observers\SubfleetObserver;
use App\Models\PirepField;
use App\Models\PirepFieldValues;
use App\Models\PirepFieldValue;
use App\Models\Setting;
use App\Models\Subfleet;
use App\Repositories\SettingRepository;
@ -49,7 +49,7 @@ class AppServiceProvider extends ServiceProvider
FlightFieldValue::observe(Sluggable::class);
PirepField::observe(Sluggable::class);
PirepFieldValues::observe(Sluggable::class);
PirepFieldValue::observe(Sluggable::class);
Setting::observe(SettingObserver::class);
Subfleet::observe(SubfleetObserver::class);

View File

@ -17,7 +17,7 @@ use App\Models\Enums\PirepStatus;
use App\Models\Enums\UserState;
use App\Models\Navdata;
use App\Models\Pirep;
use App\Models\PirepFieldValues;
use App\Models\PirepFieldValue;
use App\Models\User;
use App\Repositories\AcarsRepository;
use App\Repositories\FlightRepository;
@ -152,7 +152,7 @@ class PirepService extends Service
* Create a new PIREP with some given fields
*
* @param Pirep $pirep
* @param array [PirepFieldValues] $field_values
* @param array PirepFieldValue[] $field_values
*
* @return Pirep
*/
@ -244,7 +244,7 @@ class PirepService extends Service
public function updateCustomFields($pirep_id, array $field_values)
{
foreach ($field_values as $fv) {
PirepFieldValues::updateOrCreate(
PirepFieldValue::updateOrCreate(
['pirep_id' => $pirep_id,
'name' => $fv['name']
],

View File

@ -3,6 +3,10 @@
*
*/
.form-container {
font-weight: normal;
}
.select2-container {
margin-top: 2px;
}

View File

@ -15,9 +15,13 @@
</td>
<td>
<div class="form-group">
{{ Form::text($field->slug, null, [
@if(!$field->read_only)
{{ Form::text($field->slug, $field->value, [
'class' => 'form-control'
]) }}
@else
<p>{{ $field->value }}</p>
@endif
</div>
<p class="text-danger">{{ $errors->first($field->slug) }}</p>
</td>

View File

@ -28,9 +28,9 @@
<div>
<span class="description">
<b>DEP</b>&nbsp;
{{ $pirep->dpt_airport->icao }}&nbsp;
{{ $pirep->dpt_airport_id }}&nbsp;
<b>ARR</b>&nbsp;
{{ $pirep->arr_airport->icao }}&nbsp;
{{ $pirep->arr_airport_id }}&nbsp;
</span>
</div>
<div>

View File

@ -0,0 +1,21 @@
<tr>
<td>
{{ $field->name }}
@if($field->required === true)
<span class="text-danger">*</span>
@endif
</td>
<td>
<div class="input-group input-group-sm form-group">
@if(!$field->read_only)
{{ Form::text($field->slug, $field->value, [
'class' => 'form-control',
'readonly' => (!empty($pirep) && $pirep->read_only),
]) }}
@else
{{ $field->value }}
@endif
</div>
<p class="text-danger">{{ $errors->first($field->slug) }}</p>
</td>
</tr>

View File

@ -4,7 +4,7 @@
&nbsp;{{ trans_choice('pireps.fare', 2) }}
</h6>
<div class="form-container-body">
@foreach($aircraft->subfleet->fares as $fare)
@foreach($pirep->aircraft->subfleet->fares as $fare)
<div class="row">
<div class="col">
{{Form::label('fare_'.$fare->id, $fare->name.' ('.$fare->code.')')}}

View File

@ -276,36 +276,16 @@ flight reports that have been filed. You've been warned!
&nbsp;{{ trans_choice('common.field', 2) }}
</h6>
<div class="form-container-body">
{{--
Write out the custom fields, and label if they're required
--}}
@foreach($pirep_fields as $field)
<tr>
<td>
{{ $field->name }}
@if($field->required === true)
<span class="text-danger">*</span>
@endif
</td>
<td>
<div class="input-group input-group-sm form-group">
{{ Form::text($field->slug, null, [
'class' => 'form-control',
'readonly' => (!empty($pirep) && $pirep->read_only),
]) }}
</div>
<p class="text-danger">{{ $errors->first($field->slug) }}</p>
</td>
</tr>
@endforeach
@each('pireps.custom_fields', $pirep->fields, 'field')
</div>
</div>
<div id="fares_container">
@include('pireps.fares')
</div>
</div>
</div>
<div class="row">

View File

@ -22,16 +22,12 @@
$pirep->id]) }}">{{ $pirep->airline->code }}{{ $pirep->ident }}</a>
</td>
<td>
{{ $pirep->dpt_airport->name }}
(<a href="{{route('frontend.airports.show', [
'id' => $pirep->dpt_airport->icao
])}}">{{$pirep->dpt_airport->icao}}</a>)
@if($pirep->dpt_airport){{ $pirep->dpt_airport->name }}@endif
(<a href="{{route('frontend.airports.show', ['id' => $pirep->dpt_airport_id])}}">{{$pirep->dpt_airport_id}}</a>)
</td>
<td>
{{ $pirep->arr_airport->name }}
(<a href="{{route('frontend.airports.show', [
'id' => $pirep->arr_airport->icao
])}}">{{$pirep->arr_airport->icao}}</a>)
@if($pirep->arr_airport){{ $pirep->arr_airport->name }}@endif
(<a href="{{route('frontend.airports.show', ['id' => $pirep->arr_airport_id])}}">{{$pirep->arr_airport_id}}</a>)
</td>
<td>
@if($pirep->aircraft)

View File

@ -226,7 +226,7 @@ class AcarsTest extends TestCase
$this->assertEquals($fare->capacity, $saved_fare['count']);
# Check saved fields
$saved_fields = \App\Models\PirepFieldValues::where('pirep_id', $pirep['id'])->get();
$saved_fields = \App\Models\PirepFieldValue::where('pirep_id', $pirep['id'])->get();
$this->assertCount(1, $saved_fields);
$field = $saved_fields->first();