"Fossies" - the Fresh Open Source Software Archive

Member "email-reminder-0.8.1/EmailReminder/MonthlyEvent.pm" (19 Sep 2020, 3996 Bytes) of package /linux/privat/email-reminder-0.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "MonthlyEvent.pm" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.8.0_vs_0.8.1.

    1 # This file is part of Email-Reminder.
    2 #
    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.
    7 #
    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.
   12 #
   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.
   17 
   18 package EmailReminder::MonthlyEvent;
   19 
   20 use strict;
   21 use warnings;
   22 use overload '""' => \&str;
   23 use Date::Manip;
   24 use POSIX;
   25 use Scalar::Util;
   26 
   27 use EmailReminder::Event;
   28 use EmailReminder::Utils;
   29 
   30 use base qw(EmailReminder::Event);
   31 
   32 # XML tags
   33 __PACKAGE__->mk_accessors(qw(day));
   34 
   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");
   40 
   41 sub str {
   42     my ($self) = @_;
   43     return $self->get_type . ':' . $self->get_id . ') ' . $self->get_name . ' - ' . $self->get_day;
   44 }
   45 
   46 # Hard-coded value for this event's type (class method)
   47 sub get_type
   48 {
   49     return 'monthly';
   50 }
   51 
   52 # Number of fields this event adds to its parent (class method)
   53 sub get_nb_fields
   54 {
   55     my ($self) = @_;
   56     return $self->SUPER::get_nb_fields() + 2;
   57 }
   58 
   59 sub valid_day
   60 {
   61     my ($class, $new_value) = @_;
   62 
   63     if (!Scalar::Util::looks_like_number($new_value)) {
   64         $new_value = 1;
   65     }
   66 
   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;
   72     }
   73     
   74     return $new_value;
   75 }
   76 
   77 sub get_current_occurence_date
   78 {
   79     my ($self, $modifier) = @_;
   80 
   81     my $day = $self->get_day();
   82     return unless $day;
   83 
   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 ($current_occurence_date eq "" or UnixDate($current_occurence_date, "\%d") != $day) {
   88         $day -= 1;
   89         $current_occurence_date = ParseDate("$current_year-$current_month-$day");
   90     }
   91 
   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");
   97         }
   98         $modified_date = DateCalc($modified_date, " - $modifier days");
   99     }
  100 
  101     return $modified_date;
  102 }
  103 
  104 sub get_subject
  105 {
  106     my $self = shift;
  107     return $self->get_name();
  108 }
  109 
  110 sub get_message_body
  111 {
  112     my $self = shift;
  113 
  114     # event details
  115     my $when      = $self->{"WHEN"} || '';
  116     my $name      = $self->get_name();
  117     
  118     my $message = <<"MESSAGEEND";
  119 I just want to remind you of the following event $when:
  120 
  121 $name
  122 MESSAGEEND
  123 
  124     return $message;
  125 }
  126 
  127 # Returns 1 if the event will occur in X days (X is a param)
  128 sub will_occur
  129 {
  130     my $self = shift;
  131     my $modifier = shift;
  132     
  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;
  136     
  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;
  146     }
  147 }
  148 
  149 1;