# libmeta.pl Copyright Les Richardson, 2008
# Part of Open Admin for Schools, Licensed under the GPL.
sub metaput { # return HTML for data entry
use Crypt::GeneratePassword qw(word);
my ($table, $field, $studnum, $mode, $DBH ) = @_;
# passed table, field, studnum (to look up values),
# $mode uses checkboxes in 'add' mode, but disables to text entry in 'edit' mode.
$tableref = $table; # needed because no entries in meta for prereg or studentwd..
if ($table eq 'studentwd' or $table eq 'prereg'){ $tableref = 'student';}
if ( $DBH ) { # if passed filehandle, use it.
$dbh = $DBH;
}
# Find the record in the meta table
my $sth = $dbh->prepare("select id, arrayidx, size, defaultvalue,
formtype, viewsize from meta where tableid = ? and fieldid = ?");
$sth->execute( $tableref, $field);
if ($DBI::errstr){print $DBI::errstr; die $DBI::errstr;}
my ($id, $index, $size, $default, $formtype, $viewsize) = $sth->fetchrow;
if ( not $id ){
print " Error: missing value for Table: $table ";
print "Field: $field \n";
die;
}
my $fv;
if ( $studnum and $tableref eq 'student' ){ # passed student number; find value.
# Find the record in the student,studentwd,prereg table
my $sth = $dbh->prepare("select $field from $table
where studnum = ? ");
$sth->execute( $studnum );
if ($DBI::errstr){print $DBI::errstr; die $DBI::errstr;}
$fv = $sth->fetchrow;
} elsif ( $studnum ) { # lookup value in another table using record id
my $sth = $dbh->prepare("select $field from $table
where id = ? ");
$sth->execute( $studnum );
if ($DBI::errstr){print $DBI::errstr; die $DBI::errstr;}
$fv = $sth->fetchrow;
} elsif ( $field eq 'passwd' or $field eq 'password' ) { # put in a password
# These values now come from the admin.conf file...
#$password_minlen = 6;
#$password_maxlen = 6;
#$password_lang = 'en';
#$password_signs = 1;
#$password_caps = 2;
#$password_minfreq = .001;
#$password_avgfreq = .001;
$fv = word( $password_minlen, $password_maxlen, $password_lang, $password_signs, $password_caps, $password_minfreq, $password_avgfreq );
}
if (lc($formtype) eq 'text'){ # text input box
my $rv = "\n";
return $rv;
} elsif (lc($formtype) eq 'select'){ # submit element
my $rv = ""; # now include blank;
# above needed in edit mode when removing a value (reset to blank)
return $rv;
} elsif (lc($formtype) eq 'textarea'){ # textarea element
$viewsize =~ s/\s//g; # strip any space.
my ($rows, $cols);
my ($row,$col) = split /x/,$viewsize;
if ($row){ $rows = "rows=\"$row\""; }
if ($col){ $cols = "cols=\"$col\""; }
return "";
} elsif (lc($formtype) eq 'checkbox'){ # checkbox element
if ($mode eq 'edit'){
my $rv = "\n";
return $rv;
} else {
my $rv = "\n";
return $rv;
}
} else { # unknown form element
print "Error: Unknown form element type: $formtype for ";
print "table: $table field: $field. ";
print "We can handle text, textarea, checkbox, and select only! \n";
die;
}
} # End of MetaPut
# Don't use anymore... sub metaReplace { # return HTML replacement text.
sub metaReplace { # return HTML replacement text.
my ($table,$field) = @_;
# passed table, field
# Find the record in the meta table
my $sth = $dbh->prepare("select fieldname, required from meta
where tableid = '$table' and fieldid = '$field'");
$sth->execute;
if ($DBI::errstr){print $DBI::errstr; die $DBI::errstr;}
my ($fieldname,$required) = $sth->fetchrow;
#print "VAL: $fieldname REQ: $required \n";
if (not $fieldname){
print " Error: missing value for Table: $table";
print " Field: $field \n";
die;
}
if (uc($required) eq 'Y'){
return "$fieldname";
} else {
return $fieldname;
}
} # End of MetaReplace
1;