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.

PetugasUddController.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. namespace App\Http\Controllers\Admin\User_management;
  3. use App\Helpers\Helper;
  4. use App\Http\Controllers\Controller;
  5. use Hashids;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\DB;
  8. use Validator;
  9. use Auth;
  10. use Illuminate\Support\Facades\Route;
  11. //load modelmu
  12. use Illuminate\Support\Str;
  13. use App\Model\User\PetugasUnitDonorDarahUdds as petugas_udd;
  14. use App\Model\Master\MasterUdd;
  15. use App\Model\ModelHasRoles;
  16. use App\User;
  17. use Illuminate\Support\Facades\Hash;
  18. use Yajra\Datatables\Datatables;
  19. class PetugasUddController extends Controller
  20. {
  21. /**
  22. * Untuk CRUD Biar cepat
  23. * Silahkan ganti
  24. * petugas_udd:: => dengan model anda
  25. * $petugas_udd_id => ganti dengan id di model anda
  26. */
  27. /**
  28. * Title untuk judul di web
  29. * route digunakan untuk tempat resource (file path) + routing (route/web) diusahain sama ya biar gak ngubah"
  30. */
  31. private $title = 'Admin Usermanagement | Petugas UDD';
  32. /**jangan lupa diganti*/
  33. private $route = 'admin.user_management.petugas_udd.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
  34. private $namespace_controller = 'Admin\User_management';
  35. private $url_prefix = 'user-management/petugas-udd/';
  36. public function __construct()
  37. {
  38. DB::enableQueryLog();
  39. /** nyalakan jika sudah set rolenya, jika ini dinyalakan halaman ini tidak akan keluar */
  40. $this->middleware('permission:petugas_udd-list|petugas_udd-create|petugas_udd-update|petugas_udd-delete', ['only' => ['index', 'create', 'update']]);
  41. $this->middleware('permission:petugas_udd-create', ['only' => ['create', 'create_action']]);
  42. $this->middleware('permission:petugas_udd-update', ['only' => ['update', 'update_action']]);
  43. $this->middleware('permission:petugas_udd-delete', ['only' => ['delete']]);
  44. }
  45. /**
  46. * Ini contoh crud yang sudah jalan
  47. * index digunakna untuk tampilan awal dari menu yang akan dibuat
  48. */
  49. public function index()
  50. {
  51. if (session('success')) {
  52. alert()->html('', session('success'), 'success');
  53. }
  54. if (session('error')) {
  55. alert()->html('', session('error'), 'error');
  56. }
  57. $data = [
  58. //bawaan
  59. 'title' => $this->title,
  60. 'route' => $this->route,
  61. ];
  62. return view($this->route . 'index', $data);
  63. }
  64. public function getData()
  65. {
  66. /**
  67. * deteksi jika dia bukan superadmin
  68. * jika super admin tampil semua uddnya
  69. * jika tidak maka tampilin hanya uddnya dia sendiri
  70. * */
  71. if (!empty(Helper::is_super_admin())) {
  72. $petugas = petugas_udd::get();
  73. } else {
  74. $udd_id = Auth::user()->petugas->unit_donor_darah_id;
  75. $petugas = petugas_udd::with('udd')
  76. ->where('unit_donor_darah_id', $udd_id)
  77. ->get();
  78. }
  79. foreach ($petugas as $key => $value) {
  80. $value->no = $key + 1;
  81. $value->email = $value->user->email;
  82. $role = '';
  83. /**get role petugas */
  84. $data_roles = $value->user->roles()->get();
  85. foreach ($data_roles as $key => $value_roles) {
  86. $role .= $value_roles->name . (($key + 1) < $data_roles->count() ? ', ' : null);
  87. }
  88. $value->roles = $role;
  89. $value->udd_pmi = $value->udd->nama;
  90. }
  91. // dd($petugas);
  92. return Datatables::of($petugas)
  93. ->addColumn('aksi', function ($petugas) {
  94. /**cek role */
  95. $aksi = '';
  96. if (Auth::user()->can('petugas_udd-update')) {
  97. $aksi .= "<a href='" . route($this->route . 'update', ['petugas_udd_id' => Hashids::encode($petugas->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
  98. }
  99. if (Auth::user()->can('petugas_udd-delete')) {
  100. // $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_action', ['petugas_udd_id' => Hashids::encode($petugas->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
  101. }
  102. return $aksi;
  103. })
  104. ->rawColumns(['aksi'])
  105. ->toJson();
  106. }
  107. /**
  108. * create digunakan untuk menampilkan tampilan buat
  109. * Catatan :
  110. * Jika rolenya = 1 (superadmin) maka munculkan uddnya untuk piliham udd
  111. * jika rolenya != 1 maka langsung insert uddnya sesuai dengan udd User id
  112. */
  113. public function create()
  114. {
  115. $role = Helper::get_roles();
  116. $jumlah_role = $role->count();
  117. $limit = 100;
  118. $take = 0;
  119. $jumlah_looping = ceil($jumlah_role / $limit);
  120. $arr_role = [];
  121. for ($i = 0; $i < $jumlah_looping; $i++) {
  122. $limit_data = 100;
  123. $role = $role->take($limit_data)->skip($take);
  124. $cek_data[] = $role;
  125. $arr_role[$i] = $role;
  126. $take = $limit;
  127. $limit += 100;
  128. }
  129. $data = [
  130. //bawaan
  131. 'title' => $this->title,
  132. 'route' => $this->route,
  133. 'roles' => $arr_role,
  134. ];
  135. /**get udd jika role superadmin
  136. * ini dibagi 4 - 4
  137. */
  138. $is_super_admin = Helper::is_super_admin();
  139. if (!empty($is_super_admin)) {
  140. $udd = MasterUdd::All();
  141. $jumlah_udd = $udd->count();
  142. $limit = 4;
  143. $take = 0;
  144. $jumlah_looping = ceil($jumlah_udd / $limit);
  145. $arr_udd = [];
  146. for ($i = 0; $i < $jumlah_looping; $i++) {
  147. $limit_data = 4;
  148. $udd = $udd->take($limit_data)->skip($take);
  149. $arr_udd[$i] = $udd;
  150. $take = $limit;
  151. $limit += 4;
  152. }
  153. $data['udds'] = $arr_udd;
  154. }
  155. if (session('success')) {
  156. alert()->html('', session('success'), 'success');
  157. }
  158. if (session('error')) {
  159. alert()->html('', session('error'), 'error');
  160. }
  161. return view($this->route . 'create', $data);
  162. }
  163. /**
  164. * create_action digunakan untuk aksi post
  165. * lengkap dengan validatornya
  166. */
  167. public function create_action(Request $request)
  168. {
  169. /**digunakan untuk set rule validator */
  170. $rules = [
  171. 'nama' => 'required|min:4',
  172. 'password' => 'required|min:6',
  173. 'role_id' => 'required',
  174. // 'unit_donor_darah_id' => 'required',
  175. 'email' => 'required|unique:users,email',
  176. ];
  177. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  178. $alert = [
  179. 'unique' => ':attribute sudah tersedia',
  180. 'required' => 'The :attribute harus diisi',
  181. 'min' => ':attribute minimal :min karakter'
  182. ];
  183. $validator = Validator::make($request->all(), $rules, $alert);
  184. if ($validator->passes()) {
  185. /**menggunakan transaction */
  186. DB::beginTransaction();
  187. $data_user = [
  188. 'name' => $request['nama'],
  189. 'email' => $request['email'],
  190. 'password' => Hash::make($request['password']),
  191. ];
  192. $insert_user = User::create($data_user);
  193. $insert_user->assignRole($request['role_id']);
  194. $data_petugas = [
  195. 'nama' => $request['nama'],
  196. 'unit_donor_darah_id' => $request['unit_donor_darah_id'] ?? Auth::user()->petugas->unit_donor_darah_id,
  197. 'user_login_id' => $insert_user->id,
  198. ];
  199. $insert_petugas = petugas_udd::create($data_petugas);
  200. if ($insert_petugas && $insert_user) {
  201. DB::commit();
  202. $message = 'Berhasil';
  203. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  204. } else {
  205. DB::rollback();
  206. $message = 'Gagal';
  207. return redirect()->back()->with('error', Helper::parsing_alert($message));
  208. }
  209. }
  210. /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
  211. $message = Helper::parsing_alert($validator->errors()->all());
  212. return redirect()->back()->with('error', $message)->withInput();
  213. }
  214. /** update sama seperti create hanya saja digunakan untuk update (viewnya saja) */
  215. public function update($petugas_udd_id)
  216. {
  217. if (session('success')) {
  218. alert()->html('', session('success'), 'success');
  219. }
  220. if (session('error')) {
  221. alert()->html('', session('error'), 'error');
  222. }
  223. $petugas_udd_id = Hashids::decode($petugas_udd_id);
  224. if (!empty($petugas_udd_id)) {
  225. $cek_data = petugas_udd::where('id', $petugas_udd_id[0])->first();
  226. if ($cek_data) {
  227. $role = Helper::get_roles();
  228. $jumlah_role = $role->count();
  229. $limit = 100;
  230. $take = 0;
  231. $jumlah_looping = ceil($jumlah_role / $limit);
  232. $arr_role = [];
  233. for ($i = 0; $i < $jumlah_looping; $i++) {
  234. $limit_data = 100;
  235. $role = $role->take($limit_data)->skip($take);
  236. $arr_role[$i] = $role;
  237. $take = $limit;
  238. $limit += 100;
  239. }
  240. $data = [
  241. //bawaan
  242. 'title' => $this->title,
  243. 'route' => $this->route,
  244. 'roles' => $arr_role,
  245. 'data' => $cek_data,
  246. ];
  247. /**get udd jika role superadmin
  248. * ini dibagi 4 - 4
  249. */
  250. $is_super_admin = Helper::is_super_admin();
  251. if (!empty($is_super_admin)) {
  252. $udd = MasterUdd::All();
  253. $jumlah_udd = $udd->count();
  254. $limit = 4;
  255. $take = 0;
  256. $jumlah_looping = ceil($jumlah_udd / $limit);
  257. $arr_udd = [];
  258. for ($i = 0; $i < $jumlah_looping; $i++) {
  259. $limit_data = 4;
  260. $udd = $udd->take($limit_data)->skip($take);
  261. $arr_udd[$i] = $udd;
  262. $take = $limit;
  263. $limit += 4;
  264. }
  265. $data['udds'] = $arr_udd;
  266. }
  267. return view($this->route . 'update', $data);
  268. }
  269. $message = 'Id tidak ditemukan atau sudah dihapus';
  270. return redirect()->back()->with('error', $message);
  271. }
  272. $message = 'Id tdak ditemukan';
  273. return redirect()->back()->with('error', $message);
  274. }
  275. /**update_action POST
  276. * Logikanya :
  277. * 1. Cek Hashids::decode dari data_id apalah benar" di hash ?
  278. * -iya
  279. * 2. Cek ID yang didapatkan dari hash apakah benar adanya ?
  280. * -iya
  281. * 3. proses validate
  282. * -iya
  283. * 4. Proses update
  284. * - iya benar
  285. * - tidak salah
  286. * - tidak return error
  287. * - tidak return Id tdak ditemukan
  288. * -tidak skip reutrn id null
  289. */
  290. public function update_action(Request $request)
  291. {
  292. $rules = [
  293. 'nama' => 'required|min:4',
  294. 'role_id' => 'required',
  295. // 'unit_donor_darah_id' => 'required'
  296. ];
  297. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  298. $alert = [
  299. 'required' => 'The :attribute harus diisi',
  300. 'min' => ':attribute minimal :min karakter'
  301. ];
  302. $validator = Validator::make($request->all(), $rules, $alert);
  303. $petugas_udd_id = Hashids::decode($request['petugas_id']);
  304. if (!empty($petugas_udd_id)) {
  305. /**cek apakah data_idnya ada ? */
  306. $cek_data = petugas_udd::where('id', $petugas_udd_id[0])->first();
  307. if ($cek_data) {
  308. $cek_email = User::where('id', '!=', $cek_data->user->id)
  309. ->where(function ($query) use ($request) {
  310. $query->where('email', $request['email']);
  311. })
  312. ->first();
  313. if (!empty($cek_email)) {
  314. $message = 'Email sudah digunakan';
  315. return redirect()->back()->withInput()->with('error', $message);
  316. }
  317. /**cek apakah id data benar" ada di DB ? */
  318. if ($validator->passes()) {
  319. /**validatornya */
  320. DB::beginTransaction();
  321. $data_user = [
  322. 'name' => $request['nama'],
  323. 'email' => $request['email'],
  324. ];
  325. if ($request['password']) {
  326. $length = Str::of($request['password'])->length();
  327. if ($length < 6) {
  328. $message = 'Password minimal 6 karakter';
  329. return redirect()->back()->withInput()->with('error', $message);
  330. }
  331. $data_user['password'] = Hash::make($request['password']);
  332. }
  333. /**delete roles */
  334. ModelHasRoles::where('model_id', $cek_data->user->id)->delete();
  335. $find_user = User::findOrFail($cek_data->user->id);
  336. $update_user = $find_user->update($data_user);
  337. $find_user->assignRole($request['role_id']);
  338. $data_petugas = [
  339. 'nama' => $request['nama'],
  340. 'unit_donor_darah_id' => $request['unit_donor_darah_id'] ?? Auth::user()->petugas->unit_donor_darah_id,
  341. ];
  342. $update_petugas = $cek_data->update($data_petugas);
  343. if ($update_petugas && $update_user) {
  344. DB::commit();
  345. $message = 'Berhasil';
  346. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  347. } else {
  348. DB::rollback();
  349. $message = 'Gagal';
  350. return redirect()->back()->with('error', Helper::parsing_alert($message));
  351. }
  352. }
  353. $message = Helper::parsing_alert($validator->errors()->all());
  354. return redirect()->back()->with('error', $message);
  355. } else {
  356. $message = 'Id tdak ditemukan';
  357. return redirect()->back()->with('error', $message);
  358. }
  359. } else {
  360. $message = 'Id tidak boleh kosong';
  361. return redirect()->back()->with('error', $message);
  362. }
  363. }
  364. /**digunakan untuk delete
  365. * Logikanya :
  366. * 1. Cek hash id
  367. * -ya
  368. * 2. Cek id dari hash
  369. * - ya
  370. * 3. Proses delete
  371. * -Ya
  372. * -Tidak
  373. * - tidak return Id tidak ditemukan atau sudah dihapus
  374. * -tidak reutrn Id tdak ditemukan
  375. */
  376. public function delete_action($petugas_udd_id)
  377. {
  378. $petugas_udd_id = Hashids::decode($petugas_udd_id);
  379. /**cek idnya apakah ada ? */
  380. if (!empty($petugas_udd_id)) {
  381. $cek_data = petugas_udd::where('id', $petugas_udd_id[0])->first();
  382. if ($cek_data) {
  383. DB::beginTransaction();
  384. $delete = $cek_data->delete();
  385. if ($delete) {
  386. DB::commit();
  387. $message = 'Succesfully';
  388. $response = [
  389. 'message' => $message,
  390. 'status' => true,
  391. ];
  392. return response()->json($response);
  393. } else {
  394. DB::rollback();
  395. $message = 'Gagal';
  396. $response = [
  397. 'message' => $message,
  398. 'status' => false,
  399. ];
  400. return response()->json($response);
  401. }
  402. }
  403. $message = 'Id tidak ditemukan atau sudah dihapus';
  404. $response = [
  405. 'message' => $message,
  406. 'status' => false,
  407. ];
  408. return response()->json($response);
  409. }
  410. $message = 'Id tdak ditemukan';
  411. $response = [
  412. 'message' => $message,
  413. 'status' => false,
  414. ];
  415. return response()->json($response);
  416. }
  417. //custom route
  418. public function route()
  419. {
  420. return Route::group([
  421. 'as' => $this->route,
  422. 'prefix' => $this->url_prefix,
  423. 'namespace' => $this->namespace_controller
  424. ], function () {
  425. Route::get('/', ['as' => 'index', 'uses' => 'PetugasUddController@index']);
  426. Route::get('/create', ['as' => 'create', 'uses' => 'PetugasUddController@create']);
  427. Route::get('/update/{petugas_udd_id}', ['as' => 'update', 'uses' => 'PetugasUddController@update']);
  428. Route::get('/delete-action/{petugas_udd_id}', ['as' => 'delete_action', 'uses' => 'PetugasUddController@delete_action']);
  429. Route::get('/get-data', ['as' => 'get_data', 'uses' => 'PetugasUddController@getData']);
  430. Route::post('/create-action', ['as' => 'create_action', 'uses' => 'PetugasUddController@create_action']);
  431. Route::post('/update-action', ['as' => 'update_action', 'uses' => 'PetugasUddController@update_action']);
  432. });
  433. }
  434. //end custom route
  435. }