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: 70 additions & 0 deletions app/Filament/Admin/Actions/MergeBulkAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace App\Filament\Admin\Actions;

use Filament\Actions\Concerns\CanCustomizeProcess;
use Filament\Support\Facades\FilamentIcon;
use Filament\Tables\Actions\BulkAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class MergeBulkAction 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') ?? 'heroicon-m-trash');

$this->requiresConfirmation();

$this->modalIcon(FilamentIcon::resolve('actions::merge-action.modal') ?? 'heroicon-o-trash');

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

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

$data = $keep->toArray();

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

$keep->update($data);

// TODO: handle relationships

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

$this->success();
});

$this->deselectRecordsAfterCompletion();
}
}
96 changes: 96 additions & 0 deletions app/Filament/Admin/Resources/LocationResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Actions\MergeBulkAction;
use App\Filament\Admin\Resources\LocationResource\Pages;
use App\Models\Location;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Concerns\Translatable;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class LocationResource extends Resource
{
use Translatable;

protected static ?string $model = Location::class;

protected static ?string $navigationIcon = 'heroicon-o-map-pin';

protected static bool $isScopedToTenant = false;

public static function getNavigationGroup(): ?string
{
return __('app.navigation.configurations');
}

public static function getModelLabel(): string
{
return __('app.location.label.singular');
}

public static function getPluralModelLabel(): string
{
return __('app.location.label.plural');
}

public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->label(__('app.field.name'))
->columnSpanFull()
->maxLength(200)
->required(),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')
->label(__('app.field.id'))
->sortable()
->shrink(),

TextColumn::make('name')
->label(__('app.field.name'))
->searchable()
->sortable(),
])
->filters([
//
])
->actions([
Tables\Actions\ActionGroup::make([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
]),
])
->bulkActions([
MergeBulkAction::make(),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ManageLocations::route('/'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

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

use App\Filament\Admin\Resources\LocationResource;
use Filament\Actions;
use Filament\Resources\Pages\ManageRecords;

class ManageLocations extends ManageRecords
{
use ManageRecords\Concerns\Translatable;

protected static string $resource = LocationResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
Actions\LocaleSwitcher::make(),
];
}
}
27 changes: 27 additions & 0 deletions app/Models/Location.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Database\Factories\LocationFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class Location extends Model
{
/** @use HasFactory<LocationFactory> */
use HasFactory;
use HasTranslations;

protected static string $factory = LocationFactory::class;

protected $fillable = [
'name',
];

public array $translatable = [
'name',
];
}
67 changes: 67 additions & 0 deletions app/Policies/LocationPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace App\Policies;

use App\Models\Location;
use App\Models\User;

class LocationPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return true;
}

/**
* Determine whether the user can view the model.
*/
public function view(User $user, Location $location): bool
{
return true;
}

/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return true;
}

/**
* Determine whether the user can update the model.
*/
public function update(User $user, Location $location): bool
{
return true;
}

/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Location $location): bool
{
return true;
}

/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Location $location): bool
{
return $this->delete($user, $location);
}

/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Location $location): bool
{
return $this->delete($user, $location);
}
}
25 changes: 25 additions & 0 deletions database/factories/LocationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Location>
*/
class LocationFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->city(),
];
}
}
19 changes: 19 additions & 0 deletions database/migrations/0001_01_02_000002_create_locations_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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::create('locations', function (Blueprint $table) {
$table->id();
$table->json('name');
$table->timestamps();
});
}
};
6 changes: 6 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;

use App\Models\Location;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Mail;
Expand All @@ -21,5 +23,9 @@ public function run(): void
User::factory(['email' => 'admin@example.com'])
->superAdmin()
->create();

Location::factory()
->count(50)
->create();
}
}
45 changes: 45 additions & 0 deletions lang/en/actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

return [

'merge' => [
'single' => [
'label' => 'Merge',
'modal' => [
'heading' => 'Merge :label',
'actions' => [
'merge' => [
'label' => 'Merge',
],
],
],

'notifications' => [
'merged' => [
'title' => 'Merged',
],
],
],

'multiple' => [
'label' => 'Merge selected',
'modal' => [
'heading' => 'Merge selected :label',
'actions' => [
'merge' => [
'label' => 'Merge',
],
],
],

'notifications' => [
'merged' => [
'title' => 'Merged',
],
],
],
],

];
Loading