Completed
Push — develop ( 4b5b5c...e668b0 )
by Tony
9s
created

ArpDataTable::ajax()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 5.3846
cc 8
eloc 21
nc 1
nop 0
1
<?php
2
/**
3
 * app/DataTables/General/ArpDataTable.php
4
 *
5
 * Datatable for arp 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
32
class ArpDataTable extends BaseDataTable
33
{
34
35
    /**
36
     * Display ajax response.
37
     *
38
     * @return \Illuminate\Http\JsonResponse
39
     */
40
    public function ajax()
41
    {
42
        return $this->datatables
43
            ->eloquent($this->query())
44
            ->editColumn('hostname', function($data) {
45
                $hostname = is_null($data->device) ? trans('devices.text.deleted') : $data->device->hostname;
46
                return '<a href="'.url("devices/".$data->device_id).'">'.$hostname.'</a>';
47
            })
48
            ->editColumn('ifName', function($data) {
49
                $ifName = is_null($data->ifName) ? trans('devices.text.deleted') : $data->ifName;
50
                return '<a href="'.url("devices/".$data->device_id."/ports/".$data->port_id).'">'.$ifName.'</a>';
51
            })
52
            ->addColumn('remote_device', function($data) {
53
                $remote_device = IPv4::where('ipv4_addresses.ipv4_address', $data->ipv4_address)->with('port.device')->first();
54
                $remote_id = empty($remote_device->port->device->device_id) ? '' : $remote_device->port->device->device_id;
55
                $remote_hostname = empty($remote_device->port->device->hostname) ? trans('devices.text.deleted') : $remote_device->port->device->hostname;
56
                return '<a href="'.url("devices/".$remote_id).'">'.$remote_hostname.'</a>';
57
            })
58
            ->addColumn('remote_interface', function($data) {
59
                $remote_device = IPv4::where('ipv4_addresses.ipv4_address', $data->ipv4_address)->with('port.device')->first();
60
                $remote_id = empty($remote_device->port->device->device_id) ? '' : $remote_device->port->device->device_id;
61
                $remote_port_id = empty($remote_device->port->port_id) ? '' : $remote_device->port->port_id;
62
                $remote_port = empty($remote_device->port->ifName) ? trans('devices.text.deleted') : $remote_device->port->ifName;
63
                return '<a href="'.url("devices/".$remote_id."/ports/".$remote_port_id).'">'.$remote_port.'</a>';
64
            })
65
            ->make(true);
66
    }
67
68
    /**
69
     * Get the query object to be processed by datatables.
70
     *
71
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
72
     */
73
    public function query()
74
    {
75
        $data = IPv4Mac::join('ports', 'ports.port_id', '=', 'ipv4_mac.port_id')->join('devices', 'devices.device_id', '=', 'ports.device_id')->select('ipv4_mac.*', 'ports.*', 'devices.*');
76
        //FIXME We should use this once laravel-datatables supports it upstream $data = IPv4Mac::with('port.device')->select('ipv4_mac.*');
77
        return $this->applyScopes($data);
78
    }
79
80
    /**
81
     * Get columns.
82
     *
83
     * @return array
84
     */
85
    public function getColumns()
86
    {
87
        return [
88
            'hostname'  => [
89
                'title' => trans('devices.label.hostname'),
90
            ],
91
            'ifName'    => [
92
                'title' => trans('general.text.interface'),
93
            ],
94
            'mac_address' => [
95
                'title'   => trans('general.text.mac_address'),
96
            ],
97
            'ipv4_address' => [
98
                'title'    => trans('general.text.address'),
99
            ],
100
            'remote_device'  => [
101
                'searchable' => false,
102
            ],
103
            'remote_interface' => [
104
                'searchable'   => false,
105
            ]
106
        ];
107
    }
108
109
    /**
110
     * Get filename for export.
111
     *
112
     * @return string
113
     */
114
    protected function filename()
115
    {
116
        return 'arp';
117
    }
118
119
}
120