|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* app/DataTables/General/SearchDataTable.php |
|
4
|
|
|
* |
|
5
|
|
|
* Datatable for search |
|
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\IPv4; |
|
29
|
|
|
use App\Models\General\IPv4Mac; |
|
30
|
|
|
use App\Models\General\IPv6; |
|
31
|
|
|
use App\Models\Port; |
|
32
|
|
|
use Yajra\Datatables\Services\DataTable; |
|
33
|
|
|
|
|
34
|
|
|
class SearchDataTable extends DataTable |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
protected $type; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Display ajax response. |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
43
|
|
|
*/ |
|
44
|
|
|
public function ajax() |
|
45
|
|
|
{ |
|
46
|
|
|
if ($this->type !== "arp") |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->datatables |
|
49
|
|
|
->eloquent($this->query()) |
|
50
|
|
|
->editColumn('hostname', function($data) { |
|
|
|
|
|
|
51
|
|
|
$hostname = is_null($data->device) ? trans('devices.text.deleted') : $data->device->hostname; |
|
52
|
|
|
return '<a href="'.url("devices/".$data->device_id).'">'.$hostname.'</a>'; |
|
53
|
|
|
}) |
|
54
|
|
View Code Duplication |
->editColumn('ifName', function($data) { |
|
|
|
|
|
|
55
|
|
|
$ifName = is_null($data->ifName) ? trans('devices.text.deleted') : $data->ifName; |
|
56
|
|
|
return '<a href="'.url("devices/".$data->device_id."/ports/".$data->port_id).'">'.$ifName.'</a>'; |
|
57
|
|
|
}) |
|
58
|
|
|
->make(true); |
|
59
|
|
|
} |
|
60
|
|
|
else { |
|
61
|
|
|
return $this->datatables |
|
62
|
|
|
->eloquent($this->query()) |
|
63
|
|
|
->editColumn('hostname', function($data) { |
|
|
|
|
|
|
64
|
|
|
$hostname = is_null($data->device) ? trans('devices.text.deleted') : $data->device->hostname; |
|
65
|
|
|
return '<a href="'.url("devices/".$data->device_id).'">'.$hostname.'</a>'; |
|
66
|
|
|
}) |
|
67
|
|
View Code Duplication |
->editColumn('ifName', function($data) { |
|
|
|
|
|
|
68
|
|
|
$ifName = is_null($data->ifName) ? trans('devices.text.deleted') : $data->ifName; |
|
69
|
|
|
return '<a href="'.url("devices/".$data->device_id."/ports/".$data->port_id).'">'.$ifName.'</a>'; |
|
70
|
|
|
}) |
|
71
|
|
|
->addColumn('remote_device', function($data) { |
|
|
|
|
|
|
72
|
|
|
$remote_device = IPv4::where('ipv4_addresses.ipv4_address', $data->ipv4_address)->with('port.device')->first(); |
|
73
|
|
|
$remote_id = empty($remote_device->port->device->device_id) ? '' : $remote_device->port->device->device_id; |
|
74
|
|
|
$remote_hostname = empty($remote_device->port->device->hostname) ? trans('devices.text.deleted') : $remote_device->port->device->hostname; |
|
75
|
|
|
return '<a href="'.url("devices/".$remote_id).'">'.$remote_hostname.'</a>'; |
|
76
|
|
|
}) |
|
77
|
|
|
->addColumn('remote_interface', function($data) { |
|
|
|
|
|
|
78
|
|
|
$remote_device = IPv4::where('ipv4_addresses.ipv4_address', $data->ipv4_address)->with('port.device')->first(); |
|
79
|
|
|
$remote_id = empty($remote_device->port->device->device_id) ? '' : $remote_device->port->device->device_id; |
|
80
|
|
|
$remote_port_id = empty($remote_device->port->port_id) ? '' : $remote_device->port->port_id; |
|
81
|
|
|
$remote_port = empty($remote_device->port->ifName) ? trans('devices.text.deleted') : $remote_device->port->ifName; |
|
82
|
|
|
return '<a href="'.url("devices/".$remote_id."/ports/".$remote_port_id).'">'.$remote_port.'</a>'; |
|
83
|
|
|
}) |
|
84
|
|
|
->make(true); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get the query object to be processed by datatables. |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
|
92
|
|
|
*/ |
|
93
|
|
|
public function query() |
|
94
|
|
|
{ |
|
95
|
|
|
if ($this->type === "ipv4") |
|
96
|
|
|
{ |
|
97
|
|
|
$data = IPv4::join('ports', 'ports.port_id', '=', 'ipv4_addresses.port_id')->join('devices', 'devices.device_id', '=', 'ports.device_id')->select('ipv4_addresses.*', 'ports.*', 'devices.*'); |
|
98
|
|
|
//FIXME We should use this once laravel-datatables supports it upstream $data = IPv4::with('port.device')->select('ipv4_addresses.*'); |
|
99
|
|
|
} |
|
100
|
|
View Code Duplication |
elseif ($this->type === "ipv6") |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
$data = IPv6::join('ports', 'ports.port_id', '=', 'ipv6_addresses.port_id')->join('devices', 'devices.device_id', '=', 'ports.device_id')->select('ipv6_addresses.*', 'ports.*', 'devices.*'); |
|
103
|
|
|
//FIXME We should use this once laravel-datatables supports it upstream $data = IPv6::with('port.device')->select('ipv6_addresses.*'); |
|
104
|
|
|
} |
|
105
|
|
|
elseif ($this->type === "mac") |
|
106
|
|
|
{ |
|
107
|
|
|
$data = Port::join('devices', 'devices.device_id', '=', 'ports.device_id')->select('ports.*', 'devices.*'); |
|
108
|
|
|
//FIXME This is valid but stops us generalising this file so until the nested queries above are fixed then we default to joins |
|
109
|
|
|
//$data = Port::with('device')->select('ports.*'); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
View Code Duplication |
elseif ($this->type === "arp") |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
$data = IPv4Mac::join('ports', 'ports.port_id', '=', 'ipv4_mac.port_id')->join('devices', 'devices.device_id', '=', 'ports.device_id')->select('ipv4_mac.*', 'ports.*', 'devices.*'); |
|
114
|
|
|
//FIXME We should use this once laravel-datatables supports it upstream $data = IPv4Mac::with('port.device')->select('ipv4_mac.*'); |
|
115
|
|
|
} |
|
116
|
|
|
return $this->applyScopes($data); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Optional method if you want to use html builder. |
|
121
|
|
|
* |
|
122
|
|
|
* @return \Yajra\Datatables\Html\Builder |
|
123
|
|
|
*/ |
|
124
|
|
|
public function html() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->builder() |
|
127
|
|
|
->columns($this->getColumns()) |
|
128
|
|
|
->parameters($this->getBuilderParameters()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get columns. |
|
133
|
|
|
* |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
private function getColumns() |
|
137
|
|
|
{ |
|
138
|
|
|
$cols = [ |
|
139
|
|
|
'hostname' => [ |
|
140
|
|
|
'title' => trans('devices.label.hostname'), |
|
141
|
|
|
], |
|
142
|
|
|
'ifName' => [ |
|
143
|
|
|
'title' => trans('general.text.interface'), |
|
144
|
|
|
], |
|
145
|
|
|
]; |
|
146
|
|
|
if ($this->type === "ipv4") |
|
147
|
|
|
{ |
|
148
|
|
|
$cols = array_merge($cols, [ |
|
149
|
|
|
'ipv4_address' => [ |
|
150
|
|
|
'title' => trans('general.text.address'), |
|
151
|
|
|
], |
|
152
|
|
|
]); |
|
153
|
|
|
} |
|
154
|
|
|
if ($this->type === "ipv6") |
|
155
|
|
|
{ |
|
156
|
|
|
$cols = array_merge($cols, [ |
|
157
|
|
|
'ipv6_address' => [ |
|
158
|
|
|
'title' => trans('general.text.address'), |
|
159
|
|
|
], |
|
160
|
|
|
]); |
|
161
|
|
|
} |
|
162
|
|
|
if ($this->type === "mac") |
|
163
|
|
|
{ |
|
164
|
|
|
$cols = array_merge($cols, [ |
|
165
|
|
|
'ifPhysAddress' => [ |
|
166
|
|
|
'title' => trans('general.text.mac_address'), |
|
167
|
|
|
], |
|
168
|
|
|
]); |
|
169
|
|
|
} |
|
170
|
|
|
if ($this->type === "arp") |
|
171
|
|
|
{ |
|
172
|
|
|
$cols = array_merge($cols, [ |
|
173
|
|
|
'mac_address' => [ |
|
174
|
|
|
'title' => trans('general.text.mac_address'), |
|
175
|
|
|
], |
|
176
|
|
|
'ipv4_address' => [ |
|
177
|
|
|
'title' => trans('general.text.address'), |
|
178
|
|
|
], |
|
179
|
|
|
'remote_device' => [ |
|
180
|
|
|
'searchable' => false, |
|
181
|
|
|
], |
|
182
|
|
|
'remote_interface' => [ |
|
183
|
|
|
'searchable' => false, |
|
184
|
|
|
] |
|
185
|
|
|
]); |
|
186
|
|
|
} |
|
187
|
|
|
if ($this->type !== "arp") |
|
188
|
|
|
{ |
|
189
|
|
|
$cols = array_merge($cols, [ |
|
190
|
|
|
'ifDescr' => [ |
|
191
|
|
|
'title' => trans('general.text.port_descr'), |
|
192
|
|
|
], |
|
193
|
|
|
]); |
|
194
|
|
|
} |
|
195
|
|
|
return $cols; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Get filename for export. |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function filename() |
|
204
|
|
|
{ |
|
205
|
|
|
return 'search'; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Get Builder Params |
|
210
|
|
|
* |
|
211
|
|
|
* @return array |
|
212
|
|
|
*/ |
|
213
|
|
|
protected function getBuilderParameters() |
|
214
|
|
|
{ |
|
215
|
|
|
return [ |
|
|
|
|
|
|
216
|
|
|
'dom' => "<'row'<'col-sm-3'l><'col-sm-6 text-center'B><'col-sm-3'f>>". |
|
217
|
|
|
"<'row'<'col-sm-12'tr>>". |
|
218
|
|
|
"<'row'<'col-sm-5'i><'col-sm-7'p>>", |
|
219
|
|
|
'lengthMenu' => [[25, 50, 100, -1], [25, 50, 100, "All"]], |
|
220
|
|
|
'buttons' => [ |
|
221
|
|
|
'csv', 'excel', 'pdf', 'print', 'reset', 'reload', |
|
222
|
|
|
], |
|
223
|
|
|
]; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @param string $type |
|
229
|
|
|
*/ |
|
230
|
|
|
public function forType($type) |
|
231
|
|
|
{ |
|
232
|
|
|
$this->type = $type; |
|
233
|
|
|
return $this; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
|
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: