Skip to content

Commit

Permalink
Taxonomy - delete restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
NuwanJ committed Oct 16, 2024
1 parent ff2e611 commit 9e81b1a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 32 deletions.
2 changes: 1 addition & 1 deletion app/Domains/Taxonomy/Models/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Taxonomy extends Model
];

public static $propertyType = [
'string'=>'String',
'string' => 'String',
'integer' => 'Integer Number',
'float' => 'Floating Point Number',
'date' => 'Date',
Expand Down
11 changes: 10 additions & 1 deletion app/Domains/Taxonomy/Models/TaxonomyTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
*
Expand Down
38 changes: 19 additions & 19 deletions app/Http/Controllers/Backend/TaxonomyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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.
Expand All @@ -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);
}
Expand All @@ -66,7 +67,7 @@ public function edit(Taxonomy $taxonomy)
return abort(500);
}
}


/**
* Update the specified resource in storage.
Expand All @@ -80,30 +81,30 @@ 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
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
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'));
}


Expand All @@ -123,5 +124,4 @@ public function destroy(Taxonomy $taxonomy)
return abort(500);
}
}
}

}
36 changes: 26 additions & 10 deletions resources/views/backend/taxonomy/delete.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,34 @@
<p>Are you sure you want to delete
<strong><i>"{{ $taxonomy->name }}"</i></strong> ?
</p>
<div class="d-flex">
{!! Form::open([
'url' => route('dashboard.taxonomy.destroy', compact('taxonomy')),
'method' => 'delete',
'class' => 'container',
]) !!}

<a href="{{ route('dashboard.taxonomy.index') }}" class="btn btn-light mr-2">Back</a>
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
@if ($terms->count() > 0)
<p>The following terms are linked to this Taxonomy. Deletion is not permitted until these terms are
reassigned or deleted.</p>
<ul>
@foreach ($terms as $term)
<li>
<a href="{{ route('dashboard.taxonomy.terms.edit', compact('taxonomy', 'term')) }}">
{{ $term->name }} ({{ $term->code }})
</a>
</li>
@endforeach
</ul>
<a href="{{ route('dashboard.semesters.index') }}" class="btn btn-light mr-2">Back</a>
@else
<div class="d-flex">
{!! Form::open([
'url' => route('dashboard.taxonomy.destroy', compact('taxonomy')),
'method' => 'delete',
'class' => 'container',
]) !!}

<a href="{{ route('dashboard.taxonomy.index') }}" class="btn btn-light mr-2">Back</a>
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</div>
@endif

{!! Form::close() !!}
</div>
</x-slot>
</x-backend.card>
</div>
Expand Down
15 changes: 14 additions & 1 deletion resources/views/backend/taxonomy/terms/delete.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
<p>Are you sure you want to delete
<strong><i>"{{ $term->name }}"</i></strong> ?
</p>

@if ($term->children()->count() > 0)
<p>The following terms are linked to this Taxonomy Term, and will be deleted with this. </p>
<ul>
@foreach ($term->children()->get() as $childTerm)
<li>
<a href="{{ route('dashboard.taxonomy.terms.edit', ['taxonomy', 'childTerm']) }}">
{{ $childTerm->name }} ({{ $childTerm->code }})
</a>
</li>
@endforeach
</ul>
@endif

<div class="d-flex">
{!! Form::open([
'url' => route('dashboard.taxonomy.terms.destroy', ['taxonomy' => $taxonomy, 'term' => $term]),
Expand All @@ -29,4 +43,3 @@
</x-backend.card>
</div>
@endsection

0 comments on commit 9e81b1a

Please sign in to comment.