Passed
Push — master ( 083ff7...c84473 )
by William
03:02
created

NotificationsController::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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