package FromUntil; # # Simple gizmo that allows a user to 'request' something (eg. vacation, # a compute server, etc) from a start date until an end date. Associated # with this gizmo are the following fields: # # name - standard gizmo name # description - standard gizmo description # resource - description of resource being requested # * this is not displayed on the portal, but # * is sent in the email going to approver list # subject line- the subject line for the email sent to approvers # approvers - one or more email addresses to send the request to # * if more than one address is present (delimitted # by ',') then the first is To: and the rest are Cc: # # The email to the approvers is From: and Reply-to: of the submitting user, # thus further communication (acceptance, denial, etc) is handled via email # outside the portal. This part could be made more "fancy" I suppose, by # linking this to some sort of resource manager, using approval links in # the email, back to the portal. # # Author: Andrew Peebles # Cortina Systems, Inc. # use strict; use GizmoBuilder; use Sendmail; use vars qw(@ISA); my $version = "1.0"; sub get_version { return $version; } use Metadot qw($USER %FORM $SESSION $HTTP_HEADER_SENT $PARAMS $DISPLAY); use vars qw( $form_data ); @ISA=qw(GizmoBuilder); ### ### $form_data is the data structure used to create form elements ### using the GizmoBuilder::get_form_box() method. ### $form_data = { 'fields' => { 'start_month' => { 'label' => '', 'required' => 0, 'formtype' => 'select', 'selectsize' => 3, 'selectoptions' => "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", 'selectdefault' => "Jan", }, 'start_day' => { 'label' => '', 'required' => 0, 'formtype' => 'select', 'selectsize' => 3, 'selectoptions' => "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31", 'selectdefault' => "1", }, 'start_year' => { 'label' => '', 'required' => 0, 'formtype' => 'select', 'selectsize' => 3, 'selectoptions' => "2005,2006,2007,2008,2009,2010,2011,2012", 'selectdefault' => "2005", }, 'end_month' => { 'label' => '', 'required' => 0, 'formtype' => 'select', 'selectsize' => 3, 'selectoptions' => "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", 'selectdefault' => "Jan", }, 'end_day' => { 'label' => '', 'required' => 0, 'formtype' => 'select', 'selectsize' => 3, 'selectoptions' => "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31", 'selectdefault' => "1", }, 'end_year' => { 'label' => '', 'required' => 0, 'formtype' => 'select', 'selectsize' => 3, 'selectoptions' => "2005,2006,2007,2008,2009,2010,2011,2012", 'selectdefault' => "2005", }, }, }; # # Initialize the form_data structure so that the select defaults # match the date right now. # sub init_to_now { my $self = shift; my $ds = shift; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( time() ); $ds->{fields}->{start_day}->{selectdefault} = $mday; $ds->{fields}->{start_year}->{selectdefault} = 1900 + $year; $ds->{fields}->{start_month}->{selectdefault} = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')[$mon]; $ds->{fields}->{end_day}->{selectdefault} = $mday; $ds->{fields}->{end_year}->{selectdefault} = 1900 + $year; $ds->{fields}->{end_month}->{selectdefault} = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')[$mon]; } # # Inherit my permissions # sub customize_permissions { my $class = shift; my $AC = shift; $class->SUPER::customize_permissions($AC); # # Allows anyone who sees the gizmo to use it. # $AC->add_op('DISP','submit'); } # # My displayed gizmo name # sub get_gizmo_name { return "From/Until"; } # # Constructor ... # sub new { my $proto = shift; my $class = ref($proto) || $proto; my $args = $class->normalize_constructor_args(shift); my $id = $args->{id}; my $self; if (defined ($id)){ return $class->restore($id); } else { $self = $class->SUPER::new($args); } # # My field definitions # my $instructions = "
The Resource field is used to indicate what \"resource\" is being requested (for example, \"Paid time off\").
"; $instructions .= "It is not displayed on the web portal, but it is sent to the approver as part of the request email.

"; my $e_ins = "
The Approver Email field should contain the valid email address of the person who will approve or deny the request for this resource. The field may contain multiple email addresses, delimitted by comma: ','. If more than one email address is specified, the first will be the To:, and the rest will become the Cc:

