diff --git a/public/js/vroom.js b/public/js/vroom.js index 4cb8b86..f5eba44 100644 --- a/public/js/vroom.js +++ b/public/js/vroom.js @@ -817,7 +817,7 @@ function initAdminAudit(){ } // Intercept form submission - $('#eventSearch').submit(function(e){ + $('#events_refresh,#events_export').click(function(e){ e.preventDefault(); var startObj = new Date($('#dateStart').val()); var endObj = new Date($('#dateEnd').val()); @@ -843,8 +843,13 @@ function initAdminAudit(){ return false; } else{ - reloadEvents($('#dateStart').val(),$('#dateEnd').val()); - return; + if ($(this).attr('id') === 'events_refresh'){ + reloadEvents($('#dateStart').val(),$('#dateEnd').val()); + return; + } + else if ($(this).attr('id') === 'events_export'){ + window.location.assign('export_events?from=' + $('#dateStart').val() + '&to=' + $('#dateEnd').val()); + } } }); diff --git a/templates/default/admin_audit.html.ep b/templates/default/admin_audit.html.ep index 1086883..9a5a670 100644 --- a/templates/default/admin_audit.html.ep +++ b/templates/default/admin_audit.html.ep @@ -22,10 +22,14 @@
- +
diff --git a/vroom.pl b/vroom.pl index fc42559..dee1176 100755 --- a/vroom.pl +++ b/vroom.pl @@ -9,6 +9,7 @@ use Mojolicious::Lite; use Mojolicious::Plugin::Mail; use Mojolicious::Plugin::Database; use Mojolicious::Plugin::StaticCompressor; +use Mojolicious::Plugin::RenderFile; use Vroom::Constants; use Vroom::Conf; use Crypt::SaltedHash; @@ -24,6 +25,8 @@ use File::Path qw(make_path); use File::Basename; use DateTime; use Array::Diff; +use File::Temp; +use Excel::Writer::XLSX; use Data::Dumper; app->log->level('info'); @@ -98,6 +101,9 @@ plugin StaticCompressor => { disable_on_devmode => 1 }; +# Stream files +plugin 'RenderFile'; + ########################## # Validation helpers # ########################## @@ -1173,6 +1179,36 @@ helper get_room_conf => sub { }; }; +# Export events in XLSX +helper export_events_xlsx => sub { + my $self = shift; + my ($from,$to) = @_; + my $tmp = File::Temp->new( DIR => $config->{'directories.cache'}, SUFFIX => '.xlsx' )->filename; + my $events = $self->get_event_list($from, $to); + if (!$events){ + return 0; + } + my $xlsx = Excel::Writer::XLSX->new($tmp); + my $sheet = $xlsx->add_worksheet; + my @headers = qw(id date from_ip user event message); + # Write header + $sheet->write(0, 0, \@headers); + my $row = 1; + foreach my $e (sort {$a <=> $b } keys %$events){ + my @details = ( + $events->{$e}->{id}, + $events->{$e}->{date}, + $events->{$e}->{from_ip}, + $events->{$e}->{user}, + $events->{$e}->{event}, + $events->{$e}->{message} + ); + $sheet->write($row, 0, \@details); + $row++; + } + return $tmp; +}; + # Socket.IO handshake get '/socket.io/:ver' => sub { my $self = shift; @@ -2258,6 +2294,26 @@ group { my $self = shift; return $self->render('admin_audit'); }; + + get '/export_events' => sub { + my $self = shift; + my $from = $self->param('from') || DateTime->now->ymd; + my $to = $self->param('to') || DateTime->now->ymd; + my $file = $self->export_events_xlsx($from,$to); + if (!$file || !-e $file){ + return $self->render('error', + msg => $self->l('ERROR_EXPORT_XLSX'), + err => 'ERROR_EXPORT_XLSX', + room => '' + ); + } + $self->render_file( + filepath => $file, + filename => 'events.xlsx', + cleanup => 1, + format => 'vnd.openxmlformats-officedocument.spreadsheetml.sheet' + ); + }; }; # Catch all route: if nothing else match, it's the name of a room