NotificationsController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 92.94%

Importance

Changes 0
Metric Value
eloc 88
dl 0
loc 157
ccs 79
cts 85
cp 0.9294
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 2 1
B data_tables() 0 80 3
A mass_action() 0 29 4
A beforeFilter() 0 7 2
A initialize() 0 13 1
1
<?php
2
3
/**
4
 * Notifications controller handling notification creation and rendering.
5
 *
6
 * phpMyAdmin Error reporting server
7
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
8
 *
9
 * Licensed under The MIT License
10
 * For full copyright and license information, please see the LICENSE.txt
11
 * Redistributions of files must retain the above copyright notice.
12
 *
13
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
14
 * @license   https://opensource.org/licenses/mit-license.php MIT License
15
 *
16
 * @see      https://www.phpmyadmin.net/
17
 */
18
19
namespace App\Controller;
20
21
use Cake\Event\EventInterface;
22
use Cake\Http\Response;
23
use Cake\ORM\TableRegistry;
24
use Cake\Routing\Router;
25
26
use function array_push;
27
use function intval;
28
use function json_encode;
29
30
/**
31
 * Notifications Controller.
32
 */
33
class NotificationsController extends AppController
34
{
35
    /**
36
     * Initialization hook method.
37
     *
38
     * Use this method to add common initialization code like loading components.
39
     *
40
     * @return void Nothing
41
     */
42 21
    public function initialize(): void
43
    {
44 21
        parent::initialize();
45 21
        $this->loadComponent('RequestHandler');
46 21
        $this->loadComponent('OrderSearch');
47 21
        $this->viewBuilder()->setHelpers([
48 21
            'Html',
49
            'Form',
50
            'Reports',
51
        ]);
52 21
        $this->loadModel('Notifications');
53 21
        $this->loadModel('Developers');
54 21
        $this->loadModel('Reports');
55 21
    }
56
57 21
    public function beforeFilter(EventInterface $event)
58
    {
59 21
        if ($this->request->getParam('action') === 'clean_old_notifs') {
60
            return;
61
        }
62
63 21
        parent::beforeFilter($event);
64 21
    }
65
66 7
    public function index(): void
67
    {
68
        // no need to do anything here. Just render the view.
69 7
    }
70
71 7
    public function data_tables(): Response
72
    {
73 7
        $current_developer = TableRegistry::getTableLocator()->get('Developers')->
74 7
                    findById($this->request->getSession()->read('Developer.id'))->all()->first();
75
76 2
        $aColumns = [
77 5
            'report_id' => 'Reports.id',
78
            'error_message' => 'Reports.error_message',
79
            'error_name' => 'Reports.error_name',
80
            'pma_version' => 'Reports.pma_version',
81
            'exception_type' => 'Reports.exception_type',
82
            'created_time' => 'Notifications.created',
83
        ];
84
85 7
        $orderConditions = $this->OrderSearch->getOrder($aColumns, $this->request);
86 7
        $searchConditions = $this->OrderSearch->getSearchConditions($aColumns, $this->request);
87
88 7
        $aColumns['id'] = 'Notifications.id';
89 2
        $params = [
90 7
            'contain' => 'Reports',
91 7
            'fields' => $aColumns,
92
            'conditions' => [
93
                'AND' => [
94 7
                    ['Notifications.developer_id ' => $current_developer['id']],
95 7
                    $searchConditions,
96
                ],
97
            ],
98 7
            'order' => $orderConditions,
99
        ];
100
101 7
        $pagedParams = $params;
102 7
        $pagedParams['limit'] = intval($this->request->getQuery('iDisplayLength'));
103 7
        $pagedParams['offset'] = intval($this->request->getQuery('iDisplayStart'));
104
105 7
        $rows = $this->Notifications->find('all', $pagedParams);
106 7
        $totalFiltered = $this->Notifications->find(
107 7
            'all',
108
            [
109
                'conditions' => [
110 7
                    'developer_id' => $current_developer['id'],
111
                ],
112
            ]
113 7
        )->count();
114
115
        // Make the display rows array
116 7
        $dispRows = [];
117 7
        $tmp_row = [];
118 7
        foreach ($rows as $row) {
119 7
            $tmp_row[0] = '<input type="checkbox" name="notifs[]" value="'
120 7
                . $row['id']
121 7
                . '"/>';
122 7
            $tmp_row[1] = '<a href="'
123 7
                . Router::url(
124
                    [
125 7
                        '_name' => 'reports:view',
126 7
                        'id' => $row['report_id'],
127
                    ]
128
                )
129 7
                . '">'
130 7
                . $row['report_id']
131 7
                . '</a>';
132 7
            $tmp_row[2] = $row['error_name'];
133 7
            $tmp_row[3] = $row['error_message'];
134 7
            $tmp_row[4] = $row['pma_version'];
135 7
            $tmp_row[5] = $row['exception_type'] ? 'php' : 'js';
136 7
            $tmp_row[6] = $row['created_time'];
137 7
            array_push($dispRows, $tmp_row);
138
        }
139
140 2
        $response = [
141 7
            'iTotalDisplayRecords' => $totalFiltered,
142 7
            'iTotalRecords' => $this->Notifications->find('all', $params)->count(),
143 7
            'sEcho' => intval($this->request->getQuery('sEcho')),
144 7
            'aaData' => $dispRows,
145
        ];
146 7
        $this->disableAutoRender();
147
148 7
        return $this->response
149 7
            ->withType('application/json')
150 7
            ->withStringBody(json_encode($response));
151
    }
152
153
    /**
154
     * To carry out mass actions on Notifications.
155
     * Currently it deletes them (marks them "read").
156
     * Can be Extended for other mass operations as well.
157
     * Expects an array of Notification Ids as a POST parameter.
158
     *
159
     * @return void Nothing
160
     */
161 7
    public function mass_action(): void
162
    {
163 7
        $msg = 'Selected Notifications have been marked \'Read\'!';
164 7
        $flash_class = 'alert alert-success';
165
166 7
        if ($this->request->getData('mark_all')) {
167
            // Delete all notifications for this Developer
168 7
            $this->Notifications->deleteAll(
169 7
                ['developer_id' => $this->request->getSession()->read('Developer.id')]
170
            );
171
172 7
            $msg = 'All your notifications have been marked \'Read\'';
173
        } else {
174 7
            foreach ($this->request->getData('notifs') as $notif_id) {
175 7
                if (! $this->Notifications->delete($this->Notifications->get(intval($notif_id)))) {
176
                    $msg = '<b>ERROR</b>: There was some problem in deleting Notification(ID:'
177
                        . $notif_id
178
                        . ')!';
179
                    $flash_class = 'alert alert-error';
180
                    break;
181
                }
182
            }
183
        }
184
185 7
        $this->Flash->set(
186 7
            $msg,
187 7
            ['params' => ['class' => $flash_class]]
188
        );
189 7
        $this->redirect('/notifications/');
190 7
    }
191
}
192