Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TriviaController.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace App\Http\Controllers\Admin\Trivia;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Http\Request;
  6. use App\Helpers\Helper;
  7. use App\Model\Trivia;
  8. use App\Model\TriviaAnswer;
  9. use Yajra\Datatables\Datatables;
  10. use Auth;
  11. use Illuminate\Support\Facades\Storage;
  12. use Validator;
  13. use Hashids;
  14. use URL;
  15. //load modelmu
  16. class TriviaController extends Controller
  17. {
  18. /**
  19. * Title untuk judul di web
  20. * route digunakan untuk tempat resource (file path) + routing (route/web) diusahain sama ya biar gak ngubah"
  21. */
  22. private $title = 'Trivia'; /**jangan lupa diganti*/
  23. private $route = 'admin.trivia.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
  24. private $header = 'Trivia';
  25. private $sub_header = 'Trivia';
  26. private $url = 'admin.trivia.';
  27. public function __construct()
  28. {
  29. DB::enableQueryLog();
  30. /** nyalakan jika sudah set rolenya, jika ini dinyalakan halaman ini tidak akan keluar */
  31. // $this->middleware('permission:searching-list|searching-create|searching-update|searching-delete', ['only' => ['index', 'create', 'update']]);
  32. // $this->middleware('permission:searching-create', ['only' => ['create', 'create_action']]);
  33. // $this->middleware('permission:searching-update', ['only' => ['update', 'update_action']]);
  34. // $this->middleware('permission:searching-delete', ['only' => ['delete']]);
  35. }
  36. /**
  37. * Ini contoh crud yang sudah jalan
  38. * index digunakna untuk tampilan awal dari menu yang akan dibuat
  39. * Untuk CRUD Biar cepat
  40. * Silahkan ganti
  41. * EloquentMu:: => dengan model anda
  42. * $id_modelmu => ganti dengan id di model anda
  43. * id_di_routenya => jangan lupa disesuaikan dengan yang dideclare di route
  44. */
  45. public function index()
  46. {
  47. Helper::swal();
  48. $data = [
  49. //bawaan
  50. 'title' => $this->title,
  51. 'route' => $this->route,
  52. 'header' => $this->header,
  53. 'sub_header' => $this->sub_header,
  54. ];
  55. // dd($data);
  56. return view($this->url . 'index', $data);
  57. }
  58. public function getData($udd_id = null)
  59. {
  60. // $query = Trivia::query();
  61. $query = Trivia::leftJoin('trivia_answer', 'trivia.id', '=', 'trivia_answer.trivia_id')
  62. ->select(['trivia.question', 'trivia.id', 'trivia.image', 'trivia_answer.answer', 'trivia_answer.is_right'])
  63. ->where('trivia_answer.is_right', 't');
  64. // dd($query);
  65. return Datatables::of($query)
  66. ->addIndexColumn()
  67. ->addColumn('image', function ($query) {
  68. $image = '<img src="'.URL::to('storage/images/trivia/'.$query->image).'" alt="'.$query->image.'" style="height: 100px;">';
  69. return $image;
  70. })
  71. ->addColumn('aksi', function ($query) {
  72. /**cek role */
  73. $aksi = '';
  74. // if (Auth::user()->can('master-jenis-kantong-update')) {
  75. $aksi .= "<a href='" . route($this->route . 'update', ['trivia_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
  76. // }
  77. // if (Auth::user()->can('master-jenis-kantong-delete')) {
  78. $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_action', ['trivia_id' => Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
  79. // }
  80. return $aksi;
  81. })
  82. ->rawColumns(['aksi', 'image'])
  83. ->toJson();
  84. }
  85. public function create()
  86. {
  87. $data = [
  88. //bawaan
  89. 'title' => $this->title,
  90. 'route' => $this->route,
  91. 'header' => $this->header,
  92. 'sub_header' => $this->sub_header,
  93. ];
  94. Helper::swal();
  95. return view($this->url . 'create', $data);
  96. }
  97. public function create_action(Request $request)
  98. {
  99. //declare post untuk simpan semua request yang diinputkan oleh form name=""
  100. $post = $request->all();
  101. // dd($post);
  102. $file = $request->file('image');
  103. //dd($post);
  104. /**digunakan untuk set rule validator */
  105. $rules = [
  106. 'question' => 'required',
  107. 'image' => 'required|file|max:1000',
  108. ];
  109. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  110. $alert = [
  111. 'file' => 'File maksimal 1MB'
  112. ];
  113. $validator = Validator::make($post, $rules, $alert);
  114. if ($validator->passes()) {
  115. /**menggunakan transaction */
  116. DB::beginTransaction();
  117. $nama_file = time()."_".$file->getClientOriginalName();
  118. $tujuan_upload = 'image';
  119. //$file->move($tujuan_upload,$nama_file);
  120. Storage::putFileAs('public/images/trivia/', $file, $nama_file );
  121. $trivia = new Trivia();
  122. $trivia->question = $request->question;
  123. $trivia->image = $nama_file;
  124. $trivia->save();
  125. foreach ($request->answer as $key => $value) {
  126. $trivia_answer = new TriviaAnswer();
  127. $trivia_answer->answer = $value;
  128. $trivia_answer->trivia_id = $trivia->id;
  129. $trivia_answer->is_right = ($key==0)?"t":"f";
  130. $trivia_answer->save();
  131. }
  132. if ($trivia) {
  133. DB::commit();
  134. $message = 'Berhasil';
  135. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  136. } else {
  137. DB::rollback();
  138. $message = 'Gagal';
  139. return redirect()->back()->with('error', Helper::parsing_alert($message));
  140. }
  141. }
  142. /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
  143. $message = Helper::parsing_alert($validator->errors()->all());
  144. return redirect()->back()->with('error', $message)->withInput();
  145. }
  146. public function update($data_id)
  147. {
  148. Helper::swal();
  149. $data_id = Hashids::decode($data_id);
  150. if (!empty($data_id)) {
  151. $trivia = Trivia::where('id', $data_id[0])->first();
  152. $trivia_answer = TriviaAnswer::where('trivia_id', $data_id[0])->get();
  153. if ($trivia) {
  154. $data = [
  155. //bawaan
  156. 'title' => $this->title,
  157. 'route' => $this->route,
  158. 'header' => $this->header,
  159. 'sub_header' => $this->sub_header,
  160. 'trivia' => $trivia,
  161. 'trivia_answer' => $trivia_answer
  162. ];
  163. return view($this->url . 'update', $data);
  164. }
  165. $message = 'Id tidak ditemukan atau sudah dihapus';
  166. return redirect()->back()->with('error', $message);
  167. }
  168. $message = 'Id tdak ditemukan';
  169. return redirect()->back()->with('error', $message);
  170. }
  171. public function update_action(Request $request)
  172. {
  173. $post = $request->all();
  174. // dd($post);
  175. /**digunakan untuk set rule validator */
  176. $rules = [
  177. 'question' => 'required',
  178. ];
  179. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  180. $alert = [];
  181. $validator = Validator::make($post, $rules, $alert);
  182. $data_id = Hashids::decode($request['id']);
  183. if (!empty($data_id)) {
  184. /**cek apakah data_idnya ada ? */
  185. $trivia = Trivia::find($data_id[0]);
  186. if ($trivia) {
  187. /**cek apakah id data benar" ada di DB ? */
  188. if ($validator->passes()) {
  189. /**validatornya */
  190. DB::beginTransaction();
  191. $file = $request->file('image');
  192. if($file){
  193. $nama_file = time()."_".$file->getClientOriginalName();
  194. $tujuan_upload = 'image';
  195. //$file->move($tujuan_upload,$nama_file);
  196. Storage::putFileAs('public/images/trivia/', $file, $nama_file );
  197. //File::delete('image/'.$trivia->image);
  198. Storage::delete('public/images/trivia/'.$trivia->image);
  199. $trivia->image = $nama_file;
  200. }
  201. $trivia->question = $request->question;
  202. $trivia->save();
  203. $counter=0;
  204. foreach ($request->answer as $key => $value) {
  205. // dd(($key==0)?"t":"f");
  206. $trivia_answer = TriviaAnswer::find($key);
  207. $trivia_answer->answer = $value;
  208. $trivia_answer->trivia_id = $trivia->id;
  209. $trivia_answer->is_right = ($counter==0)?"t":"f";
  210. $trivia_answer->save();
  211. $counter += 1;
  212. }
  213. if ($trivia) {
  214. DB::commit();
  215. $message = 'Berhasil';
  216. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  217. } else {
  218. DB::rollback();
  219. $message = 'Gagal';
  220. return redirect()->back()->with('error', Helper::parsing_alert($message));
  221. }
  222. }
  223. $message = Helper::parsing_alert($validator->errors()->all());
  224. return redirect()->back()->with('error', $message);
  225. } else {
  226. $message = 'Id tdak ditemukan';
  227. return redirect()->back()->with('error', $message);
  228. }
  229. } else {
  230. $message = 'Id tidak boleh kosong';
  231. return redirect()->back()->with('error', $message);
  232. }
  233. }
  234. public function delete_action($id_modelmu)
  235. {
  236. $id = Hashids::decode($id_modelmu);
  237. /**cek idnya apakah ada ? */
  238. if (!empty($id)) {
  239. $trivia = Trivia::find($id[0]);
  240. $trivia_answer = TriviaAnswer::where('trivia_id', $id[0])->firstOrFail();
  241. if ($trivia) {
  242. DB::beginTransaction();
  243. try {
  244. //File::delete('image/'.$trivia->image);
  245. Storage::delete('public/images/trivia/'.$trivia->image);
  246. $delete = $trivia->delete();
  247. $delete2 = $trivia_answer->delete();
  248. DB::commit();
  249. $message = 'Sukses';
  250. $response = [
  251. 'message' => $message,
  252. 'status' => true,
  253. ];
  254. return response()->json($response);
  255. } catch (Exception $e) {
  256. DB::rollback();
  257. $message = 'Gagal';
  258. $response = [
  259. 'message' => $message,
  260. 'status' => false,
  261. ];
  262. return response()->json($response);
  263. }
  264. }
  265. $message = 'Id tidak ditemukan atau sudah dihapus';
  266. $response = [
  267. 'message' => $message,
  268. 'status' => false,
  269. ];
  270. return response()->json($response);
  271. }
  272. $message = 'Id tdak ditemukan';
  273. $response = [
  274. 'message' => $message,
  275. 'status' => false,
  276. ];
  277. return response()->json($response);
  278. }
  279. }