#!/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

Items in $category[$category]

~; # print menu links to $sub_header $menu = display_menu ("1",\%in,\%category_list); $html_output = list_category($datafile); } elsif ($in{'action'} eq "view_product") { print "\n"; $header = $h_pro; $sub_header = $sh_pro; $menu = display_menu ("1",\%in,\%category_list) ; $html_output = product_display($datafile,$productID,\%in); $html_output .= qq~

Items in $category[$category]

~; $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:
$sh_cart
~; } $html_output = view_cart(\%in,$cart_id); } elsif ($in{'action'} eq "view_cart") { print "\n"; $header = $h_cart; $sub_header .= qq~ Your cart contains following items
$sh_cart
~; if ($cart_id && -e "$cart_dir/$cart_id.cart") { $html_output = view_cart(\%in,$cart_id); } else { $html_output = "

Your cart is empty

"; } } elsif ($in{'action'} eq "product_info") { print "\n"; $menu = display_menu ("1",\%in,\%category_list) ; $header = $h_pro; $sub_header = $sh_pro; $html_output = product_display($datafile,$productID,\%in); } elsif ($in{'action'} eq "empty_cart") { $expires = "Thu, 31-Dec-97 12:00:00 GMT"; &send_cookie($key,$cart_id,$expires,$domain); print "\n"; if ($cart_id) { if (-e "$cart_dir/$cart_id.cart") { unlink("$cart_dir/$cart_id.cart") or my_die("Can't remove $cart_dir/$cart_id.cart",$!); create_file("$cart_dir/$cart_id.cart"); } } $header = $h_empty; $sub_header = $sh_empty; } elsif ($in{'action'} eq "re_calc") { print "\n"; # First read in $cookie{'customerID'} # then call subroutine re_calc $header = $h_cart; $sub_header .= qq~ Your cart contains following items
$sh_cart
~; $cart_id = get_cookie($key); if ($cart_id && -e "$cart_dir/$cart_id.cart") { re_calc(\%in,$cart_id); $html_output = view_cart(\%in,$cart_id); } else { $html_output = "

Your cart is empty

"; } } elsif ($in{'action'} eq "keyword_search") { # End HTTP Header print "\n"; $menu = display_menu ("1",\%in,\%category_list) ; $header = $h_search; $sub_header = $sh_search; #split keywords to multiple words @keywords = split(/\s+/,$in{'keywords'}); $html_output = ""; $html_output .= keyword_search(\%super_cat,\@keywords,\%in); } elsif ($in{'database'}) { print "\n"; # create menu links and assign to sub_header $menu = display_menu("0",\%in,\%super_cat) ; $header = $h_main; $sub_header = $sh_main; $html_output = display_main ( $datafile, \%category_list, \%category_desc, \%in); } else { # End HTTP Header print "\n"; # create menu links and assign to sub_header $menu = display_menu("0",\%in,\%super_cat) ; $header = $h_super; $sub_header = $sh_super; $html_output .= qq~ ~; foreach (sort keys %super_cat) { &toggle_color(); $html_output .= qq~ ~; } $html_output .= qq~
Category Description
$_ $super_cat_desc{$_}
 
~; } # # 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~ ~; foreach (@list_fields) { my $record = $_; $record =~ s/OPT_(.*)/$1 (Option)/; $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 .= "\n"; $html_output .= list_record($r_rowdata,\@link_fields,\@list_fields,\%in); $html_output .= "\n"; } } $html_output .= qq~
$record
 
~; 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~ ~; foreach (sort keys %$r_category_list) { &toggle_color(); $cat1 = $r_category_list->{$_}; if ($category_num{"$cat1"} eq "" ) { $category_num{"$cat1"} = "0"; } $output .= qq~ ~; } $output .= qq~
Category Description
$_ ($category_num{"$cat1"} Items) $r_category_desc->{$_}
 
~; $output; } ############################################################################ # # subroutine view_cart # -- Display items in the cart # Input: # # # Output: # # ############################################################################ sub view_cart { my ($r_in,$cart_id) = @_; my ($output); my ($total_nontaxable, $total_taxable, $total_tax, $final); # Top portion of the output text $output .= qq~
~; foreach (@cart_fields) { unless ($_ eq "Taxable") { $output .= qq~ ~; } } $output .= qq~ ~; # Read in items into reference array my $r_cart = readdata("$cart_dir/$cart_id\.cart"); &toggle_color('reset'); foreach (@{$r_cart}) { chomp; my @row = split /\|/; $output .= "\n"; my $j = -1; my $taxable; foreach (@row) { $j++; if ($cart_fields[$j] eq "ID") { $id = $_; $output .= qq~ ~; } elsif ($cart_fields[$j] eq "Price") { $price = sprintf("%.2f", $_); $output .= qq~ ~; } elsif ($cart_fields[$j] eq "Quantity") { $quantity = $_; $output .= qq~ ~; } elsif ($cart_fields[$j] eq "Weight") { my @tempweight=split(/ /,$_); my $weight = sprintf("%.1f lbs", $tempweight[0]*$quantity); $output .= qq~ ~; } elsif ($cart_fields[$j] eq "Taxable") { $taxable = $_; } elsif ($cart_fields[$j] eq "Options") { $output .= qq~ ~; } else { $output .= qq~ ~; } } toggle_color(); $sub_total = sprintf("%.2f", $price * $quantity); if ($taxable) { $total_taxable += $sub_total; } else { $total_nontaxable += $sub_total; } $output .= qq~ ~; } $total_nontaxable = sprintf("%.2f", $total_nontaxable); $total_taxable = sprintf("%.2f", $total_taxable); $total_tax = sprintf("%.2f", $total_taxable*$sales_tax); $final = sprintf("%.2f", $total_nontaxable + $total_taxable + $total_tax); $output .= qq~
$_Amount
$id \$$price $weight ~; my (@options) = split /,/; foreach (@options) { my ($temp,$opt_price); my ($option,$choice) = split /=/; if ($choice =~ /::/) { ($temp,$opt_price) = split(/::/,$choice); $price += $opt_price; $choice = $temp; $output .= qq~ $option: $choice (+\$$opt_price)
~; } elsif ($_ eq "NA") { $output .= qq~  ~; } else { $output .= qq~ $option: $choice
~; } } $output .= qq~
  $_ \$$sub_total

$taxable
  Total (Non-Taxable): \$$total_nontaxable
  Total (Taxable): \$$total_taxable
  Sales Tax: \$$total_tax
  Total: \$$final

~; $output; } ############################################################################ # # subroutine list_record # -- Displays list single record at at time # Input: # # # Output: # # ############################################################################ sub list_record { my($r_rowdata,$r_link_fields,$r_list_fields,$r_in) = @_; my($output); foreach (@$r_list_fields) { if ($_ eq "Name") { $output .= qq~ $r_rowdata->{$_} ~; } elsif ($_ eq "Price") { $output .= qq~ \$$r_rowdata->{$_} ~; } else { $output .= qq~ $r_rowdata->{$_} ~; } } $output; } ############################################################################ # # subroutine desc_record # -- Displays single record in a detail mode # Input: # # # Output: # # ############################################################################ sub desc_record { my($r_rowdata,$r_link_fields,$r_in,$buyFlag) = @_; # First read in $print_fields to search and replace my $output = $print_fields; my %search_replace = {}; my $option_flag; foreach (@{$r_link_fields}) { if ($_ eq "Image" && $r_rowdata->{$_} ne "") { $search_replace{$_} = "{$_}\" border=\"0\">"; } elsif ($_ eq "Price") { $search_replace{$_} = "Price: \$$r_rowdata->{$_}"; } elsif ($_ =~ /OPT_(.*)/) { unless ($option_flag) { $search_replace{'Option'} .= "Options:

