Use signed tags for releases
[email-reminder.git] / EmailReminder / WeeklyEvent.pm
blob6df2c2e887c0ee6698068c0720c605ddfcb8e26f
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 use base qw(EmailReminder::Event);
32 # XML tags
33 __PACKAGE__->mk_accessors(qw(day));
35 # Global date variables
36 my $current_time = ParseDate("now");
37 my $current_date = ParseDate(UnixDate($current_time, "\%x"));
38 my $current_dayofweek = UnixDate($current_time, "\%w");
40 sub str {
41 my ($self) = @_;
42 return $self->get_type . ':' . $self->get_id . ') ' . $self->get_name . ' - ' . $self->get_day;
45 # Hard-coded value for this event's type (class method)
46 sub get_type
48 return 'weekly';
51 # Number of fields this event adds to its parent (class method)
52 sub get_nb_fields
54 my ($self) = @_;
55 return $self->SUPER::get_nb_fields() + 2;
58 sub valid_day
60 my ($class, $new_value) = @_;
62 if (!Scalar::Util::looks_like_number($new_value)) {
63 # Try to parse as a string
64 my $day = UnixDate(ParseDate($new_value), "\%w");
65 if ($day) {
66 $new_value = $day;
67 } else {
68 $new_value = 7; # Default: Sunday
72 if ($new_value > 7 or $new_value < 1) {
73 # Default to Sunday for out of range dates (since zero is
74 # both 0 and 7).
75 $new_value = 7;
78 return $new_value;
81 sub get_subject
83 my $self = shift;
84 return $self->get_name();
87 sub get_message_body
89 my $self = shift;
91 # event details
92 my $when = $self->{"WHEN"} || '';
93 my $name = $self->get_name();
95 my $message = <<"MESSAGEEND";
96 I just want to remind you of the following event $when:
98 $name
99 MESSAGEEND
101 return $message;
104 # Returns 1 if the event will occur in X days (X is a param)
105 sub will_occur
107 my $self = shift;
108 my $modifier = shift;
110 # Apply the modifier to the event date
111 my $modified_day = $self->get_day();
112 return 0 unless $modified_day;
114 if ($modifier) {
115 $modified_day -= $modifier;
116 while ($modified_day > 7) {
117 $modified_day -= 7;
119 while ($modified_day < 1) {
120 $modified_day += 7;
124 if ($current_dayofweek == $modified_day) {
125 return 1;
126 } else {
127 return 0;