No Description
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.

TimelineController.php 11KB

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