Completed
Pull Request — master (#181)
by
unknown
13:57
created

ReportsController::mass_action()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 6.3141

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 27
cts 34
cp 0.7941
rs 8.5559
c 0
b 0
f 0
cc 6
nc 8
nop 0
crap 6.3141
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 1
                $this->Reports->find()->select(array('status'))->distinct(array('status')),
45 1
            'status'
46
            )
47
        );
48 1
        $this->set(
49 1
            'distinct_locations',
50 1
            $this->_findArrayList(
51 1
                $this->Reports->find()->select(array('location'))
52 1
                    ->distinct(array('location')),
53 1
                'location'
54
            )
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 1
            'distinct_error_names',
62 1
            $this->_findArrayList($this->Reports->find('all', array(
63 1
                'fields' => array('error_name'),
64
                'conditions' => array('error_name !=' => ''),
65 1
            ))->distinct(array('error_name')), 'error_name')
66
        );
67 1
        $this->set('statuses', $this->Reports->status);
68 1
        $this->autoRender = true;
69 1
    }
70
71 1
    public function view($reportId)
72
    {
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
        }
80
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 1
            $this->Reports->getIncidentsWithDescription()
88
        );
89 1
        $this->set(
90 1
            'incidents_with_stacktrace',
91 1
            $this->Reports->getIncidentsWithDifferentStacktrace()
92
        );
93 1
        $this->set('related_reports', $this->Reports->getRelatedReports());
94 1
        $this->set('status', $this->Reports->status);
95 1
        $this->_setSimilarFields($reportId);
96
97
        // if there is an unread notification for this report, then mark it as read
98 1
        $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 1
                    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
101 1
        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 1
                array('developer_id' => $current_developer['Developer']['id'],
104 1
                    'report_id' => $reportId,
105
                ),
106 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...
107
            );
108
        }
109 1
    }
110
111 1
    public function data_tables()
112
    {
113
        $subquery_params = array(
114 1
            'fields' => array(
115
                'report_id' => 'report_id',
116
                'inci_count' => 'COUNT(id)',
117
                ),
118
            'group' => 'report_id',
119
        );
120 1
        $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 1
            'id' => 'id',
125
            'error_name' => 'error_name',
126
            'error_message' => 'error_message',
127
            'location' => 'location',
128
            'pma_version' => 'pma_version',
129
            'status' => 'status',
130
            'exception_type' => 'exception_type',
131
            'inci_count' => 'inci_count',
132
        );
133
134 1
        $searchConditions = $this->OrderSearch->getSearchConditions($aColumns);
135 1
        $orderConditions = $this->OrderSearch->getOrder($aColumns);
136
137
        $params = array(
138 1
            'fields' => $aColumns,
139
            'conditions' => array(
140 1
                    $searchConditions,
141 1
                    'related_to is NULL',
142
                ),
143 1
            'order' => $orderConditions,
144
        );
145
146 1
        $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 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...
149
150 1
        $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
            )
155
        );
156
        //$rows = Sanitize::clean($rows);
157 1
        $totalFiltered = $this->Reports->find('all', $params)->count();
158
159
        // change exception_type from boolean values to strings
160
        // add incident count for related reports
161 1
        $dispRows = array();
162 1
        foreach ($rows as $row) {
163 1
            $row[5] = $this->Reports->status[$row[5]];
164 1
            $row[6] = (intval($row[6])) ? ('php') : ('js');
165
            $input_elem = "<input type='checkbox' name='reports[]' value='"
166 1
                . $row[0]
167 1
                . "'/>";
168
169
            $subquery_params_count = array(
170 1
                'fields' => array(
171
                    'report_id' => 'report_id',
172
                ),
173
            );
174 1
            $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
            );
178
179
            $params_count = array(
180 1
                'fields' => array('inci_count' => 'inci_count'),
181
                'conditions' => array(
182 1
                        'related_to = ' . $row[0],
183
                ),
184
            );
185
186 1
            $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
191 1
            $row[7] += $inci_count_related;
192
193 1
            array_unshift($row, $input_elem);
194 1
            array_push($dispRows, $row);
195
        }
196
197
        $response = array(
198 1
            'iTotalRecords' => $this->Reports->find('all')->count(),
199 1
            'iTotalDisplayRecords' => $totalFiltered,
200 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...
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
206 1
        return $this->response;
207
    }
208
209 1
    public function mark_related_to($reportId)
