##############################################################################
# M I S C E L L A N E O U S C H A N G E S #
# #
# Last Modified: 20 Feb 2006 #
##############################################################################
# #
# -- Sorts the user list in the admin display #
# -- Allows you to get dates other than the current date #
# -- Ignores a default "http://" in fields if the user has not entered a #
# URL. #
# -- Adds a utility subroutine that you might need to use -- urlencode #
# -- Adds capability for multiple selections in a select field #
# -- Inserts userid into form before record is added if there is an #
# auth_user_field defined #
# #
##############################################################################
################################################################################
######## script: db.cgi ########
######## replace ########
######## part of ########
######## sub admin_display ########
######## ########
######## What it does -- ########
######## sorts the usernames in the admin select list ########
################################################################################
# If we are inquiring, let's look for the specified user.
my (@data, $user_list, $perm, $password);
$user_list = qq~";
# Build the permissions list if we haven't inquired in someone.
################################################################################
######## script: db.cgi ########
######## replace ########
######## sub get_date ########
######## ########
######## What it does -- ########
######## allows you to pass a value to the subroutine in order to return########
######## a date other than the current one ########
################################################################################
sub get_date {
# --------------------------------------------------------
# Returns the date in the format "dd-mmm-yyyy".
# Warning: If you change the default format, you must also modify the &date_to_unix
# subroutine below which converts your date format into a unix time in seconds for sorting
# purposes.
my ($time1) = $_[0];
($time1) or ($time1 = time());
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time1);
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;
return "$day-$months[$mon]-$year";
}
################################################################################
######## script: db.cgi ########
######## replace ########
######## sub parse_form ########
######## ########
######## What it does -- ########
######## removes default "http://" if the rest of the URL is empty ########
######## eliminates false multiple selections ########
################################################################################
sub parse_form {
# --------------------------------------------------------
# Parses the form input and returns a hash with all the name
# value pairs. Removes SSI and any field with "---" as a value
# (as this denotes an empty SELECT field.
my (@pairs, %in);
my ($buffer, $pair, $name, $value);
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else {
&cgierr ("This script must be called from the Web\nusing either GET or POST requests\n\n");
}
PAIR: foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s///g; # Remove SSI.
if ($value eq "---") { next PAIR; } # This is used as a default choice for select lists and is ignored.
if ($value eq "http://") { next PAIR; } # Removes default beginning of URLs
unless ($value) { next PAIR; } # Eliminates false multiple selections
(exists $in{$name}) ?
($in{$name} .= "~~$value") : # If we have multiple select, then we tack on
($in{$name} = $value); # using the ~~ as a seperator.
}
return %in;
}
################################################################################
######## script: db.cgi ########
######## add ########
######## sub urlencode ########
######## ########
######## What it does -- ########
######## Escapes a string to make it suitable for printing as a URL. ########
################################################################################
sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.
#
my($toencode) = shift;
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
return $toencode;
}
##########################################################################################
######## script: db.cgi ########
######## add ########
######## sub build_multiple_select_field ########
######## ########
######## What it does -- ########
######## Adds multiple selection capability to select fields ########
######## ########
######## How to use it-- ########
######## You must have your options defined in your .cfg file under ########
######## %db_select_fields ########
######## Where you would use ########
######## print &build_select_field("FieldName","$rec{'FieldName'}); ########
######## Use ########
######## print &build_multiple_select_field("FieldName","$rec{'FieldName'},3);########
######## ("3" in the line above is the height of the select field) ########
######## This will not work with the autogenerate feature ########
######## This will probably not work well on a search form. It should only be ########
######## used for adding and modifying. ########
##########################################################################################
sub build_multiple_select_field {
# --------------------------------------------------------
# Builds a SELECT field based on information found
# in the database definition.
#
my ($column, $value, $size) = @_;
my ($size, %values);
$name || ($name = $column);
$size || ($size = 1);
@fields = split (/\,/, $db_select_fields{"$column"});
%values = split (/\Q$db_delim\E/, $value);
($#fields >= 0) or return "error building select field: no select fields specified in config for field '$column'!";
$output = qq|";
return $output;
}
################################################################################
######## script: db.cgi ########
######## sub get_defaults ########
######## ########
######## What it does -- ########
######## inserts the userid into the "add" form before the record ########
######## is added. ########
################################################################################
# After
my (%default);
# add
if ($auth_user_field >= 0) {
$user_field_name = $db_cols[$auth_user_field;
$default{$user_field_name} = $db_userid;
}