"Fossies" - the Fresh Open Source Software Archive

Member "email-reminder-0.8.1/EmailReminder/Event.pm" (11 Sep 2020, 6762 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 "Event.pm" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.7.8_vs_0.8.0.

    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::Event;
   19 
   20 # Base class for all other events.
   21 #
   22 # This class should never be used directly, use a derived class instead.
   23 
   24 use strict;
   25 use warnings;
   26 
   27 use Date::Manip;
   28 use XML::DOM;
   29 
   30 use EmailReminder::Utils;
   31 
   32 # XML tags
   33 __PACKAGE__->mk_accessors(qw(name));
   34 
   35 sub mk_accessors {
   36     my ($self, @field_names) = @_;
   37 
   38     # get the classname since it might be a derived class calling this
   39     my $class = ref $self || $self;
   40 
   41     foreach my $field_name ( @field_names ) {
   42         my $get_method = sub {
   43             my ($self) = @_;
   44             my $valid_sub = "valid_$field_name";
   45             my $value = EmailReminder::Utils::get_node_value($self->{XML_NODE}, $field_name);
   46             return $self->$valid_sub( $value );
   47         };
   48 
   49         my $set_method = sub {
   50             my ($self, $new_value) = @_;
   51             return EmailReminder::Utils::set_node_value($self->{XML_NODE}, $field_name, $new_value);
   52         };
   53 
   54         {
   55             no strict 'refs';
   56             *{"${class}::get_$field_name"} = $get_method;
   57             *{"${class}::set_$field_name"} = $set_method;
   58         }
   59     }
   60     return 1;
   61 }
   62 
   63 # other XML tags, attributes and values
   64 my $REMINDER_TAG = 'reminder';
   65 my $REMINDERS_TAG = 'reminders';
   66 my $RECIPIENT_TAG = 'recipient';
   67 my $RECIPIENTS_TAG = 'recipients';
   68 
   69 my $EMAIL_ATTR = 'email';
   70 my $NAME_ATTR = 'name';
   71 my $TYPE_ATTR = 'type';
   72 
   73 my $DAYS_BEFORE_VAL = 'days before';
   74 my $SAME_DAY_VAL = 'same day';
   75 
   76 # Hard-coded value for this event's type (class method)
   77 sub get_type
   78 {
   79     return;
   80 }
   81 
   82 # Number of fields this event adds to its parent (class method)
   83 sub get_nb_fields
   84 {
   85     return 1;
   86 }
   87 
   88 sub valid_name {
   89     my ($class, $new_value) = @_;
   90     return $new_value;
   91 }
   92 
   93 sub new
   94 {
   95     my $class = shift;
   96     my $event_node = shift;
   97     my $id = shift;
   98 
   99     my $self = { "OCCURRING" => 0,
  100                  "XML_NODE" => $event_node,
  101                  "ID" => $id,
  102                  "DATA" => [$id],
  103             };
  104     
  105     bless $self, $class;
  106 
  107     # Create empty data array
  108     my $count = $self->get_nb_fields() - 1;
  109     for (my $i = 0; $i < $count; $i++) {
  110         push(@{$self->{DATA}}, undef);
  111     }
  112 
  113     # Where to send this reminder email
  114     my $recipients = $self->{XML_NODE}->getElementsByTagName($RECIPIENTS_TAG)->item(0);
  115     if (defined($recipients)) {
  116         $self->{RECIPIENTS_NODE} = $recipients;
  117         $self->{RECIPIENTS_CACHE} = $self->get_recipients();
  118     }
  119 
  120     # Process reminders
  121     my $reminders = $self->{XML_NODE}->getElementsByTagName($REMINDERS_TAG)->item(0);
  122     if (defined($reminders)) {
  123         $self->{REMINDERS_NODE} = $reminders;
  124         $self->{REMINDERS_CACHE} = $self->get_reminders();
  125     }
  126 
  127     return $self;
  128 }
  129 
  130 sub get_recipients
  131 {
  132     my $self = shift;
  133 
  134     if (!defined($self->{RECIPIENTS_CACHE})) {
  135         my @recipients = ();
  136 
  137         if (defined($self->{RECIPIENTS_NODE})) {
  138             foreach my $recipient ($self->{RECIPIENTS_NODE}->getElementsByTagName($RECIPIENT_TAG)) {
  139                 my $email = $recipient->getAttribute($EMAIL_ATTR);
  140                 if (defined($email)) {
  141                     my $fname = undef;
  142                     my $lname = undef;
  143                     my $fullname = $recipient->getAttribute($NAME_ATTR);
  144 
  145                     my @name_parts = split(/ /, $fullname);
  146                     $fname = $name_parts[0];
  147                     $lname = $name_parts[-1] if @name_parts > 1;
  148 
  149                     push(@recipients, [$email, $fname, $lname]);
  150                 }
  151             }
  152         }
  153         
  154         $self->{RECIPIENTS_CACHE} = \@recipients;
  155     }
  156     return $self->{RECIPIENTS_CACHE};
  157 }
  158 
  159 sub get_reminders
  160 {
  161     my $self = shift;
  162 
  163     if (!defined($self->{REMINDERS_CACHE})) {
  164         my @reminders = ();
  165 
  166         if (defined($self->{REMINDERS_NODE}))
  167         {
  168             foreach my $reminder ($self->{REMINDERS_NODE}->getElementsByTagName($REMINDER_TAG)){
  169                 my $type = $reminder->getAttribute($TYPE_ATTR);
  170                 
  171                 if ($type eq $SAME_DAY_VAL) {
  172                     push(@reminders, 0);
  173                     
  174                     if ($self->will_occur("")) {
  175                         $self->{WHEN} = "today";
  176                         $self->{OCCURRING}++;
  177                     }
  178                 }
  179                 elsif (($type eq $DAYS_BEFORE_VAL) && 
  180                        ($reminder->getFirstChild()))
  181                 {
  182                     my $days = $reminder->getFirstChild()->getNodeValue();
  183                     push(@reminders, $days);
  184                     
  185                     if ($self->will_occur($days)) {
  186                         if ($days > 1) {
  187                             my $upcoming_date = DateCalc("today", "+${days}days");
  188                             $self->{WHEN} = "in $days days (" . UnixDate($upcoming_date, "%A %b %e") . ")";
  189                         }
  190                         elsif ($days == 1) {
  191                             $self->{WHEN} = "tomorrow";
  192                         }
  193                         elsif ($days == 0) {
  194                             $self->{WHEN} = "today";
  195                         }
  196                         else { 
  197                             next; # Negative days are ignored
  198                         } 
  199                         
  200                         $self->{OCCURRING}++;
  201                     }
  202                 }
  203             }
  204         }
  205 
  206         $self->{REMINDERS_CACHE} = \@reminders;
  207     }
  208 
  209     return $self->{REMINDERS_CACHE};
  210 }
  211 
  212 sub is_occurring
  213 {
  214     my $self = shift;
  215     return $self->{OCCURRING};
  216 }
  217 
  218 sub get_message
  219 {
  220     my $self = shift;
  221 
  222     # destination user
  223     my $first_name = shift || 'there';
  224 
  225     my $body = $self->get_message_body(@_);
  226 
  227     my $message = "";
  228     $message = <<"MESSAGEEND" if $body;
  229 Hi $first_name,
  230 
  231 $body
  232 Have a good day!
  233 
  234 --
  235 Sent by Email-Reminder $EmailReminder::Utils::VERSION
  236 https://launchpad.net/email-reminder
  237 MESSAGEEND
  238 
  239     return $message;
  240 }
  241 
  242 sub data
  243 {
  244     my $self = shift;
  245     return $self->{DATA};
  246 }
  247 
  248 sub get_id
  249 {
  250     my $self = shift;
  251     return $self->{ID};
  252 }
  253 
  254 1;