From 2dbb9a5f9bf7b121f4be1fe8efc4c15e559e9d29 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Tue, 18 Mar 2025 17:42:04 +0100 Subject: [PATCH] Add update egg bulk action (#1122) * add update egg bulk action * make phpstan happy * use `before` --- .../Resources/EggResource/Pages/ListEggs.php | 14 +++- .../Tables/Actions/UpdateEggAction.php | 8 +- .../Tables/Actions/UpdateEggBulkAction.php | 80 +++++++++++++++++++ lang/en/admin/egg.php | 11 +-- 4 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 app/Filament/Components/Tables/Actions/UpdateEggBulkAction.php diff --git a/app/Filament/Admin/Resources/EggResource/Pages/ListEggs.php b/app/Filament/Admin/Resources/EggResource/Pages/ListEggs.php index 9a6095b57..308584d12 100644 --- a/app/Filament/Admin/Resources/EggResource/Pages/ListEggs.php +++ b/app/Filament/Admin/Resources/EggResource/Pages/ListEggs.php @@ -7,6 +7,7 @@ use App\Filament\Components\Actions\ImportEggAction as ImportEggHeaderAction; use App\Filament\Components\Tables\Actions\ExportEggAction; use App\Filament\Components\Tables\Actions\ImportEggAction; use App\Filament\Components\Tables\Actions\UpdateEggAction; +use App\Filament\Components\Tables\Actions\UpdateEggBulkAction; use App\Filament\Components\Tables\Filters\TagsFilter; use App\Models\Egg; use Filament\Actions\CreateAction as CreateHeaderAction; @@ -17,6 +18,7 @@ use Filament\Tables\Actions\EditAction; use Filament\Tables\Actions\ReplicateAction; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Str; class ListEggs extends ListRecords @@ -28,7 +30,6 @@ class ListEggs extends ListRecords return $table ->searchable(true) ->defaultPaginationPageOption(25) - ->checkIfRecordIsSelectableUsing(fn (Egg $egg) => $egg->servers_count <= 0) ->columns([ TextColumn::make('id') ->label('Id') @@ -69,7 +70,16 @@ class ListEggs extends ListRecords ->successRedirectUrl(fn (Egg $replica) => EditEgg::getUrl(['record' => $replica])), ]) ->groupedBulkActions([ - DeleteBulkAction::make(), + DeleteBulkAction::make() + ->before(fn (DeleteBulkAction $action, Collection $records) => $action->records($records->filter(function ($egg) { + /** @var Egg $egg */ + return $egg->servers_count <= 0; + }))), + UpdateEggBulkAction::make() + ->before(fn (UpdateEggBulkAction $action, Collection $records) => $action->records($records->filter(function ($egg) { + /** @var Egg $egg */ + return cache()->get("eggs.$egg->uuid.update", false); + }))), ]) ->emptyStateIcon('tabler-eggs') ->emptyStateDescription('') diff --git a/app/Filament/Components/Tables/Actions/UpdateEggAction.php b/app/Filament/Components/Tables/Actions/UpdateEggAction.php index 0f286c201..dd3b79218 100644 --- a/app/Filament/Components/Tables/Actions/UpdateEggAction.php +++ b/app/Filament/Components/Tables/Actions/UpdateEggAction.php @@ -20,7 +20,7 @@ class UpdateEggAction extends Action { parent::setUp(); - $this->label(trans('admin/egg.update')); + $this->label(trans_choice('admin/egg.update', 1)); $this->icon('tabler-cloud-download'); @@ -28,9 +28,9 @@ class UpdateEggAction extends Action $this->requiresConfirmation(); - $this->modalHeading(trans('admin/egg.update_question')); + $this->modalHeading(trans_choice('admin/egg.update_question', 1)); - $this->modalDescription(trans('admin/egg.update_description')); + $this->modalDescription(trans_choice('admin/egg.update_description', 1)); $this->modalIconColor('danger'); @@ -54,7 +54,7 @@ class UpdateEggAction extends Action } Notification::make() - ->title(trans('admin/egg.updated')) + ->title(trans_choice('admin/egg.updated', 1)) ->body($egg->name) ->success() ->send(); diff --git a/app/Filament/Components/Tables/Actions/UpdateEggBulkAction.php b/app/Filament/Components/Tables/Actions/UpdateEggBulkAction.php new file mode 100644 index 000000000..8f4b70a2a --- /dev/null +++ b/app/Filament/Components/Tables/Actions/UpdateEggBulkAction.php @@ -0,0 +1,80 @@ +label(trans_choice('admin/egg.update', 2)); + + $this->icon('tabler-cloud-download'); + + $this->color('success'); + + $this->requiresConfirmation(); + + $this->modalHeading(trans_choice('admin/egg.update_question', 2)); + + $this->modalDescription(trans_choice('admin/egg.update_description', 2)); + + $this->modalIconColor('danger'); + + $this->modalSubmitAction(fn (StaticAction $action) => $action->color('danger')); + + $this->action(function (Collection $records, EggImporterService $eggImporterService) { + if ($records->count() === 0) { + Notification::make() + ->title(trans('admin/egg.no_updates')) + ->warning() + ->send(); + + return; + } + + $success = 0; + $failed = 0; + + /** @var Egg $egg */ + foreach ($records as $egg) { + try { + $eggImporterService->fromUrl($egg->update_url, $egg); + + $success++; + + cache()->forget("eggs.$egg->uuid.update"); + } catch (Exception $exception) { + $failed++; + + report($exception); + } + } + + Notification::make() + ->title(trans_choice('admin/egg.updated', 2, ['count' => $success, 'total' => $records->count()])) + ->body($failed > 0 ? trans('admin/egg.updated_failed', ['count' => $failed]) : null) + ->status($failed > 0 ? 'warning' : 'success') + ->persistent() + ->send(); + }); + + $this->authorize(fn () => auth()->user()->can('import egg')); + + $this->deselectRecordsAfterCompletion(); + } +} diff --git a/lang/en/admin/egg.php b/lang/en/admin/egg.php index 946c208e8..1f5e8423f 100644 --- a/lang/en/admin/egg.php +++ b/lang/en/admin/egg.php @@ -79,9 +79,10 @@ return [ 'no_servers' => 'No Servers', 'no_servers_help' => 'No Servers are assigned to this Egg.', - 'update' => 'Update', - 'updated' => 'Egg updated', - 'update_failed' => 'Egg Update Failed', - 'update_question' => 'Are you sure you want to update this egg?', - 'update_description' => 'If you made any changes to the egg they will be overwritten!', + 'update' => 'Update|Update selected', + 'updated' => 'Egg updated|:count/:total Eggs updated', + 'updated_failed' => ':count failed', + 'update_question' => 'Are you sure you want to update this egg?|Are you sure you want to update the selected eggs?', + 'update_description' => 'If you made any changes to the egg they will be overwritten!|If you made any changes to the eggs they will be overwritten!', + 'no_updates' => 'No updates for the selected eggs available', ];