Completed
Pull Request — master (#181)
by
unknown
02:10
created

ReportsController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 22
cts 23
cp 0.9565
rs 9.408
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\Http\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(
42 1
            'distinct_statuses',
43 1
            $this->_findArrayList(
44
                $this->Reports->find()->select(array('status'))->distinct(array('status')),
45 1
            'status'
46 1
            )
47 1
        );
48 1
        $this->set(
49 1
            'distinct_locations',
50 1
            $this->_findArrayList(
51
                $this->Reports->find()->select(array('location'))
52
                    ->distinct(array('location')),
53 1
                'location'
54 1
            )
55
        );
56 1
        $this->set(
57 1
            'distinct_versions',
58 1
            $this->_findArrayList($this->Reports->find()->select(array('pma_version'))->distinct(array('pma_version')), 'pma_version')
59
        );
60 1
        $this->set(
61
            'distinct_error_names',
62 1
            $this->_findArrayList($this->Reports->find('all', array(
63 1
                'fields' => array('error_name'),
64 1
                'conditions' => array('error_name !=' => ''),
65
            ))->distinct(array('error_name')), 'error_name')
66 1
        );
67
        $this->set('statuses', $this->Reports->status);
68 1
        $this->autoRender = true;
69
    }
70
71 1
    public function view($reportId)
72 1
    {
73 1
        if (!isset($reportId) || !$reportId) {
74
            throw new NotFoundException(__('Invalid Report'));
75
        }
76 1
        $report = $this->Reports->findById($reportId)->toArray();
77 1
        if (!$report) {
78 1
            throw new NotFoundException(__('Invalid Report'));
79 1
        }
80 1
81 1
        $this->set('report', $report);
82 1
        $this->set('project_name', Configure::read('GithubRepoPath'));
83 1
        $this->Reports->id = $reportId;
84 1
        $this->set('incidents', $this->Reports->getIncidents()->toArray());
85 1
        $this->set(
86 1
            'incidents_with_description',
87
            $this->Reports->getIncidentsWithDescription()
88
        );
89 1
        $this->set(
90 1
            'incidents_with_stacktrace',
91
            $this->Reports->getIncidentsWithDifferentStacktrace()
92 1
        );
93 1
        $this->set('related_reports', $this->Reports->getRelatedReports());
94 1
        $this->set('status', $this->Reports->status);
95 1
        $this->_setSimilarFields($reportId);
96
97 1
        // if there is an unread notification for this report, then mark it as read
98
        $current_developer = TableRegistry::get('Developers')->
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\TableRegistry::get() has been deprecated with message: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() 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...
99
                    findById($this->request->session()->read('Developer.id'))->all()->first();
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\ServerRequest::session() has been deprecated with message: 3.5.0 Use getSession() instead. The setter part will be removed.

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