|
6 | 6 | use yii\web\Controller;
|
7 | 7 | use ZakharovAndrew\user\models\NotificationGroup;
|
8 | 8 | use ZakharovAndrew\user\models\Notification;
|
| 9 | +use ZakharovAndrew\user\models\UserNotificationSetting; |
| 10 | +use ZakharovAndrew\user\models\UserRoles; |
9 | 11 | use yii\web\NotFoundHttpException;
|
10 | 12 | use yii\web\Response;
|
11 | 13 |
|
12 |
| -class NotificationAdminController extends Controller |
| 14 | +class NotificationsController extends Controller |
13 | 15 | {
|
14 | 16 | public function actionIndex()
|
15 | 17 | {
|
16 |
| - $groups = NotificationGroup::find()->all(); // Fetch all notification groups |
17 |
| - |
| 18 | + $userId = Yii::$app->user->id; |
| 19 | + |
| 20 | + $userRoleIds = UserRoles::getUserRolesIds($userId); |
| 21 | + |
| 22 | + // Запрос для получения всех групп уведомлений и связанных с ними уведомлений |
| 23 | + $groups = NotificationGroup::find() |
| 24 | + ->with([ |
| 25 | + 'notifications' => function ($query) use ($userRoleIds) { |
| 26 | + // Фильтруем уведомления: |
| 27 | + // - Если уведомление связано с ролями, проверяем, есть ли совпадение с ролями пользователя. |
| 28 | + // - Если уведомление НЕ связано с ролями, оно доступно всем. |
| 29 | + $query->joinWith('roles') |
| 30 | + ->andWhere([ |
| 31 | + 'or', |
| 32 | + ['notification_role.role_id' => null], // Уведомления без ролей (доступны всем) |
| 33 | + ['and', ['notification_role.role_id' => $userRoleIds]], // Уведомления с ролями, соответствующими пользователю |
| 34 | + ]) |
| 35 | + ->groupBy('notifications.id'); // Группируем по ID уведомлений, чтобы избежать дубликатов |
| 36 | + }, |
| 37 | + ]) |
| 38 | + ->all(); |
| 39 | + |
18 | 40 | return $this->render('index', [
|
19 | 41 | 'groups' => $groups,
|
20 |
| - 'model' => new NotificationGroup(), |
21 |
| - 'modelNotification' => new Notification() |
| 42 | + 'userId' => $userId, |
22 | 43 | ]);
|
23 | 44 | }
|
24 | 45 |
|
25 |
| - // Action to handle AJAX request for creating a new NotificationGroup |
26 |
| - public function actionCreateGroupAjax() |
27 |
| - { |
28 |
| - Yii::$app->response->format = Response::FORMAT_JSON; // Set response format to JSON |
29 |
| - |
30 |
| - $model = new NotificationGroup(); |
31 |
| - |
32 |
| - if ($model->load(Yii::$app->request->post()) && $model->save()) { |
33 |
| - return ['success' => true, 'group' => $model]; // Return success response with the new group |
34 |
| - } |
35 |
| - |
36 |
| - return ['success' => false, 'errors' => $model->getErrors()]; // Return error response if saving fails |
37 |
| - } |
38 |
| - |
39 |
| - public function actionDeleteGroup($id) |
40 |
| - { |
41 |
| - $model = NotificationGroup::findOne($id); |
42 |
| - |
43 |
| - if ($model) { |
44 |
| - $model->delete(); |
45 |
| - Yii::$app->session->setFlash('success', 'Group deleted successfully.'); |
46 |
| - } else { |
47 |
| - Yii::$app->session->setFlash('error', 'Group not found.'); |
48 |
| - } |
49 |
| - |
50 |
| - return $this->redirect(['index']); // Перенаправление обратно на страницу с группами |
51 |
| - } |
52 |
| - |
53 |
| - |
54 |
| - /** |
55 |
| - * Сreating a notification |
56 |
| - * @param integer $groupId - ID of the group to which the notification is added |
57 |
| - * @return type |
58 |
| - */ |
59 |
| - public function actionCreateNotificationAjax($groupId) |
| 46 | + public function actionSaveNotificationSetting() |
60 | 47 | {
|
61 |
| - Yii::$app->response->format = Response::FORMAT_JSON; |
| 48 | + Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; |
62 | 49 |
|
63 |
| - $group = $this->findGroup($groupId); |
64 |
| - $model = new Notification(); |
| 50 | + $userId = Yii::$app->request->post('user_id'); |
| 51 | + $notificationId = Yii::$app->request->post('notification_id'); |
| 52 | + $type = Yii::$app->request->post('type'); |
| 53 | + $value = Yii::$app->request->post('value'); |
65 | 54 |
|
66 |
| - if ($model->load(Yii::$app->request->post())) { |
67 |
| - $model->notification_group_id = $group->id; |
68 |
| - if ($model->save()) { |
69 |
| - return ['success' => true, 'notification' => $model]; |
70 |
| - } |
71 |
| - } else { |
72 |
| - echo 'asd'; |
73 |
| - } |
74 |
| - |
75 |
| - return ['success' => false, 'errors' => $model->getErrors()]; |
76 |
| - } |
77 |
| - |
78 |
| - public function actionEditNotificationAjax($id) |
79 |
| - { |
80 |
| - Yii::$app->response->format = Response::FORMAT_JSON; |
| 55 | + $setting = UserNotificationSetting::findOne([ |
| 56 | + 'user_id' => $userId, |
| 57 | + 'notification_id' => $notificationId, |
| 58 | + ]); |
81 | 59 |
|
82 |
| - $model = Notification::findOne($id); |
83 |
| - if (!$model) { |
84 |
| - return ['success' => false, 'message' => 'Notification not found']; |
| 60 | + if (!$setting) { |
| 61 | + $setting = new UserNotificationSetting([ |
| 62 | + 'user_id' => $userId, |
| 63 | + 'notification_id' => $notificationId, |
| 64 | + ]); |
85 | 65 | }
|
86 | 66 |
|
87 |
| - if ($model->load(Yii::$app->request->post())) { |
88 |
| - // Save roles |
89 |
| - $roleIds = Yii::$app->request->post('Notification')['roles'] ?? []; |
90 |
| - $model->setRoles($roleIds); |
91 |
| - |
92 |
| - if ($model->save()) { |
93 |
| - // Convert roles to an array for returning in JSON |
94 |
| - $roles = array_map(function ($role) { |
95 |
| - return $role->id; |
96 |
| - }, $model->roles); |
97 |
| - |
98 |
| - return [ |
99 |
| - 'success' => true, |
100 |
| - 'notification' => [ |
101 |
| - 'id' => $model->id, |
102 |
| - 'name' => $model->name, |
103 |
| - 'description' => $model->description, |
104 |
| - 'code_name' => $model->code_name, |
105 |
| - 'function_to_call' => $model->function_to_call, |
106 |
| - 'roles' => $roles, |
107 |
| - ], |
108 |
| - ]; |
109 |
| - } |
110 |
| - } |
111 |
| - |
112 |
| - return ['success' => false, 'errors' => $model->getErrors()]; |
113 |
| - } |
114 |
| - |
115 |
| - public function actionDeleteNotificationAjax($id) |
116 |
| - { |
117 |
| - Yii::$app->response->format = Response::FORMAT_JSON; |
118 |
| - |
119 |
| - $model = Notification::findOne($id); |
120 |
| - if ($model) { |
121 |
| - $model->delete(); |
122 |
| - return ['success' => true]; |
123 |
| - } |
124 |
| - |
125 |
| - return ['success' => false, 'message' => 'Notification not found']; |
126 |
| - } |
127 |
| - |
128 |
| - public function actionEditGroupAjax($id) |
129 |
| - { |
130 |
| - Yii::$app->response->format = Response::FORMAT_JSON; |
131 |
| - |
132 |
| - $model = $this->findGroup($id); |
133 |
| - |
134 |
| - if ($model->load(Yii::$app->request->post()) && $model->save()) { |
135 |
| - return ['success' => true, 'group' => $model]; |
136 |
| - } |
137 |
| - |
138 |
| - return ['success' => false, 'errors' => $model->getErrors()]; |
139 |
| - } |
140 |
| - |
141 |
| - |
142 |
| - // Helper method to find a NotificationGroup by ID |
143 |
| - protected function findGroup($id) |
144 |
| - { |
145 |
| - if (($model = NotificationGroup::findOne($id)) !== null) { |
146 |
| - return $model; |
| 67 | + $setting->{'send_' . $type} = ($value=="false" ? 0 : 1); |
| 68 | + |
| 69 | + if ($setting->save()) { |
| 70 | + return ['success' => true, 'res' => $value]; |
147 | 71 | }
|
148 | 72 |
|
149 |
| - throw new NotFoundHttpException('The requested page does not exist.'); // Throw an error if the group does not exist |
| 73 | + return ['success' => false, 'errors' => $setting->getErrors()]; |
150 | 74 | }
|
151 | 75 | }
|
0 commit comments