Skip to content

Commit

Permalink
feat : complete property edit lock
Browse files Browse the repository at this point in the history
  • Loading branch information
IsharaEkanayaka committed Dec 24, 2024
1 parent 5b4ddfb commit b330901
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
35 changes: 31 additions & 4 deletions app/Http/Controllers/Backend/TaxonomyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 2 additions & 6 deletions resources/views/backend/taxonomy/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@section('title', __('Edit Taxonomy'))

@section('content')
<div x-data="{ properties: {{ $taxonomy->properties }}, is_editable: {{ $taxonomy->terms()->count() > 0 ? '0' : '1' }} }">
<div x-data="{ properties: {{ $taxonomy->properties}}, is_editable: {{ $taxonomy->terms()->count() > 0 ? '0' : '1' }} }">
{!! Form::model($taxonomy, [
'url' => route('dashboard.taxonomy.update', $taxonomy->id),
'method' => 'PUT',
Expand Down Expand Up @@ -88,11 +88,7 @@
</svg>

<div>
{{-- <b>Edit</b> and <b>Delete</b> options not available since already have <a href="{{ route('dashboard.taxonomy.terms.index', $taxonomy) }}">taxonomy terms</a>. Please remove all of them to enable the Edit and Delete options. --}}

<b>Edit</b> and <b>Delete</b> options must be carefully used to avoid data issues with
existing <a href="{{ route('dashboard.taxonomy.terms.index', $taxonomy) }}">taxonomy
terms</a>.
<b>Edit</b> and <b>Delete</b> options not available since already have <a href="{{ route('dashboard.taxonomy.terms.index', $taxonomy) }}">taxonomy terms</a>.<br> Please remove all of them to enable the <b>Edit</b> and <b>Delete</b> options.
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,21 @@ class="d-flex flex-column flex-md-row justify-content-between align-items-start
<div class="d-flex flex-column flex-md-row align-items-md-end ms-auto">
<div x-show="selectedItem === index" class="btn-group" role="group" aria-label="Item actions">
<button type="button" class="btn btn-sm btn-secondary me-1 me-md-2 rounded"
@click.stop="moveUp()">
@click.stop="moveUp()"
x-show="properties.length > 1">
<i class="fas fa-chevron-up"></i>
</button>
<button type="button" class="btn btn-sm btn-secondary me-1 me-md-2 rounded"
@click.stop="moveDown()">
@click.stop="moveDown()"
x-show="properties.length > 1">
<i class="fas fa-chevron-down"></i>
</button>
<button type="button" class="btn btn-sm btn-warning me-1 me-md-2 rounded"
@click.stop="editItem()">
@click.stop="editItem()"
:disabled="is_editable === 0">
<i class="fas fa-pencil-alt"></i>
</button>
<button type="button" class="btn btn-sm btn-danger rounded" @click.stop="deleteItem()">
<button type="button" class="btn btn-sm btn-danger rounded" @click.stop="deleteItem()" :disabled="is_editable === 0">
<i class="fas fa-trash-alt"></i>
</button>
</div>
Expand Down

0 comments on commit b330901

Please sign in to comment.