Files
roxane/app/Filament/Actions/ServiceToggleAction.php

88 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2026-02-03 10:53:23 +01:00
<?php
namespace App\Filament\Actions;
use App\Models\Member;
2026-03-16 15:20:49 +01:00
use App\Models\Membership;
2026-02-03 10:53:23 +01:00
use Filament\Actions\Action;
use Illuminate\Support\Facades\Bus;
class ServiceToggleAction extends Action
{
protected string $serviceIdentifier;
2026-03-16 15:20:49 +01:00
/*
* Create a new action instance.
*/
2026-02-03 10:53:23 +01:00
public static function forService(string $serviceIdentifier): static
{
return static::make('toggle_'.$serviceIdentifier)
2026-02-03 10:53:23 +01:00
->configureForService($serviceIdentifier);
}
2026-03-16 15:20:49 +01:00
/**
* Configure the action for a specific service.
*/
2026-02-03 10:53:23 +01:00
protected function configureForService(string $serviceIdentifier): static
{
$this->serviceIdentifier = $serviceIdentifier;
return $this
2026-04-29 16:21:14 +02:00
->label(fn (Member|Membership|null $record) => $this->getMember($record)?->hasService($serviceIdentifier)
? 'Service actif'
: 'Activer le service'
)
2026-04-29 16:21:14 +02:00
->icon(fn (Member|Membership|null $record) => $this->getMember($record)?->hasService($serviceIdentifier)
? 'heroicon-o-check-circle'
: 'heroicon-o-x-circle'
2026-02-03 10:53:23 +01:00
)
2026-04-29 16:21:14 +02:00
->color(fn (Member|Membership|null $record) => $this->getMember($record)?->hasService($serviceIdentifier)
? 'success'
: 'warning'
2026-02-03 10:53:23 +01:00
)
->requiresConfirmation()
2026-04-29 16:21:14 +02:00
->modalHeading(fn (Member|Membership|null $record) => $this->getMember($record)?->hasService($serviceIdentifier)
2026-02-03 10:53:23 +01:00
? 'Désactiver le service'
: 'Activer le service'
)
2026-04-29 16:21:14 +02:00
->modalDescription(fn (Member|Membership|null $record) => $this->getMember($record)?->hasService($serviceIdentifier)
2026-02-03 10:53:23 +01:00
? 'Êtes-vous sûr·e de vouloir désactiver ce service pour ce membre ?'
: 'Êtes-vous sûr·e de vouloir activer ce service pour ce membre ?'
)
2026-04-29 16:21:14 +02:00
->modalSubmitActionLabel(fn (Member|Membership|null $record) => $this->getMember($record)?->hasService($serviceIdentifier)
2026-02-03 10:53:23 +01:00
? 'Désactiver'
: 'Activer'
)
2026-04-29 16:21:14 +02:00
->action(function (Member|Membership|null $record) {
2026-03-16 15:20:49 +01:00
$member = $this->getMember($record);
if (! $member) {
2026-03-16 15:20:49 +01:00
return;
}
2026-02-03 10:53:23 +01:00
// @todo à discuter
/* if ($record->hasService($serviceIdentifier)) {
Bus::dispatch(
new \App\Jobs\DisableServiceJob($record, $serviceIdentifier)
);
} else {
Bus::dispatch(
new \App\Jobs\EnableServiceJob($record, $serviceIdentifier)
);
}*/
2026-02-03 10:53:23 +01:00
});
}
2026-03-16 15:20:49 +01:00
/**
* Get the member associated with the given record.
*/
2026-04-29 16:21:14 +02:00
protected function getMember(Member|Membership|null $record): ?Member
2026-03-16 15:20:49 +01:00
{
2026-04-29 16:21:14 +02:00
if ($record === null) {
return null;
}
2026-03-16 15:20:49 +01:00
return $record instanceof Member ? $record : $record->member;
}
2026-02-03 10:53:23 +01:00
}