Completed
Pull Request — master (#168)
by Deven
03:45
created

ReportsController::index()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 20
cts 20
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
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\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
        $this->autoRender = false;
69
70 1
        if (!isset($reportId) || !$reportId) {
71
            throw new NotFoundException(__('Invalid Report'));
72
        }
73 1
        $report = $this->Reports->findById($reportId)->toArray();
74 1
        if (!$report) {
75 1
            throw new NotFoundException(__('Invalid Report'));
76
        }
77
78 1
        $this->set('report', $report);
79 1
        $this->set('project_name', Configure::read('GithubRepoPath'));
80 1
        $this->Reports->id = $reportId;
81 1
        $this->set('incidents', $this->Reports->getIncidents()->toArray());
82 1
        $this->set('incidents_with_description',
83 1
            $this->Reports->getIncidentsWithDescription());
84 1
        $this->set('incidents_with_stacktrace',
85 1
            $this->Reports->getIncidentsWithDifferentStacktrace());
86 1
        $this->set('related_reports', $this->Reports->getRelatedReports());
87 1
        $this->set('status', $this->Reports->status);
88 1
        $this->_setSimilarFields($reportId);
89
90
        // if there is an unread notification for this report, then mark it as read
91 1
        $current_developer = TableRegistry::get('Developers')->
92 1
                    findById($this->request->session()->read('Developer.id'))->all()->first();
93
        //$current_developer = Sanitize::clean($current_developer);
94 1
        if ($current_developer) {
95 1
            TableRegistry::get('Notifications')->deleteAll(
96 1
                array('developer_id' => $current_developer['Developer']['id'],
97 1
                    'report_id' => $reportId,
98
                ),
99 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...
100
            );
101
        }
102 1
    }
103
104 1
    public function data_tables()
105
    {
106
        $subquery_params = array(
107 1
            'fields' => array(
108
                'report_id' => 'report_id',
109
                'inci_count' => 'COUNT(id)',
110
                ),
111
            'group' => 'report_id',
112
        );
113 1
        $subquery = TableRegistry::get('incidents')->find('all', $subquery_params);
114
115
        // override automatic aliasing, for proper usage in joins
116
        $aColumns = array(
117 1
            'id' => 'id',
118
            'error_name' => 'error_name',
119
            'error_message' => 'error_message',
120
            'location' => 'location',
121
            'pma_version' => 'pma_version',
122
            'status' => 'status',
123
            'exception_type' => 'exception_type',
124
            'inci_count' => 'inci_count',
125
        );
126
127 1
        $searchConditions = $this->OrderSearch->getSearchConditions($aColumns);
128 1
        $orderConditions = $this->OrderSearch->getOrder($aColumns);
129
130
        $params = array(
131 1
            'fields' => $aColumns,
132
            'conditions' => array(
133 1
                    $searchConditions,
134 1
                    'related_to is NULL',
135
                ),
136 1
            'order' => $orderConditions,
137
        );
138
139 1
        $pagedParams = $params;
140 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...
141 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...
142
143 1
        $rows = $this->_findAllDataTable(
144 1
            $this->Reports->find('all', $pagedParams)->innerJoin(
145 1
                array('incidents' => $subquery), array('incidents.report_id = Reports.id')
146
            )
147
        );
148
        //$rows = Sanitize::clean($rows);
149 1
        $totalFiltered = $this->Reports->find('all', $params)->count();
150
151
        // change exception_type from boolean values to strings
152
        // add incident count for related reports
153 1
        $dispRows = array();
154 1
        foreach ($rows as $row) {
155 1
            $row[5] = $this->Reports->status[$row[5]];
156 1
            $row[6] = (intval($row[6])) ? ('php') : ('js');
157
            $input_elem = "<input type='checkbox' name='reports[]' value='"
158 1
                . $row[0]
159 1
                . "'/>";
160
161
            $subquery_params_count = array(
162 1
                'fields' => array(
163
                    'report_id' => 'report_id',
164
                ),
165
            );
166 1
            $subquery_count = TableRegistry::get('incidents')->find(
167 1
                'all', $subquery_params_count
168
            );
169
170
            $params_count = array(
171 1
                'fields' => array('inci_count' => 'inci_count'),
172
                'conditions' => array(
173 1
                        'related_to = ' . $row[0],
174
                ),
175
            );
176
177 1
            $inci_count_related = $this->Reports->find('all', $params_count)->innerJoin(
178 1
                array('incidents' => $subquery_count),
179 1
                array('incidents.report_id = Reports.related_to')
180 1
            )->count();
181
182 1
            $row[7] += $inci_count_related;
183
184 1
            array_unshift($row, $input_elem);
185 1
            array_push($dispRows, $row);
186
        }
187
188
        $response = array(
189 1
            'iTotalRecords' => $this->Reports->find('all')->count(),
190 1
            'iTotalDisplayRecords' => $totalFiltered,
191 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...
192 1
            'aaData' => $dispRows
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 View Code Duplication
        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