|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace RexlManu\LaravelTickets\Controllers; |
| 5 | + |
| 6 | + |
| 7 | +use Illuminate\Http\JsonResponse; |
| 8 | +use Illuminate\Http\RedirectResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Illuminate\Validation\Rule; |
| 11 | +use Illuminate\View\View; |
| 12 | +use RexlManu\LaravelTickets\Models\Ticket; |
| 13 | +use RexlManu\LaravelTickets\Models\TicketCategory; |
| 14 | +use RexlManu\LaravelTickets\Models\TicketMessage; |
| 15 | +use RexlManu\LaravelTickets\Models\TicketReference; |
| 16 | +use RexlManu\LaravelTickets\Models\TicketUpload; |
| 17 | +use RexlManu\LaravelTickets\Rule\TicketReferenceRule; |
| 18 | +use Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 19 | + |
| 20 | +/** |
| 21 | + * Class TicketController |
| 22 | + * |
| 23 | + * The main logic of the ticket system. All actions are performed here. |
| 24 | + * |
| 25 | + * If the accept header is json, the response will be a json response |
| 26 | + * |
| 27 | + * @package RexlManu\LaravelTickets\Controllers |
| 28 | + */ |
| 29 | +trait CategoryControllable |
| 30 | +{ |
| 31 | + |
| 32 | + /** |
| 33 | + * @link CategoryControllable constructor |
| 34 | + */ |
| 35 | + public function __construct() |
| 36 | + { |
| 37 | + if (!config('laravel-tickets.permission')) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + $this->middleware(config('laravel-tickets.permissions.list-category'))->only('index'); |
| 42 | + $this->middleware(config('laravel-tickets.permissions.create-category'))->only('store', 'create'); |
| 43 | + $this->middleware(config('laravel-tickets.permissions.show-category'))->only('show'); |
| 44 | + $this->middleware(config('laravel-tickets.permissions.edit-category'))->only('edit'); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Show every @return View|JsonResponse |
| 49 | + * |
| 50 | + * @link TicketCategory that the user has created |
| 51 | + * |
| 52 | + * If the accept header is json, the response will be a json response |
| 53 | + * |
| 54 | + */ |
| 55 | + public function index() |
| 56 | + { |
| 57 | + $categories = TicketCategory::orderBy('id', 'desc')->paginate(10); |
| 58 | + |
| 59 | + return request()->wantsJson() ? |
| 60 | + response()->json(compact('categories')) : |
| 61 | + view( |
| 62 | + 'laravel-tickets::categories.index', |
| 63 | + compact('categories') |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Show the create form |
| 69 | + * |
| 70 | + * @return View |
| 71 | + */ |
| 72 | + public function create() |
| 73 | + { |
| 74 | + return view('laravel-tickets::categories.create'); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Creates a @param Request $request the request |
| 79 | + * |
| 80 | + * @return View|JsonResponse|RedirectResponse |
| 81 | + * @link TicketCategory |
| 82 | + * |
| 83 | + */ |
| 84 | + public function store(Request $request) |
| 85 | + { |
| 86 | + $rules = [ |
| 87 | + 'translation' => ['required', 'string', 'max:191'], |
| 88 | + ]; |
| 89 | + $data = $request->validate($rules); |
| 90 | + |
| 91 | + if ($request->has('action') && $request->action == 'edit') { |
| 92 | + $category = TicketCategory::where('id', $request->category_id)->update( |
| 93 | + $request->only('translation') |
| 94 | + ); |
| 95 | + $message = trans('The category was successfully updated'); |
| 96 | + } else { |
| 97 | + $category = TicketCategory::create( |
| 98 | + $request->only('translation') |
| 99 | + ); |
| 100 | + $message = trans('The category was successfully created'); |
| 101 | + } |
| 102 | + |
| 103 | + return $request->wantsJson() ? |
| 104 | + response()->json(compact('category')) : |
| 105 | + redirect(route( |
| 106 | + 'laravel-tickets.categories.index' |
| 107 | + ))->with([ |
| 108 | + 'message' => $message, |
| 109 | + 'type' => 'success' |
| 110 | + ]); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Show detailed informations about the @param TicketCategory $category |
| 115 | + * |
| 116 | + * @return View|JsonResponse|RedirectResponse|void |
| 117 | + * @link TicketCategory and the informations |
| 118 | + * |
| 119 | + */ |
| 120 | + public function show(TicketCategory $category) |
| 121 | + { |
| 122 | + if ( |
| 123 | + !request()->user()->can(config('laravel-tickets.permissions.all-ticket')) |
| 124 | + ) { |
| 125 | + return abort(403); |
| 126 | + } |
| 127 | + return \request()->wantsJson() ? |
| 128 | + response()->json(compact( |
| 129 | + 'category', |
| 130 | + )) : |
| 131 | + view( |
| 132 | + 'laravel-tickets::categories.show', |
| 133 | + compact( |
| 134 | + 'category', |
| 135 | + ) |
| 136 | + )->with(['action' => 'show']); |
| 137 | + } |
| 138 | + /** |
| 139 | + * edit detailed informations about the @param TicketCategory $category |
| 140 | + * |
| 141 | + * @return View|JsonResponse|RedirectResponse|void |
| 142 | + * @link TicketCategory and the informations |
| 143 | + * |
| 144 | + */ |
| 145 | + public function edit(TicketCategory $category) |
| 146 | + { |
| 147 | + if ( |
| 148 | + !request()->user()->can(config('laravel-tickets.permissions.all-ticket')) |
| 149 | + ) { |
| 150 | + return abort(403); |
| 151 | + } |
| 152 | + return \request()->wantsJson() ? |
| 153 | + response()->json(compact( |
| 154 | + 'category', |
| 155 | + )) : |
| 156 | + view( |
| 157 | + 'laravel-tickets::categories.show', |
| 158 | + compact( |
| 159 | + 'category', |
| 160 | + ) |
| 161 | + )->with(['action' => 'edit']); |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + public function destroy(TicketCategory $category) |
| 166 | + { |
| 167 | + if ( |
| 168 | + !request()->user()->can(config('laravel-tickets.permissions.all-ticket')) |
| 169 | + ) { |
| 170 | + return abort(403); |
| 171 | + } |
| 172 | + |
| 173 | + $category->delete(); |
| 174 | + |
| 175 | + $message = trans('The category was successfully deleted'); |
| 176 | + |
| 177 | + return \request()->wantsJson() ? |
| 178 | + response()->json(compact( |
| 179 | + 'message', |
| 180 | + )) : |
| 181 | + redirect()->route('laravel-tickets.categories.index') |
| 182 | + ->with([ |
| 183 | + 'message' => $message, |
| 184 | + 'type' => 'success' |
| 185 | + ]); |
| 186 | + } |
| 187 | +} |
0 commit comments