Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TransportationController.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 Validator;
  8. use Hashids;
  9. use Yajra\Datatables\Datatables;
  10. use Auth;
  11. use Illuminate\Support\Facades\Route;
  12. use Illuminate\Support\Facades\File;
  13. use Illuminate\Support\Facades\Storage;
  14. //load modelmu
  15. use App\Model\Transportation;
  16. class TransportationController 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 = 'Transportation'; /**jangan lupa diganti*/
  23. private $route = 'admin.transportation.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
  24. private $header = 'Transportation';
  25. private $sub_header = 'Transportation';
  26. /**khusus namespace ambil pathnya aja contoh : App\Http\Controllers\Admin\Simdondar\DonorDarah -> Admin\Simdondar\DonorDarah*/
  27. private $namespace_controller = 'Admin';
  28. private $url_prefix = 'admin/transportation/';
  29. public function __construct()
  30. {
  31. // DB::enableQueryLog();
  32. // /** nyalakan jika sudah set rolenya, jika ini dinyalakan halaman ini tidak akan keluar */
  33. // $this->middleware('permission:master_alat-list|master_alat-create|master_alat-update|master_alat-delete', ['only' => ['index', 'create', 'update']]);
  34. // $this->middleware('permission:master_alat-create', ['only' => ['create', 'create_action']]);
  35. // $this->middleware('permission:master_alat-update', ['only' => ['update', 'update_action']]);
  36. // $this->middleware('permission:master_alat-delete', ['only' => ['delete']]);
  37. }
  38. /**
  39. * Ini contoh crud yang sudah jalan
  40. * index digunakna untuk tampilan awal dari menu yang akan dibuat
  41. * Untuk CRUD Biar cepat
  42. * Silahkan ganti
  43. * EloquentMu:: => dengan model anda
  44. * $id_modelmu => ganti dengan id di model anda
  45. * id_di_routenya => jangan lupa disesuaikan dengan yang dideclare di route
  46. */
  47. public function index()
  48. {
  49. Helper::swal();
  50. $data = [
  51. //bawaan
  52. 'title' => $this->title,
  53. 'route' => $this->route,
  54. 'header' => $this->header,
  55. 'sub_header' => $this->sub_header,
  56. ];
  57. // dd($data);
  58. return view($this->route . 'index', $data);
  59. }
  60. /**
  61. *digunakan untuk datatable yajra
  62. *querynya bisa ganti" sesuai hati
  63. */
  64. public function getData()
  65. {
  66. $query = Transportation::query();
  67. $data_table = Datatables::of($query)
  68. ->addIndexColumn()
  69. ->addColumn('aksi', function ($query) {
  70. /**cek role */
  71. $aksi = '';
  72. $aksi .= "<a href='" . route($this->route . 'update', ['id_di_routenya' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
  73. // if (Auth::user()->can('master_alat-delete')) {
  74. // $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_action', ['id_di_routenya' => Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
  75. // }
  76. return $aksi;
  77. })
  78. ->addIndexColumn()
  79. ->rawColumns(['aksi'])
  80. ->escapeColumns([]) //digunakan untuk render html
  81. ->toJson();
  82. return $data_table;
  83. }
  84. /**
  85. * create digunakan untuk menampilkan tampilan buat
  86. */
  87. public function create()
  88. {
  89. $data = [
  90. //bawaan
  91. 'title' => $this->title,
  92. 'route' => $this->route,
  93. 'header' => $this->header,
  94. 'sub_header' => $this->sub_header,
  95. ];
  96. Helper::swal();
  97. return view($this->route . 'create', $data);
  98. }
  99. /**
  100. * create_action digunakan untuk aksi post
  101. * lengkap dengan validatornya
  102. */
  103. public function create_action(Request $request)
  104. {
  105. //declare post untuk simpan semua request yang diinputkan oleh form name=""
  106. $post = $request->all();
  107. $file = $request->file('gambar');
  108. //dd($post);
  109. /**digunakan untuk set rule validator */
  110. $rules = [
  111. 'gambar' => 'required|file|max:1000',
  112. ];
  113. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  114. $alert = [
  115. 'file' => 'File maksimal 1MB'
  116. ];
  117. $validator = Validator::make($post, $rules, $alert);
  118. if ($validator->passes()) {
  119. /**menggunakan transaction */
  120. DB::beginTransaction();
  121. $nama_file = time()."_".$file->getClientOriginalName();
  122. $tujuan_upload = 'gambar';
  123. //$file->move($tujuan_upload,$nama_file);
  124. Storage::putFileAs('public/images/transportation/', $file, $nama_file );
  125. $query = new Transportation();
  126. $query->name = $request->name;
  127. $query->transportation_identity = $request->transportation_identity;
  128. $query->transportation_type = $request->transportation_type;
  129. $query->transportation_photo = $nama_file;
  130. $query->user_id = Auth::user()->id;
  131. $query->save();
  132. if ($query) {
  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. /** update sama seperti create hanya saja digunakan untuk update (viewnya saja) */
  147. public function update($id_modelmu)
  148. {
  149. Helper::swal();
  150. $id_modelmu = Hashids::decode($id_modelmu);
  151. if (!empty($id_modelmu)) {
  152. $cek_data = MasterAlat::where('id', $id_modelmu[0])->first();
  153. if ($cek_data) {
  154. $data = [
  155. //bawaan
  156. 'title' => $this->title,
  157. 'route' => $this->route,
  158. 'header' => $this->header,
  159. 'sub_header' => $this->sub_header,
  160. 'data' => $cek_data
  161. ];
  162. return view($this->route . 'update', $data);
  163. }
  164. $message = 'Id tidak ditemukan atau sudah dihapus';
  165. return redirect()->back()->with('error', $message);
  166. }
  167. $message = 'Id tdak ditemukan';
  168. return redirect()->back()->with('error', $message);
  169. }
  170. /**update_action POST
  171. * Logikanya :
  172. * 1. Cek Hashids::decode dari data_id apalah benar" di hash ?
  173. * -iya
  174. * 2. Cek ID yang didapatkan dari hash apakah benar adanya ?
  175. * -iya
  176. * 3. proses validate
  177. * -iya
  178. * 4. Proses update
  179. * - iya benar
  180. * - tidak salah
  181. * - tidak return error
  182. * - tidak return Id tdak ditemukan
  183. * -tidak skip reutrn id null
  184. */
  185. public function update_action(Request $request)
  186. {
  187. $post = $request->all();
  188. /**digunakan untuk set rule validator */
  189. $rules = [];
  190. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  191. $alert = [];
  192. $validator = Validator::make($post, $rules, $alert);
  193. $id_modelmu = Hashids::decode($request['id']);
  194. if (!empty($id_modelmu)) {
  195. /**cek apakah data_idnya ada ? */
  196. $query = MasterAlat::find($id_modelmu[0]);
  197. if ($query) {
  198. /**cek apakah id data benar" ada di DB ? */
  199. if ($validator->passes()) {
  200. /**validatornya */
  201. DB::beginTransaction();
  202. $file = $request->file('gambar');
  203. if($file){
  204. $nama_file = time()."_".$file->getClientOriginalName();
  205. $tujuan_upload = 'gambar';
  206. //$file->move($tujuan_upload,$nama_file);
  207. Storage::putFileAs('public/images/', $file, $nama_file );
  208. //File::delete('gambar/'.$query->gambar);
  209. Storage::delete('public/images/'.$query->gambar);
  210. $query->gambar = $nama_file;
  211. }
  212. $query->nama = $request->nama;
  213. $query->type_alat_id = $request->type_alat_id;
  214. $query->deskripsi = $request->deskripsi;
  215. $query->unit_donor_darah_id = Auth::user()->petugas->unit_donor_darah_id;
  216. $query->save();
  217. if ($query) {
  218. DB::commit();
  219. $message = 'Berhasil';
  220. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  221. } else {
  222. DB::rollback();
  223. $message = 'Gagal';
  224. return redirect()->back()->with('error', Helper::parsing_alert($message));
  225. }
  226. }
  227. $message = Helper::parsing_alert($validator->errors()->all());
  228. return redirect()->back()->with('error', $message);
  229. } else {
  230. $message = 'Id tdak ditemukan';
  231. return redirect()->back()->with('error', $message);
  232. }
  233. } else {
  234. $message = 'Id tidak boleh kosong';
  235. return redirect()->back()->with('error', $message);
  236. }
  237. }
  238. /**digunakan untuk delete
  239. * Logikanya :
  240. * 1. Cek hash id
  241. * -ya
  242. * 2. Cek id dari hash
  243. * - ya
  244. * 3. Proses delete
  245. * -Ya
  246. * -Tidak
  247. * - tidak return Id tidak ditemukan atau sudah dihapus
  248. * -tidak reutrn Id tdak ditemukan
  249. */
  250. public function delete_action($id_modelmu)
  251. {
  252. $id = Hashids::decode($id_modelmu);
  253. /**cek idnya apakah ada ? */
  254. if (!empty($id)) {
  255. $cek_data = MasterAlat::find($id[0]);
  256. if ($cek_data) {
  257. DB::beginTransaction();
  258. try {
  259. //File::delete('gambar/'.$cek_data->gambar);
  260. Storage::delete('public/images/'.$cek_data->gambar);
  261. $delete = $cek_data->delete();
  262. DB::commit();
  263. $message = 'Sukses';
  264. $response = [
  265. 'message' => $message,
  266. 'status' => true,
  267. ];
  268. return response()->json($response);
  269. } catch (Exception $e) {
  270. DB::rollback();
  271. $message = 'Gagal';
  272. $response = [
  273. 'message' => $message,
  274. 'status' => false,
  275. ];
  276. return response()->json($response);
  277. }
  278. }
  279. $message = 'Id tidak ditemukan atau sudah dihapus';
  280. $response = [
  281. 'message' => $message,
  282. 'status' => false,
  283. ];
  284. return response()->json($response);
  285. }
  286. $message = 'Id tdak ditemukan';
  287. $response = [
  288. 'message' => $message,
  289. 'status' => false,
  290. ];
  291. return response()->json($response);
  292. }
  293. /**declare routenya disni*/
  294. public function route(){
  295. $route = Route::namespace($this->namespace_controller)
  296. ->prefix($this->url_prefix)
  297. ->name($this->route)
  298. ->group(function () {
  299. Route::get('/', ['as' => 'index', 'uses' => 'TransportationController@index']);
  300. Route::get('/get-data/{udd_id?}', ['as' => 'get_data', 'uses' => 'TransportationController@getData']);
  301. Route::get('/datatables', ['as' => 'datatables', 'uses' => 'TransportationController@datatables']);
  302. Route::get('/create', ['as' => 'create', 'uses' => 'TransportationController@create']);
  303. Route::get('/update/{id_di_routenya}', ['as' => 'update', 'uses' => 'TransportationController@update']);
  304. Route::get('/delete-action/{id_di_routenya}', ['as' => 'delete_action', 'uses' => 'TransportationController@delete_action']);
  305. Route::post('/create-action', ['as' => 'create_action', 'uses' => 'TransportationController@create_action']);
  306. Route::post('/update-action', ['as' => 'update_action', 'uses' => 'TransportationController@update_action']);
  307. });
  308. return $route;
  309. }
  310. /**end declare route */
  311. }