chart generation using Chart::Clicker works
[kratosorange.git] / lib / KratosOrange / Dispatcher.pm
blob5c995ce18cd31ec4ae952a92322d8d9cf27255a2
1 package KratosOrange::Dispatcher;
2 use strict;
3 use warnings;
4 use Jifty::Dispatcher -base;
6 before '*' => run {
7 if (Jifty->web->current_user->id) {
8 # authenticated user, show options
9 my $top = Jifty->web->navigation;
10 $top->child( 'Home' => url => '/' );
11 $top->child( 'My runs' => url => '/runs' );
12 $top->child( 'Preferences' => url => '/prefs' );
13 $top->child( 'Logout' => url => '/logout' );
14 # TODO - check if there is new data to import and tangent to
15 # import page if there is
16 # (only do this if data has already been imported, because the
17 # user might want to import 'old' data first if not)
19 elsif ($1 !~ /^login|^signup/) {
20 tangent 'login';
24 on '/' => run {
25 my $emped_id = Jifty->web->current_user->user_object->empedid;
26 Jifty->log->debug('empedid: ' . $emped_id);
27 if (! $emped_id) {
28 # user has not set his emped_id yet, send him off to preferences
29 tangent '/prefs';
31 my @new_files
32 = glob("/Volumes/*/iPod_Control/Device/Trainer/Workouts/Empeds/$emped_id/latest/*.xml");
33 my @old_files
34 = glob("/Volumes/*/iPod_Control/Device/Trainer/Workouts/Empeds/$emped_id/synched/*.xml");
35 Jifty->log->debug('new files: ' . join q{, }, @new_files);
36 Jifty->log->debug('old files: ' . join q{, }, @old_files);
38 my $workouts = KratosOrange::Model::WorkoutCollection->new();
39 $workouts->limit(
40 column => 'user_info_empedid',
41 value => $emped_id,
43 if (! $workouts->count && scalar @old_files) {
44 # this user has no workouts recorded, let him import ones
45 # already given away
46 tangent '/import_synched';
49 # if new files are available, import them
50 foreach my $file (@new_files) {
51 my $workout = KratosOrange::Model::Workout->new_from_XML($file);
52 # move file to 'synched' folder
53 my $new_location = $file;
54 $new_location =~ s/latest/synched/g;
55 rename($file, $new_location);
58 show 'summary';
61 on qr{ /workout/(\d+)/chart }xms => run {
62 my $workout_id = $1;
63 set 'workout_id' => $workout_id;
64 show 'chart';
68 on '/prefs' => show 'preferences';
70 on '/import_synched' => show 'import_synched';