Skip to content

Commit

Permalink
Add never expires on relation documents
Browse files Browse the repository at this point in the history
  • Loading branch information
alexPopaCode4 committed Jan 25, 2024
1 parent cb62ab3 commit f336080
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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([
//
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions config/tables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

return [

/*
|--------------------------------------------------------------------------
| Date / Time Formatting
|--------------------------------------------------------------------------
|
| These are the formats that Filament will use to display dates and times
| by default.
|
*/

'date_format' => 'j.m.Y',
'date_time_format' => 'j.m.Y H:i:s',
'time_format' => 'H:i:s',

];

0 comments on commit f336080

Please sign in to comment.