generated from filamentphp/plugin-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Log1x/feat/refactor-menu-locations
♻ Refactor the menu location implementation
- Loading branch information
Showing
11 changed files
with
167 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Datlechin\FilamentMenuBuilder\Concerns; | ||
|
||
use Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin; | ||
use Datlechin\FilamentMenuBuilder\Models\MenuLocation; | ||
use Filament\Actions\Action; | ||
use Filament\Forms\Components; | ||
use Filament\Notifications\Notification; | ||
use Filament\Support\Enums\MaxWidth; | ||
use Illuminate\Support\Collection; | ||
|
||
trait HasLocationAction | ||
{ | ||
protected ?Collection $menuLocations = null; | ||
|
||
public function getLocationAction(): Action | ||
{ | ||
return Action::make('locations') | ||
->label(__('filament-menu-builder::menu-builder.actions.locations.label')) | ||
->modalHeading(__('filament-menu-builder::menu-builder.actions.locations.heading')) | ||
->modalDescription(__('filament-menu-builder::menu-builder.actions.locations.description')) | ||
->modalSubmitActionLabel(__('filament-menu-builder::menu-builder.actions.locations.submit')) | ||
->modalWidth(MaxWidth::Large) | ||
->color('gray') | ||
->fillForm(fn () => $this->getRegisteredLocations()->map(fn ($location, $key) => [ | ||
'location' => $location, | ||
'menu' => $this->getMenuLocations()->where('location', $key)->first()?->menu_id, | ||
])->all()) | ||
->action(function (array $data) { | ||
$locations = collect($data) | ||
->map(fn ($item) => $item['menu'] ?? null) | ||
->all(); | ||
|
||
$this->getMenuLocations() | ||
->pluck('location') | ||
->diff($this->getRegisteredLocations()->keys()) | ||
->each(fn ($location) => $this->getMenuLocations()->where('location', $location)->each->delete()); | ||
|
||
foreach ($locations as $location => $menu) { | ||
if (! $menu) { | ||
$this->getMenuLocations()->where('location', $location)->each->delete(); | ||
|
||
continue; | ||
} | ||
|
||
MenuLocation::updateOrCreate( | ||
['location' => $location], | ||
['menu_id' => $menu] | ||
); | ||
} | ||
|
||
Notification::make() | ||
->title(__('filament-menu-builder::menu-builder.notifications.locations.title')) | ||
->success() | ||
->send(); | ||
}) | ||
->form(fn () => $this->getRegisteredLocations()->map( | ||
fn ($location, $key) => Components\Grid::make(2) | ||
->statePath($key) | ||
->schema([ | ||
Components\TextInput::make('location') | ||
->label(__('filament-menu-builder::menu-builder.actions.locations.form.location.label')) | ||
->hiddenLabel($key !== $this->getRegisteredLocations()->keys()->first()) | ||
->disabled(), | ||
|
||
Components\Select::make('menu') | ||
->label(__('filament-menu-builder::menu-builder.actions.locations.form.menu.label')) | ||
->searchable() | ||
->hiddenLabel($key !== $this->getRegisteredLocations()->keys()->first()) | ||
->options($this->getModel()::all()->pluck('name', 'id')->all()), | ||
]) | ||
)->all()); | ||
} | ||
|
||
protected function getMenuLocations(): Collection | ||
{ | ||
return $this->menuLocations ??= MenuLocation::all(); | ||
} | ||
|
||
protected function getRegisteredLocations(): Collection | ||
{ | ||
return collect(FilamentMenuBuilderPlugin::get()->getLocations()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters