Completed
Pull Request — master (#154)
by Deven
12:19
created

NotificationsController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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
23
use Cake\Event\Event;
24
use Cake\ORM\TableRegistry;
25
use Cake\Routing\Router;
26
27
/**
28
 * Notifications Controller.
29
 */
30
class NotificationsController extends AppController
31
{
32
    public $components = array('RequestHandler', 'OrderSearch');
33
34
    public $helpers = array('Html', 'Form', 'Reports');
35
36
    public $uses = array('Notification', 'Developer', 'Report');
37
38 1
    public function beforeFilter(Event $event)
39
    {
40 1
        if ($this->action != 'clean_old_notifs') {
41 1
            parent::beforeFilter($event);
42
        }
43 1
    }
44
45
    public function index()
46
    {
47
        // no need to do anything here. Just render the view.
48
    }
49
50
    public function data_tables()
51
    {
52
        $current_developer = TableRegistry::get('Developers')->
53
                    findById($this->request->session()->read('Developer.id'))->all()->first();
54
55
        $aColumns = array(
56
            'report_id' => 'Reports.id',
57
            'error_message' => 'Reports.error_message',
58
            'error_name' => 'Reports.error_name',
59
            'pma_version' => 'Reports.pma_version',
60
            'exception_type' => 'Reports.exception_type',
61
            'created_time' => 'Notifications.created',
62
        );
63
64
        $orderConditions = $this->OrderSearch->getOrder($aColumns);
65
        $searchConditions = $this->OrderSearch->getSearchConditions($aColumns);
66
67
        $aColumns['id'] = 'Notifications.id';
68
        $params = array(
69
            'contain' => 'Reports',
70
            'fields' => $aColumns,
71
            'conditions' => array(
72
                'AND' => array(
73
                    array('Notifications.developer_id ' => $current_developer['id']),
74
                    $searchConditions,
75
                ),
76
            ),
77
            'order' => $orderConditions,
78
        );
79
        //$current_developer = Sanitize::clean($current_developer);
80
81
        $pagedParams = $params;
82
        $pagedParams['limit'] = intval($this->request->query('iDisplayLength'));
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\ServerRequest::query() has been deprecated with message: 3.4.0 Use getQuery() or the PSR-7 getQueryParams() and withQueryParams() methods instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
83
        $pagedParams['offset'] = intval($this->request->query('iDisplayStart'));
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\ServerRequest::query() has been deprecated with message: 3.4.0 Use getQuery() or the PSR-7 getQueryParams() and withQueryParams() methods instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
84
85
        $rows = $this->Notifications->find('all', $pagedParams);
86
        $totalFiltered = $this->Notifications->find(
87
            'all',
88
            array(
89
                'conditions' => array(
90
                    'developer_id' => $current_developer['id']
91
                )
92
            )
93
        )->count();
94
95
        // Make the display rows array
96
        $dispRows = array();
97
        $tmp_row = array();
98
        foreach ($rows as $row) {
99
            $tmp_row[0] = '<input type="checkbox" name="notifs[]" value="'
100
                . $row['id']
101
                . '"/>';
102
            $tmp_row[1] = '<a href="'
103
                . Router::url(
104
                    array(
105
                        'controller' => 'reports',
106
                        'action' => 'view',
107
                        $row['report_id'],
108
                        )
109
                    )
110
                . '">'
111
                . $row['report_id']
112
                . '</a>';
113
            $tmp_row[2] = $row['error_name'];
114
            $tmp_row[3] = $row['error_message'];
115
            $tmp_row[4] = $row['pma_version'];
116
            $tmp_row[5] = ($row['exception_type']) ? ('php') : ('js');
117
            $tmp_row[6] = $row['created_time'];
118
            array_push($dispRows, $tmp_row);
119
        }
120
121
        $response = array(
122
            'iTotalDisplayRecords' => $totalFiltered,
123
            'iTotalRecords' => $this->Notifications->find('all', $params)->count(),
124
            'sEcho' => intval($this->request->query('sEcho')),
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\ServerRequest::query() has been deprecated with message: 3.4.0 Use getQuery() or the PSR-7 getQueryParams() and withQueryParams() methods instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
125
            'aaData' => $dispRows,
126
        );
127
        $this->autoRender = false;
128
        $this->response->body(json_encode($response));
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\Response::body() has been deprecated with message: 3.4.0 Mutable response methods are deprecated. Use `withBody()` and `getBody()` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
129
130
        return $this->response;
131
    }
132
133
    /**
134
     * To carry out mass actions on Notifications.
135
     * Currently it deletes them (marks them "read").
136
     * Can be Extended for other mass operations as well.
137
     * Expects an array of Notification Ids as a POST parameter.
138
     */
139 1
    public function mass_action()
140
    {
141 1
        $msg = 'Selected Notifications have been marked \'Read\'!';
142 1
        $flash_class = 'alert alert-success';
143
144 1
        if ($this->request->getData('mark_all')) {
145
146
            // Delete all notifications for this Developer
147 1
            $this->Notifications->deleteAll(
148 1
                ['developer_id' => $this->request->session()->read('Developer.id')]
149
            );
150
151 1
            $msg = 'All your notifications have been marked \'Read\'';
152
        } else {
153 1
            foreach ($this->request->data['notifs'] as $notif_id) {
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Http\ServerRequest::$data has been deprecated with message: 3.4.0 This public property will be removed in 4.0.0. Use getData() instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
154 1
                if (!$this->Notifications->delete($this->Notifications->get(intval($notif_id)))) {
155
                    $msg = '<b>ERROR</b>: There was some problem in deleting Notification(ID:'
156
                        . $notif_id
157
                        . ')!';
158
                    $flash_class = 'alert alert-error';
159 1
                    break;
160
                }
161
            }
162
        }
163 1
        $this->Flash->default($msg,
164 1
            array('params' => array('class' => $flash_class)));
165 1
        $this->redirect('/notifications/');
166 1
    }
167
}
168