2023-02-01 10:07:37 +00:00
|
|
|
<?php
|
|
|
|
|
2023-05-04 23:11:55 +00:00
|
|
|
namespace App\Http\Controllers\Admin;
|
2023-02-01 10:07:37 +00:00
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Ticket;
|
|
|
|
use App\Models\TicketCategory;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class TicketCategoryController extends Controller
|
|
|
|
{
|
2023-05-04 23:11:55 +00:00
|
|
|
const READ_PERMISSION = "admin.tickets.read";
|
|
|
|
const WRITE_PERMISSION = "admin.tickets.write";
|
2023-02-01 10:07:37 +00:00
|
|
|
/**
|
2023-05-04 23:11:55 +00:00
|
|
|
*
|
2023-02-01 10:07:37 +00:00
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2023-05-04 23:11:55 +00:00
|
|
|
$this->checkPermission(self::READ_PERMISSION);
|
|
|
|
|
2023-02-02 12:47:21 +00:00
|
|
|
$categories = TicketCategory::all();
|
2023-05-04 23:11:55 +00:00
|
|
|
return view('admin.ticket.category')->with("categories",$categories);
|
2023-02-01 10:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2023-05-04 23:11:55 +00:00
|
|
|
$this->checkPermission(self::WRITE_PERMISSION);
|
|
|
|
|
2023-02-01 10:07:37 +00:00
|
|
|
$request->validate([
|
|
|
|
'name' => 'required|string|max:191',
|
|
|
|
]);
|
|
|
|
|
|
|
|
TicketCategory::create($request->all());
|
|
|
|
|
|
|
|
|
2023-05-04 23:11:55 +00:00
|
|
|
return redirect(route("admin.ticket.category.index"))->with("success",__("Category created"));
|
2023-02-01 10:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2023-02-02 12:47:21 +00:00
|
|
|
public function update(Request $request)
|
2023-02-01 10:07:37 +00:00
|
|
|
{
|
2023-05-04 23:11:55 +00:00
|
|
|
$this->checkPermission(self::WRITE_PERMISSION);
|
|
|
|
|
2023-02-02 12:47:21 +00:00
|
|
|
$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"));
|
|
|
|
|
2023-02-01 10:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2023-05-04 23:11:55 +00:00
|
|
|
$this->checkPermission(self::WRITE_PERMISSION);
|
|
|
|
|
2023-02-01 10:07:37 +00:00
|
|
|
$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()
|
2023-05-04 23:11:55 +00:00
|
|
|
->route('admin.ticket.category.index')
|
2023-02-01 10:07:37 +00:00
|
|
|
->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 '
|
2023-05-04 23:11:55 +00:00
|
|
|
<form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.ticket.category.destroy', $category->id).'">
|
2023-02-01 10:07:37 +00:00
|
|
|
'.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();
|
|
|
|
}
|
|
|
|
}
|