Laravel 13 + Filament 5 admin panel and REST API for a non-profit developer collective, backed by the Pireon OAuth identity server. The public site is a separate, server-rendered application consuming /api/public/v1. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
221 lines
7.6 KiB
PHP
221 lines
7.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Resources\Clients;
|
|
|
|
use App\Filament\RelationManagers\RevisionsRelationManager;
|
|
use App\Filament\Resources\Clients\Pages\CreateClient;
|
|
use App\Filament\Resources\Clients\Pages\EditClient;
|
|
use App\Filament\Resources\Clients\Pages\ListClients;
|
|
use App\Filament\Support\TranslatableTabs;
|
|
use App\Models\Client;
|
|
use BackedEnum;
|
|
use Filament\Actions\BulkActionGroup;
|
|
use Filament\Actions\DeleteAction;
|
|
use Filament\Actions\DeleteBulkAction;
|
|
use Filament\Actions\EditAction;
|
|
use Filament\Actions\ForceDeleteBulkAction;
|
|
use Filament\Actions\RestoreBulkAction;
|
|
use Filament\Forms\Components\FileUpload;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Toggle;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Schemas\Components\Section;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Support\Icons\Heroicon;
|
|
use Filament\Tables\Columns\IconColumn;
|
|
use Filament\Tables\Columns\ImageColumn;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Filters\Filter;
|
|
use Filament\Tables\Filters\TernaryFilter;
|
|
use Filament\Tables\Filters\TrashedFilter;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
use UnitEnum;
|
|
|
|
/**
|
|
* The organisations the work was for.
|
|
*
|
|
* This used to be a name typed into every project by hand. Being a record
|
|
* makes the wall of logos possible, stops the same association becoming two
|
|
* entities because of a typo, and gives "everything we did for them" an
|
|
* answer.
|
|
*/
|
|
class ClientResource extends Resource
|
|
{
|
|
protected static ?string $model = Client::class;
|
|
|
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedBuildingOffice2;
|
|
|
|
protected static ?int $navigationSort = 5;
|
|
|
|
public static function getModelLabel(): string
|
|
{
|
|
return __('panel.clients.label');
|
|
}
|
|
|
|
public static function getPluralModelLabel(): string
|
|
{
|
|
return __('panel.clients.plural');
|
|
}
|
|
|
|
public static function getNavigationGroup(): string|UnitEnum|null
|
|
{
|
|
return __('panel.groups.collective');
|
|
}
|
|
|
|
public static function getRecordTitle(?Model $record): ?string
|
|
{
|
|
return $record?->name;
|
|
}
|
|
|
|
public static function form(Schema $schema): Schema
|
|
{
|
|
return $schema->components([
|
|
Section::make()
|
|
->columns(2)
|
|
->schema([
|
|
// A proper noun: an association is called what it is called
|
|
// in every language, so this is not translated.
|
|
TextInput::make('name')
|
|
->label(__('panel.clients.fields.name'))
|
|
->required()
|
|
->maxLength(255)
|
|
->live(onBlur: true)
|
|
->afterStateUpdated(function (?string $state, callable $get, callable $set) {
|
|
if (blank($get('slug'))) {
|
|
$set('slug', Str::slug((string) $state));
|
|
}
|
|
}),
|
|
|
|
TextInput::make('slug')
|
|
->label(__('panel.clients.fields.slug'))
|
|
->required()
|
|
->maxLength(255)
|
|
->rules(fn ($record) => [
|
|
Rule::unique('clients', 'slug')->ignore($record?->id),
|
|
]),
|
|
|
|
TextInput::make('url')
|
|
->label(__('panel.clients.fields.url'))
|
|
->url()
|
|
->maxLength(2048)
|
|
->columnSpanFull(),
|
|
|
|
FileUpload::make('logo')
|
|
->label(__('panel.clients.fields.logo'))
|
|
->image()
|
|
->disk(config('media.disk'))
|
|
->directory('clients')
|
|
->helperText(__('panel.clients.fields.logo_hint'))
|
|
->columnSpanFull(),
|
|
|
|
TranslatableTabs::make(fn (string $locale) => [
|
|
TextInput::make("sector.{$locale}")
|
|
->label(__('panel.clients.fields.sector'))
|
|
->maxLength(120)
|
|
->columnSpanFull(),
|
|
])->columnSpanFull(),
|
|
|
|
Toggle::make('is_public')
|
|
->label(__('panel.clients.fields.is_public'))
|
|
// Not everybody wants to appear on somebody else's
|
|
// website, and that is their call, not ours.
|
|
->helperText(__('panel.clients.fields.is_public_hint'))
|
|
->default(true),
|
|
|
|
TextInput::make('position')
|
|
->label(__('panel.clients.fields.position'))
|
|
->numeric()
|
|
->default(0),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->defaultSort('position')
|
|
->reorderable('position')
|
|
->columns([
|
|
ImageColumn::make('logo')
|
|
->label('')
|
|
->disk(config('media.disk'))
|
|
->height(32),
|
|
|
|
TextColumn::make('name')
|
|
->label(__('panel.clients.fields.name'))
|
|
->weight('medium')
|
|
->description(fn (Client $record) => $record->getTranslation('sector', app()->getLocale(), false) ?: null)
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
TextColumn::make('projects_count')
|
|
->label(__('panel.clients.fields.projects'))
|
|
->counts('projects')
|
|
->badge()
|
|
->color('gray'),
|
|
|
|
IconColumn::make('is_public')
|
|
->label(__('panel.clients.fields.is_public'))
|
|
->boolean(),
|
|
|
|
TextColumn::make('url')
|
|
->label(__('panel.clients.fields.url'))
|
|
->url(fn (Client $record) => $record->url)
|
|
->openUrlInNewTab()
|
|
->placeholder('—')
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
])
|
|
->filters([
|
|
TernaryFilter::make('is_public')->label(__('panel.clients.fields.is_public')),
|
|
|
|
// A client with no logo is a hole in the wall of logos.
|
|
Filter::make('without_logo')
|
|
->label(__('panel.clients.filters.without_logo'))
|
|
->query(fn (Builder $query) => $query->whereNull('logo')),
|
|
|
|
TrashedFilter::make(),
|
|
])
|
|
->recordActions([
|
|
EditAction::make(),
|
|
DeleteAction::make(),
|
|
])
|
|
->toolbarActions([
|
|
BulkActionGroup::make([
|
|
DeleteBulkAction::make(),
|
|
ForceDeleteBulkAction::make(),
|
|
RestoreBulkAction::make(),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, string>
|
|
*/
|
|
public static function getGloballySearchableAttributes(): array
|
|
{
|
|
return ['name'];
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
RevisionsRelationManager::class,
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => ListClients::route('/'),
|
|
'create' => CreateClient::route('/create'),
|
|
'edit' => EditClient::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|