Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 0 additions & 70 deletions app/Filament/Admin/Actions/MergeBulkAction.php

This file was deleted.

9 changes: 6 additions & 3 deletions app/Filament/Admin/Resources/LocationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Actions\MergeBulkAction;
use App\Filament\Admin\Resources\LocationResource\Actions\MergeLocationsBulkAction;
use App\Filament\Admin\Resources\LocationResource\Pages;
use App\Models\Location;
use Filament\Forms\Components\TextInput;
Expand Down Expand Up @@ -47,7 +47,7 @@ public static function form(Form $form): Form
TextInput::make('name')
->label(__('app.field.name'))
->columnSpanFull()
->maxLength(200)
->maxLength(100)
->required(),
]);
}
Expand All @@ -64,6 +64,9 @@ public static function table(Table $table): Table

TextColumn::make('name')
->label(__('app.field.name'))
->lineClamp(2)
->limit(100)
->wrap()
->searchable()
->sortable(),
])
Expand All @@ -77,7 +80,7 @@ public static function table(Table $table): Table
]),
])
->bulkActions([
MergeBulkAction::make(),
MergeLocationsBulkAction::make(),
]);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace App\Filament\Admin\Resources\LocationResource\Actions;

use App\Models\Organization;
use App\Models\Shelter;
use Filament\Actions\Concerns\CanCustomizeProcess;
use Filament\Support\Facades\FilamentIcon;
use Filament\Tables\Actions\BulkAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class MergeLocationsBulkAction extends BulkAction
{
use CanCustomizeProcess;

public static function getDefaultName(): ?string
{
return 'merge';
}

protected function setUp(): void
{
parent::setUp();

$this->label(__('actions.merge.multiple.label'));

$this->modalHeading(fn () => __('actions.merge.multiple.modal.heading', ['label' => $this->getPluralModelLabel()]));

$this->modalSubmitActionLabel(__('actions.merge.multiple.modal.actions.merge.label'));

$this->successNotificationTitle(__('actions.merge.multiple.notifications.merged.title'));

$this->color('warning');

$this->outlined();

$this->icon(FilamentIcon::resolve('actions::merge-action') ?? 'majestic-git-pull-line');

$this->requiresConfirmation();

$this->modalIcon(FilamentIcon::resolve('actions::merge-action.modal') ?? 'majestic-git-pull-line');

$this->action(function (Collection $records) {
$keep = $records
->sortBy('id')
->first();

$records = $records->reject(fn (Model $record) => $record->is($keep));

$name = data_get($keep->toArray(), 'name');

$records->each(function (Model $record) use (&$name) {
$name = array_merge_recursive($name, data_get($record->toArray(), 'name'));
});

$name = collect($name)
->map(function (array | string $items) {
if (\is_array($items)) {
return collect($items)
->unique()
->join(', ');
}

return $items;
})
->toArray();

DB::transaction(function () use ($records, $keep, $name) {
$keep->replaceTranslations('name', $name)
->save();

Organization::query()
->whereIn('location_id', $records->pluck('id'))
->get()
->each->update(['location_id' => $keep->id]);

Shelter::query()
->whereIn('location_id', $records->pluck('id'))
->get()
->each->update(['location_id' => $keep->id]);

$records->each->delete();
});

$this->success();
});

$this->deselectRecordsAfterCompletion();
}
}
3 changes: 3 additions & 0 deletions app/Filament/Admin/Resources/OrganizationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public static function table(Table $table): Table

TextColumn::make('location.name')
->label(__('app.field.location'))
->lineClamp(2)
->limit(100)
->wrap()
->searchable()
->sortable(),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function table(Table $table): Table

TextColumn::make('location.name')
->label(__('app.field.location'))
->lineClamp(2)
->limit(100)
->wrap()
->searchable()
->sortable(),

Expand Down
13 changes: 13 additions & 0 deletions app/Models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Database\Factories\LocationFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Translatable\HasTranslations;

class Location extends Model
Expand All @@ -16,6 +18,7 @@ class Location extends Model
use HasFactory;
use HasTranslations;
use LogsActivity;
use SoftDeletes;

protected static string $factory = LocationFactory::class;

Expand All @@ -26,4 +29,14 @@ class Location extends Model
public array $translatable = [
'name',
];

public function organizations(): HasMany
{
return $this->hasMany(Organization::class);
}

public function shelters(): HasMany
{
return $this->hasMany(Shelter::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('locations', function (Blueprint $table) {
$table->timestamp('deleted_at')->nullable();
});
}
};
4 changes: 2 additions & 2 deletions tests/Feature/Admin/LocationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Tests\Feature\Admin;

use App\Filament\Admin\Actions\MergeBulkAction;
use App\Filament\Admin\Resources\LocationResource\Actions\MergeLocationsBulkAction;
use App\Filament\Admin\Resources\LocationResource\Pages\ManageLocations;
use App\Models\Location;
use App\Models\User;
Expand Down Expand Up @@ -87,7 +87,7 @@ public function superadmins_can_bulk_merge_two_locations(): void

Livewire::test(ManageLocations::class)
->assertCountTableRecords(2)
->callTableBulkAction(MergeBulkAction::class, $locations->pluck('id'))
->callTableBulkAction(MergeLocationsBulkAction::class, $locations->pluck('id'))
->assertHasNoTableActionErrors()
->assertCountTableRecords(1)
->assertSeeText($locations->pluck('name')->join(', '));
Expand Down
Loading