Added a number of new tests and example XML files for them.
[email-reminder.git] / EmailReminder / WeeklyEvent.pm
blob14f8477f0103db65adeecebdbc78d1c400073412
1 # This file is part of Email-Reminder.
3 # Email-Reminder is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License as
5 # published by the Free Software Foundation; either version 3 of the
6 # License, or (at your option) any later version.
8 # Email-Reminder is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with Email-Reminder; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 # 02110-1301, USA.
18 package EmailReminder::WeeklyEvent;
20 use strict;
21 use warnings;
22 use overload '""' => \&str;
23 use Date::Manip;
24 use POSIX;
25 use Scalar::Util;
27 use EmailReminder::Event;
28 use EmailReminder::Utils;
30 require Exporter;
31 our @ISA = ("EmailReminder::Event", "Exporter");
32 #our @EXPORT = qw(get_name valid_day);
34 # XML tags, attributes and values
35 my $DAY_TAG = 'day';
36 my $NAME_TAG = 'name';
38 # Global date variables
39 my $current_time = ParseDate("now");
40 my $current_date = ParseDate(UnixDate($current_time, "\%x"));
41 my $current_dayofweek = UnixDate($current_time, "\%w");
43 sub str {
44 my ($this) = @_;
45 return $this->get_type . ':' . $this->get_id . ') ' . $this->get_name . ' - ' . $this->get_day;
48 # Hard-coded value for this event's type (class method)
49 sub get_type
51 return 'weekly';
54 # Number of fields this event adds to its parent (class method)
55 sub get_nb_fields
57 return EmailReminder::Event->get_nb_fields() + 2;
60 sub get_name
62 my ($this) = @_;
63 my $name = EmailReminder::Utils::get_node_value($this->{XML_NODE}, $NAME_TAG);
64 return $name;
67 sub set_name
69 my ($this, $new_value) = @_;
70 return EmailReminder::Utils::set_node_value($this->{XML_NODE}, $NAME_TAG, $new_value);
73 sub valid_day
75 my ($new_value) = @_;
77 if (!Scalar::Util::looks_like_number($new_value)) {
78 # Try to parse as a string
79 my $day = UnixDate(ParseDate($new_value), "\%w");
80 if ($day) {
81 $new_value = $day;
82 } else {
83 $new_value = 7; # Default: Sunday
87 if ($new_value > 7 or $new_value < 1) {
88 # Default to Sunday for out of range dates (since zero is
89 # both 0 and 7).
90 $new_value = 7;
93 return $new_value;
96 sub get_day
98 my ($this) = @_;
99 my $day = EmailReminder::Utils::get_node_value($this->{XML_NODE}, $DAY_TAG);
100 return valid_day($day);
103 sub set_day
105 my ($this, $new_value) = @_;
106 return EmailReminder::Utils::set_node_value($this->{XML_NODE}, $DAY_TAG, valid_day($new_value));
109 sub get_subject
111 my $this = shift;
112 return $this->get_name();
115 sub get_message_body
117 my $this = shift;
119 # destination user
120 my $first_name = shift;
122 # event details
123 my $when = $this->{"WHEN"};
124 my $name = $this->get_name();
126 my $message = <<MESSAGEEND;
127 Hi $first_name,
129 I just want to remind you of the following event $when:
131 $name
132 MESSAGEEND
134 return $message;
137 # Returns 1 if the event will occur in X days (X is a param)
138 sub will_occur
140 my $this = shift;
141 my $modifier = shift;
143 # Apply the modifier to the event date
144 my $modified_day = $this->get_day();
145 return 0 unless $modified_day;
147 if ($modifier) {
148 $modified_day -= $modifier;
149 while ($modified_day > 7) {
150 $modified_day -= 7;
152 while ($modified_day < 1) {
153 $modified_day += 7;
157 if ($current_dayofweek == $modified_day) {
158 return 1;
159 } else {
160 return 0;