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

@@ -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()