#!/usr/local/bin/perl
#
###########################################################################
# Define directory path to your setup file.
# Use relative path if you can
$path = ".";
# require setup file and cgi-lib library file
require "$path/dogshop.setup";
require "$path/dclib.pl";
require "$path/cgi-lib.pl";
# Read in formdata
ReadParse();
# Define global variables
$productID = $in{'productID'};
$category = $in{'category'};
$in{'cart_dir'} = $cart_dir;
# Gav added:
if ($in{'action'} eq "") {
$in{'action'} = "view_category";
$category = 0;
$in{'database'} = "food"
}
# Get cookie - that is, cart id
$cart_id = get_cookie($key);
# Check and see if action is either
# check_out or re_calc
if ($in{'check_out'}) {
unless ($cart_id) {
print "\n";
print "empty_cart";
exit(0);
}
print "Location: $checkout_script?$cart_id\n\n";
exit;
}
elsif ($in{'re_calc'}) {
$in{'action'} = "re_calc";
}
elsif ($in{'empty'}) {
$in{'action'} = "empty_cart";
}
# data file path and name - no need to create it
# This program will look for database file, setup file
# and counter file. The only file needed is the setup file
if ($in{'database'}) {
$database = $in{'database'} . "\.txt";
$setup = $in{'database'} . "\.setup";
require "$datadir/$setup";
$datafile = "$datadir/$database";
}
# Send HTTP header to the server
# Note that the output header it not
# terminated yet. Must print at least one more
# \n ...
print "Content-type: text/html\n";
if ($in{'action'} eq "view_category") {
# print end of HTTP header
print "\n";
$header = $h_cat;
$sub_header .= qq~
$sh_cat
~;
$html_output .= list_category($datafile);
}
elsif ($in{'action'} eq "add_to_cart") {
# Add to Cart - return $cart_id
# If $card_id is not present, then
# get new cart id, create a cart, and set cookie
unless ($cart_id) {
$cart_id = get_cart_id();
create_file("$cart_dir/$cart_id.cart");
send_cookie($key,$cart_id,$expires,$domain);
}
if (! -e "$cart_dir/$cart_id.cart") {
create_file("$cart_dir/$cart_id.cart");
}
# End HTTP header
print "\n";
$qty = $in{'Quantity'};
$line = get_cart_item(\%in);
my @row = split (/\|/,$line);
# Read in items into reference array
my $r_cart = readdata("$cart_dir/$cart_id\.cart");
my $duptag = "Off";
foreach (@{$r_cart}) {
chomp;
my @testrow = split /\|/;
my $j = -1;
foreach (@row) {
$j++;
if ($cart_fields[$j] eq "ID") {
$id = $_;
if ($id eq $testrow[0]) {
$duptag = "On";
}
}
}
}
if ($duptag eq "Off") {
appenddata("$cart_dir/$cart_id.cart",$line);
# Display output
$header = $h_cart;
$sub_header .= qq~
Your item has been added to the cart
Your cart now contains following items
$sh_cart
~;
}
else {
$header = $h_cart;
$sub_header .= qq~
You have already ordered a quantity of that item.
Your may change the amount ordered from this screen:
~;
}
#
# Finally display output to the browser
#
$imageurl = $imgurl;
my %namespace = (
IMAGES => $imageurl,
HEADER => $header,
SUBHEADER => $sub_header,
MENU => $menu,
HTMLOUTPUT => $html_output );
display_output($templatefile,\%namespace);
exit(0);
############################################################################
####################### END OF THE MAIN PROGRAM ############################
############################################################################
#####
# list_category
#####
sub list_category {
my ($datafile) = shift;
my ($html_output);
# Get all items
my $r_data = readdata($datafile);
# Prepare HTML text
$html_output .= qq~
~;
}
# Since first row of $datafile contains fields
# shift out Fields
my $fields = shift @{$r_data};
# Now, go thru each record in $datafile
# and print those that have correct category
foreach (@{$r_data}) {
chomp;
my @row = split /\|/;
# Put record in $r_rowdata hash reference
foreach ($j=0; $j<@row ;$j++) {
$r_rowdata->{$link_fields[$j]} = $row[$j];
}
if ($row[$cat_id] eq $category) {
&toggle_color();
$html_output .= "
~;
return $html_output;
}
#####
#
# function get_cart_item
# Gets one record from the database
# and returns it so that it can be
# append to the shopping cart
#
#####
sub get_cart_item {
my $r_in = shift;
# get product record and put it in a hash
my ($record,$r_fields) = get_row($datafile, $productID);
my @row = split (/\|/,$record);
foreach ($j=0; $j<@row ;$j++) {
$r_rowdata->{$link_fields[$j]} = $row[$j];
if ($link_fields[$j] =~ /OPT_(.*)/) {
push(@option, "$1=$r_in->{$link_fields[$j]}" );
}
}
foreach (@cart_fields) {
if ($_ eq "Quantity") {
push(@temp,"$r_in->{'Quantity'}");
}
elsif ($_ eq "Options") {
if (@option) {
$option = join(",",@option);
push(@temp,"$option");
} else {
push(@temp,"NA");
}
}
else {
push(@temp,"$r_rowdata->{$_}");
}
}
$item = join("\|",@temp);
return $item;
}
#####
# function get_row
#####
sub get_row {
my ($datafile,$productID) = @_;
my $r_data = readdata($datafile);
my $fields = shift(@{$r_data});
chomp($fields);
my $r_fields = [ split(/\|/,$fields) ];
foreach (@{$r_data}) {
chomp;
if ($_ =~ /^$productID\|/) {
return ($_,$r_fields);
}
}
return 0;
}
############################################################################
#
# subroutine re_calc - re-calculates the
# items in the cart
#
# Input:
# $r_in - reference to %in
# $value - $cookie{'customerID'}
# Output:
# print $template to STDOUT
#
############################################################################
sub re_calc {
my ($r_in,$cart_id) = @_;
my ($html_output,@new);
# Stript off items to recalc
# If quantity is less than or equal to 0 then remove it
my $r_data = readdata("$cart_dir/$cart_id.cart");
foreach (keys %{$r_in}) {
if ($_ =~ /Quantity_(.*)/) {
if ($r_in->{$_} > 0) {
$quantity{$1} = $r_in->{$_};
}
}
}
foreach $item (@$r_data) {
chomp($item);
my @items = split(/\|/,$item);
foreach (keys %quantity) {
if ($items[0] eq $_) {
$items[$quantity_key] = $quantity{$_};
$item = join("\|",@items);
push(@new,"$item\n");
last;
}
}
}
writedata("$cart_dir/$cart_id.cart",\@new);
}
############################################################################
#
# subroutine display_main
# -- Displays the main page
# Input:
#
# $datafile - database file name
# $r_category_list - reference to %category_list
# $r_category_desc - reference to %category_desc
# $r_in - reference to %in - the form variable
#
# Output:
#
#
############################################################################
sub display_main {
my ($datafile, $r_category_list,$r_category_desc,$r_in) = @_;
my $output;
# Read in all the rows of data
my $r_data = readdata($datafile);
# Determine the number of items in each category
foreach (@$r_data) {
my @rowdata = split(/\|/,$_);
$category_num{$rowdata[$cat_id]}++;
}
$output .= qq~
~;
if ($buyFlag) {
$search_replace{"Cart"} =
"[ {'database'}&action=product_info&productID=$productID\">Add to Cart ]";
}
$output =~ s//
my $value = $search_replace{$1};
$value;
/ge;
$output;
}
############################################################################
#
# subroutine display_menu
# Input:
#
#
# Output:
#
#
############################################################################
sub display_menu {
my ($level, $r_in, $r_category_list) = @_;
my ($menu);
# $level eq "0" means that the menu we create is for the
# main category level. In this case, display links to
# the top level categories. Otherwise, link to each sub categories
if ($level eq "0") {
$menu .= qq~
~;
# foreach (sort keys %$r_category_list) {
# my $database = $r_category_list->{$_}; # database name
# $menu .= qq~
# [$_] ~;
# }
}
else {
$menu .= qq~
~;
# foreach (sort keys %$r_category_list) {
# my $sub_cat = $r_category_list->{$_}; # $sub_cat - sub category
# $menu .= qq~
# [$_]
# ~;
# }
}
# print out search engine at the top
# $menu .= qq~
#
#
#
# ~;
# return reference to menu text
$menu;
}
############################################################################
#
# subroutine keyword_search
#
# Input:
#
#
# Output:
#
#
############################################################################
sub keyword_search {
my($r_super_cat,$r_keywords,$r_in) = @_;
my($output);
# For each database, we need to scan for key word hit
my $yeshits = "";
foreach $file (values %$r_super_cat) {
# Initialize
require "$datadir/$file.setup";
my $anyhit = "";
my $datafile = "$datadir/$file.txt";
my $r_data = readdata($datafile);
my $buffer;
$r_in->{'database'} = $file;
$j=0;
foreach (@category) {
$j++;
$category_list{$_} = $j;
}
# Get rid of the first line of database
# which just contains field names
my $fields = shift(@{$r_data});
chomp($fields);
my @link_fields = split(/\|/,$fields);
foreach $line (@{$r_data}) {
chomp $line;
my $hit = "";
foreach (@{$r_keywords}) {
if ($line =~ /\b$_\b/i) {
$hit = "yes";
$anyhit = "yes";
$yeshits = "something";
last;
}
}
if ($hit) {
&toggle_color();
my($r_rowdata) = {};
my @row = split(/\|/,$line);
foreach ($k=0; $k<@row; $k++) {
$r_rowdata->{$link_fields[$k]} = $row[$k];
}
$buffer .= "