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.

MasterPointsController.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\Points;
  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 MasterPointsController 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 = 'Master Points'; /**jangan lupa diganti*/
  21. private $route = 'admin.master-points.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
  22. private $header = 'Points';
  23. private $sub_header = 'Points';
  24. private $url = 'admin.raharja.master_points.';
  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 = Points::query();
  59. return Datatables::of($query)
  60. ->addIndexColumn()
  61. ->addColumn('aksi', function ($query) {
  62. /**cek role */
  63. $aksi = '';
  64. // if (Auth::user()->can('master-jenis-kantong-update')) {
  65. $aksi .= "<a href='" . route($this->route . 'update', ['news_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
  66. // }
  67. // if (Auth::user()->can('master-jenis-kantong-delete')) {
  68. $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>";
  69. // }
  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. //dd($post);
  92. /**digunakan untuk set rule validator */
  93. $rules = [
  94. 'type' => 'required',
  95. 'value' => 'required',
  96. ];
  97. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  98. $validator = Validator::make($post, $rules);
  99. if ($validator->passes()) {
  100. /**menggunakan transaction */
  101. DB::beginTransaction();
  102. $query = new Points();
  103. $query->type = $request->type;
  104. $query->value = $request->value;
  105. $query->save();
  106. if ($query) {
  107. DB::commit();
  108. $message = 'Berhasil';
  109. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  110. } else {
  111. DB::rollback();
  112. $message = 'Gagal';
  113. return redirect()->back()->with('error', Helper::parsing_alert($message));
  114. }
  115. }
  116. /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
  117. $message = Helper::parsing_alert($validator->errors()->all());
  118. return redirect()->back()->with('error', $message)->withInput();
  119. }
  120. public function update($data_id)
  121. {
  122. Helper::swal();
  123. $data_id = Hashids::decode($data_id);
  124. if (!empty($data_id)) {
  125. $cek_data = Points::where('id', $data_id[0])->first();
  126. if ($cek_data) {
  127. $data = [
  128. //bawaan
  129. 'title' => $this->title,
  130. 'route' => $this->route,
  131. 'header' => $this->header,
  132. 'sub_header' => $this->sub_header,
  133. 'data' => $cek_data
  134. ];
  135. return view($this->url . 'update', $data);
  136. }
  137. $message = 'Id tidak ditemukan atau sudah dihapus';
  138. return redirect()->back()->with('error', $message);
  139. }
  140. $message = 'Id tdak ditemukan';
  141. return redirect()->back()->with('error', $message);
  142. }
  143. public function update_action(Request $request)
  144. {
  145. $post = $request->all();
  146. /**digunakan untuk set rule validator */
  147. $rules = [
  148. 'type' => 'required',
  149. 'value' => 'required',
  150. ];
  151. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  152. $alert = [];
  153. $validator = Validator::make($post, $rules, $alert);
  154. $data_id = Hashids::decode($request['id']);
  155. if (!empty($data_id)) {
  156. /**cek apakah data_idnya ada ? */
  157. $query = Points::find($data_id[0]);
  158. if ($query) {
  159. /**cek apakah id data benar" ada di DB ? */
  160. if ($validator->passes()) {
  161. /**validatornya */
  162. DB::beginTransaction();
  163. $query->type = $request->type;
  164. $query->value = $request->value;
  165. $query->save();
  166. if ($query) {
  167. DB::commit();
  168. $message = 'Berhasil';
  169. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  170. } else {
  171. DB::rollback();
  172. $message = 'Gagal';
  173. return redirect()->back()->with('error', Helper::parsing_alert($message));
  174. }
  175. }
  176. $message = Helper::parsing_alert($validator->errors()->all());
  177. return redirect()->back()->with('error', $message);
  178. } else {
  179. $message = 'Id tdak ditemukan';
  180. return redirect()->back()->with('error', $message);
  181. }
  182. } else {
  183. $message = 'Id tidak boleh kosong';
  184. return redirect()->back()->with('error', $message);
  185. }
  186. }
  187. public function delete_action($id_modelmu)
  188. {
  189. $id = Hashids::decode($id_modelmu);
  190. /**cek idnya apakah ada ? */
  191. if (!empty($id)) {
  192. $cek_data = Points::find($id[0]);
  193. if ($cek_data) {
  194. DB::beginTransaction();
  195. try {
  196. $cek_data->delete();
  197. DB::commit();
  198. $message = 'Sukses';
  199. $response = [
  200. 'message' => $message,
  201. 'status' => true,
  202. ];
  203. return response()->json($response);
  204. } catch (Exception $e) {
  205. DB::rollback();
  206. $message = 'Gagal';
  207. $response = [
  208. 'message' => $message,
  209. 'status' => false,
  210. ];
  211. return response()->json($response);
  212. }
  213. }
  214. $message = 'Id tidak ditemukan atau sudah dihapus';
  215. $response = [
  216. 'message' => $message,
  217. 'status' => false,
  218. ];
  219. return response()->json($response);
  220. }
  221. $message = 'Id tdak ditemukan';
  222. $response = [
  223. 'message' => $message,
  224. 'status' => false,
  225. ];
  226. return response()->json($response);
  227. }
  228. }