This commit is contained in:
Nabeel Shahzad
2018-07-05 14:07:56 -05:00
42 changed files with 1261 additions and 522 deletions

View File

@@ -73,6 +73,7 @@ class CreateFlightTables extends Migration
$table->bigIncrements('id');
$table->string('flight_id', \App\Interfaces\Model::ID_MAX_LENGTH);
$table->string('name', 50);
$table->string('slug', 50)->nullable();
$table->text('value');
$table->timestamps();

View File

@@ -394,18 +394,20 @@ flights:
flight_fields:
- name: Departure Gate
slug: departure_gate
slug: departure-gate
- name: Arrival Gate
slug: arrival_gate
slug: arrival-gate
flight_field_values:
- id: 1
flight_id: flightid_1
name: cost index
slug: cost-index
value: 80
- id: 2
flight_id: flightid_2
name: cost index
slug: cost-index
value: 100
flight_subfleet:
@@ -539,24 +541,24 @@ pirep_fares:
pirep_fields:
- id: 1
name: departure gate
slug: departure_gate
slug: departure-gate
required: 1
- id: 2
name: arrival gate
slug: arrival_gate
slug: arrival-gate
required: 0
pirep_field_values:
- id: 1
pirep_id: pirepid_1
name: arrival gate
slug: arrival_gate
slug: arrival-gate
value: 10
source: manual
- id: 2
pirep_id: pirepid_1
name: departure gate
slug: departure_gate
slug: departure-gate
value: B32
source: manual

View File

