Skip to content

Commit

Permalink
140 BE taxonomy terms create edit and delete pages (#179)
Browse files Browse the repository at this point in the history
Co-authored-by: Supun Dulara <[email protected]>
  • Loading branch information
kusaljayawardhana and supundulara authored Oct 16, 2024
1 parent 0a82200 commit ff2e611
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 102 deletions.
14 changes: 14 additions & 0 deletions app/Domains/Taxonomy/Models/TaxonomyTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
155 changes: 134 additions & 21 deletions app/Http/Controllers/Backend/TaxonomyTermController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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.
*
Expand Down
20 changes: 14 additions & 6 deletions resources/views/backend/taxonomy/terms/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<x-backend.card>

<x-slot name="body">
<form method="POST" action="{{ route('dashboard.taxonomy.terms.store', $taxonomy) }}">
@csrf

<div class="term py-2 pt-3" style="border: 1px solid rgb(207, 207, 207); border-radius:5px">

Expand All @@ -15,19 +17,25 @@

<div class="col-12 py-2">
<div class="col ps-0">
<label for="drop1">Parent Taxonomy Term</label>
<label for="drop1">Parent Taxonomy Term ( Optional)</label>
</div>
<select class="form-select">
<option style="display:none" selected></option>
<select name="parent_id" class="form-select">
<option value="" selected>Select</option>
@foreach($parentTerms as $term)
<option value="{{ $term->id }}">{{ $term->name }}</option>
@endforeach
</select>
</div>

<div class="col-12 py-2">
<div class="col ps-0">
<label for="drop1">Taxonomy*</label>
</div>
<select class="form-select">
<option style="display:none" selected></option>
<select name="taxonomy_id" class="form-select">
<option value="" selected>Select</option>
@foreach($taxonomy_id as $tax_id)
<option value="{{ $tax_id->id }}">{{ $tax_id->name }}</option>
@endforeach
</select>
</div>

Expand Down Expand Up @@ -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']]) !!}
Expand Down
Loading

0 comments on commit ff2e611

Please sign in to comment.