smallville.cgi
Live Build-a-Pizza script integrated into Smallville Gazette HTML template
#!/usr/local/bin/perl
use CGI;

$cgiobject=new CGI;
$cgiobject->use_named_parameters;

print $cgiobject->header;

#read in template page
$templateFile="smallville.html";
open(INFILE,"<$templateFile");
@templatePage=<INFILE>;
close(INFILE);

#condense page array into one scalar variable
$resultPage=join("",@templatePage);

#determine current date
($sec,$min,$hour,$mday,
 $mon,$year,$wday,$yday,$isdst)=localtime(time);
$curDay=(Sunday,Monday,Tuesday,Wednesday,
         Thursday,Friday,Saturday)[$wday];
$curMonth=(January,February,March,April,
           May,June,July,August,September,
           October,November,December)[$mon];
$liveDate="$curDay, $curMonth $mday, ".($year+1900);

#search-and-replace on date
$resultPage=~s/INSERT DATE HERE/$liveDate/g;

#generate pizza form
&build_a_pizza;

#search-and-replace on pizza form
$resultPage=~s/INSERT PIZZA HERE/$pizzaHTML/g;
 
#done, output page to browser
print $resultPage;


###build-a-pizza script###

sub build_a_pizza()
#build the pizza form using old "build-a-pizza" script
 { @allparams=$cgiobject->param();
   $pizzaHTML="";
   if ($#allparams>-1)
    {&get_state_variables} 
   else 
    {&init} 

   if ($orderComplete)
    { &complete_order }
   else 
    { &build_order }
 }

sub build_order()
 #build the pizza order
 { unless ($newOrderFlag)
    { $greeting="Welcome! Please begin building your pizza order." }
   else
    { $greeting="<H3>Build-a-Pizza</H3>You may modify or finalize your order below." }
   
   $greeting.="<br>Current pizza price: <font color='red'>\$".
              sprintf("%.2f",&calcTotal())."</font>.";   
   $pizzaHTML.=$cgiobject->start_html(-title=>'Build-a-Pizza',-bgcolor=>'white');
   $pizzaHTML.="<H2>$greeting</H2>";
   &output_form;
   $pizzaHTML.=$cgiobject->end_html;
 }

sub complete_order()
 #checkout order
 { $pizzaHTML.=$cgiobject->start_html(-title=>'Thank you from Build-a-Pizza',-bgcolor=>'white');
   $invoice= "<H2>Thank you for your pizza order!</H2>".
             "<HR>Deliveries should arrive in 30-45 minutes. ".
             "Pickup orders please arrive in about 20 minutes.<BR>".
             "The following order has been sent to the kitchen:<BR>";
   
   if ($deliver eq "deliver")
    { $invoice.="<B>Delivery</B> " }
   else
    { $invoice.="<B>Pickup</B> " }
   $invoice.="Order for: $name<br>";
   if ($deliver eq "deliver")
    { $invoice.="Delivery to: $address<br>" }
   $invoice.="Order Description:<br>A <B>$size</B> pizza with the following toppings:<br>";
   @allToppings=$cgiobject->param("order_toppings");
   $invoice.="<B>".join(",",@allToppings)."</B>";
   $invoice.="<BR>TOTAL DUE: ".sprintf("%.2f",&calcTotal());
   $pizzaHTML.=$invoice;
   
  ##here would be code to send invoice kitchen
  ##such as by way of email
  
  }

sub init()
 #define initial values for pizza parameters
 { $name="Your name";
   $address="Where to?";
   $phone="Your phone";
   $deliver="deliver";
   $toppings="pepperoni";
   $size="large";
 }

sub get_state_variables()
 #grab any parameters which were submitted
 { $name=$cgiobject->param("order_name");
   $address=$cgiobject->param("order_address");
   $phone=$cgiobject->param("order_phone");
   $deliver=$cgiobject->param("order_deliver");
   $toppings=$cgiobject->param("order_toppings");
   $size=$cgiobject->param("order_size");
   $newOrderFlag=$cgiobject->param("newOrderFlag");
   $orderComplete=$cgiobject->param("finish")
 }
 
sub calcTotal()
 #calculate pizza cost based on current parameters
 { %sizelabels=('small'=>'small (8",$5)',
                'medium'=>'medium (12",$8)',
                'large'=>'large (16",$10)',
                'xlarge'=>'x-large (18",$12)');
   %sizeprices=('small'=>5,
                'medium'=>8,
                'large'=>10,
                'xlarge'=>12);
   %toppingsprices=('small'=>.50,
                    'medium'=>.75,
                    'large'=>1.00,
                    'xlarge'=>1.25);             
                
   $baseCost=$sizeprices{$size};
   
   @allToppings=$cgiobject->param("order_toppings");
   $toppingsCost=($#allToppings+1)*($toppingsprices{$size});
   
   
   return $baseCost+$toppingsCost;
 }                                                                
                                                   
sub output_form()
 #construct and output the pizza form HTML
 { $theform=$cgiobject->startform(-name=>'pizzaform',
                                  -method=>'get',
                                  -action=>'/cgi-bin/smallville2.cgi');
   #create name text input field
   $theform.="Your name: ";
   $theform.=$cgiobject->textfield(-name=>'order_name',
                                   -size=>30,
                                   -default=>$name);
   
   #create phone text input field
   $theform.="<BR>Your telephone number: ";
   $theform.=$cgiobject->textfield(-name=>'order_phone',
                                   -size=>10,
                                   -default=>$phone);
   
   #create address text input field if deliver option selected
   if ($deliver eq "deliver"){
   $theform.="<BR>Deliver to address:<BR>";
   $theform.=$cgiobject->textarea(-name=>'order_address',
                                   -rows=>3,
                                   -default=>$address);
   }                                           
   
                                   
   #create delivery radio buttons
   $theform.="<BR>";
   $theform.=$cgiobject->radio_group(-name=>'order_deliver',
                                     -values=>["pickup","deliver"],
                                     -default=>$deliver);
                                     
   #create toppings checkboxes
   $theform.="<HR>Please select toppings from the list below:<BR>";
   $theform.="<SMALL>Note: Each topping costs .50,.75,1.00,1.25 for S,M,L,XL pizza</SMALL>";
   $theform.=$cgiobject->checkbox_group(-name=>'order_toppings',
                                        -values=>['pepperoni','sausage','meatball',
                                                  'mushroom','peppers','pineapple',
                                                  'ham','shrimp','tomato',
                                                  'onion','anchovies','liver'],
                                        -default=>$toppings,
                                        -linebreak=>'false',
                                        -columns=>3);
   
   #create size radio buttons
   $theform.="<BR>Select a pizza size: ";
   $theform.=$cgiobject->radio_group(-name=>'order_size',
                                     -values=>["small","medium","large","xlarge"],
                                     -labels=>\%sizelabels,
                                     -default=>$size);
   
   
   #create hidden field for newOrderFlag
   $theform.=$cgiobject->hidden(-name=>'newOrderFlag',-value=>'1');
   
   
   #create submit and reset buttons
   $theform.="<BR><BR>";
   $theform.=$cgiobject->submit(-name=>"calculate",
                                -label=>'Calculate');
   $theform.=$cgiobject->submit(-name=>"finish",
                                -label=>'Finish Order');
   
   $theform.=$cgiobject->endform;                                                                 
   $pizzaHTML.=$theform
 }