Completed
Pull Request — master (#154)
by Deven
02:48
created

ReportsController::data_tables()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 97
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 97
ccs 44
cts 44
cp 1
rs 8.3604
cc 3
eloc 64
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 1
            'orderConditions' => $orderConditions,
192 1
            'searchConditions' => $searchConditions
193
        );
194 1
        $this->autoRender = false;
195 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...
196
197 1
        return $this->response;
198
    }
199
200 1
    public function mark_related_to($reportId)
201
    {
202
        // Only allow POST requests
203 1
        $this->request->allowMethod(['post']);
204
205 1
        $relatedTo = $this->request->getData('related_to');
206 1
        if (!$reportId
207 1
            || !$relatedTo
208 1
            || $reportId == $relatedTo
209
        ) {
210
            throw new NotFoundException(__('Invalid Report'));
211
        }
212
213 1
        $report = $this->Reports->get($reportId);
214 1
        if (!$report) {
215
            throw new NotFoundException(__('Invalid Report'));
216
        }
217
218 1
        $this->Reports->addToRelatedGroup($report, $relatedTo);
219
220 1
        $flash_class = 'alert alert-success';
221 1
        $this->Flash->default('This report has been marked the same as #'
222 1
                . $relatedTo,
223 1
                array('params' => array('class' => $flash_class)));
224 1
        $this->redirect("/reports/view/$reportId");
225 1
    }
226
227 1
    public function unmark_related_to($reportId)
228
    {
229
        // Only allow POST requests
230 1
        $this->request->allowMethod(['post']);
231
232 1
        if (!$reportId) {
233
            throw new NotFoundException(__('Invalid Report'));
234
        }
235
236 1
        $report = $this->Reports->get($reportId);
237 1
        if (!$report) {
238
            throw new NotFoundException(__('Invalid Report'));
239
        }
240
241 1
        $this->Reports->removeFromRelatedGroup($report);
242
243 1
        $flash_class = 'alert alert-success';
244 1
        $this->Flash->default('This report has been marked as different.',
245 1
            array('params' => array('class' => $flash_class)));
246 1
        $this->redirect("/reports/view/$reportId");
247 1
    }
248
249
    public function change_state($reportId)
250
    {
251
        if (!$reportId) {
252
            throw new NotFoundException(__('Invalid Report'));
253
        }
254
255
        $report = $this->Reports->get($reportId);
256
        if (!$report) {
257
            throw new NotFoundException(__('Invalid Report'));
258
        }
259
260
        $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...
261
        $newState = $this->Reports->status[$state];
262
        if (!$newState) {
263
            throw new NotFoundException(__('Invalid State'));
264
        }
265
        $report->status = $state;
266
        $this->Reports->save($report);
267
268
        $flash_class = 'alert alert-success';
269
        $this->Flash->default('The state has been successfully changed.',
270
            array('params' => array('class' => $flash_class)));
271
        $this->redirect("/reports/view/$reportId");
272
    }
273
274
    /**
275
     * To carry out mass actions on Reports
276
     * Currently only to change their statuses.
277
     * Can be Extended for other mass operations as well.
278
     * Expects an array of Report Ids as a POST parameter.
279
     */
280
    public function mass_action()
281
    {
282
        $flash_class = 'alert alert-error';
283
        $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...
284
        $newState = $this->Reports->status[$state];
285
        if (!$newState) {
286
            Log::write(
287
                'error',
288
                'ERRORED: Invalid param "state" in ReportsController::mass_action()',
289
                'alert'
290
            );
291
            $msg = 'ERROR: Invalid State!!';
292
        } 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...
293
            $msg = 'No Reports Selected!! Please Select Reports and try again.';
294
        } else {
295
            $msg = "Status has been changed to '"
296
                . $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...
297
                . "' for selected Reports!";
298
            $flash_class = 'alert alert-success';
299
            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...
300
                $report = $this->Reports->get($report_id);
301
                if (!$report) {
302
                    Log::write(
303
                        'error',
304
                        'ERRORED: Invalid report_id in ReportsController::mass_action()',
305
                        'alert'
306
                    );
307
                    $msg = 'ERROR:Invalid Report ID:' . $report_id;
308
                    $flash_class = 'alert alert-error';
309
                    break;
310
                }
311
                $report->status = $state;
312
                $this->Reports->save($report);
313
            }
314
        }
315
316
        $this->Flash->default($msg,
317
            array('params' => array('class' => $flash_class)));
318
        $this->redirect('/reports/');
319
    }
320
321
    //# HELPERS
322
323 1
    protected function _setSimilarFields($id)
324
    {
325 1
        $this->Reports->id = $id;
326
327 1
        $this->set('columns', TableRegistry::get('Incidents')->summarizableFields);
328 1
        $relatedEntries = array();
329
330 1
        foreach (TableRegistry::get('Incidents')->summarizableFields as $field) {
331
            list($entriesWithCount, $totalEntries) =
332 1
                    $this->Reports->getRelatedByField($field, 25, true);
333 1
            $relatedEntries[$field] = $entriesWithCount;
334 1
            $this->set("${field}_distinct_count", $totalEntries);
335
        }
336
        //error_log(json_encode($relatedEntries));
337
        $this->set('related_entries', $relatedEntries);
338
    }
339
340
    protected function _findArrayList($results, $key)
341
    {
342 1
        $output = array();
343 1
        foreach ($results as $row) {
344 1
            $output[] = $row[$key];
345
        }
346
347 1
        return $output;
348
    }
349
350
    protected function _findAllDataTable($results)
351
    {
352 1
        $output = array();
353 1
        foreach ($results as $row) {
354 1
            $output_row = array();
355 1
            $row = $row->toArray();
356 1
            foreach ($row as $key => $value) {
357 1
                $output_row[] = $value;
358
            }
359 1
            $output[] = $output_row;
360
        }
361
362 1
        return $output;
363
    }
364
}
365