add web cart wrapper class
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 3 Jul 2010 20:47:06 +0000 (20:47 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 3 Jul 2010 20:47:06 +0000 (20:47 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@516 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

wob/cart.wolf
www/inc/classes/random.php
www/inc/rendering/autoload.php
www/inc/rendering/cart_listing.php
www/index.php
www/template/en/eventdetails.html
www/template/en/index.html

index 7d2f4e6..a5e2fde 100644 (file)
@@ -9,7 +9,9 @@
 <Wolf>
        <Table name="cart">
                <!--the cookie for this cart-->
-               <Column name="cartid" type="string:32" primarykey="yes"/>
+               <Column name="cartid" type="string:32" primarykey="yes">
+                       <Call lang="php" method="WebCart::getNewCartId()"/>
+               </Column>
                <!--when the cart expires-->
                <Column name="timeout" type="int64" notnull="yes"/>
                <!--shipping address during order process-->
index b65a7d3..62ec275 100644 (file)
@@ -78,7 +78,7 @@ define("RND_OTHER2",0x18);
 /**return a new Code-39 capable ID; length is the amount of characters*/
 function getCode39ID($length,$range=RND_ANYRANGE)
 {
-       $c39="23456789ABCDEFGHJKLMNPRSTUVWXYZ+";
+       $c39="123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
        $rnd=getRandom($length*8);
        $ret="";
        for($i=0;$i<$length;$i++){
index 3458417..bc9e1c6 100644 (file)
@@ -10,5 +10,6 @@
 //
 $d=dirname(__FILE__);
 wob_autoclass("EventRender",$d.'/event_listing.php');
+wob_autoclass("WebCart",$d.'/cart_listing.php');
 
 ?>
index 250b13c..36d0efd 100644 (file)
@@ -9,6 +9,90 @@
 // +----------------------------------------------------------------------
 //
 
+/** helper class wrapping Web Cart handling */
+class WebCart{
+static private $cartid=false;
+
+/**contains the name of the cookie and the HTTP parameter for the cart ID*/
+const cartIdName = "smoke_cartid";
+
+/**called from index.php - add tickets to cart*/
+static function addTickets(){
+       global $HTTPARGS;
+       $cart=self::getOrCreateCart();
+       echo $cart;
+}
+
+/**returns the current cart ID, or an empty string if there is no cart*/
+static function getCart(){
+       global $HTTPARGS,$_COOKIE;
+       if(self::$cartid!==false)return self::$cartid;
+       //search GET/POST parms
+       if(isset($HTTPARGS[self::cartIdName])){
+               //found it, test it!
+               $ci=trim($HTTPARGS[self::cartIdName]);
+               if($ci!=""){
+                       $res=WTcart::getFromDB($ci);
+                       if(is_a($res,"WTcart")){
+                               self::$cartid=$ci;
+                               return $ci;
+                       }
+               }
+       }
+       //search cookies
+       if(isset($_COOKIE[self::cartIdName])){
+               //found it, test it!
+               $ci=trim($_COOKIE[self::cartIdName]);
+               if($ci!=""){
+                       $res=WTcart::getFromDB($ci);
+                       if(is_a($res,"WTcart")){
+                               self::$cartid=$ci;
+                               return $ci;
+                       }
+               }
+       }
+       //none found
+       self::$cartid="";
+       return "";
+}
+
+/**return existing cart or create a new one*/
+static public function getOrCreateCart(){
+       global $CartTimeout;
+       //try to find it
+       self::getCart();
+       if(self::$cartid==""){
+               //none there, create it
+               $c=WTcart::newRow();
+               $exp=time()+$CartTimeout;
+               $c->timeout=$exp;
+               $c->insert();
+               //set cookies
+               setCookie(self::cartIdName,$c->cartid,0);
+               //remember
+               self::$cartid=$c->cartid;
+       }
+       //return
+       return self::$cartid;
+}
+
+/** \internal called to generate a new cart*/
+static public function getNewCartId(){
+       do{
+               //generate ID
+               $ci=getCode39ID(32);
+               //look for duplicate
+               $res=WTcart::getFromDB($ci);
+               if(is_a($res,"WTcart"))continue;
+               //return ID
+               return $ci;
+       }while(true);
+}
+
+//end of WebCart
+};
+
+
 /** creates the cart overview */
 function createCartOverview()
 {
index a40fd26..66d97c8 100644 (file)
@@ -15,6 +15,10 @@ include("inc/rendering/autoload.php");
 //include process script (TODO: rework to be autoloaded)
 include('inc/rendering/submit.php');
 
+//unify arguments
+$HTTPARGS=$_GET;
+foreach($_POST as $a=>$p)$HTTPARGS[$a]=$p;
+
 //set common basics
 $mode="index";
 if(isset($_GET["mode"])){
@@ -29,10 +33,14 @@ $loader = new Twig_Loader_Filesystem($template);
 $twig = new Twig_Environment($loader, $twigoptions );
 foreach($twigextensions as $te)$twig->addExtension($te);
 
+$cartid=WebCart::getCart();
+// if($cartid!="")$carturl="cartid=$cartid&";else $carturl="";
+
 //basic variables shared by all templates
 // script URLs
 $basevars['script']['root']=$_SERVER['SCRIPT_NAME'];
 $basevars['script']['this']=$_SERVER['REQUEST_URI'];
+$basevars['script']['index']=$_SERVER['SCRIPT_NAME']."?mode=index";
 $basevars['script']['eventDetails']=$_SERVER['SCRIPT_NAME']."?mode=eventDetails&event=";
 $basevars['script']['eventOrder']=$_SERVER['SCRIPT_NAME']."?mode=eventOrder&event=";
 //$basevars['script']['cart']=$_SERVER['SCRIPT_NAME']."?mode=cart";
@@ -45,9 +53,11 @@ $basevars['script']['eventOrder']=$_SERVER['SCRIPT_NAME']."?mode=eventOrder&even
 $basevars['inputnames']['amountTickets']="amountTickets";
 $basevars['inputnames']['event']="event";
 $basevars['inputnames']['mode']="mode";
+$basevars['inputnames']['cartid']=WebCart::cartIdName;
+$basevars['cartcookie']=WebCart::cartIdName;
 
 //strings that are used to compose the overall layout
-$page="(internal error: no page text yet)";
+$page="(internal error: no page text yet, probably no template defined)";
 
 try{
        //get page template and process it
@@ -56,7 +66,8 @@ try{
                        $page=EventRender::createEventDetails();
                        break;
                case "eventOrder":
-                       $page=
+                       WebCart::addTickets();
+                       break;
 /*             case "cart":
                        deleteEventFromCart();
                        changeTicketAmountInCart();
index adb425e..a16694a 100644 (file)
@@ -13,7 +13,7 @@
   {{event.date}} {{event.time}}, {{event.place}}<br/>
   Artist: {{event.artist}}<br/>
   {{event.description}}<br/>
-  <form action="{{script.eventOrder}}{{event.ID}}" method="GET">
+  <form action="{{script.eventOrder}}{{event.ID}}" method="POST">
   <table>
   <tr><td>Price:</td><td>Amount:</td></tr>
   {% for price in event.prices %}
index 35cdd8a..64a132a 100644 (file)
@@ -18,7 +18,7 @@
   {% for price in event.prices %}
    {{price.price}} ({{price.categoryname}})
   {% endfor %}<br/>
-  <a href="{{script.eventDetails}}{{event.ID}}">order tickets</a>
+  <a href="{{script.eventDetails}}{{event.ID}}">details/order tickets</a>
  {% endfor %}
 
 {% endblock %}