From 9e81b1af37230c3cf7b267e95054d2ec13c3ade4 Mon Sep 17 00:00:00 2001 From: NuwanJ Date: Wed, 16 Oct 2024 16:30:29 +0530 Subject: [PATCH] Taxonomy - delete restrictions --- app/Domains/Taxonomy/Models/Taxonomy.php | 2 +- app/Domains/Taxonomy/Models/TaxonomyTerm.php | 11 +++++- .../Backend/TaxonomyController.php | 38 +++++++++---------- .../views/backend/taxonomy/delete.blade.php | 36 +++++++++++++----- .../backend/taxonomy/terms/delete.blade.php | 15 +++++++- 5 files changed, 70 insertions(+), 32 deletions(-) diff --git a/app/Domains/Taxonomy/Models/Taxonomy.php b/app/Domains/Taxonomy/Models/Taxonomy.php index 2f2c82f..5a6817c 100644 --- a/app/Domains/Taxonomy/Models/Taxonomy.php +++ b/app/Domains/Taxonomy/Models/Taxonomy.php @@ -34,7 +34,7 @@ class Taxonomy extends Model ]; public static $propertyType = [ - 'string'=>'String', + 'string' => 'String', 'integer' => 'Integer Number', 'float' => 'Floating Point Number', 'date' => 'Date', diff --git a/app/Domains/Taxonomy/Models/TaxonomyTerm.php b/app/Domains/Taxonomy/Models/TaxonomyTerm.php index e72a228..beb28fd 100644 --- a/app/Domains/Taxonomy/Models/TaxonomyTerm.php +++ b/app/Domains/Taxonomy/Models/TaxonomyTerm.php @@ -55,7 +55,7 @@ public function children() public function getMetadata($code) { $metadata = json_decode($this->metadata, true); - + if (is_array($metadata)) { foreach ($metadata as $item) { if ($item['code'] === $code) { @@ -66,6 +66,15 @@ public function getMetadata($code) return null; } + protected static function boot() + { + parent::boot(); + + static::deleting(function ($taxonomyTerm) { + $taxonomyTerm->children()->delete(); + }); + } + /** * Create a new factory instance for the model. * diff --git a/app/Http/Controllers/Backend/TaxonomyController.php b/app/Http/Controllers/Backend/TaxonomyController.php index fbb47ef..7dd8573 100644 --- a/app/Http/Controllers/Backend/TaxonomyController.php +++ b/app/Http/Controllers/Backend/TaxonomyController.php @@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Auth; use App\Http\Controllers\Controller; use App\Domains\Taxonomy\Models\Taxonomy; +use App\Domains\Taxonomy\Models\TaxonomyTerm; class TaxonomyController extends Controller { @@ -17,12 +18,12 @@ class TaxonomyController extends Controller */ public function create() { - try{ + try { return view('backend.taxonomy.create'); - }catch (\Exception $ex) { - Log::error('Failed to load taxonomy creation page', ['error' => $ex->getMessage()]); + } catch (\Exception $ex) { + Log::error('Failed to load taxonomy creation page', ['error' => $ex->getMessage()]); return abort(500); - } + } } /** * Store a newly created resource in storage. @@ -32,19 +33,19 @@ public function create() */ public function store(Request $request) { - $validatedData =$request->validate([ + $validatedData = $request->validate([ 'code' => 'required|unique:taxonomies', 'name' => 'required', 'description' => 'nullable', ]); - - try{ + + try { $taxonomy = new Taxonomy($validatedData); $taxonomy->properties = $request->properties; $taxonomy->created_by = Auth::user()->id; $taxonomy->save(); return redirect()->route('dashboard.taxonomy.index')->with('Success', 'Taxonomy created successfully'); - }catch (\Exception $ex) { + } catch (\Exception $ex) { Log::error('Failed to create taxonomy', ['error' => $ex->getMessage()]); return abort(500); } @@ -66,7 +67,7 @@ public function edit(Taxonomy $taxonomy) return abort(500); } } - + /** * Update the specified resource in storage. @@ -80,22 +81,21 @@ public function update(Request $request, Taxonomy $taxonomy) $data = $request->validate([ 'code' => 'required', 'name' => 'required', - 'description' => 'nullable', + 'description' => 'nullable', ]); - - try{ + + try { $taxonomy->update($data); $taxonomy->properties = $request->properties; - $taxonomy->updated_by = Auth::user()->id; + $taxonomy->updated_by = Auth::user()->id; $taxonomy->save(); return redirect()->route('dashboard.taxonomy.index')->with('Success', 'Taxonomy updated successfully'); - }catch (\Exception $ex) { + } catch (\Exception $ex) { Log::error('Failed to update taxonomy', ['error' => $ex->getMessage()]); return abort(500); } - } - /** + /** * Confirm to delete the specified resource from storage. * * @param \App\Domains\Taxonomy\Models\Taxonomy $taxonomy @@ -103,7 +103,8 @@ public function update(Request $request, Taxonomy $taxonomy) */ public function delete(Taxonomy $taxonomy) { - return view('backend.taxonomy.delete', compact('taxonomy')); + $terms = TaxonomyTerm::where('taxonomy_id', $taxonomy->id)->get(); + return view('backend.taxonomy.delete', compact('taxonomy', 'terms')); } @@ -123,5 +124,4 @@ public function destroy(Taxonomy $taxonomy) return abort(500); } } -} - +} \ No newline at end of file diff --git a/resources/views/backend/taxonomy/delete.blade.php b/resources/views/backend/taxonomy/delete.blade.php index 187e85b..095f9de 100644 --- a/resources/views/backend/taxonomy/delete.blade.php +++ b/resources/views/backend/taxonomy/delete.blade.php @@ -13,18 +13,34 @@

Are you sure you want to delete "{{ $taxonomy->name }}" ?

-
- {!! Form::open([ - 'url' => route('dashboard.taxonomy.destroy', compact('taxonomy')), - 'method' => 'delete', - 'class' => 'container', - ]) !!} - Back - {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!} + @if ($terms->count() > 0) +

The following terms are linked to this Taxonomy. Deletion is not permitted until these terms are + reassigned or deleted.

+ + Back + @else +
+ {!! Form::open([ + 'url' => route('dashboard.taxonomy.destroy', compact('taxonomy')), + 'method' => 'delete', + 'class' => 'container', + ]) !!} + + Back + {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!} + {!! Form::close() !!} +
+ @endif - {!! Form::close() !!} -
diff --git a/resources/views/backend/taxonomy/terms/delete.blade.php b/resources/views/backend/taxonomy/terms/delete.blade.php index f3aad39..ff1c125 100644 --- a/resources/views/backend/taxonomy/terms/delete.blade.php +++ b/resources/views/backend/taxonomy/terms/delete.blade.php @@ -13,6 +13,20 @@

Are you sure you want to delete "{{ $term->name }}" ?

+ + @if ($term->children()->count() > 0) +

The following terms are linked to this Taxonomy Term, and will be deleted with this.

+ + @endif +
{!! Form::open([ 'url' => route('dashboard.taxonomy.terms.destroy', ['taxonomy' => $taxonomy, 'term' => $term]), @@ -29,4 +43,3 @@
@endsection -