Completed
Push — master ( aed5b3...caff4d )
by Michal
05:21
created

NotificationsController::data_tables()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 75
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 75
ccs 0
cts 43
cp 0
rs 9
cc 3
eloc 54
nc 3
nop 0
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        //$rows = Sanitize::clean($rows);
87
88
        // Make the display rows array
89
        $dispRows = array();
90
        $tmp_row = array();
91
        foreach ($rows as $row) {
92
            $tmp_row[0] = '<input type="checkbox" name="notifs[]" value="'
93
                . $row['id']
94
                . '"/>';
95
            $tmp_row[1] = '<a href="'
96
                . Router::url(
97
                    array(
98
                        'controller' => 'reports',
99
                        'action' => 'view',
100
                        $row['report_id'],
101
                        )
102
                    )
103
                . '">'
104
                . $row['report_id']
105
                . '</a>';
106
            $tmp_row[2] = $row['error_name'];
107
            $tmp_row[3] = $row['error_message'];
108
            $tmp_row[4] = $row['pma_version'];
109
            $tmp_row[5] = ($row['exception_type']) ? ('php') : ('js');
110
            $tmp_row[6] = $row['created_time'];
111
            array_push($dispRows, $tmp_row);
112
        }
113
114
        $response = array(
115
            'iTotalDisplayRecords' => count($dispRows),
116
            'iTotalRecords' => $this->Notifications->find('all', $params)->count(),
117
            '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...
118
            'aaData' => $dispRows,
119
        );
120
        $this->autoRender = false;
121
        $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...
122
123
        return $this->response;
124
    }
125
126
    /**
127
     * To carry out mass actions on Notifications.
128
     * Currently it deletes them (marks them "read").
129
     * Can be Extended for other mass operations as well.
130
     * Expects an array of Notification Ids as a POST parameter.
131
     */
132 1
    public function mass_action()
133
    {
134 1
        $msg = 'Selected Notifications have been marked <i>Read</i>!';
135 1
        $flash_class = 'alert alert-success';
136 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...
137 1
            if (!$this->Notifications->delete($this->Notifications->get(intval($notif_id)))) {
138
                $msg = '<b>ERROR</b>: There was some problem in deleting Notification(ID:'
139
                    . $notif_id
140
                    . ')!';
141
                $flash_class = 'alert alert-error';
142 1
                break;
143
            }
144
        }
145 1
        $this->Flash->default($msg,
146 1
            array('params' => array('class' => $flash_class)));
147 1
        $this->redirect('/notifications/');
148 1
    }
149
}
150