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  $itemsCount = count(Order::getOrder()->items);
70 
71  if(!isset($this->shoppingcart_link)){
72  $this->shoppingcart_link = substr($_SERVER['REDIRECT_URL'],1);
73  }
74  if(!isset($this->thankyoupage)
75  && isset(Yii::app()->getModule('shoppingcart')->thankyoupage[Yii::app()->language])){
76  $this->thankyoupage = Yii::app()->getModule('shoppingcart')->thankyoupage[Yii::app()->language];
77  }
78  if(isset($this->thankyoupage)){
79  Yii::app()->user->setState('thankyoupage',$this->thankyoupage);
80  }
81 
82  $this->step = isset($_REQUEST['step']) ? $_REQUEST['step'] : 1;
83  $data = array();
84 
85  switch($this->step){
86  case 1:
87  $order_id = Order::getOrder(true);
88  $items = Item::model()->findAll('order_id=:order_id',array(':order_id'=>$order_id));
89 
90  if(isset($_POST['Item']))
91  {
92  $valid=true;
93  foreach($items as $i=>$item)
94  {
95  if(isset($_POST['Item'][$i]) && is_numeric($_POST['Item'][$i]['quantity']) && $_POST['Item'][$i]['quantity'] > 0)
96  $item->quantity = $_POST['Item'][$i]['quantity'];
97  $valid = $item->validate(array('quantity'))
98  && is_numeric($_POST['Item'][$i]['quantity'])
99  && $_POST['Item'][$i]['quantity'] > 0
100  && $valid;
101  }
102 
103  if($valid){
104  foreach($items as $item){
105  $item->save();
106  }
107 
108  Yii::app()->user->setState('step_1', true);
109  $this->step++;
110  }
111  }
112 
113  break;
114  case 2:
115 
116  $model = Order::getUser();
117  $this->performAjaxValidation($model);
118 
119  if(isset($_POST['TmpUser']))
120  {
121  $model->attributes=$_POST['TmpUser'];
122 
123  if($model->validate() && $model->unsetExistFields()->save(false)){
124 
125  Yii::app()->user->setState('step_2', true);
126  $this->step++;
127  }else{
128  $this->user = $model;
129  }
130  }
131  break;
132  case 3:
133  $model = Order::getOrder();
134 
135  if(isset($_POST['Order']))
136  {
137  $model->shipping_info = EncryptHelper::decrypt($_POST['Order']['shipping_info']);
138  $model->payment_info = EncryptHelper::decrypt($_POST['Order']['payment_info']);
139 
140  if(md5($model->shipping_info.$model->payment_info) == $_POST['checksum']){
141  if($model->save(false)){
142  Yii::app()->user->setState('step_3', true);
143  $this->step++;
144  }
145  }
146  }
147  break;
148  case 4:
149 
150  $model = Order::getOrder();
151  $this->performAjaxValidation($model);
152 
153  if(isset($_POST['Order'])){
154 
155  $model->verifyCode = $_POST['Order']['verifyCode'];
156 
157 
158  if(!Yii::app()->user->getState('step_1')){
159  Yii::app()->controller->redirect(Yii::app()->createUrl($this->shoppingcart_link));
160  }else
161  if(!Yii::app()->user->getState('step_2')){
162  Yii::app()->controller->redirect(Yii::app()->createUrl($this->shoppingcart_link , array('step'=>2)));
163  }else
164  if(!Yii::app()->user->getState('step_3')){
165  Yii::app()->controller->redirect(Yii::app()->createUrl($this->shoppingcart_link , array('step'=>3)));
166  }
167 
168  if($model->validate(array('verifyCode'))){
169  $model->ordered = Order::ORDERED;
170  $this->onOrdered($model);
171  if($model->save(false)){
172  $items = $model->items;
173  $referrer = $items[count($items)-1]->referrer;
174 
175  Yii::app()->user->setState('step_1', null);
176  Yii::app()->user->setState('step_2', null);
177  Yii::app()->user->setState('step_3', null);
178 
179  // redirect to thankyoupage if set
180  if(isset($this->thankyoupage) && !empty($this->thankyoupage)){
181  Yii::app()->controller->redirect($this->thankyoupage);
182  }else{
183  Yii::app()->controller->redirect($referrer);
184  }
185  }
186  }else{
187  $this->order = $model;
188  }
189  }
190  break;
191  }
192  //Extend the lifespan of cookie
193  if(!headers_sent()){
194  $cookie = new CHttpCookie('user_id', Order::getUser(true));
195  $cookie->expire = time()+60 * Yii::app()->getModule('shoppingcart')->max_lifetime;
196  if(isset(Yii::app()->getModule('shoppingcart')->cookie_domain)){
197  $cookie->domain = Yii::app()->getModule('shoppingcart')->cookie_domain;
198  }
199  Yii::app()->request->cookies['user_id'] = $cookie;
200  }
201 
202  if(!isset($this->order)){
203  $this->order = Order::getOrder();
204  }
205 
206  $this->order->shipping_info = EncryptHelper::encrypt($this->shipping_info);
207  $this->order->payment_info = EncryptHelper::encrypt($this->payment_info);
208 
209  if(!isset($this->user)){
210  $this->user = Order::getUser()->implementFromSession();
211  }
212  if(!isset($this->items)){
213  $this->items = Item::model()->findAll('order_id=:order_id',array(':order_id'=>$this->order->id));
214  }
215  }
216 
217  /**
218  * Render ShoppingCartWidget view. In this function
219  * can be assign some variables depends of step
220  *
221  * @return void
222  */
223  public function run()
224  {
225  $params = array();
226 
227  switch($this->step){
228  case 1:
229  break;
230  case 2:
231  break;
232  case 3:
233  $params['checksum'] = md5($this->shipping_info.$this->payment_info);
234  break;
235  case 4;
236  break;
237  }
238 
239  $widgetName = "_shoppingStep_".$this->step;
240 
241  $content = $this->render($widgetName, $params, true);
242 
243  $params['content'] = $content;
244 
245  $this->render($this->layout, $params);
246  }
247 
248  /*
249  * The function performs when a user confirms the order,
250  * it marks the order as finished and sends notice to order owner and items administrators
251  */
252  protected function onOrdered($order){
253 
254  $user = Order::getUser()->implementFromSession();
255  $items = $order->items;
256  $itemsToAdmins = array();
257  foreach($items as $item){
258  $itemsToAdmins[$item->admin_email][] = $item;
259  }
260 
261  $admin_email_template = Yii::app()->getModule('shoppingcart')->adminemail_template;
262 
263  foreach($itemsToAdmins as $admin_email => $itemsToAdmin){
264 
265  $subject = ShoppingcartModule::t('Items ordered');
266 
267  $notification = new Notification(
268  $subject,
269  Yii::app()->controller->renderMail('shoppingcart.views.mail_templates.'.$admin_email_template, $subject, array('items' => $itemsToAdmin,'user' => $user),true)
270  );
271 
272  $notification->recipients[] = new EmailRecipient($admin_email, Yii::app()->getModule('shoppingcart')->notificationsEmail);
273 
274  Yii::app()->notificationManager->notifyAbout($notification);
275  }
276 
277  $usermail_template = Yii::app()->getModule('shoppingcart')->usermail_template;
278 
279  // only one email for customer!(fix)
280  // foreach($items as $item){
281  if(count($items)){
282  $subject = ShoppingcartModule::t('You ordered items');
283 
284  $notification = new Notification(
285  $subject,
286  Yii::app()->controller->renderMail('shoppingcart.views.mail_templates.'.$usermail_template, $subject, array('items' => $items),true)
287  );
288 
289  $notification->recipients[] = new EmailRecipient($user->email, Yii::app()->getModule('shoppingcart')->notificationsEmail);
290 
291  Yii::app()->notificationManager->notifyAbout($notification);
292  }
293  }
294 
295 
296 
297  /**
298  * Performs the AJAX validation.
299  * @param CModel the model to be validated
300  */
301  protected function performAjaxValidation($model)
302  {
303  if(isset($_POST['ajax']))
304  {
305  ob_clean();
306  echo CActiveForm::validate($model);
307  Yii::app()->end();
308  }
309  }
310  /**
311  * Function checks matches the csrf tokens
312  *
313  * @return boolean returns true if the tokens are matched
314  */
315 
316  public function csrfProtect(){
317  if(Yii::app()->request->isPostRequest && Yii::app()->request->enableCsrfValidation){
318  if(strcmp($_POST['YII_CSRF_TOKEN'], Yii::app()->request->getCsrfToken()) !== 0 ){
319  throw new CHttpException(403);
320  return false;
321  }
322  }
323  return true;
324  }
325 
326 }