Completed
Push — master ( c9c188...858b2f )
by Deven
03:01
created

ReportsController::mass_action()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 40
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 40
ccs 0
cts 31
cp 0
rs 8.439
cc 5
eloc 32
nc 4
nop 0
crap 30
1
<?php
2
3
/* vim: set expandtab sw=4 ts=4 sts=4: */
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('distinct_versions',
46 1
            $this->_findArrayList($this->Reports->find()->select(array('pma_version'))->distinct(array('pma_version')), 'pma_version')
47
        );
48 1
        $this->set('distinct_error_names',
49 1
            $this->_findArrayList($this->Reports->find('all', array(
50 1
                'fields' => array('error_name'),
51
                'conditions' => array('error_name !=' => ''),
52 1
            ))->distinct(array('error_name')), 'error_name')
53
        );
54 1
        $this->set('statuses', $this->Reports->status);
55 1
        $this->autoRender = true;
56 1
    }
57
58 1
    public function view($reportId)
59
    {
60 1
        if (!$reportId) {
61
            throw new NotFoundException(__('Invalid Report'));
62
        }
63 1
        $report = $this->Reports->findById($reportId)->toArray();
64 1
        if (!$report) {
65 1
            throw new NotFoundException(__('Invalid Report'));
66
        }
67
68 1
        $this->set('report', $report);
69 1
        $this->set('project_name', Configure::read('GithubRepoPath'));
70 1
        $this->Reports->id = $reportId;
71 1
        $this->set('incidents', $this->Reports->getIncidents()->toArray());
72 1
        $this->set('incidents_with_description',
73 1
            $this->Reports->getIncidentsWithDescription());
74 1
        $this->set('incidents_with_stacktrace',
75 1
            $this->Reports->getIncidentsWithDifferentStacktrace());
76 1
        $this->set('related_reports', $this->Reports->getRelatedReports());
77 1
        $this->set('status', $this->Reports->status);
78 1
        $this->_setSimilarFields($reportId);
79
80
        // if there is an unread notification for this report, then mark it as read
81 1
        $current_developer = TableRegistry::get('Developers')->
82 1
                    findById($this->request->session()->read('Developer.id'))->all()->first();
83
        //$current_developer = Sanitize::clean($current_developer);
84 1
        if ($current_developer) {
85 1
            TableRegistry::get('Notifications')->deleteAll(
86 1
                array('developer_id' => $current_developer['Developer']['id'],
87 1
                    'report_id' => $reportId,
88
                ),
89 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...
90
            );
91
        }
92 1
    }
93
94 1
    public function data_tables()
95
    {
96
        $subquery_params = array(
97 1
            'fields' => array(
98
                'report_id' => 'report_id',
99
                'inci_count' => 'COUNT(id)',
100
                ),
101
            'group' => 'report_id',
102
        );
103 1
        $subquery = TableRegistry::get('incidents')->find('all', $subquery_params);
104
105
        // override automatic aliasing, for proper usage in joins
106
        $aColumns = array(
107 1
            'id' => 'id',
108
            'error_name' => 'error_name',
109
            'error_message' => 'error_message',
110
            'pma_version' => 'pma_version',
111
            'status' => 'status',
112
            'exception_type' => 'exception_type',
113
            'inci_count' => 'inci_count',
114
        );
115
116 1
        $searchConditions = $this->OrderSearch->getSearchConditions($aColumns);
117 1
        $orderConditions = $this->OrderSearch->getOrder($aColumns);
118
119
        $params = array(
120 1
            'fields' => $aColumns,
121
            'conditions' => array(
122 1
                    $searchConditions,
123 1
                    'related_to is NULL',
124
                ),
125 1
            'order' => $orderConditions,
126
        );
127
128 1
        $pagedParams = $params;
129 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...
130 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...
131
132 1
        $rows = $this->_findAllDataTable(
133 1
            $this->Reports->find('all', $pagedParams)->innerJoin(
134 1
                array('incidents' => $subquery), array('incidents.report_id = Reports.id')
135
            )
136
        );
137
        //$rows = Sanitize::clean($rows);
138 1
        $totalFiltered = $this->Reports->find('all', $params)->count();
139
140
        // change exception_type from boolean values to strings
141
        // add incident count for related reports
142 1
        $dispRows = array();
143 1
        foreach ($rows as $row) {
144 1
            $row[4] = $this->Reports->status[$row[4]];
145 1
            $row[5] = (intval($row[5])) ? ('php') : ('js');
146
            $input_elem = "<input type='checkbox' name='reports[]' value='"
147 1
                . $row[0]
148 1
                . "'/>";
149
150
            $subquery_params_count = array(
151 1
                'fields' => array(
152
                    'report_id' => 'report_id',
153
                ),
154
            );
155 1
            $subquery_count = TableRegistry::get('incidents')->find(
156 1
                'all', $subquery_params_count
157
            );
158
159
            $params_count = array(
160 1
                'fields' => array('inci_count' => 'inci_count'),
161
                'conditions' => array(
162 1
                        'related_to = ' . $row[0],
163
                ),
164
            );
165
166 1
            $inci_count_related = $this->Reports->find('all', $params_count)->innerJoin(
167 1
                array('incidents' => $subquery_count),
168 1
                array('incidents.report_id = Reports.related_to')
169 1
            )->count();
170
171 1
            $row[6] += $inci_count_related;
172
173 1
            array_unshift($row, $input_elem);
174 1
            array_push($dispRows, $row);
175
        }
176
177
        $response = array(
178 1
            'iTotalRecords' => $this->Reports->find('all')->count(),
179 1
            'iTotalDisplayRecords' => $totalFiltered,
180 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...
181 1
            'aaData' => $dispRows,
182
        );
183 1
        $this->autoRender = false;
184 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...
185
186 1
        return $this->response;
187
    }
