Nessuna descrizione
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.

EventOrganisasiController.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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\EventOrganisasi;
  8. use App\Model\Raharja\EventOrganisasiMembers;
  9. use App\Model\Raharja\KelompokDriver;
  10. use App\Model\Raharja\Tips;
  11. use Yajra\Datatables\Datatables;
  12. use Auth;
  13. use Carbon\Carbon;
  14. use Illuminate\Support\Facades\Storage;
  15. use Validator;
  16. use Hashids;
  17. //load modelmu
  18. class EventOrganisasiController extends Controller
  19. {
  20. /**
  21. * Title untuk judul di web
  22. * route digunakan untuk tempat resource (file path) + routing (route/web) diusahain sama ya biar gak ngubah"
  23. */
  24. private $title = 'Event Organisasi'; /**jangan lupa diganti*/
  25. private $route = 'admin.event-organisasi.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
  26. private $header = 'Event Organisasi';
  27. private $sub_header = 'Event Organisasi';
  28. private $url = 'admin.raharja.event_organisasi.';
  29. public function __construct()
  30. {
  31. DB::enableQueryLog();
  32. /** nyalakan jika sudah set rolenya, jika ini dinyalakan halaman ini tidak akan keluar */
  33. }
  34. public function index()
  35. {
  36. Helper::swal();
  37. $data = [
  38. //bawaan
  39. 'title' => $this->title,
  40. 'route' => $this->route,
  41. 'header' => $this->header,
  42. 'sub_header' => $this->sub_header,
  43. ];
  44. return view($this->url . 'index', $data);
  45. }
  46. public function getData($udd_id = null)
  47. {
  48. $query = EventOrganisasi::query();
  49. return Datatables::of($query)
  50. ->addIndexColumn()
  51. ->addColumn('start', function($query){
  52. return Carbon::parse($query->start_date)->format('Y-m-d');
  53. })
  54. ->addColumn('end', function($query){
  55. return Carbon::parse($query->end_date)->format('Y-m-d');
  56. })
  57. ->addColumn('aksi', function ($query) {
  58. /**cek role */
  59. $aksi = '';
  60. $aksi .= "<a href='" . route($this->route . 'update', ['data_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
  61. $aksi .= "<a href='" . route($this->route . 'kelompok_driver', ['data_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-warning btn-edit'>Kelompok Driver</a>";
  62. $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_action', ['data_id' => Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
  63. return $aksi;
  64. })
  65. ->rawColumns(['aksi'])
  66. ->toJson();
  67. }
  68. public function create()
  69. {
  70. $data = [
  71. //bawaan
  72. 'title' => $this->title,
  73. 'route' => $this->route,
  74. 'header' => $this->header,
  75. 'sub_header' => $this->sub_header,
  76. ];
  77. Helper::swal();
  78. return view($this->url . 'create', $data);
  79. }
  80. public function create_action(Request $request)
  81. {
  82. //declare post untuk simpan semua request yang diinputkan oleh form name=""
  83. $post = $request->all();
  84. $file = $request->file('banner');
  85. /**digunakan untuk set rule validator */
  86. $rules = [
  87. 'banner' => 'required|file|max:1000',
  88. 'start_date' => 'required',
  89. 'end_date' => 'required',
  90. 'deskripsi' => 'required',
  91. ];
  92. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  93. $alert = [
  94. 'file' => 'File maksimal 1MB'
  95. ];
  96. $validator = Validator::make($post, $rules, $alert);
  97. if ($validator->passes()) {
  98. /**menggunakan transaction */
  99. DB::beginTransaction();
  100. $nama_file = time()."_".$file->getClientOriginalName();
  101. Storage::putFileAs('public/images/event_organisasi/', $file, $nama_file );
  102. $query = new EventOrganisasi();
  103. $query->start_date = $request->start_date;
  104. $query->end_date = $request->end_date;
  105. $query->deskripsi = $request->deskripsi;
  106. $query->banner = $nama_file;
  107. $query->save();
  108. if ($query) {
  109. DB::commit();
  110. $message = 'Berhasil';
  111. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  112. } else {
  113. DB::rollback();
  114. $message = 'Gagal';
  115. return redirect()->back()->with('error', Helper::parsing_alert($message));
  116. }
  117. }
  118. /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
  119. $message = Helper::parsing_alert($validator->errors()->all());
  120. return redirect()->back()->with('error', $message)->withInput();
  121. }
  122. public function update($data_id)
  123. {
  124. Helper::swal();
  125. $data_id = Hashids::decode($data_id);
  126. if (!empty($data_id)) {
  127. $cek_data = EventOrganisasi::where('id', $data_id[0])->first();
  128. if ($cek_data) {
  129. $data = [
  130. //bawaan
  131. 'title' => $this->title,
  132. 'route' => $this->route,
  133. 'header' => $this->header,
  134. 'sub_header' => $this->sub_header,
  135. 'data' => $cek_data
  136. ];
  137. return view($this->url . 'update', $data);
  138. }
  139. $message = 'Id tidak ditemukan atau sudah dihapus';
  140. return redirect()->back()->with('error', $message);
  141. }
  142. $message = 'Id tdak ditemukan';
  143. return redirect()->back()->with('error', $message);
  144. }
  145. public function update_action(Request $request)
  146. {
  147. $post = $request->all();
  148. /**digunakan untuk set rule validator */
  149. $rules = [
  150. 'banner' => 'file|max:1000',
  151. 'start_date' => 'required',
  152. 'end_date' => 'required',
  153. 'deskripsi' => 'required',
  154. ];
  155. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  156. $alert = [];
  157. $validator = Validator::make($post, $rules, $alert);
  158. $data_id = Hashids::decode($request['id']);
  159. if (!empty($data_id)) {
  160. /**cek apakah data_idnya ada ? */
  161. $query = EventOrganisasi::find($data_id[0]);
  162. if ($query) {
  163. /**cek apakah id data benar" ada di DB ? */
  164. if ($validator->passes()) {
  165. /**validatornya */
  166. DB::beginTransaction();
  167. $file = $request->file('banner');
  168. if($file){
  169. $nama_file = time()."_".$file->getClientOriginalName();
  170. Storage::putFileAs('public/images/event_organisasi/', $file, $nama_file );
  171. Storage::delete('public/images/event_organisasi/'.$query->thumbnail);
  172. $query->banner = $nama_file;
  173. }
  174. $query->start_date = $request->start_date;
  175. $query->end_date = $request->end_date;
  176. $query->deskripsi = $request->deskripsi;
  177. $query->save();
  178. if ($query) {
  179. DB::commit();
  180. $message = 'Berhasil';
  181. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  182. } else {
  183. DB::rollback();
  184. $message = 'Gagal';
  185. return redirect()->back()->with('error', Helper::parsing_alert($message));
  186. }
  187. }
  188. $message = Helper::parsing_alert($validator->errors()->all());
  189. return redirect()->back()->with('error', $message);
  190. } else {
  191. $message = 'Id tdak ditemukan';
  192. return redirect()->back()->with('error', $message);
  193. }
  194. } else {
  195. $message = 'Id tidak boleh kosong';
  196. return redirect()->back()->with('error', $message);
  197. }
  198. }
  199. public function delete_action($id_modelmu)
  200. {
  201. $id = Hashids::decode($id_modelmu);
  202. /**cek idnya apakah ada ? */
  203. if (!empty($id)) {
  204. $cek_data = EventOrganisasi::find($id[0]);
  205. if ($cek_data) {
  206. DB::beginTransaction();
  207. try {
  208. $arr_member = $cek_data->kelompokMember;
  209. foreach($arr_member as $like){
  210. $like->delete();
  211. }
  212. Storage::delete('public/images/event_organisasi/'.$cek_data->thumbnail);
  213. $delete = $cek_data->delete();
  214. DB::commit();
  215. $message = 'Sukses';
  216. $response = [
  217. 'message' => $message,
  218. 'status' => true,
  219. ];
  220. return response()->json($response);
  221. } catch (Exception $e) {
  222. DB::rollback();
  223. $message = 'Gagal';
  224. $response = [
  225. 'message' => $message,
  226. 'status' => false,
  227. ];
  228. return response()->json($response);
  229. }
  230. }
  231. $message = 'Id tidak ditemukan atau sudah dihapus';
  232. $response = [
  233. 'message' => $message,
  234. 'status' => false,
  235. ];
  236. return response()->json($response);
  237. }
  238. $message = 'Id tdak ditemukan';
  239. $response = [
  240. 'message' => $message,
  241. 'status' => false,
  242. ];
  243. return response()->json($response);
  244. }
  245. public function kelompokDriver($data_id)
  246. {
  247. Helper::swal();
  248. $data_id = Hashids::decode($data_id);
  249. if (!empty($data_id)) {
  250. $cek_data = EventOrganisasi::where('id', $data_id[0])->first();
  251. if ($cek_data) {
  252. $data = [
  253. //bawaan
  254. 'title' => $this->title,
  255. 'route' => $this->route,
  256. 'header' => $this->header,
  257. 'sub_header' => $this->sub_header,
  258. 'data' => $cek_data,
  259. 'route_get_data' => route($this->route . 'get_data_kelompok_driver', ['data_id' => $cek_data->id]),
  260. ];
  261. return view($this->url . 'kelompok_driver_member', $data);
  262. }
  263. $message = 'Id tidak ditemukan atau sudah dihapus';
  264. return redirect()->back()->with('error', $message);
  265. }
  266. $message = 'Id tdak ditemukan';
  267. return redirect()->back()->with('error', $message);
  268. }
  269. public function getDataKelompokDriver($data_id){
  270. $query = EventOrganisasiMembers::where('event_organisasi_id', $data_id);
  271. return Datatables::of($query)
  272. ->addIndexColumn()
  273. ->addColumn('nama', function ($query){
  274. return $query->kelompokDriver->nama;
  275. })
  276. ->filterColumn('nama', function($query, $keyword) {
  277. $query->whereHas('kelompokDriver', function ($sub) use ($keyword) {
  278. $sub->where('nama', 'like', '%' . $keyword . '%');
  279. });;
  280. })
  281. ->addColumn('aksi', function ($query) {
  282. /**cek role */
  283. $aksi = '';
  284. $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_kelompok_driver', ['data_id' => Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
  285. return $aksi;
  286. })
  287. ->rawColumns(['aksi'])
  288. ->toJson();
  289. }
  290. public function deleteKelompokDriver($data_id){
  291. $id = Hashids::decode($data_id);
  292. /**cek idnya apakah ada ? */
  293. if (!empty($id)) {
  294. $cek_data = EventOrganisasiMembers::find($id[0]);
  295. if ($cek_data) {
  296. DB::beginTransaction();
  297. try {
  298. $delete = $cek_data->delete();
  299. DB::commit();
  300. $message = 'Sukses';
  301. $response = [
  302. 'message' => $message,
  303. 'status' => true,
  304. ];
  305. return response()->json($response);
  306. } catch (Exception $e) {
  307. DB::rollback();
  308. $message = 'Gagal';
  309. $response = [
  310. 'message' => $message,
  311. 'status' => false,
  312. ];
  313. return response()->json($response);
  314. }
  315. }
  316. $message = 'Id tidak ditemukan atau sudah dihapus';
  317. $response = [
  318. 'message' => $message,
  319. 'status' => false,
  320. ];
  321. return response()->json($response);
  322. }
  323. $message = 'Id tdak ditemukan';
  324. $response = [
  325. 'message' => $message,
  326. 'status' => false,
  327. ];
  328. return response()->json($response);
  329. }
  330. public function selectKelompokDriver(Request $request){
  331. if (session('error')) {
  332. alert()->html('', session('error'), 'error');
  333. }
  334. $term = trim($request['nama']);
  335. $arr_organisasi = EventOrganisasiMembers::where('event_organisasi_id', $request['e_o_id'])->get()->pluck('kelompok_driver_id')->toArray();
  336. $kelompok_driver = KelompokDriver::whereNotIn('id', $arr_organisasi)->take(10);
  337. if (!empty($request['not_in'])) {
  338. $id = collect($request['not_in']);
  339. $kelompok_driver = $kelompok_driver->whereNotIn('id', $id);
  340. }
  341. if (!empty($term)) {
  342. $kelompok_driver = $kelompok_driver->where('nama', 'like', '%' . $term . '%');
  343. }
  344. $kelompok_driver = $kelompok_driver->get();
  345. $response = [];
  346. foreach ($kelompok_driver as $key => $value) {
  347. $response[] = ['id' => $value->id, 'text' => $value->nama ];
  348. }
  349. return response()->json($response);
  350. }
  351. public function addKelompokDriver(Request $request){
  352. $query = new EventOrganisasiMembers();
  353. $query->event_organisasi_id = $request->organisasi_id;
  354. $query->kelompok_driver_id = $request->kelompok_driver_id;
  355. $query->save();
  356. if ($query) {
  357. DB::commit();
  358. $message = 'Berhasil';
  359. return redirect(route($this->route . 'kelompok_driver',['data_id' => Hashids::encode($request->organisasi_id)]))->with('success', Helper::parsing_alert($message));
  360. } else {
  361. DB::rollback();
  362. $message = 'Gagal';
  363. return redirect()->back()->with('error', Helper::parsing_alert($message));
  364. }
  365. }
  366. }