From 094c536eb475b02a26f65769d6ff460d270ef454 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 6 Dec 2019 21:40:51 +0100 Subject: Allow comments to be entered while still in transit --- lib/Travelynx.pm | 25 ++++++++++++++- lib/Travelynx/Controller/Traveling.pm | 45 ++++++++++++++++++++++++++ templates/_checked_in.html.ep | 21 +++++++++++-- templates/edit_comment.html.ep | 59 +++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 templates/edit_comment.html.ep diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 8a308db..c0ddf83 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -777,6 +777,25 @@ sub startup { } ); + $self->helper( + 'update_in_transit_comment' => sub { + my ( $self, $comment ) = @_; + my $uid = $self->current_user->{id}; + + my $status = $self->pg->db->select( 'in_transit', ['user_data'], + { user_id => $uid } )->expand->hash; + if ( not $status ) { + return; + } + $status->{user_data}{comment} = $comment; + $self->pg->db->update( + 'in_transit', + { user_data => JSON->new->encode( $status->{user_data} ) }, + { user_id => $uid } + ); + } + ); + $self->helper( 'update_journey_part' => sub { my ( $self, $db, $journey_id, $key, $value ) = @_; @@ -2652,6 +2671,7 @@ sub startup { route_after => \@route_after, messages => $in_transit->{messages}, extra_data => $in_transit->{data}, + comment => $in_transit->{user_data}{comment}, }; my @parsed_messages; @@ -2818,7 +2838,7 @@ sub startup { order_by => { -desc => 'journey_id' }, limit => 1 } - )->hash; + )->expand->hash; if ($latest) { my $ts = $latest->{checkout_ts}; @@ -2843,6 +2863,7 @@ sub startup { arr_ds100 => $latest->{arr_ds100}, arr_name => $latest->{arr_name}, arr_platform => $latest->{arr_platform}, + comment => $latest->{user_data}{comment}, }; } @@ -3129,6 +3150,7 @@ sub startup { $authed_r->get('/history/:year')->to('traveling#yearly_history'); $authed_r->get('/history/:year/:month')->to('traveling#monthly_history'); $authed_r->get('/journey/add')->to('traveling#add_journey_form'); + $authed_r->get('/journey/comment')->to('traveling#comment_form'); $authed_r->get('/journey/:id')->to('traveling#journey_details'); $authed_r->get('/s/*station')->to('traveling#station'); $authed_r->get('/confirm_mail/:token')->to('account#confirm_mail'); @@ -3137,6 +3159,7 @@ sub startup { $authed_r->post('/account/insight')->to('account#insight'); $authed_r->post('/history/map')->to('traveling#map_history'); $authed_r->post('/journey/add')->to('traveling#add_journey_form'); + $authed_r->post('/journey/comment')->to('traveling#comment_form'); $authed_r->post('/journey/edit')->to('traveling#edit_journey'); $authed_r->post('/journey/passenger_rights/*filename') ->to('passengerrights#generate'); diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index c8ba024..f41d8df 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -673,6 +673,51 @@ sub journey_details { } +sub comment_form { + my ($self) = @_; + my $dep_ts = $self->param('dep_ts'); + my $status = $self->get_user_status; + + if ( not $status->{checked_in} ) { + $self->render( + 'edit_comment', + error => 'notfound', + journey => {} + ); + } + elsif ( not $dep_ts ) { + $self->param( dep_ts => $status->{sched_departure}->epoch ); + $self->param( comment => $status->{comment} ); + $self->render( + 'edit_comment', + error => undef, + journey => $status + ); + } + elsif ( $self->validation->csrf_protect->has_error('csrf_token') ) { + $self->render( + 'edit_comment', + error => undef, + journey => $status + ); + } + elsif ( $dep_ts != $status->{sched_departure}->epoch ) { + + # TODO find and update appropriate past journey (if it exists) + $self->param( comment => $status->{comment} ); + $self->render( + 'edit_comment', + error => undef, + journey => $status + ); + } + else { + $self->app->log->debug("set comment"); + $self->update_in_transit_comment( $self->param('comment') ); + $self->redirect_to('/'); + } +} + sub edit_journey { my ($self) = @_; my $journey_id = $self->param('journey_id'); diff --git a/templates/_checked_in.html.ep b/templates/_checked_in.html.ep index a493384..5631e12 100644 --- a/templates/_checked_in.html.ep +++ b/templates/_checked_in.html.ep @@ -3,6 +3,9 @@
Eingecheckt in <%= $journey->{train_type} %> <%= $journey->{train_no} %> + % if ($journey->{comment}) { +

<%= $journey->{comment} %>.

+ % }

- - undo Rückgängig - + % if ($journey->{arr_name}) { + + comment Kommentar + + % } + % else { + + undo Checkin Rückgängig + + % } % if (defined $journey->{arrival_countdown} and $journey->{arrival_countdown} <= 0) { .

+
% }
diff --git a/templates/edit_comment.html.ep b/templates/edit_comment.html.ep new file mode 100644 index 0000000..81353a2 --- /dev/null +++ b/templates/edit_comment.html.ep @@ -0,0 +1,59 @@ +

Zugfahrt kommentieren

+% if ($error or not $journey->{checked_in}) { +
+
+
+
+ Fehler +

Du bist gerade nicht eingecheckt. Vergangene Zugfahrten + kannst du über die Editierfunktion in der History + kommentieren.

+
+
+
+
+% } +% else { + %= form_for '/journey/comment' => (method => 'POST') => begin + %= csrf_field + %= hidden_field 'dep_ts' => param('dep_ts') +
+
+

+ Eintrag zu + <%= $journey->{train_type} %> <%= $journey->{train_no} %> + von + <%= $journey->{dep_name} %> + nach + <%= $journey->{arr_name} // 'irgendwo' %> + am + <%= $journey->{sched_departure}->strftime('%d.%m.%Y') %> +

+ % if (current_user()->{is_public} & 0x04) { +

+ Der hier eingetragene Text ist als Teil deines Nutzerstatus + öffentlich sichtbar. +

+ % } +
+
+
+
+ %= text_field 'comment' +
+
+
+ +
+ +
+
+ %= end +% } -- cgit v1.2.3