|
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\DataTables\BaseDataTable; |
|
29
|
|
|
use App\Models\General\IPv4; |
|
30
|
|
|
use App\Models\General\IPv4Mac; |
|
31
|
|
|
use App\Models\General\IPv6; |
|
32
|
|
|
use App\Models\Port; |
|
33
|
|
|
|
|
34
|
|
|
class SearchDataTable extends BaseDataTable |
|
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
|
|
|
->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
|
|
|
->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
|
|
|
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
|
|
|
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
|
|
|
* Get columns. |
|
121
|
|
|
* |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getColumns() |
|
125
|
|
|
{ |
|
126
|
|
|
$cols = [ |
|
127
|
|
|
'hostname' => [ |
|
128
|
|
|
'title' => trans('devices.label.hostname'), |
|
129
|
|
|
], |
|
130
|
|
|
'ifName' => [ |
|
131
|
|
|
'title' => trans('general.text.interface'), |
|
132
|
|
|
], |
|
133
|
|
|
]; |
|
134
|
|
|
if ($this->type === "ipv4") |
|
135
|
|
|
{ |
|
136
|
|
|
$cols = array_merge($cols, [ |
|
137
|
|
|
'ipv4_address' => [ |
|
138
|
|
|
'title' => trans('general.text.address'), |
|
139
|
|
|
], |
|
140
|
|
|
]); |
|
141
|
|
|
} |
|
142
|
|
|
if ($this->type === "ipv6") |
|
143
|
|
|
{ |
|
144
|
|
|
$cols = array_merge($cols, [ |
|
145
|
|
|
'ipv6_address' => [ |
|
146
|
|
|
'title' => trans('general.text.address'), |
|
147
|
|
|
], |
|
148
|
|
|
]); |
|
149
|
|
|
} |
|
150
|
|
|
if ($this->type === "mac") |
|
151
|
|
|
{ |
|
152
|
|
|
$cols = array_merge($cols, [ |
|
153
|
|
|
'ifPhysAddress' => [ |
|
154
|
|
|
'title' => trans('general.text.mac_address'), |
|
155
|
|
|
], |
|
156
|
|
|
]); |
|
157
|
|
|
} |
|
158
|
|
|
if ($this->type === "arp") |
|
159
|
|
|
{ |
|
160
|
|
|
$cols = array_merge($cols, [ |
|
161
|
|
|
'mac_address' => [ |
|
162
|
|
|
'title' => trans('general.text.mac_address'), |
|
163
|
|
|
], |
|
164
|
|
|
'ipv4_address' => [ |
|
165
|
|
|
'title' => trans('general.text.address'), |
|
166
|
|
|
], |
|
167
|
|
|
'remote_device' => [ |
|
168
|
|
|
'searchable' => false, |
|
169
|
|
|
], |
|
170
|
|
|
'remote_interface' => [ |
|
171
|
|
|
'searchable' => false, |
|
172
|
|
|
] |
|
173
|
|
|
]); |
|
174
|
|
|
} |
|
175
|
|
|
if ($this->type !== "arp") |
|
176
|
|
|
{ |
|
177
|
|
|
$cols = array_merge($cols, [ |
|
178
|
|
|
'ifDescr' => [ |
|
179
|
|
|
'title' => trans('general.text.port_descr'), |
|
180
|
|
|
], |
|
181
|
|
|
]); |
|
182
|
|
|
} |
|
183
|
|
|
return $cols; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get filename for export. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function filename() |
|
192
|
|
|
{ |
|
193
|
|
|
return 'search'; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param string $type |
|
199
|
|
|
*/ |
|
200
|
|
|
public function forType($type) |
|
201
|
|
|
{ |
|
202
|
|
|
$this->type = $type; |
|
203
|
|
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.