188
189 1
    public function mark_related_to($reportId)
190
    {
191
        // Only allow POST requests
192 1
        $this->request->allowMethod(['post']);
193
194 1
        $relatedTo = $this->request->getData('related_to');
195 1
        if (!$reportId
196 1
            || !$relatedTo
197 1
            || $reportId == $relatedTo
198
        ) {
199
            throw new NotFoundException(__('Invalid Report'));
200
        }
201
202 1
        $report = $this->Reports->get($reportId);
203 1
        if (!$report) {
204
            throw new NotFoundException(__('Invalid Report'));
205
        }
206
207 1
        $this->Reports->addToRelatedGroup($report, $relatedTo);
208
209 1
        $flash_class = 'alert alert-success';
210 1
        $this->Flash->default('This report has been marked the same as #'
211 1
                . $relatedTo,
212 1
                array('params' => array('class' => $flash_class)));
213 1
        $this->redirect("/reports/view/$reportId");
214 1
    }
215
216 1
    public function unmark_related_to($reportId)
217
    {
218
        // Only allow POST requests
219 1
        $this->request->allowMethod(['post']);
220
221 1
        if (!$reportId) {
222
            throw new NotFoundException(__('Invalid Report'));
223
        }
224
225 1
        $report = $this->Reports->get($reportId);
226 1
        if (!$report) {
227
            throw new NotFoundException(__('Invalid Report'));
228
        }
229
230 1
        $this->Reports->removeFromRelatedGroup($report);
231
232 1
        $flash_class = 'alert alert-success';
233 1
        $this->Flash->default('This report has been marked as different.',
234 1
            array('params' => array('class' => $flash_class)));
235 1
        $this->redirect("/reports/view/$reportId");
236 1
    }
237
238
    public function change_state($reportId)
239
    {
240
        if (!$reportId) {
241
            throw new NotFoundException(__('Invalid Report'));
242
        }
243
244
        $report = $this->Reports->get($reportId);
245
        if (!$report) {
246
            throw new NotFoundException(__('Invalid Report'));
247
        }
248
249
        $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...
250
        $newState = $this->Reports->status[$state];
251
        if (!$newState) {
252
            throw new NotFoundException(__('Invalid State'));
253
        }
254
        $report->status = $state;
255
        $this->Reports->save($report);
256
257
        $flash_class = 'alert alert-success';
258
        $this->Flash->default('The state has been successfully changed.',
259
            array('params' => array('class' => $flash_class)));
260
        $this->redirect("/reports/view/$reportId");
261
    }
262
263
    /**
264
     * To carry out mass actions on Reports
265
     * Currently only to change their statuses.
266
     * Can be Extended for other mass operations as well.
267
     * Expects an array of Report Ids as a POST parameter.
268
     */
269
    public function mass_action()
270
    {
271
        $flash_class = 'alert alert-error';
272
        $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...
273
        $newState = $this->Reports->status[$state];
274
        if (!$newState) {
275
            Log::write(
276
                'error',
277
                'ERRORED: Invalid param "state" in ReportsController::mass_action()',
278
                'alert'
279
            );
280
            $msg = 'ERROR: Invalid State!!';
281
        } 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...
282
            $msg = 'No Reports Selected!! Please Select Reports and try again.';
283
        } else {
284
            $msg = "Status has been changed to '"
285
                . $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
                . "' for selected Reports!";
287
            $flash_class = 'alert alert-success';
288
            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...
289
                $report = $this->Reports->get($report_id);
290
                if (!$report) {
291
                    Log::write(
292
                        'error',
293
                        'ERRORED: Invalid report_id in ReportsController::mass_action()',
294
                        'alert'
295
                    );
296
                    $msg = 'ERROR:Invalid Report ID:' . $report_id;
297
                    $flash_class = 'alert alert-error';
298
                    break;
299
                }
300
                $report->status = $state;
301
                $this->Reports->save($report);
302
            }
303
        }
304
305
        $this->Flash->default($msg,
306
            array('params' => array('class' => $flash_class)));
307
        $this->redirect('/reports/');
308
    }
309
310
    //# HELPERS
311
312 1
    protected function _setSimilarFields($id)
313
    {
314 1
        $this->Reports->id = $id;
315
316 1
        $this->set('columns', TableRegistry::get('Incidents')->summarizableFields);
317 1
        $relatedEntries = array();
318
319 1
        foreach (TableRegistry::get('Incidents')->summarizableFields as $field) {
320
            list($entriesWithCount, $totalEntries) =
321 1
                    $this->Reports->getRelatedByField($field, 25, true);
322 1
            $relatedEntries[$field] = $entriesWithCount;
323 1
            $this->set("${field}_distinct_count", $totalEntries);
324
        }
325
        //error_log(json_encode($relatedEntries));
326
        $this->set('related_entries', $relatedEntries);
327
    }
328
329
    protected function _findArrayList($results, $key)
330
    {
331 1
        $output = array();
332 1
        foreach ($results as $row) {
333 1
            $output[] = $row[$key];
334
        }
335
336 1
        return $output;
337
    }
338
339
    protected function _findAllDataTable($results)
340
    {
341 1
        $output = array();
342 1
        foreach ($results as $row) {
343 1
            $output_row = array();
344 1
            $row = $row->toArray();
345 1
            foreach ($row as $key => $value) {
346 1
                $output_row[] = $value;
347
            }
348 1
            $output[] = $output_row;
349
        }
350
351 1
        return $output;
352
    }
353
}
354