-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1390 from AndyTWF/stand-assignment-context
Stand assignment context
- Loading branch information
Showing
32 changed files
with
1,393 additions
and
142 deletions.
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
app/Filament/Resources/StandAssignmentsHistoryResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources; | ||
|
||
use App\Filament\Helpers\SelectOptions; | ||
use App\Filament\Resources\StandAssignmentsHistoryResource\Pages; | ||
use App\Models\Airfield\Airfield; | ||
use App\Models\Stand\Stand; | ||
use App\Models\Stand\StandAssignmentsHistory; | ||
use App\Models\User\RoleKeys; | ||
use App\Policies\ChecksUserRoles; | ||
use Closure; | ||
use Filament\Forms\Components\Select; | ||
use Filament\Forms\Components\TextInput; | ||
use Filament\Forms\Components\ViewField; | ||
use Filament\Resources\Form; | ||
use Filament\Resources\Resource; | ||
use Filament\Resources\Table; | ||
use Filament\Tables\Actions\ViewAction; | ||
use Filament\Tables\Columns\TextColumn; | ||
use Filament\Tables\Filters\Filter; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
class StandAssignmentsHistoryResource extends Resource | ||
{ | ||
use TranslatesStrings; | ||
use ChecksUserRoles; | ||
|
||
protected static ?string $model = StandAssignmentsHistory::class; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-collection'; | ||
protected static ?string $navigationLabel = 'Stand Assignment History'; | ||
protected static ?string $label = 'Stand Assignment History'; | ||
|
||
protected static ?string $navigationGroup = 'Airfield'; | ||
|
||
public static function getEloquentQuery(): Builder | ||
{ | ||
return StandAssignmentsHistory::with('stand', 'stand.airfield'); | ||
} | ||
|
||
public static function canGloballySearch(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
protected static function userCanAccess(): bool | ||
{ | ||
return self::checkUserHasRole( | ||
auth()->user(), | ||
[ | ||
RoleKeys::OPERATIONS_CONTRIBUTOR, | ||
RoleKeys::OPERATIONS_TEAM, | ||
RoleKeys::WEB_TEAM, | ||
RoleKeys::DIVISION_STAFF_GROUP, | ||
] | ||
); | ||
} | ||
|
||
protected static function shouldRegisterNavigation(): bool | ||
{ | ||
return self::userCanAccess(); | ||
} | ||
|
||
public function mount(): void | ||
{ | ||
dd(self::userCanAccess()); | ||
abort_unless(self::userCanAccess(), 403); | ||
} | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->disabled() | ||
->schema([ | ||
ViewField::make('context') | ||
->columnSpanFull() | ||
->view('filament.forms.stand_assignment_history_context') | ||
->label(static::translateFormPath('columns.context')), | ||
]); | ||
} | ||
|
||
public static function table(Table $table): Table | ||
{ | ||
return $table | ||
->columns([ | ||
TextColumn::make('callsign') | ||
->label(static::translateTablePath('columns.callsign')) | ||
->searchable(), | ||
TextColumn::make('identifier') | ||
->getStateUsing(fn (StandAssignmentsHistory $record) => $record->stand->airfieldIdentifier) | ||
->label(static::translateTablePath('columns.identifier')) | ||
->searchable(), | ||
TextColumn::make('assigned_at') | ||
->label(static::translateTablePath('columns.assigned_at')) | ||
->dateTime(), | ||
TextColumn::make('deleted_at') | ||
->placeholder('--') | ||
->label(static::translateTablePath('columns.deleted_at')) | ||
->dateTime(), | ||
TextColumn::make('type') | ||
->label(static::translateTablePath('columns.type')), | ||
]) | ||
->actions([ | ||
ViewAction::make('view_context') | ||
->label('View Context') | ||
->hidden( | ||
fn (StandAssignmentsHistory $record) => is_null($record->context) || empty($record->context) | ||
), | ||
]) | ||
->filters([ | ||
Filter::make('callsign') | ||
->formComponent(TextInput::class) | ||
->query( | ||
fn (Builder $query, array $data) => isset($data['isActive']) | ||
? $query->where('callsign', $data['isActive']) | ||
: $query | ||
), | ||
Filter::make('airfield_and_stand') | ||
->form([ | ||
Select::make('airfield') | ||
->options(SelectOptions::airfields()) | ||
->reactive() | ||
->searchable() | ||
->label('Airfield'), | ||
Select::make('stand') | ||
->options( | ||
fn (Closure $get) => SelectOptions::standsForAirfield(Airfield::find($get('airfield'))) | ||
) | ||
->searchable() | ||
->label('Stand') | ||
->hidden(fn (Closure $get) => !$get('airfield')), | ||
]) | ||
->indicateUsing(function (array $data) { | ||
if (isset($data['stand'])) { | ||
return 'Stand: ' . Stand::find($data['stand'])->airfieldIdentifier; | ||
} | ||
|
||
if (isset($data['airfield'])) { | ||
return 'Airfield: ' . Airfield::find($data['airfield'])->code; | ||
} | ||
|
||
return null; | ||
}) | ||
->query(function (Builder $query, array $data) { | ||
if (isset($data['airfield'])) { | ||
$query->whereHas( | ||
'stand.airfield', | ||
fn (Builder $query) => $query->where('id', $data['airfield']) | ||
); | ||
} | ||
|
||
if (isset($data['stand'])) { | ||
$query->where('stand_id', $data['stand']); | ||
} | ||
|
||
return $query; | ||
}), | ||
]); | ||
} | ||
|
||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => Pages\ListStandAssignmentsHistories::route('/'), | ||
'view' => Pages\ViewAssignmentContext::route('/{record}'), | ||
]; | ||
} | ||
|
||
protected static function translationPathRoot(): string | ||
{ | ||
return 'stand_assignments_history'; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ilament/Resources/StandAssignmentsHistoryResource/Pages/ListStandAssignmentsHistories.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\StandAssignmentsHistoryResource\Pages; | ||
|
||
use App\Filament\Resources\Pages\LimitsTableRecordListingOptions; | ||
use App\Filament\Resources\StandAssignmentsHistoryResource; | ||
use Filament\Resources\Pages\ListRecords; | ||
|
||
class ListStandAssignmentsHistories extends ListRecords | ||
{ | ||
use LimitsTableRecordListingOptions; | ||
|
||
protected static string $resource = StandAssignmentsHistoryResource::class; | ||
} |
11 changes: 11 additions & 0 deletions
11
app/Filament/Resources/StandAssignmentsHistoryResource/Pages/ViewAssignmentContext.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\StandAssignmentsHistoryResource\Pages; | ||
|
||
use App\Filament\Resources\StandAssignmentsHistoryResource; | ||
use Filament\Resources\Pages\ViewRecord; | ||
|
||
class ViewAssignmentContext extends ViewRecord | ||
{ | ||
protected static string $resource = StandAssignmentsHistoryResource::class; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Policies; | ||
|
||
use Illuminate\Auth\Access\HandlesAuthorization; | ||
use App\Models\User\User; | ||
|
||
/** | ||
* Policy that allows read only access only to users with a role. | ||
*/ | ||
class ReadOnlyWithRolePolicy | ||
{ | ||
use HandlesAuthorization; | ||
use ChecksUserRoles; | ||
use RejectsNonReadOnlyActions; | ||
|
||
public function view(?User $user): bool | ||
{ | ||
return $this->userHasAnyRole($user); | ||
} | ||
|
||
public function viewAny(?User $user): bool | ||
{ | ||
return $this->userHasAnyRole($user); | ||
} | ||
} |
Oops, something went wrong.