Completed
Pull Request — develop (#99)
by Neil
18:43
created

AlertsDataTable::getColumns()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.9713
cc 1
eloc 15
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * app/DataTables/AlertsDataTable.php
4
 *
5
 * Datatable for alerts
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Neil Lathwood
23
 * @author     Neil Lathwood <[email protected]>
24
 */
25
26
namespace App\DataTables;
27
28
use App\Models\Alerting\Alert;
29
use Yajra\Datatables\Services\DataTable;
30
31
class AlertsDataTable extends DataTable
32
{
33
    /**
34
     * Display ajax response.
35
     *
36
     * @return \Illuminate\Http\JsonResponse
37
     */
38
    public function ajax()
39
    {
40
        return $this->datatables
41
            ->eloquent($this->query())
42
            ->editColumn('id', '#{{ $id }}')
43
            ->editColumn('state', '@if ($state == 0)
44
                                        <div class="label label-success">SUCCESS</div>
45
                                    @elseif ($state == 1)
46
                                        <div class="label label-danger">FAILED</div>
47
                                    @elseif ($state == 2)
48
                                        <div class="label label-warning">MUTED</div>
49
                                    @else
50
                                        <div class="label label-primary">UNKNOWN</div>
51
                                    @endif')
52
            ->editColumn('rule.name', function($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { r...e']['name'] . '</a>'; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
                return '<a href="'.url("alerting/rules/".$this['rule']['id']).'">'.$this['rule']['name'].'</a>';
54
            })
55
            ->editColumn('device.hostname', function($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { r...'hostname'] . '</a>'; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
                return '<a href="'.url("devices/".$this['device']['device_id']).'">'.$this['device']['hostname'].'</a>';
57
            })
58
            ->addColumn('actions', function($this) {
0 ignored issues
show
Documentation introduced by
function ($this) { i.... ' fa-fw"></i></a>'; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
                if ($this['state'] == 2) {
60
                    $btn  = "btn-danger";
61
                    $icon = "volume-off";
62
                }
63
                else {
64
                    $btn  = "btn-success";
65
                    $icon = "volume-up";
66
                }
67
                return '<a id="alerts-ack" data-id="'.$this['id'].'" data-state="'.$this['state'].'" class="btn btn-xs '.$btn.'"><i class="fa fa-'.$icon.' fa-fw"></i></a>';
68
            })
69
            ->make(true);
70
    }
71
72
    /**
73
     * Get the query object to be processed by datatables.
74
     *
75
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
76
     */
77
    public function query()
78
    {
79
        $alerts = Alert::query()->where('state', '!=', '0')->with('device')->with('user')->with('rule')->select('alerts.*');
80
        return $this->applyScopes($alerts);
81
    }
82
83
    /**
84
     * Optional method if you want to use html builder.
85
     *
86
     * @return \Yajra\Datatables\Html\Builder
87
     */
88 1
    public function html()
89
    {
90 1
        return $this->builder()
91 1
                    ->columns($this->getColumns())
92 1
                    ->parameters($this->getBuilderParameters());
93
    }
94
95
    /**
96
     * Get columns.
97
     *
98
     * @return array
99
     */
100 1
    private function getColumns()
101
    {
102
        return [
103
            'id'        => [
104 1
                'title' => trans('alerting.alerts.text.id'),
105 1
            ],
106
            'state'     => [
107 1
                'title' => trans('alerting.general.text.state'),
108 1
            ],
109
            'rule.name' => [
110 1
                'title' => trans('alerting.general.text.rule'),
111 1
            ],
112
            'device.hostname' => [
113 1
                'title'       => trans('devices.label.hostname'),
114 1
            ],
115 1
            'timestamp',
116
            'rule.severity' => [
117 1
                'title'     => trans('alerting.alerts.text.severity'),
118 1
            ],
119
            'actions' => [
120 1
                'className' =>'alerts-ack',
121 1
            ],
122 1
        ];
123
    }
124
125
    /**
126
     * Get filename for export.
127
     *
128
     * @return string
129
     */
130
    protected function filename()
131
    {
132
        return 'alerts';
133
    }
134
135
    /**
136
     * Get Builder Params
137
     *
138
     * @return array
139
     */
140 1
    protected function getBuilderParameters()
141
    {
142
        return [
143 1
            'dom' => 'Blfrtip',
144 1
            'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]],
145
            'buttons' => [
146 1
                'csv', 'excel', 'pdf', 'print', 'reset', 'reload',
147 1
            ],
148 1
        ];
149
    }
150
151
}
152