/
NotificationController.php
151 lines (132 loc) · 4.11 KB
/
NotificationController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Mostly for Ajax actions
*/
class NotificationController extends Survey_Common_Action
{
/**
* List all notifications for a user
*/
public function index()
{
$this->checkPermission();
$data = array();
$data['model'] = Notification::model();
$this->_renderWrappedTemplate(null, array('notification/index'), $data);
}
/**
* Get notification as JSON
*
* @param int $notId Notification id
* @return string JSON
*/
public function getNotificationAsJSON($notId)
{
$this->checkPermission();
$not = Notification::model()->findByPk($notId);
if ($not)
{
echo json_encode(array('result' => $not->getAttributes()));
}
else
{
echo json_encode(array('error' => 'Found no notification with id ' . $notId));
}
}
/**
* Mark notification as read
*
* @param int $notId Notification id
* @return string JSON
*/
public function notificationRead($notId)
{
$this->checkPermission();
try
{
$not = Notification::model()->findByPk($notId);
$result = $not->markAsRead();
echo json_encode(array('result' => $result));
}
catch (Exception $ex)
{
echo json_encode(array('error' => $ex->getMessage()));
}
}
/**
* Spits out html used in admin menu
* @param int|null $surveyId
* @param bool $showLoader Whether or not to show spinning loader instead of notification list
* @return string
*/
public function actionGetMenuWidget($surveyId = null, $showLoader = false)
{
$this->checkPermission();
echo self::getMenuWidget($surveyId, $showLoader);
}
/**
* Delete all notifications for this user and this survey
* @param int|null $surveyId
* @return void
*/
public function clearAllNotifications($surveyId = null)
{
Notification::model()->deleteAll(
'entity = \'user\' AND entity_id = ' . Yii::app()->user->id
);
if (is_int($surveyId))
{
Notification::model()->deleteAll(
'entity = \'survey\' AND entity_id = ' . $surveyId
);
}
}
/**
* Die if user is not logged in
* @return void
*/
protected function checkPermission()
{
// Abort if user is not logged in
if(Yii::app()->user->isGuest)
{
die('No permission');
}
}
/**
* Get menu HTML for notifications
*
* @param int|null $surveyId
* @param bool $showLoader If true, show spinning loader instead of messages (fetch them using ajax)
* @return string HTML
*/
public static function getMenuWidget($surveyId = null, $showLoader = false) {
$data = array();
$data['surveyId'] = $surveyId;
$data['showLoader'] = $showLoader;
$data['clearAllNotificationsUrl'] = Yii::app()->createUrl('admin/notification', array(
'sa' => 'clearAllNotifications',
'surveyId' => $surveyId
));
$data['updateUrl'] = Notification::getUpdateUrl($surveyId);
$data['nrOfNewNotifications'] = Notification::countNewNotifications($surveyId);
$data['nrOfNotifications'] = Notification::countNotifications($surveyId);
$data['nrOfImportantNotifications'] = Notification::countImportantNotifications($surveyId);
$data['bellColor'] = $data['nrOfNewNotifications'] == 0 ? 'text-success' : 'text-warning';
// If we have any important notification we might as well load everything
if ($data['nrOfImportantNotifications'] > 0)
{
$data['showLoader'] = false;
}
// Only load all messages when we're not showing spinning loader
if (!$data['showLoader'])
{
$data['notifications'] = Notification::getNotifications($surveyId);
}
return Yii::app()->getController()->renderPartial(
'/admin/super/admin_notifications',
$data,
true
);
}
}