diff --git a/app/Http/Controllers/Backend/TaxonomyController.php b/app/Http/Controllers/Backend/TaxonomyController.php index 6e01894..de68d73 100644 --- a/app/Http/Controllers/Backend/TaxonomyController.php +++ b/app/Http/Controllers/Backend/TaxonomyController.php @@ -99,16 +99,43 @@ public function update(Request $request, Taxonomy $taxonomy) ]); try { - $taxonomy->update($data); - $taxonomy->properties = json_decode($request->properties); - $taxonomy->updated_by = Auth::user()->id; - $taxonomy->save(); + $originalProperties = json_decode($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 as it already has associated Taxonomy Terms. Please reassign or delete those first.'); + } return redirect()->route('dashboard.taxonomy.index')->with('Success', 'Taxonomy updated successfully'); } catch (\Exception $ex) { Log::error('Failed to update taxonomy', ['error' => $ex->getMessage()]); return abort(500); } } + 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 + } + + $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 + } + } + + // Allow additional properties + return count($updated) >= count($original); + } /** * Confirm to delete the specified resource from storage. * diff --git a/resources/views/backend/taxonomy/edit.blade.php b/resources/views/backend/taxonomy/edit.blade.php index 16a7fa8..08355f8 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') -
+
{!! Form::model($taxonomy, [ 'url' => route('dashboard.taxonomy.update', $taxonomy->id), 'method' => 'PUT', @@ -88,11 +88,7 @@
- {{-- Edit and Delete options not available since already have taxonomy terms. Please remove all of them to enable the Edit and Delete options. --}} - - Edit and Delete options must be carefully used to avoid data issues with - existing taxonomy - terms. + Edit and Delete options not available since already have taxonomy terms.
Please remove all of them to enable the Edit and Delete options.
diff --git a/resources/views/components/backend/taxonomy_property_adder.blade.php b/resources/views/components/backend/taxonomy_property_adder.blade.php index cf2570f..4d9e082 100644 --- a/resources/views/components/backend/taxonomy_property_adder.blade.php +++ b/resources/views/components/backend/taxonomy_property_adder.blade.php @@ -164,18 +164,21 @@ class="d-flex flex-column flex-md-row justify-content-between align-items-start
-