make cart more logical
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 17 Nov 2007 17:39:28 +0000 (17:39 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 17 Nov 2007 17:39:28 +0000 (17:39 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@72 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

www/inc/cart.php
www/inc/db.php
www/inc/db_mysql.php
www/inc/db_scheme.php

index 8f5164c..68a20c2 100644 (file)
 /**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;
@@ -36,7 +34,7 @@ class CartTicket
                        $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;
                }
        }
@@ -157,10 +155,19 @@ class Cart
                $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)
        {
@@ -170,10 +177,23 @@ class Cart
                //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*/
@@ -187,7 +207,7 @@ class Cart
                $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));
index 901272a..02dbeb4 100644 (file)
@@ -56,7 +56,7 @@ abstract class DbEngine
        /**insert values into a table; returns false on failure, the new primary key if a sequence was set, true otherwise*/
        public abstract function insert($table,array $values);
        
-       /**update database values*/
+       /**update database values; returns how many rows have been changed or false for failure*/
        public abstract function update($table,array $values,$where);
        
        /**delete database values*/
index b58d141..8f6e2dd 100644 (file)
@@ -144,7 +144,9 @@ class MysqlEngine extends DbEngine
        
        public function update($table,array $values,$where)
        {
-               mysql_query($this->sqlUpdate($table,$values,$where));
+               $res=mysql_query($this->sqlUpdate($table,$values,$where));
+               if($res!==false)return mysql_affected_rows();
+               else return false;
        }
        
        public function deleteRows($table,$where)
index 43612e7..96eb752 100644 (file)
@@ -136,10 +136,9 @@ class DbScheme {
                );
                //buying tickets
                $this->scheme["cart_ticket"]=array(
-                       "ctid" => array("seq64","primarykey"),
                        "cartid" => array("string:32","notnull","foreignkey:cart:cartid"),
                        //tickets in the cart
-                       "eventid" => array("int32","foreignkey:event:eventid"),
+                       "eventid" => array("int32","foreignkey:event:eventid","primarykey"),
                        "amount" => array("int32","notnull"),
                );
                //buying vouchers