From 3bfafc857f521dba2c672507ddca7698de1328a7 Mon Sep 17 00:00:00 2001 From: Jellyfrog Date: Thu, 15 Oct 2020 02:40:44 +0200 Subject: [PATCH] Convert Device>vlan view to Laravel (#12163) * Convert Device>vlan view to Laravel --- .../Device/Tabs/VlansController.php | 34 +++++++- app/Models/PortVlan.php | 11 +++ includes/html/pages/device/vlans.inc.php | 82 ------------------- includes/html/print-vlan.inc.php | 61 -------------- resources/views/device/tabs/vlans.blade.php | 46 +++++++++++ 5 files changed, 90 insertions(+), 144 deletions(-) delete mode 100644 includes/html/pages/device/vlans.inc.php delete mode 100644 includes/html/print-vlan.inc.php create mode 100644 resources/views/device/tabs/vlans.blade.php diff --git a/app/Http/Controllers/Device/Tabs/VlansController.php b/app/Http/Controllers/Device/Tabs/VlansController.php index 2a3071f16b..fd84da46a6 100644 --- a/app/Http/Controllers/Device/Tabs/VlansController.php +++ b/app/Http/Controllers/Device/Tabs/VlansController.php @@ -25,6 +25,7 @@ namespace App\Http\Controllers\Device\Tabs; use App\Models\Device; +use App\Models\PortVlan; use LibreNMS\Interfaces\UI\DeviceTab; class VlansController implements DeviceTab @@ -51,6 +52,37 @@ class VlansController implements DeviceTab public function data(Device $device): array { - return []; + return [ + 'vlans' => self::getVlans($device), + 'submenu' => [ + [ + ['name' => 'Basic', 'url' => ''], + ], + 'Graphs' => [ + ['name' => 'Bits', 'url' => 'bits'], + ['name' => 'Unicast Packets', 'url' => 'upkts'], + ['name' => 'Non-Unicast Packets', 'url' => 'nupkts'], + ['name' => 'Errors', 'url' => 'errors'], + ], + ], + ]; + } + + private static function getVlans(Device $device) + { + // port.device needed to prevent loading device multiple times + $portVlan = PortVlan::where('ports_vlans.device_id', $device->device_id) + ->join('vlans', function ($join) { + $join + ->on('ports_vlans.vlan', 'vlans.vlan_vlan') + ->on('vlans.device_id', 'ports_vlans.device_id'); + }) + ->with(['port.device']) + ->select('ports_vlans.*', 'vlans.vlan_name') + ->get(); + + $data = $portVlan->groupBy('vlan'); + + return $data; } } diff --git a/app/Models/PortVlan.php b/app/Models/PortVlan.php index 8b59a61362..316388e47e 100644 --- a/app/Models/PortVlan.php +++ b/app/Models/PortVlan.php @@ -7,4 +7,15 @@ class PortVlan extends PortRelatedModel protected $table = 'ports_vlans'; protected $primaryKey = 'port_vlan_id'; public $timestamps = false; + + public function getUntaggedAttribute($value) + { + if (! $value) { + if ($this->vlan == $this->port->ifVlan) { + $value = 1; + } + } + + return $value; + } } diff --git a/includes/html/pages/device/vlans.inc.php b/includes/html/pages/device/vlans.inc.php deleted file mode 100644 index b9e44bfb97..0000000000 --- a/includes/html/pages/device/vlans.inc.php +++ /dev/null @@ -1,82 +0,0 @@ - 'device', - 'device' => $device['device_id'], - 'tab' => 'vlans', -]; - -print_optionbar_start(); - -echo "VLANs » "; - -if ($vars['view'] == 'graphs' || $vars['view'] == 'minigraphs') { - if (isset($vars['graph'])) { - $graph_type = 'port_' . $vars['graph']; - } else { - $graph_type = 'port_bits'; - } -} - -if (! $vars['view']) { - $vars['view'] = 'basic'; -} - -$menu_options['basic'] = 'Basic'; -// $menu_options['details'] = 'Details'; -$sep = ''; -foreach ($menu_options as $option => $text) { - echo $sep; - if ($vars['view'] == $option) { - echo ""; - } - - echo generate_link($text, $link_array, ['view' => $option]); - if ($vars['view'] == $option) { - echo ''; - } - - $sep = ' | '; -} - -unset($sep); - -echo ' | Graphs: '; - -$graph_types = [ - 'bits' => 'Bits', - 'upkts' => 'Unicast Packets', - 'nupkts' => 'Non-Unicast Packets', - 'errors' => 'Errors', -]; - -foreach ($graph_types as $type => $descr) { - echo "$type_sep"; - if ($vars['graph'] == $type && $vars['view'] == 'graphs') { - echo ""; - } - - echo generate_link($descr, $link_array, ['view' => 'graphs', 'graph' => $type]); - if ($vars['graph'] == $type && $vars['view'] == 'graphs') { - echo ''; - } - - /* - echo(' ('); - if ($vars['graph'] == $type && $vars['type'] == "minigraphs") { echo(""); } - echo(generate_link('Mini',$link_array,array('type'=>'minigraphs','graph'=>$type))); - if ($vars['graph'] == $type && $vars['type'] == "minigraphs") { echo(""); } - echo(')'); - */ - $type_sep = ' | '; -} - -print_optionbar_end(); - -echo ''; - -include 'includes/html/print-vlan.inc.php'; - -echo '
'; - -$pagetitle[] = 'VLANs'; diff --git a/includes/html/print-vlan.inc.php b/includes/html/print-vlan.inc.php deleted file mode 100644 index 89c371621e..0000000000 --- a/includes/html/print-vlan.inc.php +++ /dev/null @@ -1,61 +0,0 @@ - $v) { - if ($k != 'vlan_vlan') { - $vlans_data[$vlan['vlan_vlan']][$k] = $v; - } - } -} - -$otherports = dbFetchRows('SELECT * FROM `ports_vlans` AS V, `ports` AS P WHERE V.`device_id` = ? AND P.port_id = V.port_id', [$device['device_id']]); -foreach ($otherports as $n => $otherport) { - if (! $otherport['untagged']) { - if ($otherport['ifvlan'] == $otherport['vlan']) { - $otherport['untagged'] = 1; - } - } - $vlan_ports[$otherport['vlan']][$otherport['ifIndex']] = $otherport; -} - -ksort($vlan_ports); - -$i = 0; -foreach ($vlan_ports as $vlan => $ports) { - $bg_colour = \LibreNMS\Config::get('list_colour.odd'); - if (! is_integer($i / 2)) { - $bg_colour = \LibreNMS\Config::get('list_colour.even'); - } - - echo ""; - echo ' Vlan ' . $vlan . ''; - echo '' . $vlans_data[$vlan]['vlan_name'] . ''; - echo ''; - - foreach ($ports as $port_id => $port) { - $port = cleanPort($port, $device); - if ($vars['view'] == 'graphs') { - echo "
-
" . makeshortif($port['ifDescr']) . "
- " . $device['hostname'] . ' - ' . $port['ifDescr'] . '
\ - ' . display($port['ifAlias']) . " \ - \ - ', CENTER, LEFT, FGCOLOR, '#e5e5e5', BGCOLOR, '#e5e5e5', WIDTH, 400, HEIGHT, 150);\" onmouseout=\"return nd();\" >" . " - -
" . substr(short_port_descr($port['ifAlias']), 0, 22) . '
- '; - } else { - echo $vlans_data[$vlan]['port_sep'] . generate_port_link($port, makeshortif($port['label'])); - $vlans_data[$vlan]['port_sep'] = ', '; - if ($port['untagged']) { - echo '(U)'; - } - } - } - echo ''; - $i++; -} diff --git a/resources/views/device/tabs/vlans.blade.php b/resources/views/device/tabs/vlans.blade.php new file mode 100644 index 0000000000..d64ef0c94b --- /dev/null +++ b/resources/views/device/tabs/vlans.blade.php @@ -0,0 +1,46 @@ +@extends('device.submenu') + +@section('tabcontent') + + + + + + + + + + + @foreach($data['vlans'] as $vlan_number => $vlans) + + + + + + @endforeach + +
@lang('VLAN Number')@lang('VLAN Name')@lang('Ports')
{{ $vlan_number }}{{ $vlans->first()->vlan_name }} + @foreach($vlans as $port) + @if(!$vars) +@portLink($port->port, $port->port->getShortLabel()) +@if($port->untagged) +(U)@endif +@if(!$loop->last), +@endif + + @else +
+ +
{{ $port->port->ifDescr }}
+ + + +
{{ $port->port->ifAlias }}
+
+ @endif + @endforeach +
+@endsection + + +