Completed
Push — master ( 7c757d...667317 )
by William
02:47
created

NotificationsController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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