diff --git a/app/Domains/Taxonomy/Models/TaxonomyTerm.php b/app/Domains/Taxonomy/Models/TaxonomyTerm.php index 143b5f7..e72a228 100644 --- a/app/Domains/Taxonomy/Models/TaxonomyTerm.php +++ b/app/Domains/Taxonomy/Models/TaxonomyTerm.php @@ -52,6 +52,20 @@ public function children() return $this->hasMany(self::class, 'parent_id'); } + public function getMetadata($code) + { + $metadata = json_decode($this->metadata, true); + + if (is_array($metadata)) { + foreach ($metadata as $item) { + if ($item['code'] === $code) { + return $item['value']; + } + } + } + return null; + } + /** * 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 e391de6..1112f5a 100644 --- a/app/Http/Controllers/Backend/TaxonomyTermController.php +++ b/app/Http/Controllers/Backend/TaxonomyTermController.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; use App\Domains\Taxonomy\Models\TaxonomyTerm; @@ -19,10 +20,13 @@ public function index(Taxonomy $taxonomy) * * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ - public function create(Taxonomy $taxonomy) + public function create(TaxonomyTerm $taxonomyTerm, Taxonomy $taxonomy) { try { - return view('backend.taxonomy.terms.create', compact('taxonomy')); + $parentTerms = TaxonomyTerm::where('taxonomy_id', $taxonomy->id )->get(); + $taxonomy_id = Taxonomy::where('id', $taxonomy->id)->get(); + + return view('backend.taxonomy.terms.create', compact('taxonomy','parentTerms','taxonomy_id')); } catch (\Exception $ex) { Log::error('Failed to load taxonomy terms creation page', ['error' => $ex->getMessage()]); return abort(500); @@ -34,18 +38,73 @@ public function create(Taxonomy $taxonomy) * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|void */ - public function store(Request $request, Taxonomy $taxonomy) + public function store(Request $request, Taxonomy $taxonomy,TaxonomyTerm $taxonomyTerm) { try { $validatedData = $request->validate([ 'code' => 'required|unique:taxonomy_terms,code', 'name' => 'required', - 'metadata' => 'nullable|json', - 'parent_id' => 'nullable|exists:taxonomy_terms,id' + 'taxonomy_id' => 'required|exists:taxonomies,id', + 'parent_id' => 'nullable|exists:taxonomy_terms,id', + 'metadata' => 'array', ]); + + + foreach (json_decode($taxonomy->properties, true) as $property) { + $metadataKey = "metadata.{$property['code']}"; + + switch ($property['data_type']) { + case 'string': + $request->validate([$metadataKey => 'nullable|string']); + break; + case 'integer': + $request->validate([$metadataKey => 'nullable|integer']); + break; + case 'float': + $request->validate([$metadataKey => 'nullable|numeric']); + break; + case 'boolean': + $request->validate([$metadataKey => 'nullable|boolean']); + break; + case 'date': + $request->validate([$metadataKey => 'nullable|date']); + break; + case 'datetime': + $request->validate([$metadataKey => 'nullable|date']); + break; + case 'url': + $request->validate([$metadataKey => 'nullable|url']); + break; + case 'image': + + if ($request->hasFile("metadata.{$property['code']}")) { + $imagePath = $this->uploadThumb(null, $request->file("metadata.{$property['code']}"), "taxonomy_terms"); + $value = $imagePath; + } else { + $value = null; + } + break; + + } + } + + $metadataArray = []; + + foreach (json_decode($taxonomy->properties, true) as $property) { + $value = $request->input("metadata.{$property['code']}"); + + if ($property['data_type'] === 'boolean') { + $value = $request->has("metadata.{$property['code']}") ? true : false; + } + $metadataArray[] = [ + 'code' => $property['code'], + 'value' => $value === '' ? null : $value + ]; + } $taxonomyTerm = new TaxonomyTerm($validatedData); - $taxonomyTerm->taxonomy_id = $taxonomy->id; + $taxonomyTerm->metadata = json_encode($metadataArray); + $taxonomyTerm->created_by = Auth::user()->id; $taxonomyTerm->save(); return redirect()->route('dashboard.taxonomy.terms.index', $taxonomy) @@ -64,7 +123,8 @@ public function store(Request $request, Taxonomy $taxonomy) public function edit(Taxonomy $taxonomy, TaxonomyTerm $term) { try { - return view('backend.taxonomy.terms.edit', compact('taxonomy', 'term')); + $parentTerms = TaxonomyTerm::where('taxonomy_id', $taxonomy->id )->get(); + return view('backend.taxonomy.terms.edit', compact('taxonomy', 'term', 'parentTerms')); } catch (\Exception $ex) { Log::error('Failed to load taxonomy term edit page', ['error' => $ex->getMessage()]); return abort(500); @@ -78,24 +138,77 @@ public function edit(Taxonomy $taxonomy, TaxonomyTerm $term) * @return \Illuminate\Http\RedirectResponse */ 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' - ]); +{ + try { + $validatedData = $request->validate([ + 'code' => 'required|unique:taxonomy_terms,code,' . $term->id, + 'name' => 'required', + 'parent_id' => 'nullable|exists:taxonomy_terms,id', + 'metadata' => 'array', + ]); - $term->update($validatedData); + foreach (json_decode($taxonomy->properties, true) as $property) { + $metadataKey = "metadata.{$property['code']}"; - 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.']); + switch ($property['data_type']) { + case 'string': + $request->validate([$metadataKey => 'nullable|string']); + break; + case 'integer': + $request->validate([$metadataKey => 'nullable|integer']); + break; + case 'float': + $request->validate([$metadataKey => 'nullable|numeric']); + break; + case 'boolean': + $request->validate([$metadataKey => 'nullable|boolean']); + break; + case 'date': + $request->validate([$metadataKey => 'nullable|date']); + break; + case 'datetime': + $request->validate([$metadataKey => 'nullable|date']); + break; + case 'url': + $request->validate([$metadataKey => 'nullable|url']); + break; + case 'image': + if ($request->hasFile("metadata.{$property['code']}")) { + $imagePath = $this->uploadThumb($term, $request->file("metadata.{$property['code']}"), "taxonomy_terms"); + $value = $imagePath; + } else { + $value = null; + } + break; + } + } + + $metadataArray = []; + foreach (json_decode($taxonomy->properties, true) as $property) { + $value = $request->input("metadata.{$property['code']}"); + + if ($property['data_type'] === 'boolean') { + $value = $request->has("metadata.{$property['code']}") ? true : false; + } + + $metadataArray[] = [ + 'code' => $property['code'], + 'value' => $value === '' ? null : $value + ]; } + + $term->update($validatedData); + $term->metadata = json_encode($metadataArray); + $term->updated_by = Auth::user()->id; + $term->save(); + + 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', ['term_id' => $term->id, 'error' => $ex->getMessage()]); + return back()->withInput()->withErrors(['error' => 'Failed to update taxonomy term. Please try again.']); } +} /** * Confirm to delete the specified resource from storage. * diff --git a/resources/views/backend/taxonomy/terms/create.blade.php b/resources/views/backend/taxonomy/terms/create.blade.php index 8637b16..064be16 100644 --- a/resources/views/backend/taxonomy/terms/create.blade.php +++ b/resources/views/backend/taxonomy/terms/create.blade.php @@ -6,6 +6,8 @@ +
+ @csrf
@@ -15,10 +17,13 @@
- +
- + + @foreach($parentTerms as $term) + + @endforeach
@@ -26,8 +31,11 @@
- + + @foreach($taxonomy_id as $tax_id) + + @endforeach
@@ -81,7 +89,7 @@ {!! Form::date("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code']]) !!} @break @case('datetime') - {!! Form::datetime("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code']]) !!} + {!! Form::datetimeLocal("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code']]) !!} @break @case('url') {!! Form::url("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code']]) !!} diff --git a/resources/views/backend/taxonomy/terms/edit.blade.php b/resources/views/backend/taxonomy/terms/edit.blade.php index 2060165..0220fd0 100644 --- a/resources/views/backend/taxonomy/terms/edit.blade.php +++ b/resources/views/backend/taxonomy/terms/edit.blade.php @@ -6,96 +6,120 @@ + + @csrf + @method('PUT') -
- -
- Term Configurations -
- -
-
- +
+
+ Term Configurations
- -
-
-
- + +
+
+ +
+ + @error('parent_id') +
{{ $message }}
+ @enderror
- -
-
-
- -
-
- {!! Form::text('tax_term_code', '', ['class' => 'form-control']) !!} + +
+
+ +
+ +
-
-
-
- -
-
- {!! Form::text('tax_term_name', '', ['class' => 'form-control']) !!} + +
+
+ +
+
+ {!! Form::text('code', old('code', $term->code), ['class' => 'form-control']) !!} + @error('code') +
{{ $message }}
+ @enderror +
-
-
- - +
+ + +