#!/usr/bin/perl # Copyright 2001-2007 Leslie Richardson # This file is part of Open Admin for Schools. # Open Admin for Schools is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. %lex = ( 'Staff Member Entry' => 'Staff Member Entry', 'Member Entry' => 'Member Entry', 'Staff' => 'Staff', 'Main' => 'Main', 'Eoy' => 'Eoy', 'Unable to open template file:' => 'Unable to open template file:', 'The staff member is now stored in the database' => 'The staff member is now stored in the database', 'Start/End of Year Page' => 'Start/End of Year Page', 'Add Another Staff Member' => 'Add Another Staff Member', 'Add Another Prereg Staff Member' => 'Add Another Prereg Staff Member', 'Sorry. There was an error storing your data.' => 'Sorry. There was an error storing your data.', 'Please record the following error string' => 'Please record the following error string', 'Please contact' => 'Please contact', 'Cannot open libmeta.pl' => 'Cannot open libmeta.pl', 'Cannot open admin.conf!' => 'Cannot open admin.conf!', 'Add Staff Member Information' => 'Add Staff Member Information', 'Warning! You should have a unique userid for each staff.' => 'Warning! You should have a unique userid for each staff.', 'This userid exists already exists.' => 'This userid exists already exists.', 'Ignore this warning if you are creating another entry' => 'Ignore this warning if you are creating another entry', 'for the same staff.' => 'for the same staff.', 'You MUST have a unique userid and staff position.' => 'You MUST have a unique userid and staff position.', 'Please go back and try again.' => 'Please go back and try again.', 'Preregistration' => 'Preregistration', 'Required Field' => 'Required Field', 'Bold' => 'Bold', ); use CGI; use DBI; my $self='staffadd.pl'; my $q = new CGI; print $q->header; my %arr = $q->Vars; # Select table to add to; staff or prereg_staff; my ($table, $tableview); if ( $arr{tbl} ){ # add to prereg_staff table $table = 'prereg_staff'; $tableview = ''. $lex{Preregistration}. ''. q{ }. $lex{Staff}; } else { $table = 'staff'; $tableview = $lex{Staff}; } unless (require "../../etc/admin.conf") { print $lex{'Cannot open admin.conf!'}; die "Cannot open admin.conf!"; } unless (require "../../lib/libmeta.pl") { print $lex{'Cannot open libmeta.pl'}; die $lex{'Cannot open libmeta.pl'}; } $dsn = "DBI:$dbtype:dbname=$dbase"; $dbh = DBI->connect($dsn,$user,$password); # Print Page Header print "$doctype\n\n"; print $lex{Staff}. q{ }. $lex{'Member Entry'}; print "\n"; print "$chartype\n[ "; print $lex{Main}, " | "; print $lex{Eoy}, " ]

"; print $tableview. q{ }. $lex{'Member Entry'}. "

\n"; if ( $arr{writeflag} ) { delete $arr{writeflag}; writeRecord(); } print "

". $lex{Bold}. " = ". $lex{'Required Field'}. "


\n"; # Read in Template unless (open (FH,"<../../template/staff.tpl")) { print $lex{'Unable to open template file:'},"$!\n"; die $lex{'Unable to open template file:'},"$!\n"; } my $text; { local $/; $text = ; close FH;} # Parse for simple field replacement / highlighting <*name*> elements while ( $text =~ m/\<\*(.*)\*\>/g){ push @replace, $1; } #print "
Replacements:
\n",@replace,"
\n"; # Create Hash to hold replacement text. foreach my $val (@replace){ $replacement{$val} = metaReplace('staff',$val); } #print "
Replacement Text
\n",%replacement,"
\n"; # Now put replacement text back in. $text =~ s{\<\*(.*?)\*\>} { exists($replacement{$1}) ? $replacement{$1} : $1 }gsex; # now parse for form entry replacement elements <@name@> # Extract fields from template while ( $text =~ m/\<\@(.*)\@\>/g){ push @fields, $1; } # Preprocess Template for Values #$text =~ s/\<\@studnum\@\>/$studnum/; # get replacement values for fields foreach my $val (@fields){ $values{$val} = &metaput('staff',$val,$studnum,'edit'); } # now put field values back into $text variable... $text =~ s{ \<\@(.*?)\@\> } { exists($values{$1}) ? $values{$1} : "$values{$1}-$1" }gsex; # print top of form, then $text, then bottom of page. print "
\n"; if ( $table eq 'prereg_staff' ) { print "\n"; } print $text,"\n"; print "\n"; print "
\n"; #-------------- sub writeRecord { #-------------- #foreach my $key (keys %arr) { print "K:$key V:$arr{$key}
\n"; } # Make sure we have userid and position filled.... if ( not $arr{userid} or not $arr{position} ){ print "

". $lex{'You MUST have a unique userid and staff position.'}. "\n"; print $lex{'Please go back and try again.'}. "

\n"; exit; } # Make sure userid doesn't already exist...else warn. $sth = $dbh->prepare("select count(*) from $table where userid = ?"); $sth->execute( $arr{userid} ); if ($DBI::errstr) { print $DBI::errstr; die $DBI::errstr; } if (my $count = $sth->fetchrow) { print "

"; print $lex{'Warning! You should have a unique userid for each staff.'}; print "
". $lex{'This userid exists already exists.'}; print "
\n"; print $lex{'Ignore this warning if you are creating another entry'}. "
\n"; print $lex{'for the same staff.'}. "

\n"; } # Create an entry array called "@fields" storing fieldid values. $sth = $dbh->prepare("select fieldid from meta where tableid = 'staff' order by arrayidx"); $sth->execute; if ($DBI::errstr){ print $DBI::errstr; die $DBI::errstr; } while (my $fld = $sth->fetchrow){ push @fields,$fld; } # Populate Hash "%values" with key (ie. fieldid) and value as value foreach my $key (keys %arr){ my ($tb,$fld) = split /:/,$key; if ($arr{$key} and $key){ $values{$key} = $arr{$key};} #print "K: $key FLD: $fld VAL: $arr{$key}
\n"; } #print "

Hash Values:\n"; #print %values,"
\n"; # Now run through @fields array and repopulate with values from hash. foreach my $fld (@fields){ if ($values{$fld}){ # if matching hash value exists, use it. $fld = $dbh->quote( $values{$fld} ). q{,}; # fieldid replaced with actual field value. } else { $fld = "$sql{default},"; # NULL value } } chop $fields[$#fields]; # chop trailing comma. #print "

Populated Fields Array:",@fields,"
\n"; $sth = $dbh->prepare("insert into $table values (@fields)"); $sth->execute; if (not $DBI::errstr ) { print "

"; print $lex{'The staff member is now stored in the database'}; print ".

"; } else { print "

".$lex{'Sorry. There was an error storing your data.'}; print $lex{'Please contact'}. " $adminname - $adminemail\n"; print $lex{'Please record the following error string'}. " $DBI::errstr"; print "

\n"; } print "

[ ".$lex{Main}." | "; print $lex{'Start/End of Year Page'}; print " | \n"; if ( $arr{tbl} ) { print ""; print $lex{'Add Another Prereg Staff Member'}." ]\n"; } else { print "". $lex{'Add Another Staff Member'}; print " ]\n"; } print "

\n"; exit; }