[FEATURE] Add/Delete Ticket Categories (#699)

This commit is contained in:
Dennis 2023-02-02 14:33:26 +01:00 committed by GitHub
commit 58917d0b3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 259 additions and 1 deletions

View file

@ -0,0 +1,117 @@
<?php
namespace App\Http\Controllers\Moderation;
use App\Http\Controllers\Controller;
use App\Models\Ticket;
use App\Models\TicketCategory;
use Illuminate\Http\Request;
class TicketCategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$categories = TicketCategory::all();
return view('moderator.ticket.category')->with("categories",$categories);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:191',
]);
TicketCategory::create($request->all());
return redirect(route("moderator.ticket.category.index"))->with("success",__("Category created"));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function update(Request $request)
{
$request->validate([
'category' => 'required|int',
'name' => 'required|string|max:191',
]);
$category = TicketCategory::where("id",$request->category)->firstOrFail();
$category->name = $request->name;
$category->save();
return redirect()->back()->with("success",__("Category name updated"));
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$category = TicketCategory::where("id",$id)->firstOrFail();
if($category->id == 5 ){ //cannot delete "other" category
return back()->with("error","You cannot delete that category");
}
$tickets = Ticket::where("ticketcategory_id",$category->id)->get();
foreach($tickets as $ticket){
$ticket->ticketcategory_id = "5";
$ticket->save();
}
$category->delete();
return redirect()
->route('moderator.ticket.category.index')
->with('success', __('Category removed'));
}
public function datatable()
{
$query = TicketCategory::withCount("tickets");
return datatables($query)
->addColumn('name', function ( TicketCategory $category) {
return $category->name;
})
->editColumn('tickets', function ( TicketCategory $category) {
return $category->tickets_count;
})
->addColumn('actions', function (TicketCategory $category) {
return '
<form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('moderator.ticket.category.destroy', $category->id).'">
'.csrf_field().'
'.method_field('DELETE').'
<button data-content="'.__('Delete').'" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
</form>
';
})
->editColumn('created_at', function (TicketCategory $category) {
return $category->created_at ? $category->created_at->diffForHumans() : '';
})
->rawColumns(['actions'])
->make();
}
}

View file

@ -10,6 +10,6 @@ class TicketCategory extends Model
public function tickets() public function tickets()
{ {
return $this->hasMany(Ticket::class); return $this->hasMany(Ticket::class,'ticketcategory_id');
} }
} }

View file

@ -21,6 +21,7 @@ use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\VoucherController; use App\Http\Controllers\Admin\VoucherController;
use App\Http\Controllers\Auth\SocialiteController; use App\Http\Controllers\Auth\SocialiteController;
use App\Http\Controllers\HomeController; use App\Http\Controllers\HomeController;
use App\Http\Controllers\Moderation\TicketCategoryController;
use App\Http\Controllers\Moderation\TicketsController as ModTicketsController; use App\Http\Controllers\Moderation\TicketsController as ModTicketsController;
use App\Http\Controllers\NotificationController; use App\Http\Controllers\NotificationController;
use App\Http\Controllers\ProductController as FrontProductController; use App\Http\Controllers\ProductController as FrontProductController;
@ -218,6 +219,11 @@ Route::middleware(['auth', 'checkSuspended'])->group(function () {
Route::post('ticket/blacklist/delete/{id}', [ModTicketsController::class, 'blacklistDelete'])->name('ticket.blacklist.delete'); Route::post('ticket/blacklist/delete/{id}', [ModTicketsController::class, 'blacklistDelete'])->name('ticket.blacklist.delete');
Route::post('ticket/blacklist/change/{id}', [ModTicketsController::class, 'blacklistChange'])->name('ticket.blacklist.change'); Route::post('ticket/blacklist/change/{id}', [ModTicketsController::class, 'blacklistChange'])->name('ticket.blacklist.change');
Route::get('ticket/blacklist/datatable', [ModTicketsController::class, 'dataTableBlacklist'])->name('ticket.blacklist.datatable'); Route::get('ticket/blacklist/datatable', [ModTicketsController::class, 'dataTableBlacklist'])->name('ticket.blacklist.datatable');
Route::get('ticket/category/datatable', [TicketCategoryController::class, 'datatable'])->name('ticket.category.datatable');
Route::resource("ticket/category", TicketCategoryController::class,['as' => 'ticket']);
}); });
Route::get('/home', [HomeController::class, 'index'])->name('home'); Route::get('/home', [HomeController::class, 'index'])->name('home');

