Skip to content

Commit

Permalink
Merge pull request #52 from ECFMP/flowmeasure-improvements
Browse files Browse the repository at this point in the history
Flowmeasure improvements
  • Loading branch information
AndyTWF authored May 23, 2022
2 parents 8933e2f + 031cce7 commit b69b42a
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 103 deletions.
17 changes: 17 additions & 0 deletions app/Enums/FlowMeasureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,21 @@ public function getFormattedName(): string
self::MANDATORY_ROUTE => 'Mandatory route',
};
}

public function getDescription(): string
{
return match ($this) {
self::MINIMUM_DEPARTURE_INTERVAL => 'Minimum departure interval in seconds',
self::AVERAGE_DEPARTURE_INTERVAL => 'Average departure interval applied over 3 aircraft in seconds',
self::PER_HOUR => 'Number of flights per hour permitted - must be > 0',
self::MILES_IN_TRAIL => 'Distance in NM of aircraft in trail',
self::MAX_IAS => 'Maximum speed in IAS',
self::MAX_MACH => 'Maximum speed in mach, 82 = 0.82 Mach, 102 = 1.02 Mach',
self::IAS_REDUCTION => 'Reduce indicated airspeed by certain value in knots',
self::MACH_REDUCTION => 'Reduced mach by certain value 5 = 0.05 Mach',
self::PROHIBIT => 'Prohibit a flight according to filters',
self::MANDATORY_ROUTE => 'Mandate routing via a single waypoint/airway only',
default => '',
};
}
}
19 changes: 14 additions & 5 deletions app/Filament/Resources/AirportGroupResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace App\Filament\Resources;

use App\Filament\Resources\AirportGroupResource\Pages;
use App\Filament\Resources\AirportGroupResource\RelationManagers;
use App\Models\AirportGroup;
use Filament\Forms;
use Filament\Tables;
use App\Models\AirportGroup;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Resources\Resource;
use Illuminate\Database\Eloquent\Model;
use App\Filament\Resources\AirportGroupResource\Pages;
use App\Filament\Resources\AirportGroupResource\RelationManagers;

