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.

RoleController.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. namespace App\Http\Controllers\Admin\User_management;
  3. use App\Helpers\Helper;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Validator;
  8. use Hashids;
  9. use Auth;
  10. use Illuminate\Support\Facades\Route;
  11. /**model */
  12. use Spatie\Permission\Models\Permission;
  13. use Spatie\Permission\Models\Role;
  14. use App\Model\RoleHasPermission;
  15. use Yajra\Datatables\Datatables;
  16. class RoleController 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 = 'Admin User Magement | Role';
  23. private $route = 'admin.user_management.role.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard'
  24. private $namespace_controller = 'Admin\User_management';
  25. private $url_prefix = 'user-management/role/';
  26. public function __construct()
  27. {
  28. // DB::getQueryLog();
  29. // $this->middleware('permission:role-list|role-create|role-update|role-delete', ['only' => ['index', 'create', 'update']]);
  30. // $this->middleware('permission:role-create', ['only' => ['create', 'create_action']]);
  31. // $this->middleware('permission:role-update', ['only' => ['update', 'update_action']]);
  32. // $this->middleware('permission:role-delete', ['only' => ['delete']]);
  33. }
  34. /**
  35. * Ini contoh crud yang sudah jalan
  36. * index digunakna untuk tampilan awal dari menu yang akan dibuat
  37. * session" itu ambil dari sweetalert langsung jadi udah langsung digunakan
  38. * untuk yang index susuannya begitu, kalau mau menambahkan silahkan
  39. */
  40. public function index()
  41. {
  42. if (session('success')) {
  43. alert()->html('', session('success'), 'success');
  44. }
  45. if (session('error')) {
  46. alert()->html('', session('error'), 'error');
  47. }
  48. $data = [
  49. //bawaan
  50. 'title' => $this->title,
  51. 'route' => $this->route,
  52. ];
  53. return view($this->route . 'index', $data);
  54. }
  55. public function getData()
  56. {
  57. $query = Role::query();
  58. return Datatables::of($query)
  59. ->addIndexColumn()
  60. ->addColumn('aksi', function ($query) {
  61. /**cek role */
  62. $aksi = '';
  63. if (Auth::user()->can('role-update')) {
  64. $aksi .= "<a href='" . route($this->route . 'update', ['role_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
  65. }
  66. if (Auth::user()->can('role-delete')) {
  67. // $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_action', ['role_id' => Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
  68. }
  69. return $aksi;
  70. })
  71. ->rawColumns(['aksi'])
  72. ->toJson();
  73. }
  74. /**
  75. * create digunakan untuk menampilkan tampilan buat
  76. */
  77. public function create()
  78. {
  79. /**set permission dengan cara dibagi 4 - 4 */
  80. $permission = Helper::get_permission_by_role();
  81. // if()
  82. $jumlah_permission = $permission->count();
  83. $limit = 4;
  84. $take = 0;
  85. $jumlah_looping = ceil($jumlah_permission / $limit);
  86. $arr_permission = [];
  87. for ($i = 0; $i < $jumlah_looping; $i++) {
  88. // $cek_limit[] = $limit;
  89. // $cek_take[] = $take;
  90. $limit_data = 4;
  91. $permission = Permission::take($limit_data)->skip($take)->get();
  92. $cek_permission[] = $permission;
  93. $arr_permission[$i] = $permission;
  94. $take = $limit;
  95. $limit += 4;
  96. }
  97. // dd($arr_permission);
  98. /**end set permission */
  99. $data = [
  100. //bawaan
  101. 'title' => $this->title,
  102. 'route' => $this->route,
  103. 'data' => $arr_permission,
  104. ];
  105. if (session('success')) {
  106. alert()->html('', session('success'), 'success');
  107. }
  108. if (session('error')) {
  109. alert()->html('', session('error'), 'error');
  110. }
  111. return view($this->route . 'create', $data);
  112. }
  113. /**
  114. * create_action digunakan untuk aksi post
  115. * lengkap dengan validatornya
  116. */
  117. public function create_action(Request $request)
  118. {
  119. /**digunakan untuk set rule validator */
  120. $cek_permission_id = !empty($request['permission_id']);
  121. // dd($cek_permission_id);
  122. if ($cek_permission_id) {
  123. $rules = [
  124. 'name' => 'required|min:2|unique:roles,name',
  125. ];
  126. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  127. $alert = [
  128. 'required' => 'The :attribute is required',
  129. 'min' => ':attribute Min 2 Char'
  130. ];
  131. $validator = Validator::make($request->all(), $rules, $alert);
  132. if ($validator->passes()) {
  133. /**menggunakan transaction */
  134. DB::beginTransaction();
  135. $insert_role = Role::create($request->only('name'));
  136. $data_has_role = [];
  137. foreach ($request['permission_id'] as $key => $value) {
  138. $data_has_role[] = [
  139. 'permission_id' => $value,
  140. 'role_id' => $insert_role->id,
  141. ];
  142. }
  143. $insert_has_role = RoleHasPermission::insert($data_has_role);
  144. if ($insert_role && $insert_has_role) {
  145. DB::commit();
  146. $message = 'Berhasil';
  147. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  148. } else {
  149. DB::rollback();
  150. $message = 'Gagal';
  151. return redirect()->back()->with('error', Helper::parsing_alert($message));
  152. }
  153. }
  154. /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
  155. $message = Helper::parsing_alert($validator->errors()->all());
  156. return redirect()->back()->with('error', $message)->withInput();
  157. }
  158. $message = 'Please filled the permission least one';
  159. return redirect()->back()->with('error', $message)->withInput();
  160. }
  161. /** update sama seperti create hanya saja digunakan untuk update (viewnya saja) */
  162. public function update($role_id)
  163. {
  164. if (session('success')) {
  165. alert()->html('', session('success'), 'success');
  166. }
  167. if (session('error')) {
  168. alert()->html('', session('error'), 'error');
  169. }
  170. $role_id = Hashids::decode($role_id);
  171. if (!empty($role_id)) {
  172. $cek_role = Role::where('id', $role_id[0])->first();
  173. /**set permission dengan cara dibagi 4 - 4 */
  174. $permission = Permission::get();
  175. $jumlah_permission = $permission->count();
  176. $limit = 4;
  177. $take = 0;
  178. $jumlah_looping = ceil($jumlah_permission / $limit);
  179. $arr_permission = [];
  180. for ($i = 0; $i < $jumlah_looping; $i++) {
  181. // $cek_limit[] = $limit;
  182. // $cek_take[] = $take;
  183. $limit_data = 4;
  184. $permission = Permission::take($limit_data)->skip($take)->get();
  185. $cek_permission[] = $permission;
  186. $arr_permission[$i] = $permission;
  187. $take = $limit;
  188. $limit += 4;
  189. }
  190. /**end set permission */
  191. /**menambahkan object permission di dalam role, digunakan untuk cek diviewnya nanti permissionnya mana aja yang dipilih */
  192. $cek_role->permission = RoleHasPermission::where('role_id', $cek_role->id)
  193. ->get()
  194. ->pluck('permission_id')
  195. ->toArray();
  196. if ($cek_role) {
  197. $data = [
  198. //bawaan
  199. 'title' => $this->title,
  200. 'route' => $this->route,
  201. 'data' => $cek_role,
  202. 'permission' => $arr_permission,
  203. ];
  204. return view($this->route . 'update', $data);
  205. }
  206. $message = 'Id role not found or has been deleted';
  207. return redirect()->back()->with('error', $message);
  208. }
  209. $message = 'Id tdak ditemukan';
  210. return redirect()->back()->with('error', $message);
  211. }
  212. /**update_action POST
  213. * LOGIKANYA :
  214. * 1. Cek Hashids::decode dari role_id apalah benar" di hash ?
  215. * -iya
  216. * 2. Cek ID yang didapatkan dari hash apakah benar adanya ?
  217. * -iya
  218. * 3. proses validate
  219. * -iya
  220. * 4. Proses update
  221. * - iya benar
  222. * - tidak salah
  223. * - tidak return error
  224. * - tidak return Id tdak ditemukan
  225. * -tidak skip reutrn id null
  226. */
  227. public function update_action(Request $request)
  228. {
  229. $rules = [
  230. 'name' => 'required|min:2',
  231. ];
  232. $alert = [
  233. 'required' => 'The :attribute is required',
  234. 'min' => ':attribute Min 2 Char'
  235. ];
  236. $validator = Validator::make($request->all(), $rules, $alert);
  237. $role_id = Hashids::decode($request['id']);
  238. $cek_permission_id = !empty($request['permission_id']);
  239. if ($cek_permission_id) {
  240. if (!empty($role_id)) {
  241. /**cek apakah role_idnya ada ? */
  242. $cek_role = Role::where('id', $role_id[0])->first();
  243. if ($cek_role) {
  244. /**cek apakah id rolenya benar" ada di DB ? */
  245. if ($validator->passes()) {
  246. /**validatornya */
  247. DB::beginTransaction();
  248. $update_role = $cek_role->update($request->only('name'));
  249. /**delete has role dlu */
  250. RoleHasPermission::where('role_id', $cek_role->id)->delete();
  251. /**proses input has role */
  252. $data_has_role = [];
  253. foreach ($request['permission_id'] as $key => $value) {
  254. $data_has_role[] = [
  255. 'permission_id' => $value,
  256. 'role_id' => $cek_role->id
  257. ];
  258. }
  259. $insert_has_role = RoleHasPermission::insert($data_has_role);
  260. if ($update_role && $insert_has_role) {
  261. DB::commit();
  262. $message = 'Berhasil';
  263. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  264. } else {
  265. DB::rollback();
  266. $message = 'Gagal';
  267. return redirect()->back()->with('error', Helper::parsing_alert($message));
  268. }
  269. }
  270. $message = Helper::parsing_alert($validator->errors()->all());
  271. return redirect()->back()->with('error', $message);
  272. } else {
  273. $message = 'Id tdak ditemukan';
  274. return redirect()->back()->with('error', $message);
  275. }
  276. } else {
  277. $message = 'Id tidak boleh kosong';
  278. return redirect()->back()->with('error', $message);
  279. }
  280. }
  281. $message = 'Permission must be filled least one';
  282. return redirect()->back()->with('error', $message);
  283. }
  284. /**digunakan untuk delete
  285. * Logikanya :
  286. * 1. Cek hash id
  287. * -ya
  288. * 2. Cek id dari hash
  289. * - ya
  290. * 3. Proses delete
  291. * -Ya
  292. * -Tidak
  293. * - tidak return Id tidak ditemukan atau sudah dihapus
  294. * -tidak reutrn Id tdak ditemukan
  295. */
  296. public function delete_action($role_id)
  297. {
  298. $role_id = Hashids::decode($role_id);
  299. /**cek role idnya apakah ada ? */
  300. if (!empty($role_id)) {
  301. $cek_role = Role::where('id', $role_id[0])->first();
  302. if ($cek_role) {
  303. DB::beginTransaction();
  304. $delete = $cek_role->delete();
  305. if ($delete) {
  306. DB::commit();
  307. $message = 'Succesfully';
  308. $response = [
  309. 'message' => $message,
  310. 'status' => true,
  311. ];
  312. return response()->json($response);
  313. } else {
  314. DB::rollback();
  315. $message = 'Gagal';
  316. $response = [
  317. 'message' => $message,
  318. 'status' => false,
  319. ];
  320. return response()->json($response);
  321. }
  322. }
  323. $message = 'Id tidak di temukan atau sudah di hapus';
  324. $response = [
  325. 'message' => $message,
  326. 'status' => false,
  327. ];
  328. return response()->json($response);
  329. }
  330. $message = 'Id tdak ditemukan';
  331. $response = [
  332. 'message' => $message,
  333. 'status' => false,
  334. ];
  335. return response()->json($response);
  336. }
  337. //custom route
  338. public function route()
  339. {
  340. return Route::group([
  341. 'as' => $this->route,
  342. 'prefix' => $this->url_prefix,
  343. 'namespace' => $this->namespace_controller
  344. ], function () {
  345. Route::get('/', ['as' => 'index', 'uses' => 'RoleController@index']);
  346. Route::get('/create', ['as' => 'create', 'uses' => 'RoleController@create']);
  347. Route::get('/update/{role_id}', ['as' => 'update', 'uses' => 'RoleController@update']);
  348. Route::get('/delete-action/{role_id}', ['as' => 'delete_action', 'uses' => 'RoleController@delete_action']);
  349. Route::get('/get-data', ['as' => 'get_data', 'uses' => 'RoleController@getData']);
  350. Route::post('/create-action', ['as' => 'create_action', 'uses' => 'RoleController@create_action']);
  351. Route::post('/update-action', ['as' => 'update_action', 'uses' => 'RoleController@update_action']);
  352. });
  353. }
  354. //end custom route
  355. }