@@ -121,6 +121,7 @@ class PirepController extends Controller
$custom_fields[] = [
'name' => $field->name,
'slug' => $field->slug,
'value' => $request->input($field->slug),
'source' => PirepSource::MANUAL
];
@@ -139,6 +140,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 +191,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 +248,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 +305,10 @@ 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;
if(!$pirep->read_only) {
$pirep->state = PirepState::DRAFT;
}
$pirep->save();
Flash::success('PIREP saved successfully.');
} else if ($attrs['submit'] === 'submit') {
@@ -317,7 +329,6 @@ class PirepController extends Controller
$pirep = $this->pirepRepo->findWithoutFail($id);
if (empty($pirep)) {
Flash::error('Pirep not found');
return redirect(route('frontend.pireps.index'));
}
@@ -327,6 +338,10 @@ class PirepController extends Controller
# set the custom fields
foreach ($pirep->fields as $field) {
if($field->slug == null) {
$field->slug = str_slug($field->name);
}
$pirep->{$field->slug} = $field->value;
}
@@ -363,6 +378,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 +400,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

@@ -18,11 +18,21 @@ class FlightFieldValue extends Model
protected $fillable = [
'flight_id',
'name',
'slug',
'value',
];
public static $rules = [];
/**
* @param $name
*/
public function setNameAttribute($name): void
{
$this->attributes['name'] = $name;
$this->attributes['slug'] = str_slug($name);
}
/**
* Relationships
*/

View File

@@ -1,28 +0,0 @@
<?php
namespace App\Models\Observers;
use App\Models\PirepField;
/**
* Class PirepFieldObserver
* @package App\Models\Observers
*/
class PirepFieldObserver
{
/**
* @param PirepField $model
*/
public function creating(PirepField $model): void
{
$model->slug = str_slug($model->name);
}
/**
* @param PirepField $model
*/
public function updating(PirepField $model): void
{
$model->slug = str_slug($model->name);
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Models\Observers;
/**
* Create a slug from a name
* @package App\Models\Observers
*/
class Sluggable
{
/**
* @param $model
*/
public function creating($model): void
{
$model->slug = str_slug($model->name);
}
/**
* @param $model
*/
public function updating($model): void
{
$model->slug = str_slug($model->name);
}
/**
* @param $name
*/
public function setNameAttribute($name): void
{
$this->attributes['name'] = $name;
$this->attributes['slug'] = str_slug($name);
}
}

View File

@@ -40,10 +40,12 @@ 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 bool read_only
* @property Acars position
* @property Acars[] acars
* @package App\Models
@@ -126,9 +128,9 @@ 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::ACCEPTED,
PirepState::REJECTED,
//PirepState::PENDING,
PirepState::CANCELLED,
];

View File

@@ -15,6 +15,7 @@ class PirepFieldValues extends Model
protected $fillable = [
'pirep_id',
'name',
'slug',
'value',
'source',
];
@@ -23,6 +24,15 @@ class PirepFieldValues extends Model
'name' => 'required',
];
/**
* @param $name
*/
public function setNameAttribute($name): void
{
$this->attributes['name'] = $name;
$this->attributes['slug'] = str_slug($name);
}
/**
* Foreign Keys
*/

View File

@@ -4,21 +4,23 @@ namespace App\Providers;
use App\Models\Aircraft;
use App\Models\Airport;
use App\Models\FlightField;
use App\Models\FlightFieldValue;
use App\Models\Journal;
use App\Models\JournalTransaction;
use App\Models\Observers\AircraftObserver;
use App\Models\Observers\AirportObserver;
use App\Models\Observers\JournalObserver;
use App\Models\Observers\JournalTransactionObserver;
use App\Models\Observers\PirepFieldObserver;
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\Setting;
use App\Models\Subfleet;
use App\Repositories\SettingRepository;
use App\Services\ModuleService;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use View;
@@ -42,10 +44,15 @@ class AppServiceProvider extends ServiceProvider
Airport::observe(AirportObserver::class);
Journal::observe(JournalObserver::class);
JournalTransaction::observe(JournalTransactionObserver::class);
PirepField::observe(PirepFieldObserver::class);
FlightField::observe(Sluggable::class);
FlightFieldValue::observe(Sluggable::class);
PirepField::observe(Sluggable::class);
PirepFieldValues::observe(Sluggable::class);
Setting::observe(SettingObserver::class);
Subfleet::observe(SubfleetObserver::class);
}
/**
@@ -55,7 +62,6 @@ class AppServiceProvider extends ServiceProvider
{
# Only dev environment stuff
if ($this->app->environment() === 'dev') {
# Only load the IDE helper if it's included. This lets use distribute the
# package without any dev dependencies
if (class_exists(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class)) {

View File

@@ -3,10 +3,8 @@
namespace App\Services;
use App\Interfaces\Service;
use App\Support\Database;
use Carbon\Carbon;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Yaml\Yaml;
use Webpatser\Uuid\Uuid;
/**
@@ -42,8 +40,7 @@ class DatabaseService extends Service
*/
public function seed_from_yaml_file($yaml_file, $ignore_errors = false): array
{
$yml = file_get_contents($yaml_file);
return $this->seed_from_yaml($yml, $ignore_errors);
return Database::seed_from_yaml_file($yaml_file, $ignore_errors);
}
/**
@@ -54,27 +51,7 @@ class DatabaseService extends Service
*/
public function seed_from_yaml($yml, $ignore_errors = false): array
{
$imported = [];
$yml = Yaml::parse($yml);
foreach ($yml as $table => $rows) {
$imported[$table] = 0;
foreach ($rows as $row) {
try {
$row = $this->insert_row($table, $row);
} catch(QueryException $e) {
if ($ignore_errors) {
continue;
}
throw $e;
}
++$imported[$table];
}
}
return $imported;
return Database::seed_from_yaml($yml, $ignore_errors);
}
/**
@@ -92,24 +69,6 @@ class DatabaseService extends Service
}
}
# encrypt any password fields
if (array_key_exists('password', $row)) {
$row['password'] = bcrypt($row['password']);
}
# if any time fields are == to "now", then insert the right time
foreach($row as $column => $value) {
if(strtolower($value) === 'now') {
$row[$column] = $this->time();
}
}
try {
DB::table($table)->insert($row);
} catch (QueryException $e) {
throw $e;
}
return $row;
return Database::insert_row($table, $row);
}
}

View File

@@ -13,6 +13,7 @@ use App\Models\Bid;
use App\Models\Enums\AcarsType;
use App\Models\Enums\PirepSource;
use App\Models\Enums\PirepState;
use App\Models\Enums\PirepStatus;
use App\Models\Enums\UserState;
use App\Models\Navdata;
use App\Models\Pirep;
@@ -212,6 +213,10 @@ class PirepService extends Service
}
}
$pirep->state = $default_state;
$pirep->status = PirepStatus::ARRIVED;
$pirep->save();
Log::info('New PIREP filed', [$pirep]);
event(new PirepFiled($pirep));

96
app/Support/Database.php Normal file
View File

@@ -0,0 +1,96 @@
<?php
/**
*
*/
namespace App\Support;
use Carbon\Carbon;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Yaml\Yaml;
use Webpatser\Uuid\Uuid;
use Log;
class Database
{
/**
* @return string
*/
protected static function time(): string
{
return Carbon::now('UTC');
}
/**
* @param $yaml_file
* @param bool $ignore_errors
* @return array
* @throws \Exception
*/
public static function seed_from_yaml_file($yaml_file, $ignore_errors = false): array
{
$yml = file_get_contents($yaml_file);
return static::seed_from_yaml($yml, $ignore_errors);
}
/**
* @param $yml
* @param bool $ignore_errors
* @return array
* @throws \Exception
*/
public static function seed_from_yaml($yml, $ignore_errors = false): array
{
$imported = [];
$yml = Yaml::parse($yml);
foreach ($yml as $table => $rows) {
$imported[$table] = 0;
foreach ($rows as $row) {
try {
static::insert_row($table, $row);
} catch (QueryException $e) {
if ($ignore_errors) {
continue;
}
throw $e;
}
++$imported[$table];
}
}
return $imported;
}
/**
* @param $table
* @param $row
* @return mixed
* @throws \Exception
*/
public static function insert_row($table, $row)
{
# encrypt any password fields
if (array_key_exists('password', $row)) {
$row['password'] = bcrypt($row['password']);
}
# if any time fields are == to "now", then insert the right time
foreach ($row as $column => $value) {
if (strtolower($value) === 'now') {
$row[$column] = static::time();
}
}
try {
DB::table($table)->insert($row);
} catch (QueryException $e) {
throw $e;
}
return $row;
}
}

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,
]);
}
}