Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ShoppingCartWidget.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  * Widget for view shoppingcart steps
11  */
13 {
14  /**
15  * @var array instances of item models included to current order
16  */
17  public $items;
18  /**
19  * @var object instance of current order model
20  */
21  public $order;
22  /**
23  * @var object instance of current user model
24  */
25  public $user;
26  /**
27  * @var integer the number of current step
28  */
29  public $step;
30  /**
31  * @var string the name of shoppingcart css class
32  */
33  public $class;
34  /**
35  * @var string information about the shipping which belongs to current order
36  */
37  public $shipping_info;
38  /**
39  * @var string information about the payment which belongs to current order
40  */
41  public $payment_info;
42  /**
43  * @var string the name of layout file
44  */
45  public $layout = "ShoppingCartWidget";
46  /**
47  * @var boolean the flag which determines when item names are references
48  */
49  public $link_items_to_page = false;
50 
51  /**
52  * @var string name of page the widget is placed
53  */
54  private $shoppingcart_link;
55 
56  /**
57  * @var string optional redirect page after order submission
58  */
59  public $thankyoupage;
60 
61  /**
62  * The function initialises widget fields which belongs to current session
63  *
64  * @return void
65  */
66  public function init()
67  {
68  $this->csrfProtect();
69  $this->step = isset($_REQUEST['step']) ? $_REQUEST['step'] : 1;
70 
71  if(Order::getOrder()===false)
72  return;
73  $itemsCount = count(Order::getOrder()->items);
74 
75  if(!isset($this->shoppingcart_link)){
76  $this->shoppingcart_link = substr($_SERVER['REDIRECT_URL'],1);
77  }
78  if(!isset($this->thankyoupage)
79  && isset(Yii::app()->getModule('shoppingcart')->thankyoupage[Yii::app()->language])){
80  $this->thankyoupage = Yii::app()->getModule('shoppingcart')->thankyoupage[Yii::app()->language];
81  }
82  if(isset($this->thankyoupage)){
83  Yii::app()->user->setState('thankyoupage',$this->thankyoupage);
84  }
85 
86  $data = array();
87 
88  switch($this->step){
89  case 1:
90  $order_id = Order::getOrder(true);
91  $items = Item::model()->findAll('order_id=:order_id',array(':order_id'=>$order_id));
92 
93  if(isset($_POST['Item']))
94  {
95  $valid=true;
96  foreach($items as $i=>$item)
97  {
98  if(isset($_POST['Item'][$i]) && is_numeric($_POST['Item'][$i]['quantity']) && $_POST['Item'][$i]['quantity'] > 0)
99  $item->quantity = $_POST['Item'][$i]['quantity'];
100  $valid = $item->validate(array('quantity'))
101  && is_numeric($_POST['Item'][$i]['quantity'])
102  && $_POST['Item'][$i]['quantity'] > 0
103  && $valid;
104  }
105 
106  if($valid){
107  foreach($items as $item){
108  $item->save();
109  }
110  Order::flushOrder();
111  Yii::app()->user->setState('step_1', true);
112  $this->step++;
113  }
114  }
115 
116  break;
117  case 2:
118 
119  $model = Order::getUser();
120  $this->performAjaxValidation($model);
121 
122  if(isset($_POST['TmpUser']))
123  {
124  $model->attributes=$_POST['TmpUser'];
125 
126  if($model->validate() && $model->unsetExistFields()->save(false)){
127 
128  Yii::app()->user->setState('step_2', true);
129  $this->step++;
130  }else{
131  $this->user = $model;
132  }
133  }
134  break;
135  case 3:
136  $model = Order::getOrder();
137 
138  if(isset($_POST['Order']))
139  {
140  $model->shipping_info = EncryptHelper::decrypt($_POST['Order']['shipping_info']);
141  $model->payment_info = EncryptHelper::decrypt($_POST['Order']['payment_info']);
142 
143  if(md5($model->shipping_info.$model->payment_info) == $_POST['checksum']){
144  if($model->save(false)){
145  Yii::app()->user->setState('step_3', true);
146  $this->step++;
147  }
148  }
149  }
150  break;
151  case 4:
152 
153  $model = Order::getOrder();
154  $this->performAjaxValidation($model);
155 
156  if(isset($_POST['Order'])){
157 
158  $model->verifyCode = $_POST['Order']['verifyCode'];
159 
160 
161  if(!Yii::app()->user->getState('step_1')){
162  Yii::app()->controller->redirect(Yii::app()->createUrl($this->shoppingcart_link));
163  }else
164  if(!Yii::app()->user->getState('step_2')){
165  Yii::app()->controller->redirect(Yii::app()->createUrl($this->shoppingcart_link , array('step'=>2)));
166  }else
167  if(!Yii::app()->user->getState('step_3')){
168  Yii::app()->controller->redirect(Yii::app()->createUrl($this->shoppingcart_link , array('step'=>3)));
169  }
170 
171  if($model->validate(array('verifyCode'))){
172  $model->ordered = Order::ORDERED;
173  $this->onOrdered($model);
174  if($model->save(false)){
175  $items = $model->items;
176  $referrer = $items[count($items)-1]->referrer;
177 
178  Yii::app()->user->setState('step_1', null);
179  Yii::app()->user->setState('step_2', null);
180  Yii::app()->user->setState('step_3', null);
181 
182  // redirect to thankyoupage if set
183  if(isset($this->thankyoupage) && !empty($this->thankyoupage)){
184  Yii::app()->controller->redirect($this->thankyoupage);
185  }else{
186  Yii::app()->controller->redirect($referrer);
187  }
188  }
189  }else{
190  $this->order = $model;
191  }
192  }
193  break;
194  }
195  //Extend the lifespan of cookie
196  if(!headers_sent()){
197  $cookie = new CHttpCookie('user_id', Order::getUser(true));
198  $cookie->expire = time()+60 * Yii::app()->getModule('shoppingcart')->max_lifetime;
199  if(isset(Yii::app()->getModule('shoppingcart')->cookie_domain)){
200  $cookie->domain = Yii::app()->getModule('shoppingcart')->cookie_domain;
201  }
202  Yii::app()->request->cookies['user_id'] = $cookie;
203  }
204 
205  if(!isset($this->order)){
206  $this->order = Order::getOrder();
207  }
208 
209  $this->order->shipping_info = EncryptHelper::encrypt($this->shipping_info);
210  $this->order->payment_info = EncryptHelper::encrypt($this->payment_info);
211 
212  if(!isset($this->user)){
213  $this->user = Order::getUser()->implementFromSession();
214  }
215  if(!isset($this->items)){
216  $this->items = Item::model()->findAll('order_id=:order_id',array(':order_id'=>$this->order->id));
217  }
218  }
219 
220  /**
221  * Render ShoppingCartWidget view. In this function
222  * can be assign some variables depends of step
223  *
224  * @return void
225  */
226  public function run()
227  {
228  $params = array();
229 
230  switch($this->step){
231  case 1:
232  break;
233  case 2:
234  break;
235  case 3:
236  $params['checksum'] = md5($this->shipping_info.$this->payment_info);
237  break;
238  case 4;
239  break;
240  }
241 
242  $widgetName = "_shoppingStep_".$this->step;
243 
244  $content = '';
245  if (Order::getOrder() !== false) {
246  $content = $this->render($widgetName, $params, true);
247  }
248  $params['content'] = $content;
249 
250  $this->render($this->layout, $params);
251  }
252 
253  /*
254  * The function performs when a user confirms the order,
255  * it marks the order as finished and sends notice to order owner and items administrators
256  */
257  protected function onOrdered($order){
258 
259  $user = Order::getUser()->implementFromSession();
260  $items = $order->items;
261  $itemsToAdmins = array();
262  foreach($items as $item){
263  $itemsToAdmins[$item->admin_email][] = $item;
264  }
265 
266  $admin_email_template = Yii::app()->getModule('shoppingcart')->adminemail_template;
267 
268  foreach($itemsToAdmins as $admin_email => $itemsToAdmin){
269 
270  $subject = ShoppingcartModule::t('Items ordered');
271 
272  $notification = new Notification(
273  $subject,
274  Yii::app()->controller->renderMail('shoppingcart.views.mail_templates.'.$admin_email_template, $subject, array('items' => $itemsToAdmin,'user' => $user),true)
275  );
276 
277  $notification->recipients[] = new EmailRecipient($admin_email, Yii::app()->getModule('shoppingcart')->notificationsEmail);
278 
279  Yii::app()->notificationManager->notifyAbout($notification);
280  }
281 
282  $usermail_template = Yii::app()->getModule('shoppingcart')->usermail_template;
283 
284  if(count($items)){
285 
286  $subject = ShoppingcartModule::t('You ordered items');
287 
288  $notification = new Notification(
289  $subject,
290  Yii::app()->controller->renderMail('shoppingcart.views.mail_templates.'.$usermail_template, $subject, array('items' => $items),true)
291  );
292 
293  $notification->recipients[] = new EmailRecipient($user->email, Yii::app()->getModule('shoppingcart')->notificationsEmail);
294 
295  try {
296  Yii::app()->notificationManager->notifyAbout($notification);
297  } catch (Exception $e) {
298  Yii::log($e->getMessage(), 'warning');
299  }
300  //Clear unnecessary items, users, orders ...
301  Order::cleaning();
302  }
303  }
304 
305 
306 
307  /**
308  * Performs the AJAX validation.
309  * @param CModel the model to be validated
310  */
311  protected function performAjaxValidation($model)
312  {
313  if(isset($_POST['ajax']))
314  {
315  ob_clean();
316  echo CActiveForm::validate($model);
317  Yii::app()->end();
318  }
319  }
320  /**
321  * Function checks matches the csrf tokens
322  *
323  * @return boolean returns true if the tokens are matched
324  */
325 
326  public function csrfProtect(){
327  if(Yii::app()->request->isPostRequest && Yii::app()->request->enableCsrfValidation){
328  if(strcmp($_POST['YII_CSRF_TOKEN'], Yii::app()->request->getCsrfToken()) !== 0 ){
329  throw new CHttpException(403);
330  return false;
331  }
332  }
333  return true;
334  }
335 
336 }