From 4533dd43ce6cbbd647aab5394e3a60f4bacefef0 Mon Sep 17 00:00:00 2001 From: Ishara Ekanayaka <133479172+IsharaEkanayaka@users.noreply.github.com> Date: Mon, 14 Oct 2024 18:16:40 +0530 Subject: [PATCH] 128 beui create and edit taxonomy (#174) Co-authored-by: Supun Dulara --- app/Domains/Taxonomy/Models/Taxonomy.php | 11 ++ .../Models/TaxonomyTerm.php} | 14 +- .../Traits/Scope/TaxonomyTermsScope.php | 11 -- .../Services/TaxonomyTermsService.php | 22 --- .../Backend/TaxonomyController.php | 46 ++++- ...troller.php => TaxonomyTermController.php} | 37 ++-- app/Http/Livewire/Backend/TaxonomyTable.php | 4 +- ...rmsFactory.php => TaxonomyTermFactory.php} | 9 +- resources/sass/backend/app.scss | 4 + resources/views/backend/layouts/app.blade.php | 60 ------ .../views/backend/taxonomy/create.blade.php | 87 +++++++++ .../views/backend/taxonomy/edit.blade.php | 85 +++++++++ .../taxonomy/index-table-row.blade.php | 20 +- .../backend/taxonomy/terms/create.blade.php | 0 .../backend/taxonomy/terms/delete.blade.php | 0 .../backend/taxonomy/terms/edit.blade.php | 0 .../backend/taxonomy/terms/index.blade.php | 0 .../backend/taxonomy_property_adder.blade.php | 180 ++++++++++++++++++ routes/backend/taxonomy.php | 58 ++++++ 19 files changed, 510 insertions(+), 138 deletions(-) rename app/Domains/{TaxonomyTerms/Models/TaxonomyTerms.php => Taxonomy/Models/TaxonomyTerm.php} (71%) delete mode 100644 app/Domains/TaxonomyTerms/Models/Traits/Scope/TaxonomyTermsScope.php delete mode 100644 app/Domains/TaxonomyTerms/Services/TaxonomyTermsService.php rename app/Http/Controllers/Backend/{TaxonomyTermsController.php => TaxonomyTermController.php} (60%) rename database/factories/{TaxonomyTermsFactory.php => TaxonomyTermFactory.php} (89%) create mode 100644 resources/views/backend/taxonomy/terms/create.blade.php create mode 100644 resources/views/backend/taxonomy/terms/delete.blade.php create mode 100644 resources/views/backend/taxonomy/terms/edit.blade.php create mode 100644 resources/views/backend/taxonomy/terms/index.blade.php create mode 100644 resources/views/components/backend/taxonomy_property_adder.blade.php diff --git a/app/Domains/Taxonomy/Models/Taxonomy.php b/app/Domains/Taxonomy/Models/Taxonomy.php index dadd4cd..450cf7c 100644 --- a/app/Domains/Taxonomy/Models/Taxonomy.php +++ b/app/Domains/Taxonomy/Models/Taxonomy.php @@ -32,6 +32,17 @@ class Taxonomy extends Model 'properties', ]; + public static $propertyType = [ + 'string'=>'String', + 'integer' => 'Integer Number', + 'float' => 'Floating Point Number', + 'date' => 'Date', + 'datetime' => 'Date Time', + 'boolean' => 'Boolean', + 'url' => 'URL', + 'image' => 'Image' + ]; + public function user() { return $this->belongsTo(User::class, 'created_by'); diff --git a/app/Domains/TaxonomyTerms/Models/TaxonomyTerms.php b/app/Domains/Taxonomy/Models/TaxonomyTerm.php similarity index 71% rename from app/Domains/TaxonomyTerms/Models/TaxonomyTerms.php rename to app/Domains/Taxonomy/Models/TaxonomyTerm.php index 7daa09d..6a8d8dc 100644 --- a/app/Domains/TaxonomyTerms/Models/TaxonomyTerms.php +++ b/app/Domains/Taxonomy/Models/TaxonomyTerm.php @@ -1,21 +1,19 @@ model = $taxonomyTerms; - } -} diff --git a/app/Http/Controllers/Backend/TaxonomyController.php b/app/Http/Controllers/Backend/TaxonomyController.php index b8e97c5..fbb47ef 100644 --- a/app/Http/Controllers/Backend/TaxonomyController.php +++ b/app/Http/Controllers/Backend/TaxonomyController.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Auth; use App\Http\Controllers\Controller; use App\Domains\Taxonomy\Models\Taxonomy; @@ -31,7 +32,22 @@ public function create() */ public function store(Request $request) { - + $validatedData =$request->validate([ + 'code' => 'required|unique:taxonomies', + 'name' => 'required', + 'description' => 'nullable', + ]); + + 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) { + Log::error('Failed to create taxonomy', ['error' => $ex->getMessage()]); + return abort(500); + } } /** * Show the form for editing the specified resource. @@ -41,13 +57,17 @@ public function store(Request $request) */ public function edit(Taxonomy $taxonomy) { - try{ - return view('backend.taxonomy.edit', ['taxonomy' => $taxonomy]); - }catch (\Exception $ex) { - Log::error('Failed to load taxonomy edit page', ['error' => $ex->getMessage()]); + try { + return view('backend.taxonomy.edit', [ + 'taxonomy' => $taxonomy, + ]); + } catch (\Exception $ex) { + Log::error('Failed to load taxonomy edit page', ['error' => $ex->getMessage()]); return abort(500); } } + + /** * Update the specified resource in storage. * @@ -57,6 +77,22 @@ public function edit(Taxonomy $taxonomy) */ public function update(Request $request, Taxonomy $taxonomy) { + $data = $request->validate([ + 'code' => 'required', + 'name' => 'required', + 'description' => 'nullable', + ]); + + try{ + $taxonomy->update($data); + $taxonomy->properties = $request->properties; + $taxonomy->updated_by = Auth::user()->id; + $taxonomy->save(); + 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); + } } /** diff --git a/app/Http/Controllers/Backend/TaxonomyTermsController.php b/app/Http/Controllers/Backend/TaxonomyTermController.php similarity index 60% rename from app/Http/Controllers/Backend/TaxonomyTermsController.php rename to app/Http/Controllers/Backend/TaxonomyTermController.php index 69e8886..2e74610 100644 --- a/app/Http/Controllers/Backend/TaxonomyTermsController.php +++ b/app/Http/Controllers/Backend/TaxonomyTermController.php @@ -5,9 +5,9 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use App\Http\Controllers\Controller; -use App\Domains\TaxonomyTerms\Models\TaxonomyTerms; +use App\Domains\Taxonomy\Models\TaxonomyTerm; -class TaxonomyTermsController extends Controller +class TaxonomyTermController extends Controller { /** * Show the form for creating a new resource. @@ -17,7 +17,7 @@ class TaxonomyTermsController extends Controller public function create() { try{ - return view('backend.taxonomyTerms.create'); + return view('backend.taxonomy.terms.create'); }catch (\Exception $ex) { Log::error('Failed to load taxonomy terms creation page', ['error' => $ex->getMessage()]); return abort(500); @@ -36,15 +36,15 @@ public function store(Request $request) /** * Show the form for editing the specified resource. * - * @param \App\Domains\TaxonomyTerms\Models\TaxonomyTerms $taxonomyTerms + * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ - public function edit(TaxonomyTerms $taxonomyTerms) + public function edit(TaxonomyTerm $taxonomyTerm) { try{ - return view('backend.taxonomyTerms.edit', ['taxonomyTerms' => $taxonomyTerms]); + return view('backend.taxonomy.terms.edit', ['taxonomyTerm' => $taxonomyTerm]); }catch (\Exception $ex) { - Log::error('Failed to load taxonomy terms edit page', ['error' => $ex->getMessage()]); + Log::error('Failed to load taxonomy term edit page', ['error' => $ex->getMessage()]); return abort(500); } } @@ -52,40 +52,39 @@ public function edit(TaxonomyTerms $taxonomyTerms) * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request - * @param \App\Domains\TaxonomyTerms\Models\TaxonomyTerms $taxonomyTerms + * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Http\RedirectResponse */ - public function update(Request $request, TaxonomyTerms $taxonomyTerms) + public function update(Request $request, TaxonomyTerm $taxonomyTerm) { } /** * Confirm to delete the specified resource from storage. * - * @param \App\Domains\TaxonomyTerms\Models\TaxonomyTerms $taxonomyTerms + * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ - public function delete(TaxonomyTerms $taxonomyTerms) + public function delete(TaxonomyTerm $taxonomyTerm) { - return view('backend.taxonomyTerms.delete', compact('taxonomyTerms')); + return view('backend.taxonomy.terms.delete', compact('taxonomyTerm')); } /** * Remove the specified resource from storage. * - * @param \App\Domains\TaxonomyTerms\Models\TaxonomyTerms $taxonomyTerms + * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Http\RedirectResponse|null */ - public function destroy(TaxonomyTerms $taxonomyTerms) + public function destroy(TaxonomyTerm $taxonomyTerm) { try { - $taxonomyTerms->delete(); - return redirect()->route('dashboard.taxonomyTerms.index')->with('Success', 'Taxonomy term was deleted !'); + $taxonomyTerm->delete(); + return redirect()->route('dashboard.taxonomy.terms.index')->with('Success', 'Taxonomy term was deleted !'); } catch (\Exception $ex) { - Log::error('Failed to delete taxonomy term', ['taxonomyTerms_id' => $taxonomyTerms->id, 'error' => $ex->getMessage()]); + Log::error('Failed to delete taxonomy term', ['taxonomyTerm_id' => $taxonomyTerm->id, 'error' => $ex->getMessage()]); return abort(500); } } -} - +} \ No newline at end of file diff --git a/app/Http/Livewire/Backend/TaxonomyTable.php b/app/Http/Livewire/Backend/TaxonomyTable.php index 9d4f3b4..0e2baae 100644 --- a/app/Http/Livewire/Backend/TaxonomyTable.php +++ b/app/Http/Livewire/Backend/TaxonomyTable.php @@ -28,6 +28,8 @@ public function columns(): array ->sortable(), Column::make("Created at", "created_at") ->sortable(), + Column::make("Updated at", "updated_at") + ->sortable(), Column::make("Actions") ]; } @@ -39,7 +41,7 @@ public function query(): Builder public function rowView(): string { - return 'backend.taxonomies.index-table-row'; + return 'backend.taxonomy.index-table-row'; } } diff --git a/database/factories/TaxonomyTermsFactory.php b/database/factories/TaxonomyTermFactory.php similarity index 89% rename from database/factories/TaxonomyTermsFactory.php rename to database/factories/TaxonomyTermFactory.php index cb580e2..117d212 100644 --- a/database/factories/TaxonomyTermsFactory.php +++ b/database/factories/TaxonomyTermFactory.php @@ -1,19 +1,16 @@ @stack('after-scripts') - - diff --git a/resources/views/backend/taxonomy/create.blade.php b/resources/views/backend/taxonomy/create.blade.php index e69de29..5208d81 100644 --- a/resources/views/backend/taxonomy/create.blade.php +++ b/resources/views/backend/taxonomy/create.blade.php @@ -0,0 +1,87 @@ +@extends('backend.layouts.app') + +@section('title', __('Create Taxonomy')) + +@section('content') +
+ {!! Form::open([ + 'url' => route('dashboard.taxonomy.store'), + 'method' => 'post', + 'class' => 'container', + 'files' => true, + 'enctype' => 'multipart/form-data', + ]) !!} + + @csrf + + + Taxonomy : Create + + + +
+
+
Basic Configurations
+ +
+ {!! Form::label('code', 'Taxonomy Code*', ['class' => 'col-form-label']) !!} +
+
+
+ {!! Form::text('code', '', ['class' => 'form-control']) !!} + @error('code') + {{ $message }} + @enderror +
+
+ + +
+ {!! Form::label('name', 'Taxonomy Name*', ['class' => 'col-form-label']) !!} +
+ +
+
+ {!! Form::text('name', '', ['class' => 'form-control']) !!} + @error('name') + {{ $message }} + @enderror +
+ +
+ + +
+ {!! Form::label('description', 'Taxonomy Description*', ['class' => 'col-form-label']) !!} +
+ +
+
+ {!! Form::textarea('description', '', ['class' => 'form-control','style'=>'overflow:hidden;height: 100px;','oninput'=>"this.style.height = '100px';this.style.height = this.scrollHeight + 'px';"]) !!} + @error('description') + {{ $message }} + @enderror +
+ +
+
+
+ +
+
+
Properties
+ + {!! Form::hidden('properties', '', ['x-model' => 'JSON.stringify(properties)']) !!} +
+
+
+ + + {!! Form::submit('Create', ['class' => 'btn btn-primary btn-w-150 float-right']) !!} + + +
+ + {!! Form::close() !!} +
+@endsection \ No newline at end of file diff --git a/resources/views/backend/taxonomy/edit.blade.php b/resources/views/backend/taxonomy/edit.blade.php index e69de29..b882f4f 100644 --- a/resources/views/backend/taxonomy/edit.blade.php +++ b/resources/views/backend/taxonomy/edit.blade.php @@ -0,0 +1,85 @@ +@extends('backend.layouts.app') + +@section('title', __('Edit Taxonomy')) + +@section('content') +
+ {!! Form::model($taxonomy, [ + 'url' => route('dashboard.taxonomy.update', $taxonomy->id), + 'method' => 'PUT', + 'class' => 'container', + 'files' => true, + 'enctype' => 'multipart/form-data', + ]) !!} + + @csrf + + + Taxonomy : Edit + + + +
+
+
Basic Configurations
+ +
+ {!! Form::label('code', 'Taxonomy Code*', ['class' => 'col-form-label']) !!} +
+
+
+ {!! Form::text('code', null, ['class' => 'form-control']) !!} + @error('code') + {{ $message }} + @enderror +
+
+ + +
+ {!! Form::label('name', 'Taxonomy Name*', ['class' => 'col-form-label']) !!} +
+ +
+
+ {!! Form::text('name', null, ['class' => 'form-control']) !!} + @error('name') + {{ $message }} + @enderror +
+
+ + +
+ {!! Form::label('description', 'Taxonomy Description*', ['class' => 'col-form-label']) !!} +
+ +
+
+ {!! Form::textarea('description', null, ['class' => 'form-control','style'=>'overflow:hidden;height: 100px;','oninput'=>"this.style.height = '100px';this.style.height = this.scrollHeight + 'px';"]) !!} + @error('description') + {{ $message }} + @enderror +
+
+
+
+ +
+
+
Properties
+ + {!! Form::hidden('properties', '', ['x-model' => 'JSON.stringify(properties)']) !!} +
+
+
+ + + {!! Form::submit('Update', ['class' => 'btn btn-primary btn-w-150 float-right']) !!} + + +
+ + {!! Form::close() !!} +
+@endsection diff --git a/resources/views/backend/taxonomy/index-table-row.blade.php b/resources/views/backend/taxonomy/index-table-row.blade.php index 3977af7..b5b65e8 100644 --- a/resources/views/backend/taxonomy/index-table-row.blade.php +++ b/resources/views/backend/taxonomy/index-table-row.blade.php @@ -1,6 +1,6 @@ - + {{ $row->code }} @@ -16,27 +16,35 @@ {{ User::find($row->updated_by)->name ?? 'N/A' }} + + {{ $row->created_at }} + + + + {{ $row->updated_at }} + +
- + {{-- - + --}} - + - + - + diff --git a/resources/views/backend/taxonomy/terms/create.blade.php b/resources/views/backend/taxonomy/terms/create.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/resources/views/backend/taxonomy/terms/delete.blade.php b/resources/views/backend/taxonomy/terms/delete.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/resources/views/backend/taxonomy/terms/edit.blade.php b/resources/views/backend/taxonomy/terms/edit.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/resources/views/backend/taxonomy/terms/index.blade.php b/resources/views/backend/taxonomy/terms/index.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/resources/views/components/backend/taxonomy_property_adder.blade.php b/resources/views/components/backend/taxonomy_property_adder.blade.php new file mode 100644 index 0000000..48ee534 --- /dev/null +++ b/resources/views/components/backend/taxonomy_property_adder.blade.php @@ -0,0 +1,180 @@ +@php + $propertyTypes = \App\Domains\Taxonomy\Models\Taxonomy::$propertyType; +@endphp +
+
+ + +
+ + + + +
    + +
+
diff --git a/routes/backend/taxonomy.php b/routes/backend/taxonomy.php index b5a143a..75cfd48 100644 --- a/routes/backend/taxonomy.php +++ b/routes/backend/taxonomy.php @@ -2,6 +2,7 @@ use Tabuna\Breadcrumbs\Trail; use App\Http\Controllers\Backend\TaxonomyController; +use App\Http\Controllers\Backend\TaxonomyTermController; use Illuminate\Support\Facades\Route; Route::group([], function () { @@ -53,4 +54,61 @@ // Destroy Route::delete('taxonomy/{taxonomy}', [TaxonomyController::class, 'destroy']) ->name('taxonomy.destroy'); + + //Taxonomy Term Routes + 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') + ->breadcrumbs(function (Trail $trail, $taxonomy) { + $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)); + }); + + // Create (show form for creating a new term) + Route::get('/create', [TaxonomyTermController::class, 'create']) + ->name('taxonomy.terms.create') + ->breadcrumbs(function (Trail $trail, $taxonomy) { + $trail->push(__('Home'), route('dashboard.home')) + ->push(__('Taxonomy'), route('dashboard.taxonomy.index')) + ->push($taxonomy->name, route('dashboard.taxonomy.edit', $taxonomy)) + ->push(__('Create Term')); + }); + + // Store (store a new term in the taxonomy) + Route::post('/', [TaxonomyTermController::class, 'store']) + ->name('taxonomy.terms.store'); + + // Edit (show form for editing a term) + Route::get('/edit/{term}', [TaxonomyTermController::class, 'edit']) + ->name('taxonomy.terms.edit') + ->breadcrumbs(function (Trail $trail, $taxonomy, $term) { + $trail->push(__('Home'), route('dashboard.home')) + ->push(__('Taxonomy'), route('dashboard.taxonomy.index')) + ->push($taxonomy->name, route('dashboard.taxonomy.edit', $taxonomy)) + ->push(__('Edit Term')); + }); + + // Update (update a term in the taxonomy) + Route::put('/{term}', [TaxonomyTermController::class, 'update']) + ->name('taxonomy.terms.update'); + + // Delete (show confirmation for deleting a term) + Route::get('/delete/{term}', [TaxonomyTermController::class, 'delete']) + ->name('taxonomy.terms.delete') + ->breadcrumbs(function (Trail $trail, $taxonomy, $term) { + $trail->push(__('Home'), route('dashboard.home')) + ->push(__('Taxonomy'), route('dashboard.taxonomy.index')) + ->push($taxonomy->name, route('dashboard.taxonomy.edit', $taxonomy)) + ->push(__('Delete Term')); + }); + + // Destroy (delete a term from the taxonomy) + Route::delete('/{term}', [TaxonomyTermController::class, 'destroy']) + ->name('taxonomy.terms.destroy'); + }); });