From c2bda310496995087bbe18af567b576c86f3af64 Mon Sep 17 00:00:00 2001 From: Ishara Ekanayaka <133479172+IsharaEkanayaka@users.noreply.github.com> Date: Mon, 6 Jan 2025 13:01:44 +0530 Subject: [PATCH] 182 UI be taxonomy improvements livewire (#249) --- app/Domains/Taxonomy/Models/Taxonomy.php | 6 ++ app/Domains/Taxonomy/Models/TaxonomyTerm.php | 5 + .../Backend/TaxonomyController.php | 57 +++++++----- .../Livewire/Backend/TaxonomyTermMetadata.php | 28 ++++++ database/seeders/TaxonomySeeder.php | 2 +- database/seeders/TaxonomyTermSeeder.php | 4 +- .../views/backend/taxonomy/edit.blade.php | 8 +- .../backend/taxonomy/terms/create.blade.php | 63 +------------ .../backend/taxonomy/terms/edit.blade.php | 86 +---------------- .../backend/taxonomy-term-metadata.blade.php | 93 +++++++++++++++++++ 10 files changed, 175 insertions(+), 177 deletions(-) create mode 100644 app/Http/Livewire/Backend/TaxonomyTermMetadata.php create mode 100644 resources/views/livewire/backend/taxonomy-term-metadata.blade.php diff --git a/app/Domains/Taxonomy/Models/Taxonomy.php b/app/Domains/Taxonomy/Models/Taxonomy.php index 08d8aca..5099fc1 100644 --- a/app/Domains/Taxonomy/Models/Taxonomy.php +++ b/app/Domains/Taxonomy/Models/Taxonomy.php @@ -47,6 +47,12 @@ class Taxonomy extends Model 'updated_at' => 'datetime', ]; + //mutator for saving properties + public function setPropertiesAttribute($value) + { + $this->attributes['properties'] = json_encode($value, JSON_UNESCAPED_SLASHES); + } + public function user() { return $this->belongsTo(User::class, 'created_by'); diff --git a/app/Domains/Taxonomy/Models/TaxonomyTerm.php b/app/Domains/Taxonomy/Models/TaxonomyTerm.php index a53a102..ec40ed0 100644 --- a/app/Domains/Taxonomy/Models/TaxonomyTerm.php +++ b/app/Domains/Taxonomy/Models/TaxonomyTerm.php @@ -33,6 +33,11 @@ class TaxonomyTerm extends Model 'updated_at' => 'datetime', ]; + public function setMetadataAttribute($value) + { + $this->attributes['metadata'] = json_encode($value, JSON_UNESCAPED_SLASHES); + } + public function getFormattedMetadataAttribute() { $response = array(); diff --git a/app/Http/Controllers/Backend/TaxonomyController.php b/app/Http/Controllers/Backend/TaxonomyController.php index aeb3b98..6ad38fe 100644 --- a/app/Http/Controllers/Backend/TaxonomyController.php +++ b/app/Http/Controllers/Backend/TaxonomyController.php @@ -50,6 +50,7 @@ public function store(Request $request) 'code' => 'required|unique:taxonomies', 'name' => 'required', 'description' => 'nullable', + 'properties' => 'string' ]); try { @@ -97,18 +98,18 @@ public function update(Request $request, Taxonomy $taxonomy) 'description' => 'nullable', 'properties' => 'string' ]); - try { - $originalProperties = json_decode($taxonomy->properties); + $originalProperties = json_decode(json_encode($taxonomy->properties)); $updatedProperties = json_decode($request->properties); - if ($this->validateProperties($originalProperties, $updatedProperties)) { - $taxonomy->update($data); - $taxonomy->properties = $request->properties; - $taxonomy->updated_by = Auth::user()->id; - $taxonomy->save(); - }else{ - return redirect()->route('dashboard.taxonomy.index')->withErrors('Can not update the Taxonomy Properties as it already has associated Taxonomy Terms. Please reassign or delete those first.'); + if ($taxonomy->terms->count() > 0 && !$this->validateProperties($originalProperties, $updatedProperties)) { + return redirect() + ->route('dashboard.taxonomy.index') + ->withErrors('Can not update the Taxonomy Properties as it already has associated Taxonomy Terms. Please reassign or delete those first.'); } + $taxonomy->update($data); + $taxonomy->properties = $updatedProperties; + $taxonomy->updated_by = Auth::user()->id; + $taxonomy->save(); return redirect()->route('dashboard.taxonomy.index')->with('Success', 'Taxonomy updated successfully'); } catch (\Exception $ex) { Log::error('Failed to update taxonomy', ['error' => $ex->getMessage()]); @@ -117,24 +118,30 @@ public function update(Request $request, Taxonomy $taxonomy) } private function validateProperties(array $original, array $updated): bool { - // Ensure existing items are not modified - foreach ($original as $index => $originalItem) { - if (!isset($updated[$index])) { - return false; // Missing an existing property - } + // $originalMap = []; + // $updatedMap = []; + // foreach ($original as $item) $originalMap[$item->code] = $item; + // foreach ($updated as $item) $updatedMap[$item->code] = $item; - $updatedItem = $updated[$index]; - if ( - $originalItem->code !== $updatedItem->code || - $originalItem->name !== $updatedItem->name || - $originalItem->data_type !== $updatedItem->data_type - ) { - return false; // An existing property was altered - } - } + // // Ensure existing items are not modified + // foreach ($originalMap as $code => $originalItem) { + // if (!isset($updatedMap[$code])) { + // // TODO Let allow to delete if not used in any term + // return false; // Missing an existing property + // } + + // $updatedItem = $updatedMap[$code]; + // if ( + // $originalItem->data_type !== $updatedItem->data_type + // ) { + // // An existing property data type was altered + // // TODO Let allow to delete if not used in any term + // return false; + // } + // } - // Allow additional properties - return count($updated) >= count($original); + // Allow changes for now + return true; } /** * Confirm to delete the specified resource from storage. diff --git a/app/Http/Livewire/Backend/TaxonomyTermMetadata.php b/app/Http/Livewire/Backend/TaxonomyTermMetadata.php new file mode 100644 index 0000000..59b9224 --- /dev/null +++ b/app/Http/Livewire/Backend/TaxonomyTermMetadata.php @@ -0,0 +1,28 @@ +property = $property; + $this->term = $term; + + // Populate metadata if $term is provided (Edit mode) + if ($this->term) { + $this->metadata = $this->term->metadata ?? []; + } + } + + public function render() + { + return view('livewire.backend.taxonomy-term-metadata'); + } +} diff --git a/database/seeders/TaxonomySeeder.php b/database/seeders/TaxonomySeeder.php index c1ba823..22ac934 100644 --- a/database/seeders/TaxonomySeeder.php +++ b/database/seeders/TaxonomySeeder.php @@ -39,7 +39,7 @@ public function run(){ 'code' => $taxonomy['code'], 'name' => $taxonomy['name'], 'description' => $taxonomy['description'], - 'properties' => json_encode($taxonomy['properties']), + 'properties' => $taxonomy['properties'], 'created_by' => 1, 'updated_by' => 1 ]); diff --git a/database/seeders/TaxonomyTermSeeder.php b/database/seeders/TaxonomyTermSeeder.php index cc1d0b0..03e62b8 100644 --- a/database/seeders/TaxonomyTermSeeder.php +++ b/database/seeders/TaxonomyTermSeeder.php @@ -178,7 +178,7 @@ private function createTermsWithChildren($terms, $taxonomyId, $parentId = null) 'name' => $term['name'], 'taxonomy_id' => $taxonomyId, 'parent_id' => $parentId, - 'metadata' => json_encode($term['metadata']), + 'metadata' => $term['metadata'], 'created_by' => 1, 'updated_by' => 1 ]); @@ -190,7 +190,7 @@ private function createTermsWithChildren($terms, $taxonomyId, $parentId = null) 'name' => $child['name'], 'taxonomy_id' => $taxonomyId, 'parent_id' => $createdTerm->id, - 'metadata' => json_encode($child['metadata']), + 'metadata' => $child['metadata'], 'created_by' => 1, 'updated_by' => 1 ]); diff --git a/resources/views/backend/taxonomy/edit.blade.php b/resources/views/backend/taxonomy/edit.blade.php index 08355f8..dc8900b 100644 --- a/resources/views/backend/taxonomy/edit.blade.php +++ b/resources/views/backend/taxonomy/edit.blade.php @@ -3,7 +3,7 @@ @section('title', __('Edit Taxonomy')) @section('content') -