Basic editing functionality
[kratosorange.git] / lib / KratosOrange / Dispatcher.pm
blob21e4075b5b46a1a43c564b171a94029512eb0bbe
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 workouts' => url => '/workouts' );
12 $top->child( 'Preferences' => url => '/prefs' );
13 $top->child( 'Logout' => url => '/logout' );
15 elsif ($1 !~ /^login|^signup/) {
16 tangent 'login';
20 on '/' => run {
21 my $emped_id = Jifty->web->current_user->user_object->empedid;
22 Jifty->log->debug('empedid: ' . $emped_id);
23 if (! $emped_id) {
24 # user has not set his emped_id yet, send him off to preferences
25 tangent '/prefs';
27 my @new_files
28 = glob("/Volumes/*/iPod_Control/Device/Trainer/Workouts/Empeds/$emped_id/latest/*.xml");
29 my @old_files
30 = glob("/Volumes/*/iPod_Control/Device/Trainer/Workouts/Empeds/$emped_id/synched/*.xml");
31 Jifty->log->debug('new files: ' . join q{, }, @new_files);
32 Jifty->log->debug('old files: ' . join q{, }, @old_files);
34 my $workouts = KratosOrange::Model::WorkoutCollection->new();
35 $workouts->limit(
36 column => 'user_info_empedid',
37 value => $emped_id,
39 if (! $workouts->count && scalar @old_files) {
40 # this user has no workouts recorded, let him import ones
41 # already given away
42 tangent '/import_synched';
45 # if new files are available, import them
46 foreach my $file (@new_files) {
47 my $workout = KratosOrange::Model::Workout->new_from_XML($file);
48 # move file to 'synched' folder
49 my $new_location = $file;
50 $new_location =~ s/latest/synched/g;
51 rename($file, $new_location);
54 show 'summary';
57 on qr{ \A /workout/(\d+) \z }xms => run {
58 my $workout_id = $1;
59 set 'workout_id' => $workout_id;
60 show 'workout';
63 on qr{ \A /workout/(\d+)/chart \z }xms => run {
64 my $workout_id = $1;
65 set 'workout_id' => $workout_id;
66 show 'chart';
69 on qr{ \A /workout/(\d+)/edit \z }xms => run {
70 my $workout_id = $1;
71 set 'workout_id' => $workout_id;
72 show 'workout_edit';
75 on '/workouts' => show 'workouts';
76 on '/prefs' => show 'preferences';
77 on '/import_synched' => show 'import_synched';