"; $self->set_field_info ( 'name', '
Name', 1, 'description', 'Description', 0, 't2', $instructions.'Resource', 1, 't3', 'Subject line to approver', 1, 't1', $e_ins.'Approver Email', 1, ); $self->set_field_type( 't1', 'text' ); $self->set_field_type( 't3', 'text' ); bless ($self, $class); $self->set_is_a($class); return $self; } sub get_resource { my $self = shift; return $self->{t2}; } sub get_subject_line { my $self = shift; return $self->{t3}; } sub get_approver_field { my $self = shift; return $self->{t1}; } sub get_email_addresses { my $self = shift; my $field = shift; $field =~ s/\s+//g; my @addrs = split( /,/, $field ); if ( $#addrs == -1 ) { $addrs[0] = $field; } return @addrs; } sub is_valid_email_address { my $self = shift; my $email = shift; if ( $email =~ /(.+)\@(.+)\.com/ ) { return 1; } else { return 0; } } # # Called to render the user-visible html. If called by show_summary(), # will display the edit buttons. # sub show { my $self = shift; my $buttons = shift; # optional my $isa = $self->is_a(); my $iid = $self->get_iid(); my $name = $self->get_name(); my $html = ''; if ( $buttons ) { $html .= "$buttons"; } # Super class show() method just displays name and description # fields. Useful for initial debug. # $html .= $self->SUPER::show(); # $html .= "
approver: " . $self->get_approver_field(); # initialize the form_data structure to show today's date # $self->init_to_now( $form_data ); # create the form # $html .= "$name
"; # if ( $self->get_description() ) { # $html .= $self->get_description() . "
"; # } $html .= "
"; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= "
Start Date:"; $html .= $self->get_form_box( 'start_month', $form_data ); $html .= ""; $html .= $self->get_form_box( 'start_day', $form_data ); $html .= ""; $html .= $self->get_form_box( 'start_year', $form_data ); $html .= "
End Date:"; $html .= $self->get_form_box( 'end_month', $form_data ); $html .= ""; $html .= $self->get_form_box( 'end_day', $form_data ); $html .= ""; $html .= $self->get_form_box( 'end_year', $form_data ); $html .= "
"; return $html; } # # Called when in edit mode, should get the edit buttons and call # the normal show(), passing in the buttons. # sub show_summary { my $self = shift; my $buttons = $self->get_buttons(); return $self->show( $buttons ); } # # This is the form submit action ... # sub www_submit { my $self = shift; my $approvers = $self->get_approver_field(); my $start_date = $FORM{start_month} . " " . $FORM{start_day} . ", " . $FORM{start_year}; my $end_date = $FORM{end_month} . " " . $FORM{end_day} . ", " . $FORM{end_year}; my $resource_msg = $self->get_resource(); my $email_submitter = $USER->email(); my $submitter_name = $USER->fullname(); ## Clean html comments from resource field ... $resource_msg =~ s/\<\!.+\>//g; my @addrs = $self->get_email_addresses( $approvers ); my $email_approver = shift( @addrs ); my $is_approver_valid = $self->is_valid_email_address( $email_approver ); my $is_submitter_valid = $self->is_valid_email_address( $email_submitter ); my $confirm_msg = ""; $confirm_msg .= "
Thank you $submitter_name. Your request for

"; $confirm_msg .= "from $start_date thru $end_date has been sent to $email_approver for approval."; if ( ! $is_submitter_valid ) { $self->print_click_back( "Problem", "You do not appear to have a valid email address: $email_submitter." ); return; } if ( ! $is_approver_valid ) { $self->print_click_back( "Problem", "The approver email ($email_approver) is not valid." ); return; } # # Mail ... # my $subject = $self->get_subject_line(); my $approver_msg = "$submitter_name requests:\n"; $approver_msg .= "\n$resource_msg\n\n"; $approver_msg .= "From $start_date through $end_date.\n\n"; $approver_msg .= "Please Reply to this email to approve or deny the request.\n"; my $cc = ''; if ( $#addrs != -1 ) { $cc = join( ',',@addrs ); } my $sm = Sendmail->new( $email_approver, $cc, '', $subject, $approver_msg ); $sm->set_from( $email_submitter ); $sm->set_reply_to( $email_submitter ); if ( $sm->sendMail() ) { # success } else { $DISPLAY->print_click_back("Error: An error occured while trying to send an email notification.

Please contact the system administrator of this portal."); return; } $DISPLAY->print_choices( $confirm_msg, "confirm", "return" ); } 1;