source: app/Http/Controllers/Dashboard/NotificationsController.php@ fb32aad

develop
Last change on this file since fb32aad was c6b84df, checked in by beratkjufliju <kufliju@…>, 3 years ago

added fileTypes controller, notifications, excel export, edited views

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[c6b84df]1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use Carbon\Carbon;
6use App\Http\Controllers\Controller;
7
8class NotificationsController extends Controller
9{
10 public $notifications;
11 private $notificationsLimit = 10;
12
13 public function __construct() {
14 $this->middleware("auth");
15 $this->notifications = null;
16 }
17
18 public function notifications() {
19 return view("dashboard.notifications.index")->with([
20 "notifications" => $this->decode(auth()->user()->notifications, true)
21 ]);
22 }
23
24 public function getNotifications() {
25 return auth()->user()->readNotifications;
26 }
27
28 public function getUnreadNotifications() {
29 return auth()->user()->unreadNotifications;
30 }
31
32 public function markNotificationsAsRead() {
33 return auth()->user()->unreadNotifications->markAsRead();
34 }
35
36 public function showNotifications() {
37
38 $this->notifications = collect();
39
40 if($this->getUnreadNotifications()->count() == 0) {
41 $this->notifications->push($this->getNotifications()->take($this->notificationsLimit));
42 $this->notifications = $this->notifications->flatten();
43 return response()->json($this->decode($this->notifications->reverse()));
44 }
45
46 if($this->getUnreadNotifications()->count() > 10) {
47 $this->notifications->push($this->getUnreadNotifications()->take($this->notificationsLimit));
48 } else {
49 $readNotifications = $this->notificationsLimit - $this->getUnreadNotifications()->count();
50 $this->notifications->push($this->getUnreadNotifications());
51 $this->notifications->push($this->getNotifications()->take($readNotifications));
52 }
53
54 $this->notifications = $this->notifications->flatten();
55
56 return response()->json($this->decode($this->notifications->reverse()));
57 }
58
59 public function decode($notifications, $isShowOnly = false) {
60
61 $response = array();
62 $decoded = json_decode($notifications);
63
64 foreach($decoded as $notification) {
65 $isRead = empty($notification->read_at) ? false : true;
66 array_push($response, [
67 "isRead" => $isRead,
68 // "from" => $notification->data->from,
69 "url" => $notification->data->url,
70 "message" => $notification->data->message,
71 "ago" => Carbon::parse($notification->created_at)->diffForHumans(),
72 ]);
73 }
74
75 if(!$isShowOnly) {
76 $this->markNotificationsAsRead();
77 }
78
79 return $response;
80 }
81}
Note: See TracBrowser for help on using the repository browser.