class AirportGroupResource extends Resource
{
Expand All @@ -21,6 +22,14 @@ class AirportGroupResource extends Resource

protected static ?string $navigationGroup = 'Admin';

public static function getGlobalSearchResultDetails(Model $record): array
{
/** @var AirportGroup $record */
return [
__('Airports') => $record->airport_codes
];
}

public static function form(Form $form): Form
{
return $form
Expand Down
5 changes: 3 additions & 2 deletions app/Filament/Resources/EventResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ class EventResource extends Resource

public static function getGlobalSearchResultDetails(Model $record): array
{
/** @var Event $record */
return [
'FIR' => $record->flightInformationRegion->name,
__('Start') => $record->date_start,
__('End') => $record->date_end,
__('Start') => $record->date_start->format('M j, Y H:i\z'),
__('End') => $record->date_end->format('M j, Y H:i\z'),
];
}

Expand Down
78 changes: 68 additions & 10 deletions app/Filament/Resources/FlowMeasureResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
use App\Enums\RoleKey;
use Filament\Pages\Page;
use App\Models\FlowMeasure;
use App\Models\AirportGroup;
use Filament\Resources\Form;
use Filament\Resources\Table;
use App\Enums\FlowMeasureType;
use Illuminate\Support\Carbon;
use Filament\Resources\Resource;
use Illuminate\Support\Collection;
use Filament\Tables\Filters\Filter;
use App\Models\FlightInformationRegion;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\TextInput;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Filament\Resources\Pages\CreateRecord;
use Filament\Forms\Components\Builder\Block;
use App\Filament\Resources\FlowMeasureResource\Pages;
use App\Filament\Resources\FlowMeasureResource\RelationManagers;
use App\Filament\Resources\FlowMeasureResource\Widgets\ActiveFlowMeasures;
use Illuminate\Support\Carbon;

class FlowMeasureResource extends Resource
{
Expand All @@ -41,6 +41,10 @@ private static function setFirOptions(Collection $firs)

public static function form(Form $form): Form
{
$events = Event::where('date_end', '>', now()->addHours(6))
->get(['id', 'name', 'date_start', 'date_end', 'flight_information_region_id'])
->keyBy('id');

return $form
->schema([
Forms\Components\Select::make('flight_information_region_id')
Expand All @@ -56,14 +60,26 @@ public static function form(Form $form): Form
self::setFirOptions(auth()->user()
->flightInformationRegions)
)
->disabled(fn (Page $livewire) => !$livewire instanceof CreateRecord)
->dehydrated(fn (Page $livewire) => $livewire instanceof CreateRecord)
->required(fn (Closure $get) => $get('event_id') == null),
Forms\Components\Select::make('event_id')
->label(__('Event'))
->hintIcon('heroicon-o-calendar')
->searchable()
->options(
Event::where('date_end', '>', now()->addHours(6))->get()->mapWithKeys(fn (Event $event) => [$event->id => $event->name_date])
$events->mapWithKeys(fn (Event $event) => [$event->id => $event->name_date])
)
->afterStateUpdated(function (Closure $set, $state) use ($events) {
if ($state) {
$set('flight_information_region_id', $events[$state]->flight_information_region_id);
$set('start_time', $events[$state]->date_start);
$set('end_time', $events[$state]->date_end);
}
})
->disabled(fn (Page $livewire) => !$livewire instanceof CreateRecord)
->dehydrated(fn (Page $livewire) => $livewire instanceof CreateRecord)
->reactive()
->required(fn (Closure $get) => $get('flight_information_region_id') == null),
Forms\Components\DateTimePicker::make('start_time')
->default(now()->addMinutes(5))
Expand Down Expand Up @@ -93,6 +109,13 @@ public static function form(Form $form): Form
Forms\Components\Select::make('type')
->options(collect(FlowMeasureType::cases())
->mapWithKeys(fn (FlowMeasureType $type) => [$type->value => $type->getFormattedName()]))
->helperText(function (string|FlowMeasureType|null $state) {
if (is_a($state, FlowMeasureType::class)) {
return $state->getDescription();
}

return FlowMeasureType::tryFrom($state)?->getDescription() ?: '';
})
->reactive()
->required(),
Forms\Components\TextInput::make('value')
Expand All @@ -119,7 +142,20 @@ public static function form(Form $form): Form
->required()
->label('Departure(s) [ADEP]')
->schema([
Forms\Components\TextInput::make('value')
Forms\Components\Select::make('value_type')
->options([
'airport_group' => __('Airport Group'),
'custom_value' => __('Custom value'),
])
->reactive()
->required(),
Forms\Components\Select::make('airport_group')
->visible(fn (Closure $get) => $get('value_type') == 'airport_group')
->searchable()
->options(AirportGroup::all()->pluck('name_codes', 'id'))
->required(),
Forms\Components\TextInput::make('custom_value')
->visible(fn (Closure $get) => $get('value_type') == 'custom_value')
->length(4)
->default('****')
->required()
Expand All @@ -129,7 +165,20 @@ public static function form(Form $form): Form
->required()
->label('Arrival(s) [ADES]')
->schema([
Forms\Components\TextInput::make('value')
Forms\Components\Select::make('value_type')
->options([
'airport_group' => __('Airport Group'),
'custom_value' => __('Custom value'),
])
->reactive()
->required(),
Forms\Components\Select::make('airport_group')
->visible(fn (Closure $get) => $get('value_type') == 'airport_group')
->searchable()
->options(AirportGroup::all()->pluck('name_codes', 'id'))
->required(),
Forms\Components\TextInput::make('custom_value')
->visible(fn (Closure $get) => $get('value_type') == 'custom_value')
->length(4)
->default('****')
->required()
Expand Down Expand Up @@ -188,7 +237,7 @@ public static function form(Form $form): Form
->hintIcon('heroicon-o-calendar')
->searchable()
->options(
Event::where('date_end', '>', now()->addHours(6))->get()->mapWithKeys(fn (Event $event) => [$event->id => $event->name_date])
$events->mapWithKeys(fn (Event $event) => [$event->id => $event->name_date])
)
]),
Block::make('member_non_event')
Expand All @@ -198,11 +247,20 @@ public static function form(Form $form): Form
->hintIcon('heroicon-o-calendar')
->searchable()
->options(
Event::where('date_end', '>', now()->addHours(6))->get()->mapWithKeys(fn (Event $event) => [$event->id => $event->name_date])
$events->mapWithKeys(fn (Event $event) => [$event->id => $event->name_date])
)
]),
])
])
]),
]),
// TODO: Make it possible to also search by identifier
Forms\Components\Fieldset::make('FAO')
->schema([
Forms\Components\BelongsToManyMultiSelect::make('notified_flight_information_regions')
->columnSpan('full')
->label(__("FIR's"))
->relationship('notifiedFlightInformationRegions', 'name')
->getOptionLabelFromRecordUsing(fn (Model $record) => $record->identifierName)
])
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Filament\Resources\Pages\CreateRecord;
use App\Filament\Resources\FlowMeasureResource;
use App\Helpers\FlowMeasureIdentifierGenerator;
use App\Models\AirportGroup;
use App\Models\Event;
use App\Models\FlightInformationRegion;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -42,10 +43,10 @@ protected function mutateFormDataBeforeCreate(array $data): array

