2019-03-21 21:12:45 +00:00
|
|
|
|
package Travelynx::Controller::Traveling;
|
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
|
2019-03-27 20:20:59 +00:00
|
|
|
|
use DateTime;
|
2019-04-08 19:20:04 +00:00
|
|
|
|
use DateTime::Format::Strptime;
|
2019-07-20 13:42:11 +00:00
|
|
|
|
use List::UtilsBy qw(uniq_by);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
use Travel::Status::DE::IRIS::Stations;
|
|
|
|
|
|
|
|
|
|
sub homepage {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
if ( $self->is_user_authenticated ) {
|
2019-04-13 08:43:05 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'landingpage',
|
2019-04-30 16:18:09 +00:00
|
|
|
|
version => $self->app->config->{version} // 'UNKNOWN',
|
2019-04-13 08:43:05 +00:00
|
|
|
|
with_autocomplete => 1,
|
|
|
|
|
with_geolocation => 1
|
|
|
|
|
);
|
2019-04-30 10:47:32 +00:00
|
|
|
|
$self->mark_seen( $self->current_user->{id} );
|
2019-03-21 21:12:45 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-04-30 16:18:09 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'landingpage',
|
|
|
|
|
version => $self->app->config->{version} // 'UNKNOWN',
|
|
|
|
|
intro => 1
|
|
|
|
|
);
|
2019-03-21 21:12:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-30 21:23:49 +00:00
|
|
|
|
sub user_status {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
my $name = $self->stash('name');
|
2019-05-29 18:01:52 +00:00
|
|
|
|
my $ts = $self->stash('ts');
|
2019-04-30 21:23:49 +00:00
|
|
|
|
my $user = $self->get_privacy_by_name($name);
|
|
|
|
|
|
|
|
|
|
if ( $user and ( $user->{public_level} & 0x02 ) ) {
|
|
|
|
|
my $status = $self->get_user_status( $user->{id} );
|
2019-05-29 16:10:45 +00:00
|
|
|
|
|
|
|
|
|
my %tw_data = (
|
|
|
|
|
card => 'summary',
|
|
|
|
|
site => '@derfnull',
|
|
|
|
|
image => $self->url_for('/static/icons/icon-512x512.png')
|
|
|
|
|
->to_abs->scheme('https'),
|
|
|
|
|
);
|
|
|
|
|
|
2019-05-29 18:01:52 +00:00
|
|
|
|
if (
|
|
|
|
|
$ts
|
|
|
|
|
and ( not $status->{checked_in}
|
|
|
|
|
or $status->{sched_departure}->epoch != $ts )
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
$tw_data{title} = "Bahnfahrt beendet";
|
|
|
|
|
$tw_data{description} = "${name} hat das Ziel erreicht";
|
|
|
|
|
}
|
|
|
|
|
elsif ( $status->{checked_in} ) {
|
2019-05-29 16:10:45 +00:00
|
|
|
|
$tw_data{title} = "${name} ist unterwegs";
|
|
|
|
|
$tw_data{description} = sprintf(
|
|
|
|
|
'%s %s von %s nach %s',
|
|
|
|
|
$status->{train_type},
|
|
|
|
|
$status->{train_line} // $status->{train_no},
|
|
|
|
|
$status->{dep_name},
|
|
|
|
|
$status->{arr_name} // 'irgendwo'
|
|
|
|
|
);
|
|
|
|
|
if ( $status->{real_arrival}->epoch ) {
|
|
|
|
|
$tw_data{description} .= $status->{real_arrival}
|
|
|
|
|
->strftime(' – Ankunft gegen %H:%M Uhr');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$tw_data{title} = "${name} ist gerade nicht eingecheckt";
|
|
|
|
|
$tw_data{description} = "Letztes Fahrtziel: $status->{arr_name}";
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-30 21:23:49 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'user_status',
|
|
|
|
|
name => $name,
|
2019-05-29 16:10:45 +00:00
|
|
|
|
journey => $status,
|
|
|
|
|
twitter => \%tw_data,
|
2019-04-30 21:23:49 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render('not_found');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub public_status_card {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
my $name = $self->stash('name');
|
|
|
|
|
my $user = $self->get_privacy_by_name($name);
|
|
|
|
|
|
2019-05-02 08:05:15 +00:00
|
|
|
|
delete $self->stash->{layout};
|
|
|
|
|
|
2019-04-30 21:23:49 +00:00
|
|
|
|
if ( $user and ( $user->{public_level} & 0x02 ) ) {
|
|
|
|
|
my $status = $self->get_user_status( $user->{id} );
|
|
|
|
|
$self->render(
|
|
|
|
|
'_public_status_card',
|
|
|
|
|
name => $name,
|
|
|
|
|
journey => $status
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render('not_found');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 20:27:13 +00:00
|
|
|
|
sub status_card {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
my $status = $self->get_user_status;
|
|
|
|
|
|
|
|
|
|
delete $self->stash->{layout};
|
|
|
|
|
|
|
|
|
|
if ( $status->{checked_in} ) {
|
2019-04-24 07:00:30 +00:00
|
|
|
|
$self->render( '_checked_in', journey => $status );
|
2019-04-23 20:27:13 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-04-24 07:00:30 +00:00
|
|
|
|
$self->render( '_checked_out', journey => $status );
|
2019-04-23 20:27:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 21:12:45 +00:00
|
|
|
|
sub geolocation {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
my $lon = $self->param('lon');
|
|
|
|
|
my $lat = $self->param('lat');
|
|
|
|
|
|
|
|
|
|
if ( not $lon or not $lat ) {
|
|
|
|
|
$self->render( json => { error => 'Invalid lon/lat received' } );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
my @candidates = map {
|
|
|
|
|
{
|
|
|
|
|
ds100 => $_->[0][0],
|
|
|
|
|
name => $_->[0][1],
|
|
|
|
|
eva => $_->[0][2],
|
|
|
|
|
lon => $_->[0][3],
|
|
|
|
|
lat => $_->[0][4],
|
|
|
|
|
distance => $_->[1],
|
|
|
|
|
}
|
|
|
|
|
} Travel::Status::DE::IRIS::Stations::get_station_by_location( $lon,
|
2019-07-20 13:42:11 +00:00
|
|
|
|
$lat, 10 );
|
|
|
|
|
@candidates = uniq_by { $_->{name} } @candidates;
|
2019-10-19 14:47:33 +00:00
|
|
|
|
if ( @candidates > 5 ) {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
candidates => [ @candidates[ 0 .. 4 ] ],
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
candidates => [@candidates],
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub log_action {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
my $params = $self->req->json;
|
|
|
|
|
|
|
|
|
|
if ( not exists $params->{action} ) {
|
|
|
|
|
$params = $self->req->params->to_hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( not $self->is_user_authenticated ) {
|
|
|
|
|
|
|
|
|
|
# We deliberately do not set the HTTP status for these replies, as it
|
|
|
|
|
# confuses jquery.
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
success => 0,
|
|
|
|
|
error => 'Session error, please login again',
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( not $params->{action} ) {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
success => 0,
|
|
|
|
|
error => 'Missing action value',
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $station = $params->{station};
|
|
|
|
|
|
|
|
|
|
if ( $params->{action} eq 'checkin' ) {
|
|
|
|
|
|
|
|
|
|
my ( $train, $error )
|
|
|
|
|
= $self->checkin( $params->{station}, $params->{train} );
|
2019-05-19 08:32:57 +00:00
|
|
|
|
my $destination = $params->{dest};
|
2019-03-21 21:12:45 +00:00
|
|
|
|
|
|
|
|
|
if ($error) {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
success => 0,
|
|
|
|
|
error => $error,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-05-19 08:32:57 +00:00
|
|
|
|
elsif ( not $destination ) {
|
2019-03-21 21:12:45 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
2019-04-23 16:08:07 +00:00
|
|
|
|
success => 1,
|
|
|
|
|
redirect_to => '/',
|
2019-03-21 21:12:45 +00:00
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-05-19 08:32:57 +00:00
|
|
|
|
else {
|
|
|
|
|
# Silently ignore errors -- if they are permanent, the user will see
|
|
|
|
|
# them when selecting the destination manually.
|
|
|
|
|
my ( $still_checked_in, undef )
|
|
|
|
|
= $self->checkout( $destination, 0 );
|
|
|
|
|
my $station_link = '/s/' . $destination;
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
success => 1,
|
|
|
|
|
redirect_to => $still_checked_in ? '/' : $station_link,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
|
}
|
|
|
|
|
elsif ( $params->{action} eq 'checkout' ) {
|
2019-04-23 16:08:07 +00:00
|
|
|
|
my ( $still_checked_in, $error )
|
|
|
|
|
= $self->checkout( $params->{station}, $params->{force} );
|
|
|
|
|
my $station_link = '/s/' . $params->{station};
|
2019-03-21 21:12:45 +00:00
|
|
|
|
|
|
|
|
|
if ($error) {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
success => 0,
|
|
|
|
|
error => $error,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
2019-04-23 16:08:07 +00:00
|
|
|
|
success => 1,
|
|
|
|
|
redirect_to => $still_checked_in ? '/' : $station_link,
|
2019-03-21 21:12:45 +00:00
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elsif ( $params->{action} eq 'undo' ) {
|
2019-04-25 07:22:54 +00:00
|
|
|
|
my $status = $self->get_user_status;
|
2019-04-26 17:53:01 +00:00
|
|
|
|
my $error = $self->undo( $params->{undo_id} );
|
2019-03-21 21:12:45 +00:00
|
|
|
|
if ($error) {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
success => 0,
|
|
|
|
|
error => $error,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2019-04-25 07:22:54 +00:00
|
|
|
|
my $redir = '/';
|
2019-04-26 17:53:01 +00:00
|
|
|
|
if ( $status->{checked_in} or $status->{cancelled} ) {
|
2019-04-25 07:22:54 +00:00
|
|
|
|
$redir = '/s/' . $status->{dep_ds100};
|
|
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
2019-04-23 16:08:07 +00:00
|
|
|
|
success => 1,
|
2019-04-25 07:22:54 +00:00
|
|
|
|
redirect_to => $redir,
|
2019-03-21 21:12:45 +00:00
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elsif ( $params->{action} eq 'cancelled_from' ) {
|
|
|
|
|
my ( undef, $error )
|
2019-04-23 21:02:44 +00:00
|
|
|
|
= $self->checkin( $params->{station}, $params->{train} );
|
2019-03-21 21:12:45 +00:00
|
|
|
|
|
|
|
|
|
if ($error) {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
success => 0,
|
|
|
|
|
error => $error,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
2019-04-23 16:08:07 +00:00
|
|
|
|
success => 1,
|
|
|
|
|
redirect_to => '/',
|
2019-03-21 21:12:45 +00:00
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elsif ( $params->{action} eq 'cancelled_to' ) {
|
2019-04-23 16:08:07 +00:00
|
|
|
|
my ( undef, $error )
|
2019-04-23 21:02:44 +00:00
|
|
|
|
= $self->checkout( $params->{station}, 1 );
|
2019-03-21 21:12:45 +00:00
|
|
|
|
|
|
|
|
|
if ($error) {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
success => 0,
|
|
|
|
|
error => $error,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
2019-04-23 16:08:07 +00:00
|
|
|
|
success => 1,
|
|
|
|
|
redirect_to => '/',
|
2019-03-21 21:12:45 +00:00
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-04 16:26:53 +00:00
|
|
|
|
elsif ( $params->{action} eq 'delete' ) {
|
2019-04-23 16:08:07 +00:00
|
|
|
|
my $error = $self->delete_journey( $params->{id}, $params->{checkin},
|
2019-04-04 16:26:53 +00:00
|
|
|
|
$params->{checkout} );
|
|
|
|
|
if ($error) {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
success => 0,
|
|
|
|
|
error => $error,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
2019-04-23 16:08:07 +00:00
|
|
|
|
success => 1,
|
|
|
|
|
redirect_to => '/history',
|
2019-04-04 16:26:53 +00:00
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-21 21:12:45 +00:00
|
|
|
|
else {
|
|
|
|
|
$self->render(
|
|
|
|
|
json => {
|
|
|
|
|
success => 0,
|
|
|
|
|
error => 'invalid action value',
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub station {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
my $station = $self->stash('station');
|
|
|
|
|
my $train = $self->param('train');
|
|
|
|
|
|
2019-07-14 19:29:52 +00:00
|
|
|
|
my $status = $self->get_departures( $station, 120, 30, 1 );
|
2019-03-21 21:12:45 +00:00
|
|
|
|
|
|
|
|
|
if ( $status->{errstr} ) {
|
|
|
|
|
$self->render(
|
|
|
|
|
'landingpage',
|
2019-04-30 16:18:09 +00:00
|
|
|
|
version => $self->app->config->{version} // 'UNKNOWN',
|
2019-04-13 08:43:05 +00:00
|
|
|
|
with_autocomplete => 1,
|
|
|
|
|
with_geolocation => 1,
|
|
|
|
|
error => $status->{errstr}
|
2019-03-21 21:12:45 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
# You can't check into a train which terminates here
|
|
|
|
|
my @results = grep { $_->departure } @{ $status->{results} };
|
|
|
|
|
|
|
|
|
|
@results = map { $_->[0] }
|
|
|
|
|
sort { $b->[1] <=> $a->[1] }
|
|
|
|
|
map { [ $_, $_->departure->epoch // $_->sched_departure->epoch ] }
|
|
|
|
|
@results;
|
|
|
|
|
|
|
|
|
|
if ($train) {
|
|
|
|
|
@results
|
|
|
|
|
= grep { $_->type . ' ' . $_->train_no eq $train } @results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$self->render(
|
|
|
|
|
'departures',
|
2019-10-19 08:15:18 +00:00
|
|
|
|
ds100 => $status->{station_ds100},
|
|
|
|
|
results => \@results,
|
|
|
|
|
station => $status->{station_name},
|
|
|
|
|
related_stations => $status->{related_stations},
|
|
|
|
|
title => "travelynx: $status->{station_name}",
|
2019-03-21 21:12:45 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-04-30 10:47:32 +00:00
|
|
|
|
$self->mark_seen( $self->current_user->{id} );
|
2019-03-21 21:12:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub redirect_to_station {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
my $station = $self->param('station');
|
|
|
|
|
|
|
|
|
|
$self->redirect_to("/s/${station}");
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-07 16:44:33 +00:00
|
|
|
|
sub cancelled {
|
2019-03-22 15:56:49 +00:00
|
|
|
|
my ($self) = @_;
|
2019-04-07 16:44:33 +00:00
|
|
|
|
my @journeys = $self->get_user_travels( cancelled => 1 );
|
2019-03-27 20:20:59 +00:00
|
|
|
|
|
2019-03-22 15:56:49 +00:00
|
|
|
|
$self->respond_to(
|
2019-03-27 20:20:59 +00:00
|
|
|
|
json => { json => [@journeys] },
|
|
|
|
|
any => {
|
2019-04-07 16:44:33 +00:00
|
|
|
|
template => 'cancelled',
|
2019-03-27 20:20:59 +00:00
|
|
|
|
journeys => [@journeys]
|
|
|
|
|
}
|
2019-03-22 15:56:49 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-07 16:44:33 +00:00
|
|
|
|
sub history {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
$self->render( template => 'history' );
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-22 15:56:49 +00:00
|
|
|
|
sub json_history {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
2019-04-07 16:44:33 +00:00
|
|
|
|
$self->render( json => [ $self->get_user_travels ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub yearly_history {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
my $year = $self->stash('year');
|
|
|
|
|
my @journeys;
|
|
|
|
|
my $stats;
|
|
|
|
|
|
2019-05-10 23:35:57 +00:00
|
|
|
|
# DateTime is very slow when looking far into the future due to DST changes
|
|
|
|
|
# -> Limit time range to avoid accidental DoS.
|
|
|
|
|
if ( not( $year =~ m{ ^ [0-9]{4} $ }x and $year > 1990 and $year < 2100 ) )
|
|
|
|
|
{
|
2019-04-07 16:44:33 +00:00
|
|
|
|
@journeys = $self->get_user_travels;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
my $interval_start = DateTime->new(
|
|
|
|
|
time_zone => 'Europe/Berlin',
|
|
|
|
|
year => $year,
|
|
|
|
|
month => 1,
|
|
|
|
|
day => 1,
|
|
|
|
|
hour => 0,
|
|
|
|
|
minute => 0,
|
|
|
|
|
second => 0,
|
|
|
|
|
);
|
|
|
|
|
my $interval_end = $interval_start->clone->add( years => 1 );
|
|
|
|
|
@journeys = $self->get_user_travels(
|
|
|
|
|
after => $interval_start,
|
|
|
|
|
before => $interval_end
|
|
|
|
|
);
|
|
|
|
|
$stats = $self->get_journey_stats( year => $year );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$self->respond_to(
|
|
|
|
|
json => {
|
|
|
|
|
json => {
|
|
|
|
|
journeys => [@journeys],
|
|
|
|
|
statistics => $stats
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
any => {
|
|
|
|
|
template => 'history_by_year',
|
|
|
|
|
journeys => [@journeys],
|
|
|
|
|
year => $year,
|
|
|
|
|
statistics => $stats
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2019-03-22 15:56:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:20:59 +00:00
|
|
|
|
sub monthly_history {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
my $year = $self->stash('year');
|
|
|
|
|
my $month = $self->stash('month');
|
|
|
|
|
my @journeys;
|
|
|
|
|
my $stats;
|
|
|
|
|
my @months
|
|
|
|
|
= (
|
|
|
|
|
qw(Januar Februar März April Mai Juni Juli August September Oktober November Dezember)
|
|
|
|
|
);
|
|
|
|
|
|
2019-05-10 23:35:57 +00:00
|
|
|
|
if (
|
|
|
|
|
not( $year =~ m{ ^ [0-9]{4} $ }x
|
|
|
|
|
and $year > 1990
|
|
|
|
|
and $year < 2100
|
|
|
|
|
and $month =~ m{ ^ [0-9]{1,2} $ }x
|
|
|
|
|
and $month > 0
|
|
|
|
|
and $month < 13 )
|
|
|
|
|
)
|
2019-03-27 20:20:59 +00:00
|
|
|
|
{
|
2019-04-07 16:44:33 +00:00
|
|
|
|
@journeys = $self->get_user_travels;
|
2019-03-27 20:20:59 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
my $interval_start = DateTime->new(
|
|
|
|
|
time_zone => 'Europe/Berlin',
|
|
|
|
|
year => $year,
|
|
|
|
|
month => $month,
|
|
|
|
|
day => 1,
|
|
|
|
|
hour => 0,
|
|
|
|
|
minute => 0,
|
|
|
|
|
second => 0,
|
|
|
|
|
);
|
|
|
|
|
my $interval_end = $interval_start->clone->add( months => 1 );
|
|
|
|
|
@journeys = $self->get_user_travels(
|
2019-04-07 14:55:35 +00:00
|
|
|
|
after => $interval_start,
|
|
|
|
|
before => $interval_end
|
|
|
|
|
);
|
|
|
|
|
$stats = $self->get_journey_stats(
|
|
|
|
|
year => $year,
|
|
|
|
|
month => $month
|
2019-03-27 20:20:59 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$self->respond_to(
|
|
|
|
|
json => {
|
|
|
|
|
json => {
|
|
|
|
|
journeys => [@journeys],
|
|
|
|
|
statistics => $stats
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
any => {
|
2019-04-07 16:44:33 +00:00
|
|
|
|
template => 'history_by_month',
|
2019-03-27 20:20:59 +00:00
|
|
|
|
journeys => [@journeys],
|
|
|
|
|
year => $year,
|
2019-03-27 20:46:52 +00:00
|
|
|
|
month => $month,
|
|
|
|
|
month_name => $months[ $month - 1 ],
|
2019-03-27 20:20:59 +00:00
|
|
|
|
statistics => $stats
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-22 15:56:49 +00:00
|
|
|
|
sub journey_details {
|
|
|
|
|
my ($self) = @_;
|
2019-04-23 16:08:07 +00:00
|
|
|
|
my $journey_id = $self->stash('id');
|
|
|
|
|
|
|
|
|
|
my $uid = $self->current_user->{id};
|
2019-03-22 15:56:49 +00:00
|
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
|
$self->param( journey_id => $journey_id );
|
2019-04-07 18:20:37 +00:00
|
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
|
if ( not($journey_id) ) {
|
2019-03-22 15:56:49 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'journey',
|
|
|
|
|
error => 'notfound',
|
|
|
|
|
journey => {}
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
|
my $journey = $self->get_journey(
|
|
|
|
|
uid => $uid,
|
|
|
|
|
journey_id => $journey_id,
|
|
|
|
|
verbose => 1,
|
2019-03-22 15:56:49 +00:00
|
|
|
|
);
|
2019-04-23 16:08:07 +00:00
|
|
|
|
|
|
|
|
|
if ($journey) {
|
|
|
|
|
$self->render(
|
|
|
|
|
'journey',
|
|
|
|
|
error => undef,
|
|
|
|
|
journey => $journey,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2019-03-22 15:56:49 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'journey',
|
|
|
|
|
error => 'notfound',
|
|
|
|
|
journey => {}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-07 18:20:37 +00:00
|
|
|
|
sub edit_journey {
|
2019-04-23 16:08:07 +00:00
|
|
|
|
my ($self) = @_;
|
|
|
|
|
my $journey_id = $self->param('journey_id');
|
|
|
|
|
my $uid = $self->current_user->{id};
|
2019-04-07 18:20:37 +00:00
|
|
|
|
|
2019-04-23 16:08:07 +00:00
|
|
|
|
if ( not( $journey_id =~ m{ ^ \d+ $ }x ) ) {
|
2019-04-07 18:20:37 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'edit_journey',
|
|
|
|
|
error => 'notfound',
|
|
|
|
|
journey => {}
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-08 19:20:04 +00:00
|
|
|
|
my $journey = $self->get_journey(
|
2019-04-23 16:08:07 +00:00
|
|
|
|
uid => $uid,
|
|
|
|
|
journey_id => $journey_id
|
2019-04-07 18:20:37 +00:00
|
|
|
|
);
|
2019-04-08 19:20:04 +00:00
|
|
|
|
|
|
|
|
|
if ( not $journey ) {
|
2019-04-07 18:20:37 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'edit_journey',
|
|
|
|
|
error => 'notfound',
|
|
|
|
|
journey => {}
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-08 19:20:04 +00:00
|
|
|
|
my $error = undef;
|
|
|
|
|
|
|
|
|
|
if ( $self->param('action') and $self->param('action') eq 'save' ) {
|
|
|
|
|
my $parser = DateTime::Format::Strptime->new(
|
|
|
|
|
pattern => '%d.%m.%Y %H:%M',
|
|
|
|
|
locale => 'de_DE',
|
|
|
|
|
time_zone => 'Europe/Berlin'
|
|
|
|
|
);
|
|
|
|
|
|
2019-04-22 10:11:22 +00:00
|
|
|
|
my $db = $self->pg->db;
|
|
|
|
|
my $tx = $db->begin;
|
2019-04-08 19:20:04 +00:00
|
|
|
|
|
|
|
|
|
for my $key (qw(sched_departure rt_departure sched_arrival rt_arrival))
|
|
|
|
|
{
|
|
|
|
|
my $datetime = $parser->parse_datetime( $self->param($key) );
|
|
|
|
|
if ( $datetime and $datetime->epoch ne $journey->{$key}->epoch ) {
|
2019-04-23 16:08:07 +00:00
|
|
|
|
$error = $self->update_journey_part( $db, $journey->{id},
|
|
|
|
|
$key, $datetime );
|
2019-04-08 19:20:04 +00:00
|
|
|
|
if ($error) {
|
|
|
|
|
last;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-23 10:02:22 +00:00
|
|
|
|
for my $key (qw(comment)) {
|
|
|
|
|
if (
|
|
|
|
|
defined $self->param($key)
|
|
|
|
|
and ( not $journey->{user_data}
|
|
|
|
|
or $journey->{user_data}{$key} ne $self->param($key) )
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
$error = $self->update_journey_part( $db, $journey->{id}, $key,
|
|
|
|
|
$self->param($key) );
|
|
|
|
|
if ($error) {
|
|
|
|
|
last;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-08 19:20:04 +00:00
|
|
|
|
|
2019-04-22 10:11:22 +00:00
|
|
|
|
if ( not $error ) {
|
2019-04-08 19:20:04 +00:00
|
|
|
|
$journey = $self->get_journey(
|
2019-04-23 16:08:07 +00:00
|
|
|
|
uid => $uid,
|
|
|
|
|
db => $db,
|
|
|
|
|
journey_id => $journey_id,
|
|
|
|
|
verbose => 1
|
2019-04-08 19:20:04 +00:00
|
|
|
|
);
|
|
|
|
|
$error = $self->journey_sanity_check($journey);
|
|
|
|
|
}
|
2019-04-22 10:11:22 +00:00
|
|
|
|
if ( not $error ) {
|
|
|
|
|
$tx->commit;
|
2019-04-23 16:08:07 +00:00
|
|
|
|
$self->redirect_to("/journey/${journey_id}");
|
2019-04-22 10:11:22 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-04-08 19:20:04 +00:00
|
|
|
|
}
|
2019-04-07 18:20:37 +00:00
|
|
|
|
|
|
|
|
|
for my $key (qw(sched_departure rt_departure sched_arrival rt_arrival)) {
|
|
|
|
|
if ( $journey->{$key} and $journey->{$key}->epoch ) {
|
|
|
|
|
$self->param(
|
|
|
|
|
$key => $journey->{$key}->strftime('%d.%m.%Y %H:%M') );
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-23 10:02:22 +00:00
|
|
|
|
for my $key (qw(comment)) {
|
|
|
|
|
if ( $journey->{user_data} and $journey->{user_data}{$key} ) {
|
|
|
|
|
$self->param( $key => $journey->{user_data}{$key} );
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-07 18:20:37 +00:00
|
|
|
|
|
|
|
|
|
$self->render(
|
|
|
|
|
'edit_journey',
|
2019-04-08 19:20:04 +00:00
|
|
|
|
error => $error,
|
2019-04-07 18:20:37 +00:00
|
|
|
|
journey => $journey
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub add_journey_form {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
2019-04-13 08:43:05 +00:00
|
|
|
|
if ( $self->param('action') and $self->param('action') eq 'save' ) {
|
|
|
|
|
my $parser = DateTime::Format::Strptime->new(
|
|
|
|
|
pattern => '%d.%m.%Y %H:%M',
|
|
|
|
|
locale => 'de_DE',
|
|
|
|
|
time_zone => 'Europe/Berlin'
|
|
|
|
|
);
|
|
|
|
|
my %opt;
|
|
|
|
|
|
|
|
|
|
my @parts = split( qr{\s+}, $self->param('train') );
|
|
|
|
|
|
|
|
|
|
if ( @parts == 2 ) {
|
|
|
|
|
@opt{ 'train_type', 'train_no' } = @parts;
|
|
|
|
|
}
|
|
|
|
|
elsif ( @parts == 3 ) {
|
|
|
|
|
@opt{ 'train_type', 'train_line', 'train_no' } = @parts;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$self->render(
|
|
|
|
|
'add_journey',
|
|
|
|
|
with_autocomplete => 1,
|
|
|
|
|
error =>
|
|
|
|
|
'Zug muss als „Typ Nummer“ oder „Typ Linie Nummer“ eingegeben werden.'
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for my $key (qw(sched_departure rt_departure sched_arrival rt_arrival))
|
|
|
|
|
{
|
2019-04-26 17:53:01 +00:00
|
|
|
|
if ( $self->param($key) ) {
|
|
|
|
|
my $datetime = $parser->parse_datetime( $self->param($key) );
|
|
|
|
|
if ( not $datetime ) {
|
|
|
|
|
$self->render(
|
|
|
|
|
'add_journey',
|
|
|
|
|
with_autocomplete => 1,
|
|
|
|
|
error => "${key}: Ungültiges Datums-/Zeitformat"
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$opt{$key} = $datetime;
|
2019-04-13 08:43:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 17:53:01 +00:00
|
|
|
|
for my $key (qw(dep_station arr_station cancelled comment)) {
|
2019-04-13 08:43:05 +00:00
|
|
|
|
$opt{$key} = $self->param($key);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 17:53:01 +00:00
|
|
|
|
my $db = $self->pg->db;
|
|
|
|
|
my $tx = $db->begin;
|
|
|
|
|
|
|
|
|
|
$opt{db} = $db;
|
|
|
|
|
|
|
|
|
|
my ( $journey_id, $error ) = $self->add_journey(%opt);
|
2019-04-13 08:43:05 +00:00
|
|
|
|
|
2019-04-26 17:53:01 +00:00
|
|
|
|
if ( not $error ) {
|
|
|
|
|
my $journey = $self->get_journey(
|
|
|
|
|
uid => $self->current_user->{id},
|
|
|
|
|
db => $db,
|
|
|
|
|
journey_id => $journey_id,
|
|
|
|
|
verbose => 1
|
|
|
|
|
);
|
|
|
|
|
$error = $self->journey_sanity_check($journey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($error) {
|
|
|
|
|
$self->render(
|
|
|
|
|
'add_journey',
|
|
|
|
|
with_autocomplete => 1,
|
|
|
|
|
error => $error,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$tx->commit;
|
|
|
|
|
$self->redirect_to("/journey/${journey_id}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2019-04-13 08:43:05 +00:00
|
|
|
|
$self->render(
|
|
|
|
|
'add_journey',
|
|
|
|
|
with_autocomplete => 1,
|
2019-04-26 17:53:01 +00:00
|
|
|
|
error => undef
|
2019-04-13 08:43:05 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-04-07 18:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 21:12:45 +00:00
|
|
|
|
1;
|