Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ItemController.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  * Item controller class file.
11  */
13 {
14  /*
15  * Add item to cart
16  * Function adds the item to order cart or update necessary values in exist record
17  * @return void
18  */
19  public function actionAdd()
20  {
21  $model = new Item;
22 
23  $this->csrfProtect();
24 
25  $this->cleaning();
26 
27  if(isset($_POST['Item']) && is_numeric($_POST['Item']['quantity']) && $_POST['Item']['quantity'] > 0)
28  {
29  $order_id = Order::getOrder(true);
30  $item_id = EncryptHelper::decrypt($_POST['Item']['item_id']);
31 
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
35  )
36  );
37 
38  if(isset($exist_model)){
39  $model = $exist_model;
40  $model->quantity += $_POST['Item']['quantity'];
41  $model->ordered_at = new CDbExpression('NOW()');
42  }else{
43  $model->title =EncryptHelper::decrypt($_POST['Item']['title']);
44  $model->quantity =$_POST['Item']['quantity'];
45  $model->price =EncryptHelper::decrypt($_POST['Item']['price']);
46  $model->item_id =$item_id;
47  $model->page_id =EncryptHelper::decrypt($_POST['Item']['page_id']);
48  $model->admin_email =EncryptHelper::decrypt($_POST['Item']['admin_email']);
49  $model->order_id =$order_id;
50  $model->item_referrer =$_POST['Item']['item_referrer'];
51  }
52  $model->ip_address =$_SERVER['REMOTE_ADDR'];
53  $model->referrer =$_SERVER['HTTP_REFERER'];
54 
55  if($model->getItemChecksum()==$_POST['checksum']){
56  if($model->save()){
57  //Extend the lifespan of cookie
58  if (!headers_sent()){
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;
63  }
64  Yii::app()->request->cookies['user_id'] = $cookie;
65  }
66  Yii::app()->user->setFlash('itemAdded',ShoppingcartModule::t('Item successfully added'));
67  }
68  }
69  }
70 
71  if(isset($_SERVER['HTTP_REFERER'])){
72  Yii::app()->user->setState('criticalReferrer',$_SERVER['HTTP_REFERER']);
73  }
74 
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);
79  }
80  }
81  /*
82  * Delete item from cart by id
83  * @throws CHttpException
84  * @return void
85  */
86  public function actionRemove()
87  {
88  $this->csrfProtect();
89 
90  if(isset($_GET['id'])){
91  // we only allow deletion via POST request
92  $model = Item::model()->findByPk($_GET['id']);
93 
94  if(isset($model)
95  && (Order::getOrder(true)==$model->order_id)){
96  $model->delete();
97  }else{
98  throw new CHttpException(403);
99  }
100 
101  if(isset($_SERVER['HTTP_REFERER'])){
102  $this->redirect($_SERVER['HTTP_REFERER']);
103  }
104  }
105  }
106  /**
107  * The function that deletes unnecessary items, orders and users
108  * lifetime which has already gone
109  *
110  * @return void
111  */
112  protected function cleaning(){
113 
114  $stateProcess = Order::PROCESS;
115  $stateOrdered = Order::ORDERED;
116  $max_lifetime = Yii::app()->getModule('shoppingcart')->max_lifetime;
117 
118  $connection = Yii::app()->db;
119  //Find all process orders which should be removed
120 
121  Yii::beginProfile('garbageCollector');
122 
123  $ordersToRemove = $command=$connection
124  ->createCommand(
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)
139  ->queryAll();
140 
141  foreach($ordersToRemove as $order){
142 
143  $user_to_remove = $order['user_to_remove'];
144 
145  $transaction = $connection->beginTransaction();
146  try
147  {
148  //Delete items
149  $connection->createCommand(
150  "DELETE FROM {{item}} WHERE {{item}}.order_id = :order_id"
151  )
152  ->bindParam(":order_id",$order['order_id'],PDO::PARAM_STR)
153  ->execute();
154  //Delete user if he has not another finished order
155  if($user_to_remove){
156  $connection->createCommand(
157  "DELETE FROM {{tmp_user}} WHERE {{tmp_user}}.id = :user_id"
158  )
159  ->bindParam(":user_id",$order['user_id'],PDO::PARAM_STR)
160  ->execute();
161  }
162  //Delete order
163  $connection->createCommand(
164  "DELETE FROM {{order}} WHERE {{order}}.id = :order_id"
165  )
166  ->bindParam(":order_id",$order['order_id'],PDO::PARAM_STR)
167  ->execute();
168 
169  $transaction->commit();
170  }
171  catch(Exception $e){
172 
173  $transaction->rollback();
174  }
175  }
176  Yii::endProfile('garbageCollector');
177  }
178 
179 }