Skip to content

Commit

Permalink
Term Display improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
NuwanJ committed Oct 17, 2024
1 parent b4db0cb commit d61328e
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 96 deletions.
4 changes: 3 additions & 1 deletion app/Domains/Taxonomy/Models/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public function user_updated()

public function terms()
{
return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id');
return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id')
->orderBy('parent_id', 'asc')
->orderBy('code', 'asc');
}

protected static function newFactory()
Expand Down
14 changes: 14 additions & 0 deletions app/Domains/Taxonomy/Models/TaxonomyTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ public function getMetadata($code)
return null;
}

public static function getHierarchicalPath($id)
{
$term = TaxonomyTerm::find($id);
if ($term != null) {
if ($term->parent_id != null) {
return TaxonomyTerm::getHierarchicalPath($term->parent_id) . " > " . $term->name;
} else {
return $term->name;
}
} else {
return '';
}
}

protected static function newFactory()
{
return TaxonomyTermFactory::new();
Expand Down
167 changes: 81 additions & 86 deletions app/Http/Controllers/Backend/TaxonomyTermController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,19 @@ public function index(Taxonomy $taxonomy)
public function create(TaxonomyTerm $taxonomyTerm, Taxonomy $taxonomy)
{
try {
$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'));
return view('backend.taxonomy.terms.create', compact('taxonomy'));
} catch (\Exception $ex) {
Log::error('Failed to load taxonomy terms creation page', ['error' => $ex->getMessage()]);
Log::error('Failed to load taxonomy terms creation page', ['error' => $ex->getMessage()]);
return abort(500);
}
}
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|void
*/
public function store(Request $request, Taxonomy $taxonomy,TaxonomyTerm $taxonomyTerm)
public function store(Request $request, Taxonomy $taxonomy, TaxonomyTerm $taxonomyTerm)
{
try {
$validatedData = $request->validate([
Expand All @@ -48,11 +45,11 @@ public function store(Request $request, Taxonomy $taxonomy,TaxonomyTerm $taxonom
'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']);
Expand All @@ -79,20 +76,19 @@ public function store(Request $request, Taxonomy $taxonomy,TaxonomyTerm $taxonom

if ($request->hasFile("metadata.{$property['code']}")) {
$imagePath = $this->uploadThumb(null, $request->file("metadata.{$property['code']}"), "taxonomy_terms");
$value = $imagePath;
$value = $imagePath;
} else {
$value = null;
$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;
}
Expand All @@ -108,9 +104,9 @@ public function store(Request $request, Taxonomy $taxonomy,TaxonomyTerm $taxonom
$taxonomyTerm->save();

return redirect()->route('dashboard.taxonomy.terms.index', $taxonomy)
->with('Success', 'Taxonomy term was created successfully!');
->with('Success', 'Taxonomy term was created successfully!');
} catch (\Exception $ex) {
Log::error('Failed to create taxonomy term', ['error' => $ex->getMessage()]);
Log::error('Failed to create taxonomy term', ['error' => $ex->getMessage()]);
return back()->withInput()->withErrors(['error' => 'Failed to create taxonomy term. Please try again.']);
}
}
Expand All @@ -123,10 +119,9 @@ public function store(Request $request, Taxonomy $taxonomy,TaxonomyTerm $taxonom
public function edit(Taxonomy $taxonomy, TaxonomyTerm $term)
{
try {
$parentTerms = TaxonomyTerm::where('taxonomy_id', $taxonomy->id )->get();
return view('backend.taxonomy.terms.edit', compact('taxonomy', 'term', 'parentTerms'));
return view('backend.taxonomy.terms.edit', compact('taxonomy', 'term'));
} catch (\Exception $ex) {
Log::error('Failed to load taxonomy term edit page', ['error' => $ex->getMessage()]);
Log::error('Failed to load taxonomy term edit page', ['error' => $ex->getMessage()]);
return abort(500);
}
}
Expand All @@ -138,78 +133,78 @@ 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',
'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($term, $request->file("metadata.{$property['code']}"), "taxonomy_terms");
$value = $imagePath;
} else {
$value = null;
}
break;
}
}
{
try {
$validatedData = $request->validate([
'code' => 'required|unique:taxonomy_terms,code,' . $term->id,
'name' => 'required',
'parent_id' => 'nullable|exists:taxonomy_terms,id',
'metadata' => 'array',
]);

$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;
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($term, $request->file("metadata.{$property['code']}"), "taxonomy_terms");
$value = $imagePath;
} else {
$value = null;
}
break;
}
}

$metadataArray[] = [
'code' => $property['code'],
'value' => $value === '' ? null : $value
];
}
$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();
$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.']);
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.
*
* @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm
Expand Down
10 changes: 6 additions & 4 deletions resources/views/backend/taxonomy/terms/create.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<?php use App\Domains\Taxonomy\Models\TaxonomyTerm; ?>

@extends('backend.layouts.app')

@section('title', __('Create Taxonomy Term'))
Expand Down Expand Up @@ -36,14 +38,14 @@
</div>
<select name="parent_id" class="form-select">
<option value="" selected>Select</option>
@foreach ($parentTerms as $term)
<option value="{{ $term->id }}">{{ $term->name }}</option>
@foreach ($taxonomy->terms as $sibling)
<option value="{{ $sibling->id }}">
{{ TaxonomyTerm::getHierarchicalPath($sibling->id) }}
</option>
@endforeach
</select>
</div>



<!-- Taxonomy Term Code -->
<div class="col-12 py-2">
<div class="col ps-0">
Expand Down
12 changes: 7 additions & 5 deletions resources/views/backend/taxonomy/terms/edit.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<?php use App\Domains\Taxonomy\Models\TaxonomyTerm; ?>

@extends('backend.layouts.app')

@section('title', __('Edit Taxonomy Term'))
Expand Down Expand Up @@ -33,11 +35,11 @@
</div>
<select name="parent_id" class="form-select">
<option value="" selected>Select</option>
@foreach ($parentTerms as $parent)
@if ($parent->id != $term->id)
<option value="{{ $parent->id }}"
{{ old('parent_id', $term->parent_id) == $parent->id ? 'selected' : '' }}>
{{ $parent->name }}
@foreach ($term->taxonomy->terms as $sibling)
@if ($sibling->id != $term->id)
<option value="{{ $sibling->id }}"
{{ old('parent_id', $sibling->parent_id) == $sibling->id ? 'selected' : '' }}>
{{ TaxonomyTerm::getHierarchicalPath($sibling->id) }}
</option>
@endif
@endforeach
Expand Down

0 comments on commit d61328e

Please sign in to comment.