$filters->add([
'type' => 'ADEP',
'value' => Arr::pluck($data['adep'], 'value'),
'value' => $this->getAirportValues($data, 'adep')
])->add([
'type' => 'ADES',
'value' => Arr::pluck($data['ades'], 'value'),
'value' => $this->getAirportValues($data, 'ades')
]);

$data['filters'] = $filters->toArray();
Expand All @@ -54,4 +55,21 @@ protected function mutateFormDataBeforeCreate(array $data): array

return $data;
}

private function getAirportValues(array $data, string $type): array
{
$output = [];
foreach ($data[$type] as $filterData) {
if ($filterData['value_type'] == 'airport_group') {
// Making sure it actually exists
$airportGroup = AirportGroup::findOrFail($filterData['airport_group'], ['id']);

$output[] = $airportGroup->getKey();
} else {
$output[] = $filterData['custom_value'];
}
}

return $output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace App\Filament\Resources\FlowMeasureResource\Pages;

use App\Filament\Resources\FlowMeasureResource;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Arr;
use App\Models\AirportGroup;
use App\Enums\FlowMeasureType;
use Filament\Resources\Pages\EditRecord;
use App\Filament\Resources\FlowMeasureResource;

class EditFlowMeasure extends EditRecord
{
Expand All @@ -16,11 +18,11 @@ protected function mutateFormDataBeforeFill(array $data): array

$filters['adep'] = collect($filters['ADEP']['value'])
->map(function ($value) {
return ['value' => $value];
return $this->buildAirportFilter($value);
});
$filters['ades'] = collect($filters['ADES']['value'])
->map(function ($value) {
return ['value' => $value];
return $this->buildAirportFilter($value);
});

$data['adep'] = $filters['adep']->toArray();
Expand All @@ -45,6 +47,10 @@ protected function mutateFormDataBeforeFill(array $data): array

protected function mutateFormDataBeforeSave(array $data): array
{
if ($data['type'] == FlowMeasureType::MANDATORY_ROUTE) {
Arr::pull($data, 'value');
}

$filters = collect($data['filters'])->map(function (array $filter) {
$filter['value'] = $filter['data']['value'];
Arr::pull($filter, 'data');
Expand All @@ -54,10 +60,10 @@ protected function mutateFormDataBeforeSave(array $data): array

$filters->add([
'type' => 'ADEP',
'value' => Arr::pluck($data['adep'], 'value'),
'value' => $this->getAirportValues($data, 'adep')
])->add([
'type' => 'ADES',
'value' => Arr::pluck($data['ades'], 'value'),
'value' => $this->getAirportValues($data, 'ades')
]);

$data['filters'] = $filters->toArray();
Expand All @@ -66,4 +72,38 @@ protected function mutateFormDataBeforeSave(array $data): array

return $data;
}

private function buildAirportFilter(string $value): array
{
if (AirportGroup::find($value)) {
return [
'value_type' => 'airport_group',
'airport_group' => $value,
'custom_value' => '',
];
}

return [
'value_type' => 'custom_value',
'airport_group' => null,
'custom_value' => $value,
];
}

private function getAirportValues(array $data, string $type): array
{
$output = [];
foreach ($data[$type] as $filterData) {
if ($filterData['value_type'] == 'airport_group') {
// Making sure it actually exists
$airportGroup = AirportGroup::findOrFail($filterData['airport_group'], ['id']);

$output[] = $airportGroup->getKey();
} else {
$output[] = $filterData['custom_value'];
}
}

return $output;
}
}
Loading

0 comments on commit b69b42a

Please sign in to comment.