From ccd0a3c7c8ab3bc89910a5e5ec7ebcb0fb286383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Fri, 4 Apr 2025 12:54:05 +0100 Subject: [PATCH 1/2] feat: make shelters listable --- .../SheltersRelationManager.php | 6 +++++- .../Widgets/ReferToShelterWidget.php | 1 + app/Livewire/RequestPage.php | 1 + app/Models/Shelter.php | 12 ++++++++++++ database/factories/ShelterFactory.php | 1 + ...add_is_listed_column_to_shelters_table.php | 19 +++++++++++++++++++ lang/en/app.php | 1 + 7 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 database/migrations/0001_01_04_000009_add_is_listed_column_to_shelters_table.php diff --git a/app/Filament/Admin/Resources/OrganizationResource/RelationManagers/SheltersRelationManager.php b/app/Filament/Admin/Resources/OrganizationResource/RelationManagers/SheltersRelationManager.php index df4f954..137b5d6 100644 --- a/app/Filament/Admin/Resources/OrganizationResource/RelationManagers/SheltersRelationManager.php +++ b/app/Filament/Admin/Resources/OrganizationResource/RelationManagers/SheltersRelationManager.php @@ -9,6 +9,7 @@ use Filament\Resources\RelationManagers\RelationManager; use Filament\Tables; use Filament\Tables\Columns\TextColumn; +use Filament\Tables\Columns\ToggleColumn; use Filament\Tables\Table; class SheltersRelationManager extends RelationManager @@ -26,6 +27,10 @@ public function table(Table $table): Table return $table ->recordTitleAttribute('name') ->columns([ + ToggleColumn::make('is_listed') + ->label(__('app.field.is_listed')) + ->shrink(), + TextColumn::make('name') ->label(__('app.field.name')) ->searchable() @@ -50,7 +55,6 @@ public function table(Table $table): Table ->wrap() ->searchable() ->sortable(), - ]) ->filters([ // diff --git a/app/Filament/Shelter/Resources/RequestResource/Widgets/ReferToShelterWidget.php b/app/Filament/Shelter/Resources/RequestResource/Widgets/ReferToShelterWidget.php index dd4e9ae..7e3f8cf 100644 --- a/app/Filament/Shelter/Resources/RequestResource/Widgets/ReferToShelterWidget.php +++ b/app/Filament/Shelter/Resources/RequestResource/Widgets/ReferToShelterWidget.php @@ -33,6 +33,7 @@ public function table(Table $table): Table return $table ->query( fn () => Shelter::query() + ->whereListed() ->whereNot('id', Filament::getTenant()->id) ->with('shelterVariables') ) diff --git a/app/Livewire/RequestPage.php b/app/Livewire/RequestPage.php index 3bc0120..52735e4 100644 --- a/app/Livewire/RequestPage.php +++ b/app/Livewire/RequestPage.php @@ -87,6 +87,7 @@ public function handle() public function form(Form $form): Form { $shelters = Shelter::query() + ->whereListed() ->get(['id', 'name', 'address']); return $form diff --git a/app/Models/Shelter.php b/app/Models/Shelter.php index ecce365..f75975d 100644 --- a/app/Models/Shelter.php +++ b/app/Models/Shelter.php @@ -8,6 +8,7 @@ use App\Concerns\LogsActivity; use App\Data\PersonData; use Database\Factories\ShelterFactory; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -32,6 +33,7 @@ class Shelter extends Model 'address', 'coordinator', 'notes', + 'is_listed', ]; protected function casts(): array @@ -39,6 +41,7 @@ protected function casts(): array return [ 'capacity' => 'integer', 'coordinator' => PersonData::class, + 'is_listed' => 'boolean', ]; } @@ -75,6 +78,15 @@ public function shelterVariables(): BelongsToMany return $this->belongsToMany(ShelterVariable::class); } + public function scopeWhereListed(Builder $query): Builder + { + return $query + ->where('is_listed', true) + ->whereHas('organization', function (Builder $query) { + $query->whereActive(); + }); + } + public function availableCapacity(): Attribute { return Attribute::make( diff --git a/database/factories/ShelterFactory.php b/database/factories/ShelterFactory.php index 00ef965..62f9fc8 100644 --- a/database/factories/ShelterFactory.php +++ b/database/factories/ShelterFactory.php @@ -41,6 +41,7 @@ public function definition(): array phone: fake()->e164PhoneNumber(), ), 'notes' => fake()->paragraph(), + 'is_listed' => fake()->boolean(), ]; } diff --git a/database/migrations/0001_01_04_000009_add_is_listed_column_to_shelters_table.php b/database/migrations/0001_01_04_000009_add_is_listed_column_to_shelters_table.php new file mode 100644 index 0000000..bd9202d --- /dev/null +++ b/database/migrations/0001_01_04_000009_add_is_listed_column_to_shelters_table.php @@ -0,0 +1,19 @@ +boolean('is_listed') + ->default(true) + ->after('capacity'); + }); + } +}; diff --git a/lang/en/app.php b/lang/en/app.php index 5f4e6a1..aa7fd6f 100644 --- a/lang/en/app.php +++ b/lang/en/app.php @@ -56,6 +56,7 @@ 'id' => 'ID', 'identifier' => 'Identifier', 'is_indefinite' => 'Indefinite end date', + 'is_listed' => 'Listed', 'label' => 'Label', 'latest_stay' => 'Latest stay', 'legal_documents' => 'Legal documents', From b2e555a5ea1e56b51fc39d7d79a512216bb4452a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Fri, 4 Apr 2025 15:57:12 +0100 Subject: [PATCH 2/2] wip --- .../Actions/ListShelterAction.php | 45 +++++++++++++++++++ .../Actions/UnlistShelterAction.php | 45 +++++++++++++++++++ .../SheltersRelationManager.php | 21 ++++++--- app/Models/Shelter.php | 24 ++++++++++ ...add_is_listed_column_to_shelters_table.php | 2 +- lang/en/app.php | 18 ++++++++ 6 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 app/Filament/Admin/Resources/OrganizationResource/Actions/ListShelterAction.php create mode 100644 app/Filament/Admin/Resources/OrganizationResource/Actions/UnlistShelterAction.php diff --git a/app/Filament/Admin/Resources/OrganizationResource/Actions/ListShelterAction.php b/app/Filament/Admin/Resources/OrganizationResource/Actions/ListShelterAction.php new file mode 100644 index 0000000..1ece397 --- /dev/null +++ b/app/Filament/Admin/Resources/OrganizationResource/Actions/ListShelterAction.php @@ -0,0 +1,45 @@ +visible(fn (Shelter $record) => $record->isUnlisted() && Filament::auth()->user()->can('update', $record)); + + $this->label(__('app.shelter.actions.list.button')); + + $this->color('success'); + + $this->icon('heroicon-o-eye'); + + $this->outlined(); + + $this->requiresConfirmation(); + + $this->modalHeading(__('app.shelter.actions.list.confirm.title')); + + $this->modalDescription(__('app.shelter.actions.list.confirm.description')); + + $this->action(function (Shelter $record) { + $record->list(); + $this->success(); + }); + + $this->successNotificationTitle(__('app.shelter.actions.list.confirm.success')); + } +} diff --git a/app/Filament/Admin/Resources/OrganizationResource/Actions/UnlistShelterAction.php b/app/Filament/Admin/Resources/OrganizationResource/Actions/UnlistShelterAction.php new file mode 100644 index 0000000..44d6f80 --- /dev/null +++ b/app/Filament/Admin/Resources/OrganizationResource/Actions/UnlistShelterAction.php @@ -0,0 +1,45 @@ +visible(fn (Shelter $record) => $record->isListed() && Filament::auth()->user()->can('update', $record)); + + $this->label(__('app.shelter.actions.unlist.button')); + + $this->color('danger'); + + $this->icon('heroicon-o-eye-slash'); + + $this->outlined(); + + $this->requiresConfirmation(); + + $this->modalHeading(__('app.shelter.actions.unlist.confirm.title')); + + $this->modalDescription(__('app.shelter.actions.unlist.confirm.description')); + + $this->action(function (Shelter $record) { + $record->unlist(); + $this->success(); + }); + + $this->successNotificationTitle(__('app.shelter.actions.unlist.confirm.success')); + } +} diff --git a/app/Filament/Admin/Resources/OrganizationResource/RelationManagers/SheltersRelationManager.php b/app/Filament/Admin/Resources/OrganizationResource/RelationManagers/SheltersRelationManager.php index 137b5d6..6d001fb 100644 --- a/app/Filament/Admin/Resources/OrganizationResource/RelationManagers/SheltersRelationManager.php +++ b/app/Filament/Admin/Resources/OrganizationResource/RelationManagers/SheltersRelationManager.php @@ -4,12 +4,14 @@ namespace App\Filament\Admin\Resources\OrganizationResource\RelationManagers; +use App\Filament\Admin\Resources\OrganizationResource\Actions\ListShelterAction; +use App\Filament\Admin\Resources\OrganizationResource\Actions\UnlistShelterAction; use App\Filament\Admin\Resources\OrganizationResource\Schemas\SheltersForm; use Filament\Forms\Form; use Filament\Resources\RelationManagers\RelationManager; use Filament\Tables; +use Filament\Tables\Columns\IconColumn; use Filament\Tables\Columns\TextColumn; -use Filament\Tables\Columns\ToggleColumn; use Filament\Tables\Table; class SheltersRelationManager extends RelationManager @@ -27,8 +29,9 @@ public function table(Table $table): Table return $table ->recordTitleAttribute('name') ->columns([ - ToggleColumn::make('is_listed') + IconColumn::make('is_listed') ->label(__('app.field.is_listed')) + ->boolean() ->shrink(), TextColumn::make('name') @@ -64,9 +67,17 @@ public function table(Table $table): Table ->createAnother(false), ]) ->actions([ - Tables\Actions\ViewAction::make(), - Tables\Actions\EditAction::make(), - Tables\Actions\DeleteAction::make(), + Tables\Actions\ActionGroup::make([ + Tables\Actions\ViewAction::make(), + + Tables\Actions\EditAction::make(), + + ListShelterAction::make(), + + UnlistShelterAction::make(), + + Tables\Actions\DeleteAction::make(), + ]), ]); } } diff --git a/app/Models/Shelter.php b/app/Models/Shelter.php index f75975d..f82a933 100644 --- a/app/Models/Shelter.php +++ b/app/Models/Shelter.php @@ -95,4 +95,28 @@ public function availableCapacity(): Attribute ->count(), ); } + + public function isListed(): bool + { + return $this->is_listed; + } + + public function isUnlisted(): bool + { + return ! $this->isListed(); + } + + public function list(): bool + { + return $this->update([ + 'is_listed' => true, + ]); + } + + public function unlist(): bool + { + return $this->update([ + 'is_listed' => false, + ]); + } } diff --git a/database/migrations/0001_01_04_000009_add_is_listed_column_to_shelters_table.php b/database/migrations/0001_01_04_000009_add_is_listed_column_to_shelters_table.php index bd9202d..1d41a14 100644 --- a/database/migrations/0001_01_04_000009_add_is_listed_column_to_shelters_table.php +++ b/database/migrations/0001_01_04_000009_add_is_listed_column_to_shelters_table.php @@ -12,7 +12,7 @@ public function up(): void { Schema::table('shelters', function (Blueprint $table) { $table->boolean('is_listed') - ->default(true) + ->default(false) ->after('capacity'); }); } diff --git a/lang/en/app.php b/lang/en/app.php index aa7fd6f..89fb822 100644 --- a/lang/en/app.php +++ b/lang/en/app.php @@ -259,6 +259,24 @@ 'plural' => 'shelters', ], 'profile' => 'Profile', + 'actions' => [ + 'list' => [ + 'button' => 'List shelter', + 'confirm' => [ + 'description' => 'Once a shelter is listed, it will be displayed in the public request form and in the referral process. In order to eliminate the shelter from these lists, it needs to be unlisted.', + 'title' => 'List shelter', + 'success' => 'Shelter listed successfully.', + ], + ], + 'unlist' => [ + 'button' => 'Unlist shelter', + 'confirm' => [ + 'description' => 'Once a shelter is unlisted, it will no longer be displayed in the public request form or in the referral process. To include the shelter in these lists again, it needs to be listed.', + 'title' => 'Unlist shelter', + 'success' => 'Shelter unlisted successfully.', + ], + ], + ], ], 'user' => [ 'label' => [