From: konrad Date: Sat, 3 Jul 2010 20:47:06 +0000 (+0000) Subject: add web cart wrapper class X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=d53b06135172c70d145333c9af512c3ee0d6c0e0;p=web%2Fkonrad%2Fsmoke.git add web cart wrapper class git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@516 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- diff --git a/wob/cart.wolf b/wob/cart.wolf index 7d2f4e6..a5e2fde 100644 --- a/wob/cart.wolf +++ b/wob/cart.wolf @@ -9,7 +9,9 @@ - + + + diff --git a/www/inc/classes/random.php b/www/inc/classes/random.php index b65a7d3..62ec275 100644 --- a/www/inc/classes/random.php +++ b/www/inc/classes/random.php @@ -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++){ diff --git a/www/inc/rendering/autoload.php b/www/inc/rendering/autoload.php index 3458417..bc9e1c6 100644 --- a/www/inc/rendering/autoload.php +++ b/www/inc/rendering/autoload.php @@ -10,5 +10,6 @@ // $d=dirname(__FILE__); wob_autoclass("EventRender",$d.'/event_listing.php'); +wob_autoclass("WebCart",$d.'/cart_listing.php'); ?> diff --git a/www/inc/rendering/cart_listing.php b/www/inc/rendering/cart_listing.php index 250b13c..36d0efd 100644 --- a/www/inc/rendering/cart_listing.php +++ b/www/inc/rendering/cart_listing.php @@ -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() { diff --git a/www/index.php b/www/index.php index a40fd26..66d97c8 100644 --- a/www/index.php +++ b/www/index.php @@ -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(); diff --git a/www/template/en/eventdetails.html b/www/template/en/eventdetails.html index adb425e..a16694a 100644 --- a/www/template/en/eventdetails.html +++ b/www/template/en/eventdetails.html @@ -13,7 +13,7 @@ {{event.date}} {{event.time}}, {{event.place}}
Artist: {{event.artist}}
{{event.description}}
-
+
{% for price in event.prices %} diff --git a/www/template/en/index.html b/www/template/en/index.html index 35cdd8a..64a132a 100644 --- a/www/template/en/index.html +++ b/www/template/en/index.html @@ -18,7 +18,7 @@ {% for price in event.prices %} {{price.price}} ({{price.categoryname}}) {% endfor %}
- order tickets + details/order tickets {% endfor %} {% endblock %}
Price:Amount: