Ei kuvausta
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.

TipsController.php 11KB

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