Export events in XLSX

Fix #102
master
Daniel Berteaud 9 years ago
parent b8e1321af2
commit c18c02ead1
  1. 11
      public/js/vroom.js
  2. 6
      templates/default/admin_audit.html.ep
  3. 56
      vroom.pl

@ -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());
}
}
});

@ -22,10 +22,14 @@
<div class="input-group">
<input type="text" id="dateEnd" name="dateEnd" class="form-control date-picker" value="<%= DateTime->now->ymd %>"/>
<span class="input-group-btn">
<button type=submit" class="btn btn-primary">
<button id="events_refresh" class="btn btn-primary">
<span class="glyphicon glyphicon-play">
</span>
</button>
<button id="events_export" class="btn btn-default">
<span class="glyphicon glyphicon-save">
</span>
</button>
</span>
</div>
</div>

@ -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

Loading…
Cancel
Save