"; $option_flag = "checked"; } my @options = split(/,/, $r_rowdata->{$_}); my $string = "\n"; $search_replace{'Option'} .= "$1: $string
"; } else { $search_replace{$_} = "$r_rowdata->{$_}\n"; } } $search_replace{'Quantity'} .= qq~ Quantity:
 
~; 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~ #

#
# # Keyword Search # #
#
# ~; # 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 .= "\n"; $buffer .= list_record($r_rowdata,\@link_fields,\@list_fields,$r_in); $buffer .= "\n"; } } if ($anyhit) { my $temp_buffer .= qq~ ~; foreach (@list_fields) { $temp_buffer .= qq~ ~; } $temp_buffer .= "\n"; $buffer = $temp_buffer . $buffer . "
Match in category $file
$_ 
\n"; } $output .= $buffer; } unless ($yeshits) { $output = "

No match...try another keyword.

"; } $output; } ############################################################################ # # subroutine product_display # # Input: # # # Output: # # ############################################################################ sub product_display { my($datafile,$productID,$r_in) = @_; my($output); my @opt_fields; $output .= qq~
~; #Read in product data my ($record,$r_fields) = get_row($datafile, $productID); my @row = split (/\|/,$record); my $num_fields = @{$r_fields}; for ($j=0; $j < $num_fields; $j++) { $r_rowdata->{$link_fields[$j]} = $row[$j]; if ($r_fields->[$j] =~ /^OPT_(.*)/) { push(@opt_fields,$r_fields->[$j]); } if ($r_fields->[$j] eq "ID") { $product_id = $j; } elsif ($r_fields->[$j] eq "category") { $cat_id = $j; } } $output .= desc_record($r_rowdata,$r_fields,$r_in,""); $output .= qq~
~; $output; } ############################################################################ # # subroutine get_cart_id # # Input: # None: # # Output: # customerID -- parsed IPADDRE ############################################################################ sub get_cart_id { my $customerID; $customerID = get_session_id(); return $customerID; }