From d6505c653d65b48d944f52f536452d52afdffad8 Mon Sep 17 00:00:00 2001 From: marco76tv Date: Thu, 7 Dec 2023 16:21:18 +0100 Subject: [PATCH] Refactor HandlesTreeBuilder trait --- .../Pages/Concerns/HandlesTreeBuilder.php | 65 ++++++++++++++----- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/Filament/Resources/TreeResource/Pages/Concerns/HandlesTreeBuilder.php b/Filament/Resources/TreeResource/Pages/Concerns/HandlesTreeBuilder.php index 50a2fa81..fe690d4f 100644 --- a/Filament/Resources/TreeResource/Pages/Concerns/HandlesTreeBuilder.php +++ b/Filament/Resources/TreeResource/Pages/Concerns/HandlesTreeBuilder.php @@ -1,4 +1,7 @@ getResource()::getModel(); + // $model::where('id', $item['id'])->delete(); + // data_set($this, $parentPath, Arr::except($parent, $uuid)); + $this->mountedItem = $statePath; + $this->mountAction('delete'); + } + + public function deleteItem(?Model $record, array $data): void + { + $statePath = $this->mountedItem; $uuid = Str::afterLast($statePath, '.'); $parentPath = Str::beforeLast($statePath, '.'); $parent = data_get($this, $parentPath); + $item = Arr::except(data_get($this, $statePath), 'children'); + + $model = $this->getResource()::getModel(); + $model::where('id', $item['id'])->delete(); data_set($this, $parentPath, Arr::except($parent, $uuid)); + $this->mountedItem = null; } public function editItem(string $statePath): void @@ -122,6 +145,17 @@ public function storeChildItem(Model $record, array $data): void { $parent = data_get($this, $this->mountedChildTarget); $data['parent_id'] = $parent['id']; + if (Str::contains($data['parent_id'], '-')) { + $last_son = $record::class::where('parent_id', $data['parent_id']) + ->orderByDesc('id') + ->first(); + if (null == $last_son) { + $data['id'] = $data['parent_id'].'-1'; + } else { + $new_id = intval(Str::afterLast($last_son['id'], '-')) + 1; + $data['id'] = $data['parent_id'].'-'.$new_id; + } + } $row = $record::class::create($data); $data = $row->toArray(); @@ -159,6 +193,16 @@ protected function getHeaderActions(): array // $formSchema=$this->getFormSchema(); return [ + Action::make('delete') + ->action(function (array $data, $record): void { + if ($this->mountedItem) { // delete + $this->deleteItem($record, $data); + + return; + } + }) + ->requiresConfirmation() + ->visible(null != $this->mountedItem), Action::make('item') ->mountUsing(function (ComponentContainer $form): void { if (! $this->mountedItem) { @@ -170,17 +214,6 @@ protected function getHeaderActions(): array ->form($formSchema) ->modalWidth('xl') ->action(function (array $data, $record): void { - /* - dddx([ - 'record'=>$record, - 'data'=>$data, - 'mountedItemData'=>$this->mountedItemData, - 'mountedActionData'=>$this->mountedActionData, - 'mountedItem'=>$this->mountedItem, - 'mountedChildTarget'=>$this->mountedChildTarget, - ]); - //*/ - if ($this->mountedItem) { // UPDATE $this->updateItem($record, $data); @@ -197,7 +230,7 @@ protected function getHeaderActions(): array $this->storeItem($record, $data); }) - ->modalButton(__('filament-navigation::filament-navigation.items-modal.btn')) + ->modalSubmitActionLabel(__('filament-navigation::filament-navigation.items-modal.btn')) ->label(__('filament-navigation::filament-navigation.items-modal.title')), ]; }