/**this class represents a bunch of tickets in the shopping cart, it is created by Cart*/
class CartTicket
{
- private $ctid;
private $cartid;
private $eventid;
private $amount;
/**used by Cart to create the tickets, never use this directly*/
- public function __construct($ctid,$cid,$eid,$amt)
+ public function __construct($cid,$eid,$amt)
{
- $this->ctid=$ctid;
$this->cartid=$cid;
$this->eventid=$eid;
$this->amount=$amt;
$db->deleteRows("cart_ticket","cartid=".$db->escapeString($this->cartid)." and eventid=".$this->eventid);
$this->amount=0;
}else{
- $db->update("cart_ticket",array("amount"=>($amt+0)),"ctid=".$db->escapeInt($this->ctid));
+ $db->update("cart_ticket",array("amount"=>($amt+0)),"cartid=".$db->escapeInt($this->cartid)." AND eventid=".$db->escapeInt($this->eventid));
$this->amount=$amt;
}
}
$ret=array();
if(count($res)>0)
foreach($res as $k => $tc)
- $ret[]=new CartTicket($tc["ctid"],$tc["cartid"],$tc["eventid"],$tc["amount"]);
+ $ret[]=new CartTicket($tc["cartid"],$tc["eventid"],$tc["amount"]);
return $ret;
}
+ /**use this to get tickets by eventid; returns false if it does not exist*/
+ public function getTicketsByEvent($eventid)
+ {
+ $where="cartid=".$db->escapeString($this->cartid)." AND eventid=".$db->escapeInt($eventid);
+ $res=$db->select("cart_ticket","*",$where);
+ if(count($res>0))
+ return new CartTicket($res[0]["cartid"],$res[0]["eventid"],$res[0]["amount"]);
+ }
+
/**use this to add tickets, returns new CartTicket object or false if the event does not exist or is cancelled*/
public function addTickets($eventid,$amount)
{
//check that ticket can be sold
$event=new Event($eventid);
if($event->isCancelled())return false;
- //insert into cart
- $nid=$db->insert("cart_ticket",array("cartid"=>$this->cartid,"eventid"=>$eventid,"amount"=>$amount));
- if($nid===false)return false;
- return new CartTicket($nid,$this->cartid,$eventid,$amount);
+ //begin transaction, get current data
+ $db->beginTransaction();
+ $where="cartid=".$db->escapeString($this->cartid)." AND eventid=".$db->escapeInt($eventid);
+ $res=$db->select("cart_ticket","*",$where);
+ if(count($res)>0){
+ $amount+=$res[0]["amount"];
+ $ret=$db->update("cart_ticket",array("amount"=>$amount),$where);
+ }else{
+ //insert into cart
+ $ret=$db->insert("cart_ticket",array("cartid"=>$this->cartid,"eventid"=>$eventid,"amount"=>$amount));
+ }
+ if($ret===false){
+ $db->rollbackTransaction();
+ return false;
+ }
+ $db->commitTransaction();
+ return new CartTicket($this->cartid,$eventid,$amount);
}
/**checks that the whole content of the cart can be ordered; returns an empty array on success or an array of CartError objects on failure*/
$res=$db->select("cart_ticket","*","where cartid=".$db->escapeString($this->cartid));
if(count($res)>0)
foreach($res as $k=>$tc)
- $ret[]=new CartTicket($tc["ctid"],$tc["cartid"],$tc["eventid"],$tc["amount"]);
+ $ret[]=new CartTicket($tc["cartid"],$tc["eventid"],$tc["amount"]);
//vouchers are ok by default, just check amount
$itmcnt=count($res);
$res=$db->select("cart_voucher","cvid","where cartid=".$db->escapeString($this->cartid));