collect-reminders also contains the year number
[email-reminder.git] / EmailReminder / MonthlyEvent.pm
blob81eb6510ccd512f170b4b896e4056cd595055c17
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::MonthlyEvent;
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_month = UnixDate($current_time, "\%m");
39 my $current_year = UnixDate($current_time, "\%Y");
41 sub str {
42 my ($self) = @_;
43 return $self->get_type . ':' . $self->get_id . ') ' . $self->get_name . ' - ' . $self->get_day;
46 # Hard-coded value for this event's type (class method)
47 sub get_type
49 return 'monthly';
52 # Number of fields this event adds to its parent (class method)
53 sub get_nb_fields
55 my ($self) = @_;
56 return $self->SUPER::get_nb_fields() + 2;
59 sub valid_day
61 my ($class, $new_value) = @_;
63 if (!Scalar::Util::looks_like_number($new_value)) {
64 $new_value = 1;
67 # Make sure the value is a valid number
68 if ($new_value > 31) {
69 $new_value = 31;
70 } elsif ($new_value < 1) {
71 $new_value = 1;
74 return $new_value;
77 sub get_current_occurence_date
79 my ($self, $modifier) = @_;
81 my $day = $self->get_day();
82 return unless $day;
84 # Set the day of the month where the event occurs, make sure the date is valid
85 # (e.g. fix-up for event on the 31st when the month has only 30 days)
86 my $current_occurence_date = ParseDate("$current_year-$current_month-$day");
87 while (UnixDate($current_occurence_date, "\%d") != $day) {
88 $day -= 1;
89 $current_occurence_date = ParseDate("$current_year-$current_month-$day");
92 my $modified_date = $current_occurence_date;
93 if ($modifier) {
94 if ($modifier >= $day) {
95 # We are warning about an event in a few days, past the end of the current month
96 $modified_date = DateCalc($modified_date, " + 1 month");
98 $modified_date = DateCalc($modified_date, " - $modifier days");
101 return $modified_date;
104 sub get_subject
106 my $self = shift;
107 return $self->get_name();
110 sub get_message_body
112 my $self = shift;
114 # event details
115 my $when = $self->{"WHEN"};
116 my $name = $self->get_name();
118 my $message = <<"MESSAGEEND";
119 I just want to remind you of the following event $when:
121 $name
122 MESSAGEEND
124 return $message;
127 # Returns 1 if the event will occur in X days (X is a param)
128 sub will_occur
130 my $self = shift;
131 my $modifier = shift;
133 # Apply the modifier to the event date
134 my $current_occurence_date = $self->get_current_occurence_date($modifier);
135 return 0 unless $current_occurence_date;
137 my $tomorrow = DateCalc($current_date, " + 1 day");
138 if (Date_Cmp($current_date, $current_occurence_date) == 0) {
139 return 1;
140 } elsif (Date_Cmp($current_date, $current_occurence_date) < 0 and
141 Date_Cmp($tomorrow, $current_occurence_date) > 0) {
142 # e.g. if today is the 30, the reminder is on the 31st and tomorrow is the 1st
143 return 1;
144 } else {
145 return 0;