package CGI_mis;
use URI::Escape;
# I've only included two functions that I believe are key to the solution:
################################################################################
#
# Name: GetAllParams
#
# Parameters:
#
# [I] $script - Name of script parameters have been passed to.
#
# Returns:
#
# Hash Ref - Containing all the parameters.
#
# Implementation:
#
# Gets all the parameters passed to the script, plus the default parameters
# for this script, provided they have not been overridden. Parameters with
# multiple values are held in an array reference, as are single valued
# parameters that are known to be able to hold multiple values.
#
# Params are got from CGI query, database defaults then PageConfig defaults
# in that order.
#
################################################################################
sub GetAllParams
{
my $script = shift || basename($0);
my %all_params;
my @params = $query->param();
my $p;
my $db_defaults;
foreach $p (@params)
{
@{$all_params{$p}} = $query->param($p);
if(scalar(@{$all_params{$p}}) == 1)
{
# Make it scalar unless it's meant to be an array
if((! defined $PageConfig::scripts{$script}->{$p}) ||
ref($PageConfig::scripts{$script}->{$p}->{'default'}) ne 'ARRAY')
{
$all_params{$p} = ${$all_params{$p}}[0];
}
}
}
# Check any defaults that are not set
# check the DB first
$db_defaults = PageConfig::GetDBDefaults($script);
if(defined $db_defaults)
{
foreach $p (keys %$db_defaults)
{
# Set the db default is it has not already been set
if(! defined $all_params{$p})
{
$all_params{$p} = $db_defaults->{$p};
}
}
}
# then the 'default' default
if(defined $PageConfig::scripts{$script})
{
foreach $p (keys %{$PageConfig::scripts{$script}})
{
if(! defined $all_params{$p})
{
$all_params{$p} =
$PageConfig::scripts{$script}->{$p}->{'default'};
}
}
}
return \%all_params;
}
################################################################################
#
# Name: GetQueryString
#
# Parameters:
#
# $all_params_ref - Hash Ref contining all the params to construct the
# query from.
#
# Returns:
#
# A text string of the built up query.
#
# Implementation:
#
# Simple concats the parameters together.
#
################################################################################
sub GetQueryString
{
my $all_params_ref = shift || GetAllParams();
my $q_string = "";
foreach my $p (keys %$all_params_ref)
{
next if (! defined $all_params_ref->{$p});
if(ref($all_params_ref->{$p}) eq 'ARRAY')
{
foreach (@{$all_params_ref->{$p}})
{
$p = uri_escape($p);
$q_string .= "$p=".uri_escape($_)."&";
}
}
else
{
$q_string .= "$p=".uri_escape($all_params_ref->{$p})."&";
}
}
$q_string =~ s/\s/\%20/g if $q_string;
#$q_string =~ s/#/HASH/g;
return $q_string;
}
# end of CGI_mis
#####################################
# this is the line in the front end page that gets all the parameters from the above script
my $all_params_ref = CGI_mis::GetAllParams($script);