19 public function actionAdd()
27 if(isset($_POST[
'Item']) && is_numeric($_POST[
'Item'][
'quantity']) && $_POST[
'Item'][
'quantity'] > 0)
29 $order_id = Order::getOrder(
true);
32 $exist_model =
Item::model()->find(
'item_id=:item_id AND order_id=:order_id',
33 array(
':item_id' =>$item_id,
34 ':order_id'=>$order_id
38 if(isset($exist_model)){
39 $model = $exist_model;
40 $model->quantity += $_POST[
'Item'][
'quantity'];
41 $model->ordered_at =
new CDbExpression(
'NOW()');
44 $model->quantity =$_POST[
'Item'][
'quantity'];
46 $model->item_id =$item_id;
49 $model->order_id =$order_id;
50 $model->item_referrer =$_POST[
'Item'][
'item_referrer'];
52 $model->ip_address =$_SERVER[
'REMOTE_ADDR'];
53 $model->referrer =$_SERVER[
'HTTP_REFERER'];
55 if($model->getItemChecksum()==$_POST[
'checksum']){
59 $cookie =
new CHttpCookie(
'user_id', Order::getUser(
true));
60 $cookie->expire = time()+60 * Yii::app()->getModule(
'shoppingcart')->max_lifetime;
61 if(isset(Yii::app()->getModule(
'shoppingcart')->cookie_domain)){
62 $cookie->domain = Yii::app()->getModule(
'shoppingcart')->cookie_domain;
64 Yii::app()->request->cookies[
'user_id'] = $cookie;
71 if(isset($_SERVER[
'HTTP_REFERER'])){
72 Yii::app()->user->setState(
'criticalReferrer',$_SERVER[
'HTTP_REFERER']);
75 if(!empty($_POST[
'shoppingcart_link'])){
76 $this->redirect($_POST[
'shoppingcart_link']);
77 }elseif(isset($_SERVER[
'HTTP_REFERER'])){
78 $this->redirect($_SERVER[
'HTTP_REFERER'].
'#item-'.$model->item_id);
86 public function actionRemove()
90 if(isset($_GET[
'id'])){
95 && (Order::getOrder(
true)==$model->order_id)){
98 throw new CHttpException(403);
101 if(isset($_SERVER[
'HTTP_REFERER'])){
102 $this->redirect($_SERVER[
'HTTP_REFERER']);
114 $stateProcess = Order::PROCESS;
115 $stateOrdered = Order::ORDERED;
116 $max_lifetime = Yii::app()->getModule(
'shoppingcart')->max_lifetime;
118 $connection = Yii::app()->db;
121 Yii::beginProfile(
'garbageCollector');
123 $ordersToRemove = $command=$connection
125 "SELECT ProcessOrder.id AS order_id,
126 ProcessOrder.user_id,
127 IF((DATE_ADD(IFNULL(MAX(Item.ordered_at),ProcessOrder.started_at), INTERVAL :max_lifetime MINUTE) < NOW()) , TRUE , FALSE) AS order_to_remove,
128 IF((COUNT(OrderedOrder.id) < 1) , TRUE, FALSE) AS user_to_remove
129 FROM {{order}} AS ProcessOrder
130 LEFT JOIN {{item}} AS Item
131 ON ProcessOrder.id = Item.order_id
132 LEFT JOIN {{order}} AS OrderedOrder
133 ON ProcessOrder.user_id = OrderedOrder.user_id AND OrderedOrder.ordered = :ordered
134 WHERE ProcessOrder.ordered=:process GROUP BY ProcessOrder.id
135 HAVING order_to_remove = TRUE")
136 ->bindParam(
":max_lifetime",$max_lifetime,PDO::PARAM_STR)
137 ->bindParam(
":ordered",$stateOrdered,PDO::PARAM_STR)
138 ->bindParam(
":process",$stateProcess,PDO::PARAM_STR)
141 foreach($ordersToRemove as $order){
143 $user_to_remove = $order[
'user_to_remove'];
145 $transaction = $connection->beginTransaction();
149 $connection->createCommand(
150 "DELETE FROM {{item}} WHERE {{item}}.order_id = :order_id"
152 ->bindParam(
":order_id",$order[
'order_id'],PDO::PARAM_STR)
156 $connection->createCommand(
157 "DELETE FROM {{tmp_user}} WHERE {{tmp_user}}.id = :user_id"
159 ->bindParam(
":user_id",$order[
'user_id'],PDO::PARAM_STR)
163 $connection->createCommand(
164 "DELETE FROM {{order}} WHERE {{order}}.id = :order_id"
166 ->bindParam(
":order_id",$order[
'order_id'],PDO::PARAM_STR)
169 $transaction->commit();
173 $transaction->rollback();
176 Yii::endProfile(
'garbageCollector');