From 0a8220011a38637dbd45e233cdb20c62906492e5 Mon Sep 17 00:00:00 2001 From: Ishara Ekanayaka <133479172+IsharaEkanayaka@users.noreply.github.com> Date: Tue, 15 Oct 2024 19:12:15 +0530 Subject: [PATCH] 126 UI taxonomy terms index create edit and delete pages (#177) Co-authored-by: Hasan10100 <150885485+Hasan10100@users.noreply.github.com> Co-authored-by: kusaljayawardhana --- app/Domains/Taxonomy/Models/Taxonomy.php | 6 + app/Domains/Taxonomy/Models/TaxonomyTerm.php | 15 +++ .../Backend/TaxonomyTermController.php | 74 +++++++++--- app/Http/Livewire/Backend/TaxonomyTable.php | 3 +- .../Livewire/Backend/TaxonomyTermTable.php | 58 ++++++++++ .../taxonomy/index-table-row.blade.php | 8 +- .../backend/taxonomy/terms/create.blade.php | 109 ++++++++++++++++++ .../backend/taxonomy/terms/delete.blade.php | 32 +++++ .../backend/taxonomy/terms/edit.blade.php | 109 ++++++++++++++++++ .../taxonomy/terms/index-table-row.blade.php | 46 ++++++++ .../backend/taxonomy/terms/index.blade.php | 31 +++++ .../backend/event-item-table.blade.php | 3 - .../livewire/backend/taxonomy-table.blade.php | 3 - routes/backend/taxonomy.php | 7 +- 14 files changed, 471 insertions(+), 33 deletions(-) create mode 100644 app/Http/Livewire/Backend/TaxonomyTermTable.php create mode 100644 resources/views/backend/taxonomy/terms/index-table-row.blade.php delete mode 100644 resources/views/livewire/backend/event-item-table.blade.php delete mode 100644 resources/views/livewire/backend/taxonomy-table.blade.php diff --git a/app/Domains/Taxonomy/Models/Taxonomy.php b/app/Domains/Taxonomy/Models/Taxonomy.php index 450cf7c9..2f2c82f3 100644 --- a/app/Domains/Taxonomy/Models/Taxonomy.php +++ b/app/Domains/Taxonomy/Models/Taxonomy.php @@ -6,6 +6,7 @@ use Database\Factories\TaxonomyFactory; use Illuminate\Database\Eloquent\Model; use Spatie\Activitylog\Traits\LogsActivity; +use App\Domains\Taxonomy\Models\TaxonomyTerm; use Illuminate\Database\Eloquent\Factories\HasFactory; use App\Domains\Taxonomy\Models\Traits\Scope\TaxonomyScope; @@ -48,6 +49,11 @@ public function user() return $this->belongsTo(User::class, 'created_by'); } + public function terms() + { + return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id'); + } + /** * Create a new factory instance for the model. * diff --git a/app/Domains/Taxonomy/Models/TaxonomyTerm.php b/app/Domains/Taxonomy/Models/TaxonomyTerm.php index 6a8d8dc3..143b5f73 100644 --- a/app/Domains/Taxonomy/Models/TaxonomyTerm.php +++ b/app/Domains/Taxonomy/Models/TaxonomyTerm.php @@ -37,6 +37,21 @@ public function user() return $this->belongsTo(User::class, 'created_by'); } + public function taxonomy() + { + return $this->belongsTo(Taxonomy::class, 'taxonomy_id'); + } + + public function parent() + { + return $this->belongsTo(self::class, 'parent_id'); + } + + public function children() + { + return $this->hasMany(self::class, 'parent_id'); + } + /** * Create a new factory instance for the model. * diff --git a/app/Http/Controllers/Backend/TaxonomyTermController.php b/app/Http/Controllers/Backend/TaxonomyTermController.php index 2e74610e..e391de6e 100644 --- a/app/Http/Controllers/Backend/TaxonomyTermController.php +++ b/app/Http/Controllers/Backend/TaxonomyTermController.php @@ -5,23 +5,28 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use App\Http\Controllers\Controller; +use App\Domains\Taxonomy\Models\Taxonomy; use App\Domains\Taxonomy\Models\TaxonomyTerm; class TaxonomyTermController extends Controller { + public function index(Taxonomy $taxonomy) + { + return view('backend.taxonomy.terms.index', compact('taxonomy')); + } /** * Show the form for creating a new resource. * * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ - public function create() + public function create(Taxonomy $taxonomy) { - try{ - return view('backend.taxonomy.terms.create'); - }catch (\Exception $ex) { + try { + return view('backend.taxonomy.terms.create', compact('taxonomy')); + } catch (\Exception $ex) { Log::error('Failed to load taxonomy terms creation page', ['error' => $ex->getMessage()]); return abort(500); - } + } } /** * Store a newly created resource in storage. @@ -29,9 +34,26 @@ public function create() * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|void */ - public function store(Request $request) + public function store(Request $request, Taxonomy $taxonomy) { + try { + $validatedData = $request->validate([ + 'code' => 'required|unique:taxonomy_terms,code', + 'name' => 'required', + 'metadata' => 'nullable|json', + 'parent_id' => 'nullable|exists:taxonomy_terms,id' + ]); + + $taxonomyTerm = new TaxonomyTerm($validatedData); + $taxonomyTerm->taxonomy_id = $taxonomy->id; + $taxonomyTerm->save(); + return redirect()->route('dashboard.taxonomy.terms.index', $taxonomy) + ->with('Success', 'Taxonomy term was created successfully!'); + } catch (\Exception $ex) { + Log::error('Failed to create taxonomy term', ['error' => $ex->getMessage()]); + return back()->withInput()->withErrors(['error' => 'Failed to create taxonomy term. Please try again.']); + } } /** * Show the form for editing the specified resource. @@ -39,11 +61,11 @@ public function store(Request $request) * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ - public function edit(TaxonomyTerm $taxonomyTerm) + public function edit(Taxonomy $taxonomy, TaxonomyTerm $term) { - try{ - return view('backend.taxonomy.terms.edit', ['taxonomyTerm' => $taxonomyTerm]); - }catch (\Exception $ex) { + try { + return view('backend.taxonomy.terms.edit', compact('taxonomy', 'term')); + } catch (\Exception $ex) { Log::error('Failed to load taxonomy term edit page', ['error' => $ex->getMessage()]); return abort(500); } @@ -55,9 +77,24 @@ public function edit(TaxonomyTerm $taxonomyTerm) * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Http\RedirectResponse */ - public function update(Request $request, TaxonomyTerm $taxonomyTerm) + public function update(Request $request, Taxonomy $taxonomy, TaxonomyTerm $term) { + try { + $validatedData = $request->validate([ + 'code' => 'required|unique:taxonomy_terms,code,' . $term->id, + 'name' => 'required', + 'metadata' => 'nullable|json', + 'parent_id' => 'nullable|exists:taxonomy_terms,id' + ]); + + $term->update($validatedData); + return redirect()->route('dashboard.taxonomy.terms.index', $taxonomy) + ->with('Success', 'Taxonomy term was updated successfully!'); + } catch (\Exception $ex) { + Log::error('Failed to update taxonomy term', ['error' => $ex->getMessage()]); + return back()->withInput()->withErrors(['error' => 'Failed to update taxonomy term. Please try again.']); + } } /** * Confirm to delete the specified resource from storage. @@ -65,26 +102,25 @@ public function update(Request $request, TaxonomyTerm $taxonomyTerm) * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ - public function delete(TaxonomyTerm $taxonomyTerm) + public function delete(Taxonomy $taxonomy, TaxonomyTerm $term) { - return view('backend.taxonomy.terms.delete', compact('taxonomyTerm')); + return view('backend.taxonomy.terms.delete', compact('taxonomy', 'term')); } - /** * Remove the specified resource from storage. * * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Http\RedirectResponse|null */ - public function destroy(TaxonomyTerm $taxonomyTerm) + public function destroy(Taxonomy $taxonomy, TaxonomyTerm $term) { try { - $taxonomyTerm->delete(); - return redirect()->route('dashboard.taxonomy.terms.index')->with('Success', 'Taxonomy term was deleted !'); + $term->delete(); + return redirect()->route('dashboard.taxonomy.terms.index', $taxonomy)->with('Success', 'Taxonomy term was deleted !'); } catch (\Exception $ex) { - Log::error('Failed to delete taxonomy term', ['taxonomyTerm_id' => $taxonomyTerm->id, 'error' => $ex->getMessage()]); - return abort(500); + Log::error('Failed to delete taxonomy term', ['term_id' => $term->id, 'error' => $ex->getMessage()]); + return back()->withErrors(['error' => 'Failed to delete taxonomy term. Please try again.']); } } } \ No newline at end of file diff --git a/app/Http/Livewire/Backend/TaxonomyTable.php b/app/Http/Livewire/Backend/TaxonomyTable.php index 0e2baae4..9028c80e 100644 --- a/app/Http/Livewire/Backend/TaxonomyTable.php +++ b/app/Http/Livewire/Backend/TaxonomyTable.php @@ -36,7 +36,8 @@ public function columns(): array public function query(): Builder { - return Taxonomy::query(); + return Taxonomy::query() + ->with('user'); } public function rowView(): string diff --git a/app/Http/Livewire/Backend/TaxonomyTermTable.php b/app/Http/Livewire/Backend/TaxonomyTermTable.php new file mode 100644 index 00000000..871dbdc0 --- /dev/null +++ b/app/Http/Livewire/Backend/TaxonomyTermTable.php @@ -0,0 +1,58 @@ +taxonomy = $taxonomy; + } + + public function columns(): array + { + return [ + Column::make("Code", "code") + ->searchable()->sortable(), + Column::make("Name", "name") + ->searchable()->sortable(), + Column::make("Taxonomy", "taxonomy.name") + ->searchable() + ->sortable(), + Column::make("Created by", "created_by") + ->sortable(), + Column::make("Updated by", "updated_by") + ->sortable(), + Column::make("Created at", "created_at") + ->sortable(), + Column::make("Updated at", "updated_at") + ->sortable(), + Column::make("Actions") + ]; + } + + public function query(): Builder + { + return TaxonomyTerm::query() + ->where('taxonomy_id', $this->taxonomy->id) + ->with('user'); + } + + public function rowView(): string + { + return 'backend.taxonomy.terms.index-table-row'; + } +} diff --git a/resources/views/backend/taxonomy/index-table-row.blade.php b/resources/views/backend/taxonomy/index-table-row.blade.php index b5b65e83..19174a30 100644 --- a/resources/views/backend/taxonomy/index-table-row.blade.php +++ b/resources/views/backend/taxonomy/index-table-row.blade.php @@ -29,22 +29,22 @@
- {{-- + {{-- --}} - + - + - + diff --git a/resources/views/backend/taxonomy/terms/create.blade.php b/resources/views/backend/taxonomy/terms/create.blade.php index e69de29b..8637b169 100644 --- a/resources/views/backend/taxonomy/terms/create.blade.php +++ b/resources/views/backend/taxonomy/terms/create.blade.php @@ -0,0 +1,109 @@ +@extends('backend.layouts.app') + +@section('content') +
+ + + + + +
+ +
+ Term Configurations +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ {!! Form::text('code', '', ['class' => 'form-control']) !!} +
+
+ +
+
+ +
+
+ {!! Form::text('name', '', ['class' => 'form-control']) !!} +
+
+
+ + +
+ + + {!! Form::submit('Create', ['class' => 'btn btn-primary btn-w-150 float-right', 'id' => 'submit-button']) !!} + + +
+ +
+ +@endsection diff --git a/resources/views/backend/taxonomy/terms/delete.blade.php b/resources/views/backend/taxonomy/terms/delete.blade.php index e69de29b..f3aad391 100644 --- a/resources/views/backend/taxonomy/terms/delete.blade.php +++ b/resources/views/backend/taxonomy/terms/delete.blade.php @@ -0,0 +1,32 @@ +@extends('backend.layouts.app') + +@section('title', __('Delete Taxonomy Term')) + +@section('content') +
+ + + Taxonomy Term: Delete | {{ $term->id }} + + + +

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

+
+ {!! Form::open([ + 'url' => route('dashboard.taxonomy.terms.destroy', ['taxonomy' => $taxonomy, 'term' => $term]), + 'method' => 'delete', + 'class' => 'container', + ]) !!} + + Back + {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!} + + {!! Form::close() !!} +
+
+
+
+@endsection + diff --git a/resources/views/backend/taxonomy/terms/edit.blade.php b/resources/views/backend/taxonomy/terms/edit.blade.php index e69de29b..20601654 100644 --- a/resources/views/backend/taxonomy/terms/edit.blade.php +++ b/resources/views/backend/taxonomy/terms/edit.blade.php @@ -0,0 +1,109 @@ +@extends('backend.layouts.app') + +@section('content') +
+ + + + + +
+ +
+ Term Configurations +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ {!! Form::text('tax_term_code', '', ['class' => 'form-control']) !!} +
+
+ +
+
+ +
+
+ {!! Form::text('tax_term_name', '', ['class' => 'form-control']) !!} +
+
+
+ + +
+ + + {!! Form::submit('Update', ['class' => 'btn btn-primary btn-w-150 float-right', 'id' => 'submit-button']) !!} + + +
+ +
+ +@endsection diff --git a/resources/views/backend/taxonomy/terms/index-table-row.blade.php b/resources/views/backend/taxonomy/terms/index-table-row.blade.php new file mode 100644 index 00000000..f4675676 --- /dev/null +++ b/resources/views/backend/taxonomy/terms/index-table-row.blade.php @@ -0,0 +1,46 @@ + + + + {{ $row->code }} + + + + {{ $row->name }} + + + + {{ $row->taxonomy->name }} + + + + {{ User::find($row->created_by)->name ?? 'N/A' }} + + + + {{ User::find($row->updated_by)->name ?? 'N/A' }} + + + + {{ $row->created_at }} + + + + {{ $row->updated_at }} + + + +
+
+ + + + + + + + + + +
+
+
diff --git a/resources/views/backend/taxonomy/terms/index.blade.php b/resources/views/backend/taxonomy/terms/index.blade.php index e69de29b..f8a06992 100644 --- a/resources/views/backend/taxonomy/terms/index.blade.php +++ b/resources/views/backend/taxonomy/terms/index.blade.php @@ -0,0 +1,31 @@ +@extends('backend.layouts.app') + +@section('title', __('Taxonomy Terms')) + +@section('content') +
+ + + Taxonomy Terms: {{ $taxonomy->name }} + + + + + + + + + @if (session('Success')) +
+ {{ session('Success') }} + +
+ @endif + + +
+
+
+@endsection diff --git a/resources/views/livewire/backend/event-item-table.blade.php b/resources/views/livewire/backend/event-item-table.blade.php deleted file mode 100644 index 7a4f2102..00000000 --- a/resources/views/livewire/backend/event-item-table.blade.php +++ /dev/null @@ -1,3 +0,0 @@ -
- {{-- Do your work, then step back. --}} -
diff --git a/resources/views/livewire/backend/taxonomy-table.blade.php b/resources/views/livewire/backend/taxonomy-table.blade.php deleted file mode 100644 index 0bf1ae77..00000000 --- a/resources/views/livewire/backend/taxonomy-table.blade.php +++ /dev/null @@ -1,3 +0,0 @@ -
- {{-- Care about people's approval and you will be their prisoner. --}} -
diff --git a/routes/backend/taxonomy.php b/routes/backend/taxonomy.php index 75cfd489..07c11c7b 100644 --- a/routes/backend/taxonomy.php +++ b/routes/backend/taxonomy.php @@ -59,9 +59,7 @@ Route::group(['prefix' => 'taxonomy/{taxonomy}/terms'], function () { // Index (list terms for a taxonomy) - Route::get('/', function(){ - return view('backend.taxonomy.terms.index'); - })->name('taxonomy.terms.index') + Route::get('/', [TaxonomyTermController::class, 'index'])->name('taxonomy.terms.index') ->breadcrumbs(function (Trail $trail, $taxonomy) { $trail->push(__('Home'), route('dashboard.home')) ->push(__('Taxonomy'), route('dashboard.taxonomy.index')) @@ -76,6 +74,7 @@ $trail->push(__('Home'), route('dashboard.home')) ->push(__('Taxonomy'), route('dashboard.taxonomy.index')) ->push($taxonomy->name, route('dashboard.taxonomy.edit', $taxonomy)) + ->push(__('Terms'), route('dashboard.taxonomy.terms.index', $taxonomy)) ->push(__('Create Term')); }); @@ -90,6 +89,7 @@ $trail->push(__('Home'), route('dashboard.home')) ->push(__('Taxonomy'), route('dashboard.taxonomy.index')) ->push($taxonomy->name, route('dashboard.taxonomy.edit', $taxonomy)) + ->push(__('Terms'), route('dashboard.taxonomy.terms.index', $taxonomy)) ->push(__('Edit Term')); }); @@ -104,6 +104,7 @@ $trail->push(__('Home'), route('dashboard.home')) ->push(__('Taxonomy'), route('dashboard.taxonomy.index')) ->push($taxonomy->name, route('dashboard.taxonomy.edit', $taxonomy)) + ->push(__('Terms'), route('dashboard.taxonomy.terms.index', $taxonomy)) ->push(__('Delete Term')); });