diff options
author | cos <cos> | 2015-02-21 16:16:43 +0100 |
---|---|---|
committer | cos <cos> | 2015-04-04 13:28:19 +0200 |
commit | e98d984bb167d8d09cdab1a810e9c3536af972de (patch) | |
tree | e1b8f7e8555b0b94415f68317b68c065bbd6ac33 | |
parent | fa76332e1b202e1477110951e40257bad8282629 (diff) | |
download | mat-e98d984bb167d8d09cdab1a810e9c3536af972de.zip |
Add cmd_setcomment().
-rwxr-xr-x | mat | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -162,6 +162,39 @@ sub cmd_setmeal { return 0; } +sub cmd_setcomment { + my ( $date, $mealtype, $comment ) = @_; + + if ($date =~ /^Mon|Tue|Wed|Thu|Fri|Sat|Sun/) { + my $wday; + $wday = 1 if ($date =~ /^Mon/); + $wday = 2 if ($date =~ /^Tue/); + $wday = 3 if ($date =~ /^Wed/); + $wday = 4 if ($date =~ /^Thu/); + $wday = 5 if ($date =~ /^Fri/); + $wday = 6 if ($date =~ /^Sat/); + $wday = 7 if ($date =~ /^Sun/); + + my $dt = DateTime->now(); + while ($dt->wday() != $wday) { + $dt->add(days => 1); + } + + $date = $dt->ymd(); + } + return unless $mealtype =~ "^(frukost)|(elvakaffe)|(lunch)|(fruktstund)|(middag)\$"; + return unless $date =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}\$"; + + my $sql = "INSERT INTO comments VALUES (NULL, '$comment');"; + $db->do($sql); + my $comment_id = $db->last_insert_id(undef, undef, undef, undef); + $sql = "INSERT INTO plan (date, mealtype, comment_id) VALUES ('$date', + '$mealtype', $comment_id);"; + $db->do($sql); + + return 0; +} + sub cmd_movemeal { my ( $source, $destination ) = @_; my $mealtype = "lunch"; @@ -477,6 +510,7 @@ sub cmd_help() { print "showrecipe <recipe_id>\n"; print "movemeal <source_date> <destination_date>\n"; print "setmeal <date> <mealtype> <meal_id>\n"; + print "setcomment <date> <mealtype> <comment>\n"; print "randmeal <date>\n"; print "showplan [days] [date]\n"; print "postpone <date> <gap>\n"; @@ -815,6 +849,8 @@ if ($ARGV[0]) { print "Command failed!\n" unless (cmd_movemeal($ARGV[1], $ARGV[2]) >= 0); } elsif ( $ARGV[0] eq "setmeal") { print "Command failed!\n" unless (cmd_setmeal($ARGV[1], $ARGV[2], $ARGV[3]) >= 0); + } elsif ( $ARGV[0] eq "setcomment") { + print "Command failed!\n" unless (cmd_setcomment($ARGV[1], $ARGV[2], $ARGV[3]) >= 0); } elsif ( $ARGV[0] eq "randmeal") { print "Command failed!\n" unless (cmd_randmeal($ARGV[1]) >= 0); } elsif ( $ARGV[0] eq "showplan") { |