From f336080262bc0ee85fbded71a1bd0b22a1ec6c65 Mon Sep 17 00:00:00 2001 From: Alex Popa Date: Thu, 25 Jan 2024 12:14:14 +0200 Subject: [PATCH] Add never expires on relation documents --- .../DocumentsRelationManager.php | 39 ++++++++++++++++--- app/Models/Document.php | 2 + config/tables.php | 21 ++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 config/tables.php diff --git a/app/Filament/Resources/OrganisationResource/RelationManagers/DocumentsRelationManager.php b/app/Filament/Resources/OrganisationResource/RelationManagers/DocumentsRelationManager.php index b5f7b5e..24bd461 100644 --- a/app/Filament/Resources/OrganisationResource/RelationManagers/DocumentsRelationManager.php +++ b/app/Filament/Resources/OrganisationResource/RelationManagers/DocumentsRelationManager.php @@ -7,8 +7,12 @@ use App\Enum\DocumentType; use App\Filament\Resources\DocumentResource; use App\Filament\Tables\Actions\ExportAction; +use App\Models\Document; +use Closure; +use Filament\Forms\Components\Checkbox; use Filament\Forms\Components\DatePicker; use Filament\Forms\Components\Grid; +use Filament\Forms\Components\Group; use Filament\Forms\Components\Select; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; use Filament\Forms\Components\TextInput; @@ -63,12 +67,28 @@ public static function form(Form $form): Form DatePicker::make('signed_at') ->label(__('document.field.signed_at')) ->required(), - - DatePicker::make('expires_at') - ->label(__('document.field.expires_at')) - ->after('signed_at') - ->required(), + Group::make()->schema([ + DatePicker::make('expires_at') + ->label(__('document.field.expires_at')) + ->after('signed_at') + ->required(fn (Closure $get) => ! $get('never_expires')) + ->disabled(fn (Closure $get) => (bool) $get('never_expires')) + ->afterStateHydrated(function (Closure $set, $state) { + if (blank($state)) { + $set('never_expires', true); + } + }), + + Checkbox::make('never_expires') + ->label(__('document.field.never_expires')) + ->afterStateUpdated(function (Closure $set, $state) { + if ($state === true) { + $set('expires_at', null); + } + }) + ->reactive(), ]), + ]), SpatieMediaLibraryFileUpload::make('document') ->enableDownload() @@ -108,7 +128,14 @@ public static function table(Table $table): Table TextColumn::make('expires_at') ->label(__('document.field.expires_at')) ->date() - ->sortable(), + ->sortable() + ->formatStateUsing(function (Document $record) { + if (DocumentType::protocol->isNot($record->type)) { + return $record->expires_at?->format(config('tables.date_format')); + } + + return $record->expires_at?->format(config('tables.date_format')) ?: __('document.field.never_expires'); + }), ]) ->filters([ // diff --git a/app/Models/Document.php b/app/Models/Document.php index 50c1f66..54944cf 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -5,6 +5,7 @@ namespace App\Models; use App\Concerns\LimitsVisibility; +use App\Enum\DocumentType; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -30,6 +31,7 @@ class Document extends Model implements HasMedia protected $casts = [ 'signed_at' => 'date', 'expires_at' => 'date', + 'type' => DocumentType::class ]; public function registerMediaConversions(Media $media = null): void diff --git a/config/tables.php b/config/tables.php new file mode 100644 index 0000000..90be6e3 --- /dev/null +++ b/config/tables.php @@ -0,0 +1,21 @@ + 'j.m.Y', + 'date_time_format' => 'j.m.Y H:i:s', + 'time_format' => 'H:i:s', + +];