Completed
Push — master ( f9dc97...9ded53 )
by Deven
03:22
created

ReportsController::unmark_related_to()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 13
cp 0.8462
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 1
crap 3.0327
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 (!isset($reportId) || !$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
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 1
    public function change_state($reportId)
248
    {
249 1
        if (!$reportId) {
250
            throw new NotFoundException(__('Invalid Report'));
251
        }
252
253 1
        $report = $this->Reports->get($reportId);
254 1
        if (!$report) {
255
            throw new NotFoundException(__('Invalid Report'));
256
        }
257
258 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...
259 1
        $newState = null;
260
261 1
        if (array_key_exists($state, $this->Reports->status)) {
262 1
            $newState = $this->Reports->status[$state];
263
        }
264 1
        if (!$newState) {
265 1
            throw new NotFoundException(__('Invalid State'));
266
        }
267 1
        $report->status = $state;
268 1
        $this->Reports->save($report);
269
270 1
        $flash_class = 'alert alert-success';
271 1
        $this->Flash->default('The state has been successfully changed.',
272 1
            array('params' => array('class' => $flash_class)));
273 1
        $this->redirect("/reports/view/$reportId");
274 1
    }
275
276
    /**
277
     * To carry out mass actions on Reports
278
     * Currently only to change their statuses.
279
     * Can be Extended for other mass operations as well.
280
     * Expects an array of Report Ids as a POST parameter.
281
     */
282 1
    public function mass_action()
283
    {
284 1
        $flash_class = 'alert alert-error';
285 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...
286 1
        $newState = null;
287 1
        if (array_key_exists($state, $this->Reports->status)) {
288 1
            $newState = $this->Reports->status[$state];
289
        }
290
291 1
        if (!$newState) {
292 1
            Log::write(
293 1
                'error',
294 1
                'ERRORED: Invalid param "state" in ReportsController::mass_action()',
295 1
                'alert'
296
            );
297 1
            $msg = 'ERROR: Invalid State!!';
298 1
        } 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...
299 1
            $msg = 'No Reports Selected!! Please Select Reports and try again.';
300
        } else {
301
            $msg = "Status has been changed to '"
302 1
                . $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
                . "' for selected Reports!";
304 1
            $flash_class = 'alert alert-success';
305 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...
306 1
                $report = $this->Reports->get($report_id);
307 1
                if (!$report) {
308
                    Log::write(
309
                        'error',
310
                        'ERRORED: Invalid report_id in ReportsController::mass_action()',
311
                        'alert'
312
                    );
313
                    $msg = 'ERROR:Invalid Report ID:' . $report_id;
314
                    $flash_class = 'alert alert-error';
315
                    break;
316
                }
317 1
                $report->status = $state;
318 1
                $this->Reports->save($report);
319
            }
320
        }
321
322 1
        $this->Flash->default($msg,
323 1
            array('params' => array('class' => $flash_class)));
324 1
        $this->redirect('/reports/');
325 1
    }
326
327
    //# HELPERS
328
329 1
    protected function _setSimilarFields($id)
330
    {
331 1
        $this->Reports->id = $id;
332
333 1
        $this->set('columns', TableRegistry::get('Incidents')->summarizableFields);
334 1
        $relatedEntries = array();
335
336 1 View Code Duplication
        foreach (TableRegistry::get('Incidents')->summarizableFields as $field) {
337
            list($entriesWithCount, $totalEntries) =
338 1
                    $this->Reports->getRelatedByField($field, 25, true);
339 1
            $relatedEntries[$field] = $entriesWithCount;
340 1
            $this->set("${field}_distinct_count", $totalEntries);
341
        }
342
        //error_log(json_encode($relatedEntries));
343
        $this->set('related_entries', $relatedEntries);
344
    }
345
346
    protected function _findArrayList($results, $key)
347
    {
348 1
        $output = array();
349 1
        foreach ($results as $row) {
350 1
            $output[] = $row[$key];
351
        }
352
353 1
        return $output;
354
    }
355
356
    protected function _findAllDataTable($results)
357
    {
358 1
        $output = array();
359 1
        foreach ($results as $row) {
360 1
            $output_row = array();
361 1
            $row = $row->toArray();
362 1
            foreach ($row as $key => $value) {
363 1
                $output_row[] = $value;
364
            }
365 1
            $output[] = $output_row;
366
        }
367
368 1
        return $output;
369
    }
370
}
371