From: konrad Date: Mon, 15 Oct 2007 20:31:24 +0000 (+0000) Subject: first draft of cart class X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=7b5f068c4b237da162c6414b3988bb27a5dfebab;p=konrad%2Fsmoke.git first draft of cart class git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@54 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- diff --git a/www/inc/cart.php b/www/inc/cart.php new file mode 100644 index 0000000..41c8143 --- /dev/null +++ b/www/inc/cart.php @@ -0,0 +1,108 @@ +, (C) 2007 +// +// Copyright: See README/COPYING files that come with this distribution +// +// + +/**this class represents a bunch of tickets in the shopping cart, it is created by Cart*/ +class CartTicket +{ + private $cartid; + private $eventid; + private $amount; + + /**used by Cart to create the tickets, never use this directly*/ + public function __construct($cid,$eid,$amt) + { + $this->cartid=$cid; + $this->eventid=$eid; + $this->amount=$amt; + } + + /**use this to increase or decrease the amount of tickets*/ + public function changeAmount($amt) + { + global $db; + if($amd<=0){ + $db->deleteRows("cart_ticket","cartid='".addslashes($this->cartid)."' and eventid=".$this->eventid); + }else{ + $db->update("cart_ticket",array("amount"=>($amt+0)),"cartid='".addslashes($this->cartid)."' and eventid=".$this->eventid); + } + } + + /**use this to get the actual event*/ + public function eventObject() + { + return new Event($this->eventid); + } +}; + +/**this class represents a shopping cart*/ +class Cart +{ + private $cartid=false; + + /**reloads a cart from the database, if $id is false a new one is created, use isValid() to check whether the cart really exists in the DB (it may have expired)*/ + public function __construct($id=false) + { + global $db; + global $CartTimeout; + if($id===false){ + $db->beginTransaction(); + while(1){ + //generate ID + $id=getRandom(128); + //check it does not exist + $res=$db->select("cart","cartid","cartid='".addslashes($id)."'"); + if(count($res)==0){ + $this->cartid=$id; + break; + } + } + //create entry + $db->insert("cart",array("cartid"=>$id,"timeout"=>(time()+$CartTimeout))); + $db->commitTransaction(); + }else{ + //check that cart exists + $res=$db->select("cart","cartid","cartid='".addslashes($id)."'"); + if(count($res)>0)$this->cartid=$id; + } + } + + /**returns true if this is a valid shopping cart, if it returns false, try to create a new one*/ + public function isValid() + { + return $this->cartid!==false; + } + + /**use this to get all existing tickets in this cart, then manipulate the tickets directly*/ + public function getTickets() + { + if($this->cartid===false)return array(); + $res=$db->select("cart_ticket","*","where cartid='".addslashed($this->cartid)."'"); + $ret=array(); + if(count($res)>0) + foreach($res as $k=>$tc) + $ret[]=new CartTicket($tc["cartid"],$tc["eventid"],$tc["amount"]); + return $ret; + } + + /**use this to add tickets*/ + public function addTickets($eventid,$amount) + { + //sanity check + if($amount<=0)return; + //TODO: check that ticket can be sold + //insert into cart + $db->insert("cart_ticket",array()); + } +}; + +?> \ No newline at end of file