View file

@ -0,0 +1,132 @@
@extends('layouts.main')
@section('content')
<!-- CONTENT HEADER -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>{{ __('Ticket Categories') }}</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="{{ route('home') }}">{{ __('Dashboard') }}</a></li>
<li class="breadcrumb-item"><a class="text-muted"
href="{{ route("moderator.ticket.category.index") }}">{{ __('Ticket Categories') }}</a>
</li>
</ol>
</div>
</div>
</div>
</section>
<!-- END CONTENT HEADER -->
<!-- MAIN CONTENT -->
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-8">
<div class="card">
<div class="card-header">
<div class="d-flex justify-content-between">
<h5 class="card-title"><i class="fas fas fa-users mr-2"></i>{{__('Categories')}}</h5>
</div>
</div>
<div class="card-body table-responsive">
<table id="datatable" class="table table-striped">
<thead>
<tr>
<th>{{__('ID')}}</th>
<th>{{__('Name')}}</th>
<th>{{__('Tickets')}}</th>
<th>{{__('Created At')}}</th>
<th>{{__('Actions')}}</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="card">
<div class="card-header">
<h5 class="card-title">{{__('Add Category')}}
</div>
<div class="card-body">
<form action="{{route("moderator.ticket.category.store")}}" method="POST" class="ticket-form">
@csrf
<div class="form-group ">
<label for="name" class="control-label">{{__("Name")}}</label>
<input id="name" type="text" class="form-control" name="name" required>
</div>
<button type="submit" class="btn btn-primary">
{{__('Submit')}}
</button>
</form>
</div>
</div>
<div class="card">
<div class="card-header">
<h5 class="card-title">{{__('Edit Category')}}
</div>
<div class="card-body">
<form action="{{route("moderator.ticket.category.update","1")}}" method="POST" class="ticket-form">
@csrf
@method('PATCH')
<select id="category" style="width:100%" class="custom-select" name="category"
required autocomplete="off" @error('category') is-invalid @enderror>
@foreach ($categories as $category)
<option value="{{ $category->id }}">{{ __($category->name) }}</option>
@endforeach
</select>
<div class="form-group ">
<label for="name" class="control-label">{{__("New Name")}}</label>
<input id="name" type="text" class="form-control" name="name" required>
</div>
<button type="submit" class="btn btn-primary">
{{__('Submit')}}
</button>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- END CONTENT -->
<script>
document.addEventListener("DOMContentLoaded", function () {
$('#datatable').DataTable({
language: {
url: '//cdn.datatables.net/plug-ins/1.11.3/i18n/{{config("SETTINGS::LOCALE:DATATABLES")}}.json'
},
processing: true,
serverSide: true,
stateSave: true,
ajax: "{{route('moderator.ticket.category.datatable')}}",
columns: [
{data: 'id'},
{data: 'name'},
{data: 'tickets'},
{data: 'created_at', sortable: false},
{data: 'actions', sortable: false},
],
fnDrawCallback: function( oSettings ) {
$('[data-toggle="popover"]').popover();
}
});
});
document.addEventListener('DOMContentLoaded', (event) => {
$('.custom-select').select2();
})
</script>
@endsection

View file

@ -30,8 +30,11 @@
<div class="d-flex justify-content-between"> <div class="d-flex justify-content-between">
<h5 class="card-title"><i class="fas fa-ticket-alt mr-2"></i>{{__('Ticket List')}}</h5> <h5 class="card-title"><i class="fas fa-ticket-alt mr-2"></i>{{__('Ticket List')}}</h5>
</div> </div>
<a href="{{route("moderator.ticket.category.index")}}"><button class="btn btn-primary float-right">+ {{__("Add Category")}}</button></a>
</div> </div>
<div class="card-body table-responsive"> <div class="card-body table-responsive">
<table id="datatable" class="table table-striped"> <table id="datatable" class="table table-striped">