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  if(isset($_POST['Item']) && is_numeric($_POST['Item']['quantity']) && $_POST['Item']['quantity'] > 0)
26  {
27  $order_id = Order::getOrder(true);
28  $item_id = EncryptHelper::decrypt($_POST['Item']['item_id']);
29 
30  $exist_model = Item::model()->find('item_id=:item_id AND order_id=:order_id',
31  array(':item_id' =>$item_id,
32  ':order_id'=>$order_id
33  )
34  );
35 
36  if(isset($exist_model)){
37  $model = $exist_model;
38  $model->quantity += $_POST['Item']['quantity'];
39  $model->ordered_at = new CDbExpression('NOW()');
40  }else{
41  $model->title =EncryptHelper::decrypt($_POST['Item']['title']);
42  $model->quantity =$_POST['Item']['quantity'];
43  $model->price =EncryptHelper::decrypt($_POST['Item']['price']);
44  $model->item_id =$item_id;
45  $model->page_id =EncryptHelper::decrypt($_POST['Item']['page_id']);
46  $model->admin_email =EncryptHelper::decrypt($_POST['Item']['admin_email']);
47  $model->order_id =$order_id;
48  $model->item_referrer =$_POST['Item']['item_referrer'];
49  }
50  $model->ip_address =$_SERVER['REMOTE_ADDR'];
51  $model->referrer =$_SERVER['HTTP_REFERER'];
52 
53  if($model->getItemChecksum()==$_POST['checksum']){
54  if($model->save()){
55  Order::flushOrder();
56  //Extend the lifespan of cookie
57  if (!headers_sent()){
58  $cookie = new CHttpCookie('user_id', Order::getUser(true));
59  $cookie->expire = time()+60 * Yii::app()->getModule('shoppingcart')->max_lifetime;
60  if(isset(Yii::app()->getModule('shoppingcart')->cookie_domain)){
61  $cookie->domain = Yii::app()->getModule('shoppingcart')->cookie_domain;
62  }
63  Yii::app()->request->cookies['user_id'] = $cookie;
64  }
65  Yii::app()->user->setFlash('itemAdded',ShoppingcartModule::t('Item successfully added'));
66  }
67  }
68  }
69 
70  if (!empty($_POST['shoppingcart_link'])) {
71  $this->redirect($_POST['shoppingcart_link']);
72  } elseif(isset($_SERVER['HTTP_REFERER'])) {
73  Yii::app()->user->setState('criticalReferrer',$_SERVER['HTTP_REFERER']);
74  $this->redirect($_SERVER['HTTP_REFERER'].'#item-'.$model->item_id);
75  }
76  }
77  /*
78  * Delete item from cart by id
79  * @throws CHttpException
80  * @return void
81  */
82  public function actionRemove()
83  {
84  $this->csrfProtect();
85 
86  if (isset($_GET['id'])) {
87  // we only allow deletion via POST request
88  $model = Item::model()->findByPk($_GET['id']);
89  if (isset($model) && (Order::getOrder(true)==$model->order_id)) {
90  $model->delete();
91  Order::flushOrder();
92  } else {
93  throw new CHttpException(403);
94  }
95  if(isset($_SERVER['HTTP_REFERER'])){
96  $this->redirect($_SERVER['HTTP_REFERER']);
97  }
98  }
99  }
100 }