-
Notifications
You must be signed in to change notification settings - Fork 34
/
NotificationsController.php
188 lines (169 loc) · 5.99 KB
/
NotificationsController.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* Notifications controller handling notification creation and rendering.
*
* phpMyAdmin Error reporting server
* Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @see https://www.phpmyadmin.net/
*/
namespace App\Controller;
use Cake\Event\EventInterface;
use Cake\Http\Response;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use function array_push;
use function intval;
use function json_encode;
/**
* Notifications Controller.
*/
class NotificationsController extends AppController
{
/**
* Initialization hook method.
*
* Use this method to add common initialization code like loading components.
*
* @return void Nothing
*/
public function initialize(): void
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('OrderSearch');
$this->viewBuilder()->setHelpers([
'Html',
'Form',
'Reports',
]);
$this->loadModel('Notification');
$this->loadModel('Developer');
$this->loadModel('Report');
}
public function beforeFilter(EventInterface $event)
{
if ($this->request->getParam('action') === 'clean_old_notifs') {
return;
}
parent::beforeFilter($event);
}
public function index(): void
{
// no need to do anything here. Just render the view.
}
public function data_tables(): Response
{
$current_developer = TableRegistry::getTableLocator()->get('Developers')->
findById($this->request->getSession()->read('Developer.id'))->all()->first();
$aColumns = [
'report_id' => 'Reports.id',
'error_message' => 'Reports.error_message',
'error_name' => 'Reports.error_name',
'pma_version' => 'Reports.pma_version',
'exception_type' => 'Reports.exception_type',
'created_time' => 'Notifications.created',
];
$orderConditions = $this->OrderSearch->getOrder($aColumns, $this->request);
$searchConditions = $this->OrderSearch->getSearchConditions($aColumns, $this->request);
$aColumns['id'] = 'Notifications.id';
$params = [
'contain' => 'Reports',
'fields' => $aColumns,
'conditions' => [
'AND' => [
['Notifications.developer_id ' => $current_developer['id']],
$searchConditions,
],
],
'order' => $orderConditions,
];
$pagedParams = $params;
$pagedParams['limit'] = intval($this->request->getQuery('iDisplayLength'));
$pagedParams['offset'] = intval($this->request->getQuery('iDisplayStart'));
$rows = $this->Notifications->find('all', $pagedParams);
$totalFiltered = $this->Notifications->find(
'all',
[
'conditions' => [
'developer_id' => $current_developer['id'],
],
]
)->count();
// Make the display rows array
$dispRows = [];
$tmp_row = [];
foreach ($rows as $row) {
$tmp_row[0] = '<input type="checkbox" name="notifs[]" value="'
. $row['id']
. '"/>';
$tmp_row[1] = '<a href="'
. Router::url(
[
'_name' => 'reports:view',
'id' => $row['report_id'],
]
)
. '">'
. $row['report_id']
. '</a>';
$tmp_row[2] = $row['error_name'];
$tmp_row[3] = $row['error_message'];
$tmp_row[4] = $row['pma_version'];
$tmp_row[5] = $row['exception_type'] ? 'php' : 'js';
$tmp_row[6] = $row['created_time'];
array_push($dispRows, $tmp_row);
}
$response = [
'iTotalDisplayRecords' => $totalFiltered,
'iTotalRecords' => $this->Notifications->find('all', $params)->count(),
'sEcho' => intval($this->request->getQuery('sEcho')),
'aaData' => $dispRows,
];
$this->autoRender = false;
$this->response->withStringBody(json_encode($response));
return $this->response;
}
/**
* To carry out mass actions on Notifications.
* Currently it deletes them (marks them "read").
* Can be Extended for other mass operations as well.
* Expects an array of Notification Ids as a POST parameter.
*
* @return void Nothing
*/
public function mass_action(): void
{
$msg = 'Selected Notifications have been marked \'Read\'!';
$flash_class = 'alert alert-success';
if ($this->request->getData('mark_all')) {
// Delete all notifications for this Developer
$this->Notifications->deleteAll(
['developer_id' => $this->request->getSession()->read('Developer.id')]
);
$msg = 'All your notifications have been marked \'Read\'';
} else {
foreach ($this->request->data['notifs'] as $notif_id) {
if (! $this->Notifications->delete($this->Notifications->get(intval($notif_id)))) {
$msg = '<b>ERROR</b>: There was some problem in deleting Notification(ID:'
. $notif_id
. ')!';
$flash_class = 'alert alert-error';
break;
}
}
}
$this->Flash->set(
$msg,
['params' => ['class' => $flash_class]]
);
$this->redirect('/notifications/');
}
}