#!/usr/bin/perl -w
use CGI qw(:standard -noxhtml);
use CGI::Carp "fatalsToBrowser";
$| = 1;
require '/home/sites/www.scenartech.com/dbconf/constants.pl';
open TMPL,'/home/sites/www.scenartech.com/web/login/template.html';
my $template = join('',);
close TMPL;
my $q = new CGI;
if ( defined $q->param('lastname') ){
my $errorPtr = mandatory($q);
if ( @$errorPtr ) {
form($q,$errorPtr);
} else {
saveData($q);
sendAdmin($q);
thanksPage($q);
}
} else {
form($q);
}
exit;
sub sendAdmin {
my $mailprog = '/usr/sbin/sendmail -t -oi';
open(MAIL, "|$mailprog") or die "Can't open $mailprog!";
open(TEMPLT,$DBCONF{'notifyAdminMessage'}) or die "Can't find email template $DBCONF{'notifyAdminMessage'}: $!";
while ( ) {
s//$ADMINEMAIL/g;
print MAIL;
}
close TEMPLT;
close MAIL;
}
sub form {
my ($q,$errorPtr) = @_;
my $content;
if ( (defined $errorPtr) and @$errorPtr ) {
$content .= table({-cellpadding=>5,-bgcolor=>'#FF9999',-border=>1},
Tr(td(b(big('Error')),br,join(br,@$errorPtr)))
);
}
$content .= start_form(-action=>script_name,-method=>'POST');
$content .= table({-cellspacing=>2,-cellpadding=>0,-border=>0,
-style=>'font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #333366;'},
map {
my $type = $fields{$_}[1];
$type eq 't'
? Tr(td(b($fields{$_}[3])),td($q->textfield(-name=>$_,-size=>40)))
: $type eq 'y'
? Tr(td(b($fields{$_}[3])),td($q->checkbox(-name=>$_,-value=>1,-label=>'Yes')))
: $type eq 'dont'
? Tr(td(b($fields{$_}[3])),td($q->checkbox(-name=>$_,-value=>1,-label=>"Don't show")))
: $type eq 'memtype'
? Tr(td(b($fields{$_}[3])),td($q->popup_menu(-name=>$_,-value=>[sort keys %memberTypes],-labels=>\%memberTypes))).
Tr(td({-colspan=>2},small(qq(If applying to be become a "Friend" please fill in as much as you wish, other than all your contact details. All other applications, please be as detailed as possible and provide copies of your relevant certificates by mail to ISTA, 29 Juer Street, London, England, SW11 4RE.))))
: $type eq 'boxq'
? Tr(td({-colspan=>2},b($fields{$_}[3]).br.
i('[qualification] [where acquired] [trained by whom] [when acquired-dates of course] [days duration]').br.
textarea(-name=>$_,-cols=>80,-rows=>15)))
: $type eq 'boxoq'
? Tr(td({-colspan=>2},b($fields{$_}[3]).br.
i('[qualification] [where acquired] [when acquired]').br.
textarea(-name=>$_,-cols=>80,-rows=>15)))
: $type eq 'boxpi'
? Tr(td({-colspan=>2},b($fields{$_}[3]).br.
i('(Enter any personal information you want shown to the general public.)').br.
textarea(-name=>$_,-cols=>80,-rows=>15)))
: $type eq 'box'
? Tr(td({-colspan=>2},b($fields{$_}[3]).br.
textarea(-name=>$_,-cols=>80,-rows=>15)))
: $type eq 'mf'
? Tr(td(b($fields{$_}[3])),td(radio_group(-name=>$_,-value=>[reverse sort keys %genders],-labels=>\%genders)))
: Tr(td({-colspan=>2},b($fields{$_}[3])))
} sort {
$fields{$a}[0] <=> $fields{$b}[0]
} grep {
$fields{$_}[2] == 0
} keys %fields
);
$content .= p(submit(-name=>'Submit',-value=>'Send form to Scenartech'));
print header;
$template =~ s{}{Join ISTA}i;
$template =~ s{art/heads/subhead_login.jpg}{art/heads/subhead_onlineform.jpg}i;
$template =~ s{alt="subhead graphic"}{alt="Online Application Form"}i;
$template =~ s{\Q}
{$content}i;
print $template;
}
sub mandatory {
my $q = shift;
my @errors;
foreach $f ( keys %fields ) {
next unless defined $fields{$f}[4]; # No validation routine - optional field
my $err = &{$fields{$f}[4]}($q,$f);
next unless defined $err;
push @errors,$err;
}
return \@errors;
}
sub saveData {
use DBI;
my $q = shift;
my $profileDBH = DBI->connect($DBCONF{'memberName'}, $DBCONF{'memberUser'}, $DBCONF{'memberPW'}, {
PrintError => 0,
RaiseError => 0,
} )
or die "Can't connect to member profile database: $DBI::errstr\n";
my @fields = grep { $fields{$_}[2] == 0 } grep { ! m/notused/ } keys %fields;
my $sth = $profileDBH->prepare( "
INSERT INTO profile
(".join(', ',@fields).") VALUES (".
join(', ',map { defined $q->param($_)
? $profileDBH->quote($q->param($_))
: NULL
} @fields).")
" ) or die "Can't prepare statement: $DBI::errstr\n";
$sth->execute or die "Can't execute statement: $DBI::errstr\n";
# Get the new ID number
my $newID = $profileDBH->{'mysql_insertid'};
$q->param(-name=>'sequence',-value=>$newID);
$profileDBH->disconnect or warn "Disconnection from profile failed: $DBI::errstr\n";
}
sub thanksPage {
my $q = shift;
print $q->header,start_html(-title=>'Form has been submitted');
print table({-width=>'100%',-height=>'100%'},
Tr(td({-valign=>'middle',-align=>'center'},
'Thank You',br,
'Your form has been submitted.',br,
'You will receive an email when your application has been processed.',br,
a({-href=>'http://www.scenartech.com/'},'Return to Home Page')
)),
);
print end_html;
}