Completed
Push — develop ( 6757aa...8285e3 )
by Tony
12s
created

DeviceDataTable   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 2
cbo 4
dl 0
loc 139
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B ajax() 0 38 3
A query() 0 5 1
A html() 0 6 1
B getColumns() 0 36 1
A filename() 0 4 1
A getBuilderParameters() 0 11 1
1
<?php
2
/**
3
 * app/DataTables/DeviceDataTable.php
4
 *
5
 * Datatable for devices
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\Device;
29
use Yajra\Datatables\Services\DataTable;
30
31
class DeviceDataTable 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('status_reason', function($device) {
43
                if ($device->status == 0)
44
                {
45
                    return '<span data-toggle="tooltip" title="down" class="badge bg-red">'.$device->status_reason.'</span>';
46
                }
47
                else
48
                {
49
                    return '<span data-toggle="tooltip" title="up" class="badge bg-light-blue">up</span>';
50
                }
51
            })
52
            ->editColumn('vendor', function($device) {
53
                return '<img src="'.$device->logo().'" border="0" alt="'.$device->os.'">';
54
            })
55
            ->editColumn('hostname', function($device) {
56
                $hostname = is_null($device) ? trans('devices.text.deleted') : $device->hostname;
57
                return '<a href="'.url("devices/".$device->device_id).'">'.$hostname.'</a>';
58
            })
59
            ->editColumn('resources', function($device) {
60
                $ports   = $device->ports()->count();
61
                $sensors = $device->sensors()->count();
62
                return '<span data-toggle="tooltip" title="'.$ports.' Ports" class="badge bg-light-blue"><i class="fa fa-link"></i>&nbsp; '.$ports.'</span><br />
63
                        <span data-toggle="tooltip" title="'.$sensors.' Sensors" class="badge bg-light-blue"><i class="fa fa-dashboard"></i>&nbsp; '.$sensors.'</span>';
64
            })
65
            ->editColumn('hardware', function($device) {
66
                return $device->hardware.'<br />'.$device->features;
67
            })
68
            ->editColumn('os', function($device) {
69
                return ucfirst($device->os).'<br />'.$device->version;
70
            })
71
            ->editColumn('location', function($device) {
72
                return $device->formatUptime($device->uptime).'<br />'.$device->location;
73
            })
74
            ->make(true);
75
    }
76
77
    /**
78
     * Get the query object to be processed by datatables.
79
     *
80
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
81
     */
82
    public function query()
83
    {
84
        $device = Device::select('devices.*');
85
        return $this->applyScopes($device);
86
    }
87
88
    /**
89
     * Optional method if you want to use html builder.
90
     *
91
     * @return \Yajra\Datatables\Html\Builder
92
     */
93
    public function html()
94
    {
95
        return $this->builder()
96
                    ->columns($this->getColumns())
97
                    ->parameters($this->getBuilderParameters());
98
    }
99
100
    /**
101
     * Get columns.
102
     *
103
     * @return array
104
     */
105
    private function getColumns()
106
    {
107
        return [
108
            'status'    => [
109
                'title' => trans('general.text.status'),
110
                'data'  => 'status_reason',
111
                'width' => '40px',
112
            ],
113
            'vendor'        => [
114
                'title' => trans('devices.text.vendor'),
115
                'width' => '20px',
116
            ],
117
            'hostname'  => [
118
                'title' => trans('devices.label.hostname'),
119
            ],
120
            'resources'  => [
121
                'title'  => '',
122
                'search' => false,
123
            ],
124
            'hardware'  => [
125
                'title' => trans('devices.text.platform'),
126
            ],
127
            'features'    => [
128
                'visible' => false,
129
            ],
130
            'os'        => [
131
                'title' => trans('devices.text.os'),
132
            ],
133
            'version'     => [
134
                'visible' => false,
135
            ],
136
            'location'  => [
137
                'title' => trans('devices.text.uptime_location'),
138
            ],
139
        ];
140
    }
141
142
    /**
143
     * Get filename for export.
144
     *
145
     * @return string
146
     */
147
    protected function filename()
148
    {
149
        return 'devices';
150
    }
151
152
    /**
153
     * Get Builder Params
154
     *
155
     * @return array
156
     */
157
    protected function getBuilderParameters()
158
    {
159
        return [
160
            'dom' => 'Blfrtip',
161
            'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]],
162
            'buttons' => [
163
                'csv', 'excel', 'pdf', 'print', 'reset', 'reload',
164
            ],
165
            'autoWidth' => false,
166
        ];
167
    }
168
169
}
170