TicketCategoryController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Http\Controllers\Moderation;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Ticket;
  5. use App\Models\TicketCategory;
  6. use Illuminate\Http\Request;
  7. class TicketCategoryController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index()
  15. {
  16. $categories = TicketCategory::all();
  17. return view('moderator.ticket.category')->with("categories",$categories);
  18. }
  19. /**
  20. * Store a newly created resource in storage.
  21. *
  22. * @param \Illuminate\Http\Request $request
  23. * @return \Illuminate\Http\Response
  24. */
  25. public function store(Request $request)
  26. {
  27. $request->validate([
  28. 'name' => 'required|string|max:191',
  29. ]);
  30. TicketCategory::create($request->all());
  31. return redirect(route("moderator.ticket.category.index"))->with("success",__("Category created"));
  32. }
  33. /**
  34. * Update the specified resource in storage.
  35. *
  36. * @param \Illuminate\Http\Request $request
  37. * @return \Illuminate\Http\Response
  38. */
  39. public function update(Request $request)
  40. {
  41. $request->validate([
  42. 'category' => 'required|int',
  43. 'name' => 'required|string|max:191',
  44. ]);
  45. $category = TicketCategory::where("id",$request->category)->firstOrFail();
  46. $category->name = $request->name;
  47. $category->save();
  48. return redirect()->back()->with("success",__("Category name updated"));
  49. }
  50. /**
  51. * Remove the specified resource from storage.
  52. *
  53. * @param int $id
  54. * @return \Illuminate\Http\Response
  55. */
  56. public function destroy($id)
  57. {
  58. $category = TicketCategory::where("id",$id)->firstOrFail();
  59. if($category->id == 5 ){ //cannot delete "other" category
  60. return back()->with("error","You cannot delete that category");
  61. }
  62. $tickets = Ticket::where("ticketcategory_id",$category->id)->get();
  63. foreach($tickets as $ticket){
  64. $ticket->ticketcategory_id = "5";
  65. $ticket->save();
  66. }
  67. $category->delete();
  68. return redirect()
  69. ->route('moderator.ticket.category.index')
  70. ->with('success', __('Category removed'));
  71. }
  72. public function datatable()
  73. {
  74. $query = TicketCategory::withCount("tickets");
  75. return datatables($query)
  76. ->addColumn('name', function ( TicketCategory $category) {
  77. return $category->name;
  78. })
  79. ->editColumn('tickets', function ( TicketCategory $category) {
  80. return $category->tickets_count;
  81. })
  82. ->addColumn('actions', function (TicketCategory $category) {
  83. return '
  84. <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('moderator.ticket.category.destroy', $category->id).'">
  85. '.csrf_field().'
  86. '.method_field('DELETE').'
  87. <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>
  88. </form>
  89. ';
  90. })
  91. ->editColumn('created_at', function (TicketCategory $category) {
  92. return $category->created_at ? $category->created_at->diffForHumans() : '';
  93. })
  94. ->rawColumns(['actions'])
  95. ->make();
  96. }
  97. }