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 .= "