Completed
Push — master ( 858b2f...a848a8 )
by Michal
15s
created

ReportsController::data_tables()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 95
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 95
ccs 42
cts 42
cp 1
rs 8.4117
c 0
b 0
f 0
cc 3
eloc 62
nc 3
nop 0
crap 3

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
/* vim: set expandtab sw=4 ts=4 sts=4: */
3
4
/**
5
 * Reports controller handling reports 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 App\Utility\Sanitize;
23
use Cake\Core\Configure;
24
use Cake\Log\Log;
25
use Cake\Network\Exception\NotFoundException;
26
use Cake\ORM\TableRegistry;
27
28
/**
29
 * Reports controller handling reports modification and rendering.
30
 */
31
class ReportsController extends AppController
32
{
33
    public $components = array('RequestHandler', 'OrderSearch');
34
35
    public $helpers = array('Html', 'Form', 'Reports', 'Incidents');
36
    public $uses = array('Incidents', 'Reports', 'Notifications', 'Developers');
37
38 1
    public function index()
39
    {
40 1
        $this->Reports->recursive = -1;
41 1
        $this->set('distinct_statuses',
42 1
            $this->_findArrayList($this->Reports->find()->select(array('status'))->distinct(array('status')),
43 1
            'status')
44
        );
45 1
        $this->set(
46 1
            'distinct_locations',
47 1
            $this->_findArrayList(
48 1
                $this->Reports->find()->select(array('location'))
49 1
                    ->distinct(array('location')),
50 1
                'location'
51
            )
52
        );
53 1
        $this->set('distinct_versions',
54 1
            $this->_findArrayList($this->Reports->find()->select(array('pma_version'))->distinct(array('pma_version')), 'pma_version')
55
        );
56 1
        $this->set('distinct_error_names',
57 1
            $this->_findArrayList($this->Reports->find('all', array(
58 1
                'fields' => array('error_name'),
59
                'conditions' => array('error_name !=' => ''),
60 1
            ))->distinct(array('error_name')), 'error_name')
61
        );
62 1
        $this->set('statuses', $this->Reports->status);
63 1
        $this->autoRender = true;
64 1
    }
65
66 1
    public function view($reportId)
67
    {
68 1
        if (!$reportId) {
69
            throw new NotFoundException(__('Invalid Report'));
70
        }
71 1
        $report = $this->Reports->findById($reportId)->toArray();
72 1
        if (!$report) {
73 1
            throw new NotFoundException(__('Invalid Report'));
74
        }
75
76 1
        $this->set('report', $report);
77 1
        $this->set('project_name', Configure::read('GithubRepoPath'));
78 1
        $this->Reports->id = $reportId;
79 1
        $this->set('incidents', $this->Reports->getIncidents()->toArray());
80 1
        $this->set('incidents_with_description',
81 1
            $this->Reports->getIncidentsWithDescription());
82 1
        $this->set('incidents_with_stacktrace',
83 1
            $this->Reports->getIncidentsWithDifferentStacktrace());
84 1
        $this->set('related_reports', $this->Reports->getRelatedReports());
85 1
        $this->set('status', $this->Reports->status);
86 1
        $this->_setSimilarFields($reportId);
87
88
        // if there is an unread notification for this report, then mark it as read
89 1
        $current_developer = TableRegistry::get('Developers')->
90 1
                    findById($this->request->session()->read('Developer.id'))->all()->first();
91
        //$current_developer = Sanitize::clean($current_developer);
92 1
        if ($current_developer) {
93 1
            TableRegistry::get('Notifications')->deleteAll(
94 1
                array('developer_id' => $current_developer['Developer']['id'],
95 1
                    'report_id' => $reportId,
96
                ),
97 1
                false
0 ignored issues
show
Unused Code introduced by
The call to Table::deleteAll() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
98
            );
99
        }
100 1
    }
101
102 1
    public function data_tables()
103
    {
104
        $subquery_params = array(
105 1
            'fields' => array(
106
                'report_id' => 'report_id',
107
                'inci_count' => 'COUNT(id)',
108
                ),
109
            'group' => 'report_id',
110
        );
111 1
        $subquery = TableRegistry::get('incidents')->find('all', $subquery_params);
112
113
        // override automatic aliasing, for proper usage in joins
114
        $aColumns = array(
115 1
            'id' => 'id',
116
            'error_name' => 'error_name',
117
            'error_message' => 'error_message',
118
            'location' => 'location',
119
            'pma_version' => 'pma_version',
120
            'status' => 'status',
121
            'exception_type' => 'exception_type',
122
            'inci_count' => 'inci_count',
123
        );
124
125 1
        $searchConditions = $this->OrderSearch->getSearchConditions($aColumns);
126 1
        $orderConditions = $this->OrderSearch->getOrder($aColumns);
127
128
        $params = array(
129 1
            'fields' => $aColumns,
130
            'conditions' => array(
131 1
                    $searchConditions,
132 1
                    'related_to is NULL',
133
                ),
134 1
            'order' => $orderConditions,
135
        );
136
137 1
        $pagedParams = $params;
138 1
        $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...
139 1
        $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...
140
141 1
        $rows = $this->_findAllDataTable(
142 1
            $this->Reports->find('all', $pagedParams)->innerJoin(
143 1
                array('incidents' => $subquery), array('incidents.report_id = Reports.id')
144
            )
145
        );
146
        //$rows = Sanitize::clean($rows);
147 1
        $totalFiltered = $this->Reports->find('all', $params)->count();
148
149
        // change exception_type from boolean values to strings
150
        // add incident count for related reports
151 1
        $dispRows = array();
152 1
        foreach ($rows as $row) {
153 1
            $row[5] = $this->Reports->status[$row[5]];
154 1
            $row[6] = (intval($row[6])) ? ('php') : ('js');
155
            $input_elem = "<input type='checkbox' name='reports[]' value='"
156 1
                . $row[0]
157 1
                . "'/>";
158
159
            $subquery_params_count = array(
160 1
                'fields' => array(
161
                    'report_id' => 'report_id',
162
                ),
163
            );
164 1
            $subquery_count = TableRegistry::get('incidents')->find(
165 1
                'all', $subquery_params_count
166
            );
167
168
            $params_count = array(
169 1
                'fields' => array('inci_count' => 'inci_count'),
170
                'conditions' => array(
171 1
                        'related_to = ' . $row[0],
172
                ),
173
            );
174
175 1
            $inci_count_related = $this->Reports->find('all', $params_count)->innerJoin(
176 1
                array('incidents' => $subquery_count),
177 1
                array('incidents.report_id = Reports.related_to')
178 1
            )->count();
179
180 1
            $row[7] += $inci_count_related;
181
182 1
            array_unshift($row, $input_elem);
183 1
            array_push($dispRows, $row);
184
        }
185
186
        $response = array(
187 1
            'iTotalRecords' => $this->Reports->find('all')->count(),
188 1
            'iTotalDisplayRecords' => $totalFiltered,
189 1
            '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...
190 1
            'aaData' => $dispRows,
191
        );
192 1
        $this->autoRender = false;
193 1
        $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...
194
195 1
        return $this->response;
196
    }
197
198 1
    public function mark_related_to($reportId)
199
    {
200
        // Only allow POST requests
201 1
        $this->request->allowMethod(['post']);
202
203 1
        $relatedTo = $this->request->getData('related_to');
204 1
        if (!$reportId
205 1
            || !$relatedTo
206 1
            || $reportId == $relatedTo
207
        ) {
208
            throw new NotFoundException(__('Invalid Report'));
209
        }
210
211 1
        $report = $this->Reports->get($reportId);
212 1
        if (!$report) {
213
            throw new NotFoundException(__('Invalid Report'));
214
        }
215
216 1
        $this->Reports->addToRelatedGroup($report, $relatedTo);
217
218 1
        $flash_class = 'alert alert-success';
219 1
        $this->Flash->default('This report has been marked the same as #'
220 1
                . $relatedTo,
221 1
                array('params' => array('class' => $flash_class)));
222 1
        $this->redirect("/reports/view/$reportId");
223 1
    }
224
225 1
    public function unmark_related_to($reportId)
226
    {
227
        // Only allow POST requests
228 1
        $this->request->allowMethod(['post']);
229
230 1
        if (!$reportId) {
231
            throw new NotFoundException(__('Invalid Report'));
232
        }
233
234 1
        $report = $this->Reports->get($reportId);
235 1
        if (!$report) {
236
            throw new NotFoundException(__('Invalid Report'));
237
        }
238
239 1
        $this->Reports->removeFromRelatedGroup($report);
240
241 1
        $flash_class = 'alert alert-success';
242 1
        $this->Flash->default('This report has been marked as different.',
243 1
            array('params' => array('class' => $flash_class)));
244 1
        $this->redirect("/reports/view/$reportId");
245 1
    }
246
247
    public function change_state($reportId)
248
    {
249
        if (!$reportId) {
250
            throw new NotFoundException(__('Invalid Report'));
251
        }
252
253
        $report = $this->Reports->get($reportId);
254
        if (!$report) {
255
            throw new NotFoundException(__('Invalid Report'));
256
        }
257
258
        $state = $this->request->data['state'];
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...
259
        $newState = $this->Reports->status[$state];
260
        if (!$newState) {
261
            throw new NotFoundException(__('Invalid State'));
262
        }
263
        $report->status = $state;
264
        $this->Reports->save($report);
265
266
        $flash_class = 'alert alert-success';
267
        $this->Flash->default('The state has been successfully changed.',
268
            array('params' => array('class' => $flash_class)));
269
        $this->redirect("/reports/view/$reportId");
270
    }
271
272
    /**
273
     * To carry out mass actions on Reports
274
     * Currently only to change their statuses.
275
     * Can be Extended for other mass operations as well.
276
     * Expects an array of Report Ids as a POST parameter.
277
     */
278
    public function mass_action()
279
    {
280
        $flash_class = 'alert alert-error';
281
        $state = $this->request->data['state'];
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...
282
        $newState = $this->Reports->status[$state];
283
        if (!$newState) {
284
            Log::write(
285
                'error',
286
                'ERRORED: Invalid param "state" in ReportsController::mass_action()',
287
                'alert'
288
            );
289
            $msg = 'ERROR: Invalid State!!';
290
        } elseif (count($this->request->data['reports']) == 0) {
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...
291
            $msg = 'No Reports Selected!! Please Select Reports and try again.';
292
        } else {
293
            $msg = "Status has been changed to '"
294
                . $this->request->data['state']
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...
295
                . "' for selected Reports!";
296
            $flash_class = 'alert alert-success';
297
            foreach ($this->request->data['reports'] as $report_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...
298
                $report = $this->Reports->get($report_id);
299
                if (!$report) {
300
                    Log::write(
301
                        'error',
302
                        'ERRORED: Invalid report_id in ReportsController::mass_action()',
303
                        'alert'
304
                    );
305
                    $msg = 'ERROR:Invalid Report ID:' . $report_id;
306
                    $flash_class = 'alert alert-error';
307
                    break;
308
                }
309
                $report->status = $state;
310
                $this->Reports->save($report);
311
            }
312
        }
313
314
        $this->Flash->default($msg,
315
            array('params' => array('class' => $flash_class)));
316
        $this->redirect('/reports/');
317
    }
318
319
    //# HELPERS
320
321 1
    protected function _setSimilarFields($id)
322
    {
323 1
        $this->Reports->id = $id;
324
325 1
        $this->set('columns', TableRegistry::get('Incidents')->summarizableFields);
326 1
        $relatedEntries = array();
327
328 1
        foreach (TableRegistry::get('Incidents')->summarizableFields as $field) {
329
            list($entriesWithCount, $totalEntries) =
330 1
                    $this->Reports->getRelatedByField($field, 25, true);
331 1
            $relatedEntries[$field] = $entriesWithCount;
332 1
            $this->set("${field}_distinct_count", $totalEntries);
333
        }
334
        //error_log(json_encode($relatedEntries));
335
        $this->set('related_entries', $relatedEntries);
336
    }
337
338
    protected function _findArrayList($results, $key)
339
    {
340 1
        $output = array();
341 1
        foreach ($results as $row) {
342 1
            $output[] = $row[$key];
343
        }
344
345 1
        return $output;
346
    }
347
348
    protected function _findAllDataTable($results)
349
    {
350 1
        $output = array();
351 1
        foreach ($results as $row) {
352 1
            $output_row = array();
353 1
            $row = $row->toArray();
354 1
            foreach ($row as $key => $value) {
355 1
                $output_row[] = $value;
356
            }
357 1
            $output[] = $output_row;
358
        }
359
360 1
        return $output;
361
    }
362
}
363