| 1 | package App::Netdisco::Web::GenericReport; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC; | ||
| 5 | use Dancer::Plugin::Auth::Extensible; | ||
| 6 | |||
| 7 | use App::Netdisco::Web::Plugin; | ||
| 8 | use App::Netdisco::Util::Web 'omit_empty_columns'; | ||
| 9 | use Path::Class 'file'; | ||
| 10 | use Storable 'dclone'; | ||
| 11 | use Safe; | ||
| 12 | |||
| 13 | use HTML::Entities 'encode_entities'; | ||
| 14 | use Regexp::Common qw( RE_net_IPv4 RE_net_IPv6 RE_net_MAC RE_net_domain ); | ||
| 15 | use Regexp::Common::net::CIDR (); | ||
| 16 | |||
| 17 | our ($config, @data); | ||
| 18 | |||
| 19 | foreach my $report (@{setting('reports')}) { | ||
| 20 | my $r = $report->{tag}; | ||
| 21 | |||
| 22 | register_report({ | ||
| 23 | tag => $r, | ||
| 24 | label => $report->{label}, | ||
| 25 | category => ($report->{category} || 'My Reports'), | ||
| 26 | ($report->{hidden} ? (hidden => true) : ()), | ||
| 27 | provides_csv => true, | ||
| 28 | api_endpoint => true, | ||
| 29 | bind_params => [ map {ref $_ ? $_->{param} : $_} @{ $report->{bind_params} } ], | ||
| 30 | api_parameters => $report->{api_parameters}, | ||
| 31 | }); | ||
| 32 | |||
| 33 | get "/ajax/content/report/$r" => require_login sub { | ||
| 34 | # TODO: this should be done by creating a new Virtual Result class on | ||
| 35 | # the fly (package...) and then calling DBIC register_class on it. | ||
| 36 | |||
| 37 | my $schema = ($report->{database} || vars->{'tenant'}); | ||
| 38 | my $rs = schema($schema)->resultset('Virtual::GenericReport')->result_source; | ||
| 39 | (my $query = $report->{query}) =~ s/;$//; | ||
| 40 | |||
| 41 | # unpick the rather hairy config of 'columns' to get field, | ||
| 42 | # displayname, and "_"-prefixed options | ||
| 43 | my %column_config = (); | ||
| 44 | my @column_order = (); | ||
| 45 | foreach my $col (@{ $report->{columns} }) { | ||
| 46 | foreach my $k (keys %$col) { | ||
| 47 | if ($k !~ m/^_/) { | ||
| 48 | push @column_order, $k; | ||
| 49 | $column_config{$k} = dclone($col || {}); | ||
| 50 | $column_config{$k}->{displayname} = delete $column_config{$k}->{$k}; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | $rs->view_definition($query); | ||
| 56 | $rs->remove_columns($rs->columns); | ||
| 57 | $rs->add_columns( exists $report->{query_columns} | ||
| 58 | ? @{ $report->{query_columns} } : @column_order | ||
| 59 | ); | ||
| 60 | |||
| 61 | my $set = schema($schema)->resultset('Virtual::GenericReport') | ||
| 62 | ->search(undef, { | ||
| 63 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', | ||
| 64 | ( (exists $report->{bind_params}) | ||
| 65 | ? (bind => [map { param($_) } map {ref $_ ? $_->{param} : $_} @{ $report->{bind_params} }]) : () ), | ||
| 66 | }); | ||
| 67 | @data = $set->all; | ||
| 68 | |||
| 69 | # Data Munging support... | ||
| 70 | |||
| 71 | my $compartment = Safe->new; | ||
| 72 | $config = $report; # closure for the config of this report | ||
| 73 | $compartment->share(qw/$config @data/); | ||
| 74 | $compartment->permit_only(qw/:default sort/); | ||
| 75 | |||
| 76 | my $munger = file(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'site_plugins', $r)->stringify; | ||
| 77 | my @results = ((-f $munger) ? $compartment->rdo( $munger ) : @data); | ||
| 78 | return if $@ or (0 == scalar @results); | ||
| 79 | @column_order = omit_empty_columns(\@results, \@column_order, \%column_config); | ||
| 80 | |||
| 81 | if (request->is_ajax) { | ||
| 82 | # searchable field support.. | ||
| 83 | |||
| 84 | my $recidr4 = $RE{net}{CIDR}{IPv4}{-keep}; #RE_net_CIDR_IPv4(-keep); | ||
| 85 | my $rev4 = RE_net_IPv4(-keep); | ||
| 86 | my $rev6 = RE_net_IPv6(-keep); | ||
| 87 | my $remac = RE_net_MAC(-keep); | ||
| 88 | |||
| 89 | foreach my $row (@results) { | ||
| 90 | foreach my $col (@column_order) { | ||
| 91 | next unless $column_config{$col}->{_searchable}; | ||
| 92 | my $fields = (ref $row->{$col} ? $row->{$col} : [$row->{$col}]); | ||
| 93 | |||
| 94 | foreach my $f (@$fields) { | ||
| 95 | |||
| 96 | encode_entities($f); | ||
| 97 | |||
| 98 | $f =~ s!\b${recidr4}\b!'<a href="'. | ||
| 99 | uri_for('/search', {q => "$1/$2"})->path_query | ||
| 100 | .qq{">$1/$2</a>}!gex; | ||
| 101 | |||
| 102 | if (not $1 and not $2) { | ||
| 103 | $f =~ s!\b${rev4}\b!'<a href="'. | ||
| 104 | uri_for('/search', {q => $1})->path_query .qq{">$1</a>}!gex; | ||
| 105 | } | ||
| 106 | |||
| 107 | $f =~ s!\b${rev6}\b!'<a href="'. | ||
| 108 | uri_for('/search', {q => $1})->path_query .qq{">$1</a>}!gex; | ||
| 109 | |||
| 110 | $f =~ s!\b${remac}\b!'<a href="'. | ||
| 111 | uri_for('/search', {q => $1})->path_query .qq{">$1</a>}!gex; | ||
| 112 | |||
| 113 | $row->{$col} = $f if not ref $row->{$col}; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | template 'ajax/report/generic_report.tt', | ||
| 119 | { results => \@results, | ||
| 120 | is_custom_report => true, | ||
| 121 | column_options => \%column_config, | ||
| 122 | headings => [map {$column_config{$_}->{displayname}} @column_order], | ||
| 123 | columns => [@column_order] }, { layout => 'noop' }; | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | header( 'Content-Type' => 'text/comma-separated-values' ); | ||
| 127 | template 'ajax/report/generic_report_csv.tt', | ||
| 128 | { results => \@results, | ||
| 129 | headings => [map {$column_config{$_}->{displayname}} @column_order], | ||
| 130 | columns => [@column_order] }, { layout => 'noop' }; | ||
| 131 | } | ||
| 132 | }; | ||
| 133 | } | ||
| 134 | |||
| 135 | true; |