Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ShoppingcartController.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  * Shoppingcart controller class file.
11  */
13 {
14  /**
15  * Returns a list of external action classes.
16  *
17  * @return array
18  */
19 
20 
21  public function actions(){
22  return array(
23  'captcha'=>array(
24  'class'=>'CCaptchaAction',
25  'fixedVerifyCode' => YII_DEBUG ? 'polomo' : null
26  ),
27  );
28  }
29  /*
30  * The function performs all operations associated with shoppingcart:
31  * updates items quantities, saves user profile, shipping and payment information
32  * @return void
33  */
34  public function actionIndex(){
35 
36  $itemsCount = count(Order::getOrder()->items);
37 
38  if(!$itemsCount || !$this->csrfProtect()){
39  $criticalReferrer = Yii::app()->user->getState('criticalReferrer');
40  if(isset($criticalReferrer)){
41  $this->redirect($criticalReferrer);
42  }
43 
44  throw new CHttpException(403);
45  }
46 
47  $step = isset($_REQUEST['step']) ? $_REQUEST['step'] : 1;
48 
49  $data = array();
50 
51  switch($step){
52  case 1:
53 
54  $order_id = Order::getOrder(true);
55  $items = Item::model()->findAll('order_id=:order_id',array(':order_id'=>$order_id));
56 
57  if(isset($_POST['Item']))
58  {
59  $valid=true;
60  foreach($items as $i=>$item)
61  {
62  if(isset($_POST['Item'][$i]) && is_numeric($_POST['Item'][$i]['quantity']) && $_POST['Item'][$i]['quantity'] > 0)
63  $item->quantity = $_POST['Item'][$i]['quantity'];
64  $valid = $item->validate(array('quantity'))
65  && is_numeric($_POST['Item'][$i]['quantity'])
66  && $_POST['Item'][$i]['quantity'] > 0
67  && $valid;
68  }
69 
70  if($valid){
71  foreach($items as $item){
72  $item->save();
73  }
74 
75  Yii::app()->user->setState('step_1', true);
76  $step++;
77  }
78  }
79 
80  break;
81  case 2:
82 
83  $model = Order::getUser();
84  $this->performAjaxValidation($model);
85 
86  if(isset($_POST['TmpUser']))
87  {
88  $model->attributes=$_POST['TmpUser'];
89 
90  if($model->validate() && $model->unsetExistFields()->save(false)){
91 
92  Yii::app()->user->setState('step_2', true);
93  $step++;
94  }else{
95  $data['user'] = $model;
96  }
97  }
98  break;
99  case 3:
100  $model = Order::getOrder();
101 
102  if(isset($_POST['Order']))
103  {
104  $model->shipping_info = EncryptHelper::decrypt($_POST['Order']['shipping_info']);
105  $model->payment_info = EncryptHelper::decrypt($_POST['Order']['payment_info']);
106 
107  if(md5($model->shipping_info.$model->payment_info) == $_POST['checksum']){
108  if($model->save(false)){
109  Yii::app()->user->setState('step_3', true);
110  $step++;
111  }
112  }
113  }
114  break;
115  case 4:
116 
117  $model = Order::getOrder();
118  $this->performAjaxValidation($model);
119 
120  if(isset($_POST['Order'])){
121 
122  $model->verifyCode = $_POST['Order']['verifyCode'];
123 
124  if(!Yii::app()->user->getState('step_1')){
125  $this->redirect(Yii::app()->createUrl('shoppingcart/shoppingcart'));
126  }else
127  if(!Yii::app()->user->getState('step_2')){
128  $this->redirect(Yii::app()->createUrl('shoppingcart/shoppingcart',array('step'=>2)));
129  }else
130  if(!Yii::app()->user->getState('step_3')){
131  $this->redirect(Yii::app()->createUrl('shoppingcart/shoppingcart',array('step'=>3)));
132  }
133 
134  if($model->validate(array('verifyCode'))){
135  $model->ordered = Order::ORDERED;
136  $this->onOrdered($model);
137  if($model->save()){
138  $items = $model->items;
139  $referrer = $items[count($items)-1]->referrer;
140 
141  Yii::app()->user->setState('step_1', null);
142  Yii::app()->user->setState('step_2', null);
143  Yii::app()->user->setState('step_3', null);
144  Yii::app()->user->setState('user_id', null);
145 
146  $this->redirect($referrer);
147  }
148  }else{
149  $data['order'] = $model;
150  }
151  }
152  break;
153  }
154 
155  //Extend the lifespan of cookie
156  $cookie = new CHttpCookie('user_id', Order::getUser(true));
157  $cookie->expire = time()+60 * Yii::app()->getModule('shoppingcart')->max_lifetime;
158  Yii::app()->request->cookies['cookie_name'] = $cookie;
159  //
160 
161  $data['step'] = $step;
162  $this->render('index',array('data'=>$data));
163  }
164  /*
165  * The function performs when a user confirms the order,
166  * it marks the order as finished and sends notice to order owner and items administrators
167  */
168  protected function onOrdered($order){
169 
170  $user = Order::getUser()->implementFromSession();
171  $items = $order->items;
172  $itemsToAdmins = array();
173  foreach($items as $item){
174  $itemsToAdmins[$item->admin_email][] = $item;
175  }
176 
177  $admin_email_template = Yii::app()->getModule('shoppingcart')->adminemail_template;
178 
179  foreach($itemsToAdmins as $admin_email => $itemsToAdmin){
180 
181  $subject = CommentsModule::t('Items ordered');
182 
183  $notification = new Notification(
184  $subject,
185  $this->renderMail('/mail_templates/'.$admin_email_template, $subject, array('items' => $itemsToAdmin))
186  );
187 
188  $notification->recipients[] = new EmailRecipient($admin_email);
189 
190  Yii::app()->notificationManager->notifyAbout($notification);
191  }
192 
193  $usermail_template = Yii::app()->getModule('shoppingcart')->usermail_template;
194 
195  foreach($items as $item){
196 
197  $subject = CommentsModule::t('You ordered items');
198 
199  $notification = new Notification(
200  $subject,
201  $this->renderMail('/mail_templates/'.$usermail_template, $subject, array('items' => $items))
202  );
203 
204  $notification->recipients[] = new EmailRecipient($user->email);
205 
206  Yii::app()->notificationManager->notifyAbout($notification);
207  }
208  }
209 
210  /**
211  * Performs the AJAX validation.
212  * @param CModel the model to be validated
213  */
214  protected function performAjaxValidation($model)
215  {
216  if(isset($_POST['ajax']))
217  {
218  echo CActiveForm::validate($model);
219  Yii::app()->end();
220  }
221  }
222 }