feat: legal sections
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
use Filament\Support\Contracts\HasLabel;
|
||||
|
||||
enum LegalDocumentType: string implements HasLabel
|
||||
{
|
||||
case PRIVACY_POLICY = 'privacy_policy';
|
||||
case TERMS = 'terms';
|
||||
|
||||
public function getLabel(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::PRIVACY_POLICY => __('enums.legal_document_type.privacy_policy'),
|
||||
self::TERMS => __('enums.legal_document_type.terms'),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\LegalDocuments;
|
||||
|
||||
use App\Filament\Resources\LegalDocuments\Pages\CreateLegalDocument;
|
||||
use App\Filament\Resources\LegalDocuments\Pages\EditLegalDocument;
|
||||
use App\Filament\Resources\LegalDocuments\Pages\ListLegalDocuments;
|
||||
use App\Filament\Resources\LegalDocuments\Pages\ViewLegalDocument;
|
||||
use App\Filament\Resources\LegalDocuments\Schemas\LegalDocumentForm;
|
||||
use App\Filament\Resources\LegalDocuments\Schemas\LegalDocumentInfolist;
|
||||
use App\Filament\Resources\LegalDocuments\Tables\LegalDocumentsTable;
|
||||
use App\Models\LegalDocument;
|
||||
use BackedEnum;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class LegalDocumentResource extends Resource
|
||||
{
|
||||
protected static ?string $model = LegalDocument::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedDocumentText;
|
||||
|
||||
public static function getNavigationLabel(): string
|
||||
{
|
||||
return __('admin.legal_documents.navigation.label');
|
||||
}
|
||||
|
||||
public static function getModelLabel(): string
|
||||
{
|
||||
return __('admin.legal_documents.navigation.singular');
|
||||
}
|
||||
|
||||
public static function getPluralModelLabel(): string
|
||||
{
|
||||
return __('admin.legal_documents.navigation.plural');
|
||||
}
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return LegalDocumentForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function infolist(Schema $schema): Schema
|
||||
{
|
||||
return LegalDocumentInfolist::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return LegalDocumentsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function createNewVersionAction(): Action
|
||||
{
|
||||
return Action::make('createNewVersion')
|
||||
->label(__('admin.legal_documents.actions.create_new_version'))
|
||||
->icon(Heroicon::OutlinedDocumentDuplicate)
|
||||
->requiresConfirmation()
|
||||
->modalDescription(__('admin.legal_documents.actions.create_new_version_confirmation'))
|
||||
->successNotificationTitle(__('admin.legal_documents.actions.create_new_version_success'))
|
||||
->action(function (LegalDocument $record, Action $action): void {
|
||||
$newRecord = $record->createNextVersion();
|
||||
|
||||
$action->success();
|
||||
$action->redirect(self::getUrl('edit', ['record' => $newRecord]));
|
||||
});
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListLegalDocuments::route('/'),
|
||||
'create' => CreateLegalDocument::route('/create'),
|
||||
'view' => ViewLegalDocument::route('/{record}'),
|
||||
'edit' => EditLegalDocument::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\LegalDocuments\Pages;
|
||||
|
||||
use App\Filament\Resources\LegalDocuments\LegalDocumentResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateLegalDocument extends CreateRecord
|
||||
{
|
||||
protected static string $resource = LegalDocumentResource::class;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\LegalDocuments\Pages;
|
||||
|
||||
use App\Filament\Resources\LegalDocuments\LegalDocumentResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditLegalDocument extends EditRecord
|
||||
{
|
||||
protected static string $resource = LegalDocumentResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
ViewAction::make(),
|
||||
LegalDocumentResource::createNewVersionAction(),
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\LegalDocuments\Pages;
|
||||
|
||||
use App\Filament\Resources\LegalDocuments\LegalDocumentResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListLegalDocuments extends ListRecords
|
||||
{
|
||||
protected static string $resource = LegalDocumentResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\LegalDocuments\Pages;
|
||||
|
||||
use App\Filament\Resources\LegalDocuments\LegalDocumentResource;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewLegalDocument extends ViewRecord
|
||||
{
|
||||
protected static string $resource = LegalDocumentResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
EditAction::make(),
|
||||
LegalDocumentResource::createNewVersionAction(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\LegalDocuments\Schemas;
|
||||
|
||||
use App\Enums\LegalDocumentType;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Repeater;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class LegalDocumentForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make(__('admin.legal_documents.sections.identity'))
|
||||
->columns(3)
|
||||
->schema([
|
||||
TextInput::make('slug')
|
||||
->label(__('admin.legal_documents.fields.slug'))
|
||||
->required()
|
||||
->maxLength(255)
|
||||
->helperText(__('admin.legal_documents.helpers.slug')),
|
||||
Select::make('type')
|
||||
->label(__('admin.legal_documents.fields.type'))
|
||||
->options(LegalDocumentType::class)
|
||||
->required(),
|
||||
TextInput::make('version')
|
||||
->label(__('admin.legal_documents.fields.version'))
|
||||
->integer()
|
||||
->minValue(1)
|
||||
->default(1)
|
||||
->required(),
|
||||
]),
|
||||
Section::make(__('admin.legal_documents.sections.publication'))
|
||||
->columns(2)
|
||||
->schema([
|
||||
Toggle::make('is_active')
|
||||
->label(__('admin.legal_documents.fields.is_active'))
|
||||
->helperText(__('admin.legal_documents.helpers.is_active')),
|
||||
DateTimePicker::make('published_at')
|
||||
->label(__('admin.legal_documents.fields.published_at'))
|
||||
->seconds(false)
|
||||
->helperText(__('admin.legal_documents.helpers.published_at')),
|
||||
]),
|
||||
Section::make(__('admin.legal_documents.sections.content'))
|
||||
->schema([
|
||||
TextInput::make('title.fr')
|
||||
->label(__('admin.legal_documents.fields.title'))
|
||||
->default('')
|
||||
->maxLength(255)
|
||||
->required()
|
||||
->columnSpanFull(),
|
||||
Repeater::make('content.fr.intro')
|
||||
->label(__('admin.legal_documents.fields.intro'))
|
||||
->simple(
|
||||
Textarea::make('paragraph')
|
||||
->label(__('admin.legal_documents.fields.paragraph'))
|
||||
->rows(3)
|
||||
->required()
|
||||
)
|
||||
->default([
|
||||
__('admin.legal_documents.defaults.intro'),
|
||||
])
|
||||
->addActionLabel(__('admin.legal_documents.actions.add_intro_paragraph'))
|
||||
->reorderable()
|
||||
->required()
|
||||
->columnSpanFull(),
|
||||
Repeater::make('content.fr.sections')
|
||||
->label(__('admin.legal_documents.fields.sections'))
|
||||
->schema([
|
||||
TextInput::make('title')
|
||||
->label(__('admin.legal_documents.fields.section_title'))
|
||||
->default(__('admin.legal_documents.defaults.section_title'))
|
||||
->required()
|
||||
->maxLength(255),
|
||||
Repeater::make('body')
|
||||
->label(__('admin.legal_documents.fields.section_body'))
|
||||
->simple(
|
||||
Textarea::make('paragraph')
|
||||
->label(__('admin.legal_documents.fields.paragraph'))
|
||||
->rows(3)
|
||||
->required()
|
||||
)
|
||||
->default([
|
||||
__('admin.legal_documents.defaults.paragraph'),
|
||||
])
|
||||
->addActionLabel(__('admin.legal_documents.actions.add_section_paragraph'))
|
||||
->reorderable()
|
||||
->required()
|
||||
->columnSpanFull(),
|
||||
])
|
||||
->default([
|
||||
[
|
||||
'title' => __('admin.legal_documents.defaults.section_title'),
|
||||
'body' => [
|
||||
__('admin.legal_documents.defaults.paragraph'),
|
||||
],
|
||||
],
|
||||
])
|
||||
->itemLabel(fn (array $state): ?string => $state['title'] ?? null)
|
||||
->addActionLabel(__('admin.legal_documents.actions.add_section'))
|
||||
->collapsible()
|
||||
->reorderable()
|
||||
->required()
|
||||
->columnSpanFull(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\LegalDocuments\Schemas;
|
||||
|
||||
use Filament\Infolists\Components\KeyValueEntry;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class LegalDocumentInfolist
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Section::make(__('admin.legal_documents.sections.identity'))
|
||||
->columns(3)
|
||||
->schema([
|
||||
TextEntry::make('id')
|
||||
->label(__('admin.legal_documents.fields.id'))
|
||||
->copyable(),
|
||||
TextEntry::make('slug')
|
||||
->label(__('admin.legal_documents.fields.slug'))
|
||||
->copyable(),
|
||||
TextEntry::make('type')
|
||||
->label(__('admin.legal_documents.fields.type'))
|
||||
->badge(),
|
||||
TextEntry::make('version')
|
||||
->label(__('admin.legal_documents.fields.version')),
|
||||
TextEntry::make('is_active')
|
||||
->label(__('admin.legal_documents.fields.is_active'))
|
||||
->badge()
|
||||
->formatStateUsing(fn (bool $state): string => $state
|
||||
? __('admin.legal_documents.status.active')
|
||||
: __('admin.legal_documents.status.inactive')),
|
||||
TextEntry::make('published_at')
|
||||
->label(__('admin.legal_documents.fields.published_at'))
|
||||
->dateTime()
|
||||
->placeholder(__('admin.legal_documents.placeholders.empty')),
|
||||
]),
|
||||
Section::make(__('admin.legal_documents.sections.content'))
|
||||
->schema([
|
||||
KeyValueEntry::make('title')
|
||||
->label(__('admin.legal_documents.fields.title')),
|
||||
TextEntry::make('content')
|
||||
->label(__('admin.legal_documents.fields.content'))
|
||||
->formatStateUsing(fn (mixed $state): string => json_encode($state ?: [], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?: '{}')
|
||||
->fontFamily('mono')
|
||||
->columnSpanFull(),
|
||||
]),
|
||||
Section::make(__('admin.legal_documents.sections.metadata'))
|
||||
->columns(2)
|
||||
->schema([
|
||||
TextEntry::make('created_at')
|
||||
->label(__('admin.legal_documents.fields.created_at'))
|
||||
->dateTime(),
|
||||
TextEntry::make('updated_at')
|
||||
->label(__('admin.legal_documents.fields.updated_at'))
|
||||
->dateTime(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\LegalDocuments\Tables;
|
||||
|
||||
use App\Enums\LegalDocumentType;
|
||||
use App\Filament\Resources\LegalDocuments\LegalDocumentResource;
|
||||
use App\Models\LegalDocument;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class LegalDocumentsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('id')
|
||||
->label(__('admin.legal_documents.fields.id'))
|
||||
->copyable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
TextColumn::make('slug')
|
||||
->label(__('admin.legal_documents.fields.slug'))
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('type')
|
||||
->label(__('admin.legal_documents.fields.type'))
|
||||
->badge()
|
||||
->sortable(),
|
||||
TextColumn::make('version')
|
||||
->label(__('admin.legal_documents.fields.version'))
|
||||
->numeric()
|
||||
->sortable(),
|
||||
TextColumn::make('localized_title')
|
||||
->label(__('admin.legal_documents.fields.localized_title'))
|
||||
->state(fn (LegalDocument $record): string => (string) ($record->title['fr'] ?? collect($record->title)->first() ?? __('admin.legal_documents.placeholders.empty'))),
|
||||
IconColumn::make('is_active')
|
||||
->label(__('admin.legal_documents.fields.is_active'))
|
||||
->boolean()
|
||||
->sortable(),
|
||||
TextColumn::make('published_at')
|
||||
->label(__('admin.legal_documents.fields.published_at'))
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->placeholder(__('admin.legal_documents.placeholders.empty')),
|
||||
TextColumn::make('updated_at')
|
||||
->label(__('admin.legal_documents.fields.updated_at'))
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('type')
|
||||
->label(__('admin.legal_documents.fields.type'))
|
||||
->options(LegalDocumentType::class),
|
||||
TernaryFilter::make('is_active')
|
||||
->label(__('admin.legal_documents.fields.is_active')),
|
||||
])
|
||||
->recordActions([
|
||||
ViewAction::make(),
|
||||
EditAction::make(),
|
||||
LegalDocumentResource::createNewVersionAction(),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\LegalDocumentResource;
|
||||
use App\Models\LegalDocument;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
|
||||
class LegalDocumentController extends Controller
|
||||
{
|
||||
public function index(Request $request): AnonymousResourceCollection
|
||||
{
|
||||
$query = LegalDocument::query()
|
||||
->public()
|
||||
->latest('published_at')
|
||||
->latest('version');
|
||||
|
||||
if ($request->filled('type')) {
|
||||
$query->where('type', $request->string('type')->toString());
|
||||
}
|
||||
|
||||
return LegalDocumentResource::collection($query->get());
|
||||
}
|
||||
|
||||
public function show(string $slug): LegalDocumentResource
|
||||
{
|
||||
$document = LegalDocument::publicForSlug($slug);
|
||||
|
||||
abort_if(! $document, 404, __('api.legal_documents.not_found'));
|
||||
|
||||
return new LegalDocumentResource($document);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\LegalDocument;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/** @mixin LegalDocument */
|
||||
class LegalDocumentResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'slug' => $this->slug,
|
||||
'type' => $this->type?->value,
|
||||
'typeLabel' => $this->type?->getLabel(),
|
||||
'version' => $this->version,
|
||||
'title' => $this->title,
|
||||
'content' => $this->content,
|
||||
'isActive' => $this->is_active,
|
||||
'publishedAt' => $this->published_at,
|
||||
'createdAt' => $this->created_at,
|
||||
'updatedAt' => $this->updated_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\LegalDocumentType;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class LegalDocument extends Model
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\LegalDocumentFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'slug',
|
||||
'type',
|
||||
'version',
|
||||
'title',
|
||||
'content',
|
||||
'is_active',
|
||||
'published_at',
|
||||
];
|
||||
|
||||
public function scopePublic(Builder $query): Builder
|
||||
{
|
||||
return $query
|
||||
->where('is_active', true)
|
||||
->whereNotNull('published_at')
|
||||
->where('published_at', '<=', now());
|
||||
}
|
||||
|
||||
public function scopeForSlug(Builder $query, string $slug): Builder
|
||||
{
|
||||
return $query->where('slug', $slug);
|
||||
}
|
||||
|
||||
public static function publicForSlug(string $slug): ?self
|
||||
{
|
||||
return self::query()
|
||||
->public()
|
||||
->forSlug($slug)
|
||||
->latest('version')
|
||||
->first();
|
||||
}
|
||||
|
||||
public function createNextVersion(): self
|
||||
{
|
||||
$nextVersion = ((int) self::query()
|
||||
->where('slug', $this->slug)
|
||||
->max('version')) + 1;
|
||||
|
||||
return self::query()->create([
|
||||
'slug' => $this->slug,
|
||||
'type' => $this->type,
|
||||
'version' => $nextVersion,
|
||||
'title' => $this->title,
|
||||
'content' => $this->content,
|
||||
'is_active' => false,
|
||||
'published_at' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saved(function (LegalDocument $document): void {
|
||||
if (! $document->is_active) {
|
||||
return;
|
||||
}
|
||||
|
||||
static::query()
|
||||
->where('slug', $document->slug)
|
||||
->whereKeyNot($document->getKey())
|
||||
->where('is_active', true)
|
||||
->update(['is_active' => false]);
|
||||
});
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'type' => LegalDocumentType::class,
|
||||
'version' => 'integer',
|
||||
'title' => 'array',
|
||||
'content' => 'array',
|
||||
'is_active' => 'boolean',
|
||||
'published_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user