NotificationController.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Http\Controllers\Dev\Notification;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Model\Notification\Notification;
  6. use App\Model\MetadataProgramNotification;
  7. use App\Model\TahunProgramDetail;
  8. use App\Model\MetadataProgramNotificationRead;
  9. use App\Model\Menu\Module;
  10. use App\Model\Privillage\Role;
  11. use App\Model\Region\Provinsi;
  12. use App\Model\Setting\UPT;
  13. use Auth;
  14. class NotificationController extends Controller
  15. {
  16. public function __construct()
  17. {
  18. $this->middleware('auth');
  19. }
  20. public function getNotification($id_tahun_program_detail)
  21. {
  22. $tahunProgramDetail = TahunProgramDetail::find($id_tahun_program_detail);
  23. $user = Auth::user();
  24. if (Role::where('id',Auth::user()->role_id)->value('akses') == 'administrator') {
  25. $getNotification = MetadataProgramNotification::where('id_tahun_program', $tahunProgramDetail->id_tahun_program)
  26. ->where('id_template_program_detail',$tahunProgramDetail->id_template_program_detail)
  27. ->orderBy('id','desc')
  28. ->get();
  29. }else{
  30. $getNotification = MetadataProgramNotification::where('id_tahun_program', $tahunProgramDetail->id_tahun_program)
  31. ->where('id_template_program_detail',$tahunProgramDetail->id_template_program_detail)
  32. ->where('id_upt',$user->upt)
  33. ->orderBy('id','desc')
  34. ->get();
  35. }
  36. $html = '';
  37. $id = '';
  38. foreach ($getNotification as $notification) {
  39. $program = !empty($tahunProgramDetail->templateProgram)?$tahunProgramDetail->templateProgram->nama:'-';
  40. $sub_program = !empty($tahunProgramDetail->templateProgramDetail)?$tahunProgramDetail->templateProgramDetail->nama:'-';
  41. if ($notification != '') {
  42. $html .= '<tr><td>'.$program.'</td><td>'.$notification->message.'</td><td>'.$notification->created_at.'</td></tr>';
  43. $id .= $notification->id.',';
  44. }
  45. else{
  46. $html = '<tr><td>Tidak Ada Pemberitahuan</td></tr>';
  47. }
  48. }
  49. return response()->json(['html' => $html,'id' => $id]);
  50. }
  51. public function updateToRead($id_tahun_program_detail)
  52. {
  53. $ok = 'false';
  54. $tahunProgramDetail = TahunProgramDetail::find($id_tahun_program_detail);
  55. $user = Auth::user();
  56. if (Role::where('id',Auth::user()->role_id)->value('akses') == 'administrator') {
  57. $getNotification = MetadataProgramNotification::where('id_tahun_program', $tahunProgramDetail->id_tahun_program)
  58. ->where('id_template_program',$tahunProgramDetail->id_template_program)
  59. ->pluck('id');
  60. }else{
  61. $getNotification = MetadataProgramNotification::where('id_tahun_program', $tahunProgramDetail->id_tahun_program)
  62. ->where('id_template_program',$tahunProgramDetail->id_template_program)
  63. ->where('id_upt',$user->upt)
  64. ->pluck('id');
  65. }
  66. foreach($getNotification as $notif_id){
  67. $attribute = [
  68. 'id_user'=> $user->id,
  69. 'id_metadata_program_notification'=> $notif_id,
  70. ];
  71. MetadataProgramNotificationRead::updateOrCreate($attribute,$attribute);
  72. $ok = 'true';
  73. }
  74. return $ok;
  75. }
  76. }