#!/usr/local/bin/perl
# Programe update.cgi
# Description - provides the ability to add new wines to the
# wine file.
# Input: URL-encoded data from a form generated by this program
# Output: An HTML document containing a form with the input data
# inserted together with the input data fields and error messages
# if data is valid, a new record is appended to the wine.dat file
#
# Author Chris Wallace
# Date: 22 February
#
# implementation notes:
# this program relies on uninitialised variables being blank
#
# change history
#
# modified 2 Feb 1999
# set default values
use CGI qw(:standard);
&getFields;
@params = param();
if ($#params > 0) { # completed form
&validateFields;
if (!$error) {
$reply="Wine added successfully";
&addRecord;
}
}
else {
&setDefaults;
}
&outputForm;
#-----------------------------------------------------------------
# get fields from URL encoded data into variables.
# Remove leading and trailing spaces
sub getFields {
$ref=&trim(param('ref'));
$name=&trim(param('name'));
$description=param('description');
# get rid of redundent new lines
$description =~s/\n/ /g;
$sweetness=&trim(param('sweetness'));
$fullness=&trim(param('fullness'));
$bottlePrice=&trim(param('bottlePrice'));
$casePrice=&trim(param('casePrice'));
$vintage=&trim(param('vintage'));
$newWine=param('newWine');
if ($newWine ) { $newWine = "checked";}
$newVintage=param('newVintage');
if ($newVintage ) { $newVintage = "checked";}
}
sub setDefaults {
$ref=$name=$description=$sweetness=$fullness=$bottlePrice=$casePrice=
$vintage="";
$newWine=$newVintage="";
}
#--------------------------------------------------------------------
sub trim {
local($str) = $_[0];
$str=~s/^\s*//;
$str=~s/\s*$//;
return $str;
}
#------------------------------------------------------------------
# validate all fields. Some are mandatory and all must conform to
# a pattern of characters
# error messages are left in $M variables for inclusion in the form
#
sub validateFields {
if ($ref !~/^[A-Z][A-Z]\d+$/) {$Mref = 'invalid';}
if (!$name) {$Mname='missing';}
if (!$description) {$Mdescription = 'missing';}
if ($sweetness && $sweetness !~/^[1-7]$/) {$Msweetness = 'invalid';}
if ($fullness && $fullness !~/^[ABC]$/) {$Mfullness = 'invalid';}
if (!$bottlePrice) { $MbottlePrice='missing';}
elsif ($bottlePrice !~/^\d+\.\d\d$/) {$MbottlePrice = 'invalid';}
if (!$casePrice) { $McasePrice = "missing";}
elsif ($casePrice !~/^\d+\.\d\d$/) {$McasePrice = 'invalid';}
if ($vintage && $vintage !~/^\d\d\d\d$/) {$Mvintage = 'invalid';}
$error =$Mref.$Mname.$Mdescription.$Msweetness.$Mfullness.$MbottlePrice.$McasePrice.$Mvintage;
}
#-------------------------------------------------------------------------
# add the new record to the wine file. Fields are tab separated
sub addRecord {
open(WINES,'>>wine.dat');
print WINES "$ref\t$name\t$description\t$sweetness\t$fullness\t$bottlePrice\t$casePrice\t$vintage\t$newWine\t$newVintage\n";
}
#--------------------------------------------------------------------
#generate the HTML document containing the input form, together with
# the input values(if any) and error messages
#
sub outputForm {
print <Frenchay Wine Society - Data Entry
Frenchay Wine Society - Data Entry
$reply