Skip to content

Commit

Permalink
126 UI taxonomy terms index create edit and delete pages (#177)
Browse files Browse the repository at this point in the history
Co-authored-by: Hasan10100 <[email protected]>
Co-authored-by: kusaljayawardhana <[email protected]>
  • Loading branch information
3 people authored Oct 15, 2024
1 parent 4533dd4 commit 0a82200
Show file tree
Hide file tree
Showing 14 changed files with 471 additions and 33 deletions.
6 changes: 6 additions & 0 deletions app/Domains/Taxonomy/Models/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
15 changes: 15 additions & 0 deletions app/Domains/Taxonomy/Models/TaxonomyTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
74 changes: 55 additions & 19 deletions app/Http/Controllers/Backend/TaxonomyTermController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,67 @@
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.
*
* @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.
*
* @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);
}
Expand All @@ -55,36 +77,50 @@ 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.
*
* @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.']);
}
}
}
3 changes: 2 additions & 1 deletion app/Http/Livewire/Backend/TaxonomyTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function columns(): array

public function query(): Builder
{
return Taxonomy::query();
return Taxonomy::query()
->with('user');
}

public function rowView(): string
Expand Down
58 changes: 58 additions & 0 deletions app/Http/Livewire/Backend/TaxonomyTermTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Http\Livewire\Backend;

use App\Domains\Taxonomy\Models\TaxonomyTerm;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;

class TaxonomyTermTable extends DataTableComponent
{
public array $perPageAccepted = [10, 25, 50];
public bool $perPageAll = true;

public string $defaultSortColumn = 'created_at';
public string $defaultSortDirection = 'desc';

public $taxonomy;

public function mount($taxonomy)
{
$this->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';
}
}
8 changes: 4 additions & 4 deletions resources/views/backend/taxonomy/index-table-row.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@
<div class="btn-group" role="group" aria-label="">

<!-- View Button -->
{{-- <a href="{{ route('taxonomy.view', $row->id) }}" class="btn btn-sm btn-primary">
{{-- <a href="{{ route('taxonomy.view', $row) }}" class="btn btn-sm btn-primary">
<i class="fa fa-eye" title="View"></i>
</a> --}}

<!-- Manage Button -->
<a href="{{ route('dashboard.taxonomy.terms.index', $row->id) }}" class="btn btn-sm btn-secondary">
<a href="{{ route('dashboard.taxonomy.terms.index', $row) }}" class="btn btn-sm btn-secondary">
<i class="fa fa-list" title="Manage"></i>
</a>

<!-- Edit Button -->
<a href="{{ route('dashboard.taxonomy.edit', $row->id) }}" class="btn btn-sm btn-warning">
<a href="{{ route('dashboard.taxonomy.edit', $row) }}" class="btn btn-sm btn-warning">
<i class="fa fa-pencil" title="Edit"></i>
</a>

<!-- Delete Button -->
<a href="{{ route('dashboard.taxonomy.delete', $row->id) }}" class="btn btn-sm btn-danger">
<a href="{{ route('dashboard.taxonomy.delete', $row) }}" class="btn btn-sm btn-danger">
<i class="fa fa-trash" title="Delete"></i>
</a>

Expand Down
109 changes: 109 additions & 0 deletions resources/views/backend/taxonomy/terms/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
@extends('backend.layouts.app')

@section('content')
<div>

<x-backend.card>

<x-slot name="body">

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

<div class="col-12 pb-3">
<strong>Term Configurations</strong>
</div>

<div class="col-12 py-2">
<div class="col ps-0">
<label for="drop1">Parent Taxonomy Term</label>
</div>
<select class="form-select">
<option style="display:none" selected></option>
</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>
</div>

<div class="col-12 py-2">
<div class="col ps-0">
<label for="drop1">Taxonomy Term Code*</label>
</div>
<div class="col-md-12 px-0">
{!! Form::text('code', '', ['class' => 'form-control']) !!}
</div>
</div>

<div class="col-12 py-2">
<div class="col ps-0">
<label for="drop1">Taxonomy Term Name*</label>
</div>
<div class="col-md-12 px-0">
{!! Form::text('name', '', ['class' => 'form-control']) !!}
</div>
</div>
</div>

<div class="metadata py-3 mt-5 mb-3" style="border: 1px solid rgb(207, 207, 207); border-radius:5px">

<div class="col-12 pb-3">
<strong>Metadata</strong>
</div>

@foreach(json_decode($taxonomy->properties, true) as $property)
<div class="col-12 py-2">
<div class="col ps-0">
<label >{{ $property['name'] }} ({{\App\Domains\Taxonomy\Models\Taxonomy::$propertyType[$property['data_type']]}})</label>
</div>
<div class="col-md-12 px-0">
@switch($property['data_type'])
@case('string')
{!! Form::text("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code']]) !!}
@break
@case('integer')
{!! Form::number("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code'], 'step' => '1']) !!}
@break
@case('float')
{!! Form::number("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code'], 'step' => 'any']) !!}
@break
@case('boolean')
<div class="form-check">
{!! Form::checkbox("metadata[{$property['code']}]", 1, null, ['class' => 'form-check-input', 'id' => $property['code']]) !!}
</div>
@break
@case('date')
{!! 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']]) !!}
@break
@case('url')
{!! Form::url("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code']]) !!}
@break
@case('image')
{!! Form::file("metadata[{$property['code']}]", ['class' => 'form-control', 'id' => $property['code']]) !!}
@break
@default
{!! Form::text("metadata[{$property['code']}]", null, ['class' => 'form-control', 'id' => $property['code']]) !!}
@endswitch
</div>
</div>
@endforeach
</div>
</x-slot>

<x-slot name="footer">
{!! Form::submit('Create', ['class' => 'btn btn-primary btn-w-150 float-right', 'id' => 'submit-button']) !!}
</x-slot>

</x-backend.card>

</div>

@endsection
Loading

0 comments on commit 0a82200

Please sign in to comment.