210
    {
211
        // Only allow POST requests
212 1
        $this->request->allowMethod(['post']);
213
214 1
        $relatedTo = $this->request->getData('related_to');
215 1
        if (!$reportId
216 1
            || !$relatedTo
217 1
            || $reportId == $relatedTo
218
        ) {
219
            throw new NotFoundException(__('Invalid Report'));
220
        }
221
222 1
        $report = $this->Reports->get($reportId);
223 1
        if (!$report) {
224
            throw new NotFoundException(__('Invalid Report'));
225
        }
226
227 1
        $this->Reports->addToRelatedGroup($report, $relatedTo);
228
229 1
        $flash_class = 'alert alert-success';
230 1
        $this->Flash->default(
231
            'This report has been marked the same as #'
232 1
                . $relatedTo,
233 1
                array('params' => array('class' => $flash_class))
234
        );
235 1
        $this->redirect("/reports/view/$reportId");
236 1
    }
237
238 1
    public function unmark_related_to($reportId)
239
    {
240
        // Only allow POST requests
241 1
        $this->request->allowMethod(['post']);
242
243 1
        if (!$reportId) {
244
            throw new NotFoundException(__('Invalid Report'));
245
        }
246
247 1
        $report = $this->Reports->get($reportId);
248 1
        if (!$report) {
249
            throw new NotFoundException(__('Invalid Report'));
250
        }
251
252 1
        $this->Reports->removeFromRelatedGroup($report);
253
254 1
        $flash_class = 'alert alert-success';
255 1
        $this->Flash->default(
256 1
            'This report has been marked as different.',
257 1
            array('params' => array('class' => $flash_class))
258
        );
259 1
        $this->redirect("/reports/view/$reportId");
260 1
    }
261
262 1
    public function change_state($reportId)
263
    {
264 1
        if (!$reportId) {
265
            throw new NotFoundException(__('Invalid Report'));
266
        }
267
268 1
        $report = $this->Reports->get($reportId);
269 1
        if (!$report) {
270
            throw new NotFoundException(__('Invalid Report'));
271
        }
272
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 1
        if (array_key_exists($state, $this->Reports->status)) {
277 1
            $newState = $this->Reports->status[$state];
278
        }
279 1
        if (!$newState) {
280 1
            throw new NotFoundException(__('Invalid State'));
281
        }
282 1
        $report->status = $state;
283 1
        $this->Reports->save($report);
284
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 1
        $this->redirect("/reports/view/$reportId");
291 1
    }
292
293
    /**
294
     * To carry out mass actions on Reports
295
     * Currently only to change their statuses.
296
     * Can be Extended for other mass operations as well.
297
     * Expects an array of Report Ids as a POST parameter.
298
     */
299 1
    public function mass_action()
300
    {
301 1
        $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
        }
307
308 1
        if (!$newState) {
309 1
            Log::write(
310 1
                'error',
311 1
                'ERRORED: Invalid param "state" in ReportsController::mass_action()',
312 1
                'alert'
313
            );
314 1
            $msg = 'ERROR: Invalid State!!';
315 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...
316 1
            $msg = 'No Reports Selected!! Please Select Reports and try again.';
317
        } else {
318
            $msg = "Status has been changed to '"
319 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...
320 1
                . "' for selected Reports!";
321 1
            $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
                    Log::write(
326
                        'error',
327
                        'ERRORED: Invalid report_id in ReportsController::mass_action()',
328
                        'alert'
329
                    );
330
                    $msg = 'ERROR:Invalid Report ID:' . $report_id;
331
                    $flash_class = 'alert alert-error';
332
                    break;
333
                }
334 1
                $report->status = $state;
335 1
                $this->Reports->save($report);
336
            }
337
        }
338
339 1
        $this->Flash->default(
340 1
            $msg,
341 1
            array('params' => array('class' => $flash_class))
342
        );
343 1
        $this->redirect('/reports/');
344 1
    }
345
346
    //# HELPERS
347
348 1
    protected function _setSimilarFields($id)
349
    {
350 1
        $this->Reports->id = $id;
351
352 1
        $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 1 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 1
                    $this->Reports->getRelatedByField($field, 25, true);
358 1
            $relatedEntries[$field] = $entriesWithCount;
359 1
            $this->set("${field}_distinct_count", $totalEntries);
360
        }
361
        //error_log(json_encode($relatedEntries));
362
        $this->set('related_entries', $relatedEntries);
363
    }
364
365
    protected function _findArrayList($results, $key)
366
    {
367 1
        $output = array();
368 1
        foreach ($results as $row) {
369 1
            $output[] = $row[$key];
370
        }
371
372 1
        return $output;
373
    }
374
375
    protected function _findAllDataTable($results)
376
    {
377 1
        $output = array();
378 1
        foreach ($results as $row) {
379 1
            $output_row = array();
380 1
            $row = $row->toArray();
381 1
            foreach ($row as $key => $value) {
382 1
                $output_row[] = $value;
383
            }
384 1
            $output[] = $output_row;
385
        }
386
387 1
        return $output;
388
    }
389
}
390