|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* app/DataTables/General/InventoryDataTable.php |
|
4
|
|
|
* |
|
5
|
|
|
* Datatable for inventory |
|
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\General; |
|
27
|
|
|
|
|
28
|
|
|
use App\Models\General\Inventory; |
|
29
|
|
|
use Yajra\Datatables\Services\DataTable; |
|
30
|
|
|
|
|
31
|
|
View Code Duplication |
class InventoryDataTable 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('device.hostname', function($inventory) { |
|
|
|
|
|
|
43
|
|
|
return '<a href="'.url("devices/".$inventory->device->device_id).'">'.$inventory->device->hostname.'</a>'; |
|
44
|
|
|
}) |
|
45
|
|
|
->make(true); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the query object to be processed by datatables. |
|
50
|
|
|
* |
|
51
|
|
|
* @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
|
52
|
|
|
*/ |
|
53
|
|
|
public function query() |
|
54
|
|
|
{ |
|
55
|
|
|
$inventory = Inventory::with('device')->select('entPhysical.*'); |
|
56
|
|
|
return $this->applyScopes($inventory); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Optional method if you want to use html builder. |
|
61
|
|
|
* |
|
62
|
|
|
* @return \Yajra\Datatables\Html\Builder |
|
63
|
|
|
*/ |
|
64
|
|
|
public function html() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->builder() |
|
67
|
|
|
->columns($this->getColumns()) |
|
68
|
|
|
->parameters($this->getBuilderParameters()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get columns. |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
private function getColumns() |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
'device.hostname' => [ |
|
80
|
|
|
'title' => trans('devices.label.hostname'), |
|
81
|
|
|
], |
|
82
|
|
|
'entPhysicalDescr' => [ |
|
83
|
|
|
'title' => trans('general.text.description'), |
|
84
|
|
|
], |
|
85
|
|
|
'entPhysicalName' => [ |
|
86
|
|
|
'title' => trans('general.text.name'), |
|
87
|
|
|
], |
|
88
|
|
|
'entPhysicalModelName' => [ |
|
89
|
|
|
'title' => trans('general.text.model'), |
|
90
|
|
|
], |
|
91
|
|
|
'entPhysicalSerialNum' => [ |
|
92
|
|
|
'title' => trans('general.text.serial'), |
|
93
|
|
|
], |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get filename for export. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function filename() |
|
103
|
|
|
{ |
|
104
|
|
|
return 'inventory'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get Builder Params |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getBuilderParameters() |
|
113
|
|
|
{ |
|
114
|
|
|
return [ |
|
|
|
|
|
|
115
|
|
|
'dom' => 'Blfrtip', |
|
116
|
|
|
'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]], |
|
117
|
|
|
'buttons' => [ |
|
118
|
|
|
'csv', 'excel', 'pdf', 'print', 'reset', 'reload', |
|
119
|
|
|
], |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.