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.

MessageController.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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\CategoryMesssage;
  8. use App\Model\Raharja\Message;
  9. use App\Model\Raharja\News;
  10. use App\Model\Raharja\UserReceive;
  11. use App\User;
  12. use Yajra\Datatables\Datatables;
  13. use Auth;
  14. use Carbon\Carbon;
  15. use Illuminate\Support\Facades\Storage;
  16. use Validator;
  17. use Hashids;
  18. //load modelmu
  19. class MessageController extends Controller
  20. {
  21. /**
  22. * Title untuk judul di web
  23. * route digunakan untuk tempat resource (file path) + routing (route/web) diusahain sama ya biar gak ngubah"
  24. */
  25. private $title = 'Message'; /**jangan lupa diganti*/
  26. private $route = 'admin.message.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
  27. private $header = 'Message';
  28. private $sub_header = 'Message';
  29. private $url = 'admin.raharja.message.';
  30. public function __construct()
  31. {
  32. DB::enableQueryLog();
  33. /** nyalakan jika sudah set rolenya, jika ini dinyalakan halaman ini tidak akan keluar */
  34. // $this->middleware('permission:searching-list|searching-create|searching-update|searching-delete', ['only' => ['index', 'create', 'update']]);
  35. // $this->middleware('permission:searching-create', ['only' => ['create', 'create_action']]);
  36. // $this->middleware('permission:searching-update', ['only' => ['update', 'update_action']]);
  37. // $this->middleware('permission:searching-delete', ['only' => ['delete']]);
  38. }
  39. /**
  40. * Ini contoh crud yang sudah jalan
  41. * index digunakna untuk tampilan awal dari menu yang akan dibuat
  42. * Untuk CRUD Biar cepat
  43. * Silahkan ganti
  44. * EloquentMu:: => dengan model anda
  45. * $id_modelmu => ganti dengan id di model anda
  46. * id_di_routenya => jangan lupa disesuaikan dengan yang dideclare di route
  47. */
  48. public function index()
  49. {
  50. Helper::swal();
  51. $data = [
  52. //bawaan
  53. 'title' => $this->title,
  54. 'route' => $this->route,
  55. 'header' => $this->header,
  56. 'sub_header' => $this->sub_header,
  57. ];
  58. // dd($data);
  59. return view($this->url . 'index', $data);
  60. }
  61. public function getData($udd_id = null)
  62. {
  63. $query = Message::query();
  64. return Datatables::of($query)
  65. ->addIndexColumn()
  66. ->addColumn('aksi', function ($query) {
  67. /**cek role */
  68. $aksi = '';
  69. // if (Auth::user()->can('master-jenis-kantong-update')) {
  70. // $aksi .= "<a href='" . route($this->route . 'send', ['data_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Kirim</a>";
  71. // }
  72. // if (Auth::user()->can('master-jenis-kantong-delete')) {
  73. $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>";
  74. // }
  75. return $aksi;
  76. })
  77. ->rawColumns(['aksi'])
  78. ->toJson();
  79. }
  80. public function create()
  81. {
  82. $arr_category = CategoryMesssage::get();
  83. $data = [
  84. //bawaan
  85. 'title' => $this->title,
  86. 'route' => $this->route,
  87. 'header' => $this->header,
  88. 'sub_header' => $this->sub_header,
  89. 'categories' => $arr_category,
  90. ];
  91. Helper::swal();
  92. return view($this->url . 'create', $data);
  93. }
  94. public function create_action(Request $request)
  95. {
  96. //declare post untuk simpan semua request yang diinputkan oleh form name=""
  97. $post = $request->all();
  98. //dd($post);
  99. /**digunakan untuk set rule validator */
  100. $rules = [
  101. 'title' => 'required',
  102. 'content' => 'required',
  103. 'category' => 'required',
  104. ];
  105. /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
  106. $alert = [
  107. 'file' => 'File maksimal 1MB'
  108. ];
  109. $validator = Validator::make($post, $rules, $alert);
  110. if ($validator->passes()) {
  111. /**menggunakan transaction */
  112. DB::beginTransaction();
  113. $set_data_message = [
  114. 'category_message_id' => $post['category'],
  115. 'title' => $post['title'],
  116. 'user_id' => Auth::user()->id,
  117. 'content' => Helper::input_summernote($post['content']),
  118. ];
  119. $message = Message::create($set_data_message);
  120. if($post['keterangan_kirim'] == 'semua'){
  121. $arr_user = User::get()->pluck('id')->toArray();
  122. }else{
  123. $arr_user = $post['pilih_user'];
  124. }
  125. foreach($arr_user as $key => $value){
  126. $set_data_user_recive[] = [
  127. 'message_id' => $message->id,
  128. 'user_id' => $value,
  129. 'created_at' => Carbon::now(),
  130. 'is_check' => 0,
  131. ];
  132. }
  133. $user_receive = UserReceive::insert($set_data_user_recive);
  134. if ($message && $user_receive) {
  135. DB::commit();
  136. $message = 'Berhasil';
  137. return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
  138. } else {
  139. DB::rollback();
  140. $message = 'Gagal';
  141. return redirect()->back()->with('error', Helper::parsing_alert($message));
  142. }
  143. }
  144. /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
  145. $message = Helper::parsing_alert($validator->errors()->all());
  146. return redirect()->back()->with('error', $message)->withInput();
  147. }
  148. public function delete_action($id_modelmu)
  149. {
  150. $id = Hashids::decode($id_modelmu);
  151. /**cek idnya apakah ada ? */
  152. if (!empty($id)) {
  153. $cek_data = Message::find($id[0]);
  154. if ($cek_data) {
  155. DB::beginTransaction();
  156. try {
  157. $arr_user_receive = UserReceive::Where('message_id', $cek_data->id)->delete();
  158. $delete = $cek_data->delete();
  159. DB::commit();
  160. $message = 'Sukses';
  161. $response = [
  162. 'message' => $message,
  163. 'status' => true,
  164. ];
  165. return response()->json($response);
  166. } catch (Exception $e) {
  167. DB::rollback();
  168. $message = 'Gagal';
  169. $response = [
  170. 'message' => $message,
  171. 'status' => false,
  172. ];
  173. return response()->json($response);
  174. }
  175. }
  176. $message = 'Id tidak ditemukan atau sudah dihapus';
  177. $response = [
  178. 'message' => $message,
  179. 'status' => false,
  180. ];
  181. return response()->json($response);
  182. }
  183. $message = 'Id tdak ditemukan';
  184. $response = [
  185. 'message' => $message,
  186. 'status' => false,
  187. ];
  188. return response()->json($response);
  189. }
  190. public function selectUser(Request $request){
  191. if (session('error')) {
  192. alert()->html('', session('error'), 'error');
  193. }
  194. $term = trim($request['nama']);
  195. $user = User::take(10);
  196. if (!empty($request['not_in'])) {
  197. $id = collect($request['not_in']);
  198. $user = $user->whereNotIn('id', $id);
  199. }
  200. if (!empty($term)) {
  201. $user = $user->where('nama', 'like', '%' . $term . '%');
  202. }
  203. $user = $user->get();
  204. $response = [];
  205. foreach ($user as $key => $value) {
  206. $response[] = ['id' => $value->id, 'text' => $value->name ];
  207. }
  208. return response()->json($response);
  209. }
  210. public function send($data_id){
  211. $id = Hashids::decode($data_id);
  212. $query = Message::where('id', $id)->first();
  213. foreach($query->userReceive as $key => $value){
  214. $set_data_user_recive[] = [
  215. 'message_id' => $query->id,
  216. 'user_receive_id' => $value->user_id,
  217. 'author_id' => $query->user_id,
  218. 'title' => $query->title,
  219. 'content' => $query->content,
  220. 'category' => $query->categoryMessage->name,
  221. ];
  222. }
  223. if($set_data_user_recive){
  224. $result = $set_data_user_recive;
  225. $res_status = true;
  226. $msg = 'Kirim Data';
  227. $status_msg = $msg;
  228. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  229. }else {
  230. $res_status = false;
  231. $msg = 'Data tidak ditemukan';
  232. $status_msg = $msg;
  233. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  234. }
  235. }
  236. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  237. {
  238. $response['response'] = array(
  239. 'status' => $status,
  240. 'message' => $msg,
  241. 'status_msg' => $status_msg,
  242. );
  243. $response['param'] = !empty($param) ? $param : '';
  244. $response['results'] = $result;
  245. return response()->json($response, 200);
  246. }
  247. }