From f5fd6d42e11bc8f6a5bd84a3653a40fa03ed5497 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 19 Apr 2020 18:26:20 +0200 Subject: Add CSV Export --- cpanfile | 1 + lib/Travelynx.pm | 2 ++ lib/Travelynx/Controller/Traveling.pm | 49 +++++++++++++++++++++++++++++++++++ templates/history.html.ep | 15 ++++++++--- 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/cpanfile b/cpanfile index 51c57cb..3a70126 100644 --- a/cpanfile +++ b/cpanfile @@ -11,6 +11,7 @@ requires 'MIME::Entity'; requires 'Mojolicious'; requires 'Mojolicious::Plugin::Authentication'; requires 'Mojo::Pg'; +requires 'Text::CSV'; requires 'Travel::Status::DE::DBWagenreihung'; requires 'Travel::Status::DE::IRIS'; requires 'UUID::Tiny'; diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 7495b76..285cfef 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -72,6 +72,7 @@ sub startup { $self->defaults( layout => 'default' ); + $self->types->type( csv => 'text/csv; charset=utf-8' ); $self->types->type( json => 'application/json; charset=utf-8' ); $self->plugin('Config'); @@ -4043,6 +4044,7 @@ sub startup { $authed_r->get('/account/mail')->to('account#change_mail'); $authed_r->get('/export.json')->to('account#json_export'); $authed_r->get('/history.json')->to('traveling#json_history'); + $authed_r->get('/history.csv')->to('traveling#csv_history'); $authed_r->get('/history')->to('traveling#history'); $authed_r->get('/history/commute')->to('traveling#commute'); $authed_r->get('/history/map')->to('traveling#map_history'); diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index ae5be69..702a89e 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -7,6 +7,7 @@ use JSON; use List::Util qw(uniq min max); use List::UtilsBy qw(max_by uniq_by); use List::MoreUtils qw(first_index); +use Text::CSV; use Travel::Status::DE::IRIS::Stations; sub homepage { @@ -644,6 +645,54 @@ sub json_history { $self->render( json => [ $self->get_user_travels ] ); } +sub csv_history { + my ($self) = @_; + + my $csv = Text::CSV->new( { eol => "\r\n" } ); + my $buf = q{}; + + $csv->combine( + qw(Zugtyp Linie Nummer Start Ziel), + 'Start (DS100)', + 'Ziel (DS100)', + 'Abfahrt (soll)', + 'Abfahrt (ist)', + 'Ankunft (soll)', + 'Ankunft (ist)', + 'Kommentar', + 'ID' + ); + $buf .= $csv->string; + + for my $journey ( $self->get_user_travels( with_datetime => 1 ) ) { + if ( + $csv->combine( + $journey->{type}, + $journey->{line}, + $journey->{no}, + $journey->{from_name}, + $journey->{to_name}, + $journey->{from_ds100}, + $journey->{to_ds100}, + $journey->{sched_departure}->strftime('%Y-%m-%d %H:%M'), + $journey->{rt_departure}->strftime('%Y-%m-%d %H:%M'), + $journey->{sched_arrival}->strftime('%Y-%m-%d %H:%M'), + $journey->{rt_arrival}->strftime('%Y-%m-%d %H:%M'), + $journey->{user_data}{comment} // q{}, + $journey->{id} + ) + ) + { + $buf .= $csv->string; + } + } + + $self->render( + text => $buf, + format => 'csv' + ); +} + sub yearly_history { my ($self) = @_; my $year = $self->stash('year'); diff --git a/templates/history.html.ep b/templates/history.html.ep index cb3d332..3751741 100644 --- a/templates/history.html.ep +++ b/templates/history.html.ep @@ -41,22 +41,29 @@

Ausfälle und Verspätungen

 

Rohdaten

 
+
+
+
 
+
 
+
-- cgit v1.2.3