1package App::Netdisco::Web::Plugin::Report::PortSsid;
2
3use Dancer ':syntax';
4use Dancer::Plugin::DBIC;
5use Dancer::Plugin::Auth::Extensible;
6
7use App::Netdisco::Web::Plugin;
8
9register_report(
10    {   category     => 'Port',
11        tag          => 'portssid',
12        label        => 'Port SSID Inventory',
13        provides_csv => 1,
14        api_endpoint => 1,
15        api_parameters => [
16          ssid => {
17            description => 'Get details for this SSID',
18          },
19        ],
20    }
21);
22
23hook 'before_template' => sub {
24    my $tokens = shift;
25
26    return
27        unless (
28        request->path eq uri_for('/report/portssid')->path
29        or index(
30            request->path, uri_for('/ajax/content/report/portssid')->path
31        ) == 0
32        );
33
34    # used in the search sidebar template to set selected items
35    foreach my $opt (qw/ssid/) {
36        my $p = (
37            ref [] eq ref param($opt)
38            ? param($opt)
39            : ( param($opt) ? [ param($opt) ] : [] )
40        );
41        $tokens->{"${opt}_lkp"} = { map { $_ => 1 } @$p };
42    }
43};
44
45get '/ajax/content/report/portssid' => require_login sub {
46
47    my $ssid = param('ssid');
48
49    my $rs = schema('netdisco')->resultset('DevicePortSsid');
50
51    if ( defined $ssid ) {
52
53        $rs = $rs->search(
54            { ssid => $ssid },
55            {   '+columns' => [
56                    qw/ device.dns device.name device.model device.vendor port.port/
57                ],
58                join     => [qw/ device port /],
59                collapse => 1,
60            }
61        )->order_by( [qw/ port.ip port.port /] )->hri;
62    }
63    else {
64        $rs = $rs->get_ssids->hri;
65
66    }
67
68    my @results = $rs->all;
69    return unless scalar @results;
70
71    if ( request->is_ajax ) {
72        my $json = to_json( \@results );
73        template 'ajax/report/portssid.tt',
74            { results => $json, opt => $ssid };
75    }
76    else {
77        header( 'Content-Type' => 'text/comma-separated-values' );
78        template 'ajax/report/portssid_csv.tt',
79            { results => \@results, opt => $ssid };
80    }
81};
82
831;
84