"Fossies" - the Fresh Open Source Software Archive

Member "email-reminder-0.8.1/EmailReminder/WeeklyEvent.pm" (14 Mar 2015, 3058 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 "WeeklyEvent.pm" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.7.7_vs_0.7.8.

    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::WeeklyEvent;
   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_dayofweek = UnixDate($current_time, "\%w");
   39 
   40 sub str {
   41     my ($self) = @_;
   42     return $self->get_type . ':' . $self->get_id . ') ' . $self->get_name . ' - ' . $self->get_day;
   43 }
   44 
   45 # Hard-coded value for this event's type (class method)
   46 sub get_type
   47 {
   48     return 'weekly';
   49 }
   50 
   51 # Number of fields this event adds to its parent (class method)
   52 sub get_nb_fields
   53 {
   54     my ($self) = @_;
   55     return $self->SUPER::get_nb_fields() + 2;
   56 }
   57 
   58 sub valid_day
   59 {
   60     my ($class, $new_value) = @_;
   61 
   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
   69         }
   70     }
   71 
   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;
   76     }
   77 
   78     return $new_value;
   79 }
   80 
   81 sub get_subject
   82 {
   83     my $self = shift;
   84     return $self->get_name();
   85 }
   86 
   87 sub get_message_body
   88 {
   89     my $self = shift;
   90 
   91     # event details
   92     my $when      = $self->{"WHEN"} || '';
   93     my $name      = $self->get_name();
   94     
   95     my $message = <<"MESSAGEEND";
   96 I just want to remind you of the following event $when:
   97 
   98 $name
   99 MESSAGEEND
  100 
  101     return $message;
  102 }
  103 
  104 # Returns 1 if the event will occur in X days (X is a param)
  105 sub will_occur
  106 {
  107     my $self = shift;
  108     my $modifier = shift;
  109     
  110     # Apply the modifier to the event date
  111     my $modified_day = $self->get_day();
  112     return 0 unless $modified_day;
  113 
  114     if ($modifier) {
  115         $modified_day -= $modifier;
  116         while ($modified_day > 7) {
  117             $modified_day -= 7;
  118         }
  119         while ($modified_day < 1) {
  120             $modified_day += 7;
  121         }
  122     }
  123     
  124     if ($current_dayofweek == $modified_day) {
  125         return 1;
  126     } else {
  127         return 0;
  128     }
  129 }
  130 
  131 1;