Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Order.php
1 <?php
2 
3 /**
4  * Gentics Portal.Node PHP
5  * Author & Copyright (c) by Gentics Software GmbH
6  * sales@gentics.com
7  * http://www.gentics.com
8  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
9  * You must not use this software without a valid license agreement.
10  *
11  * This is the model class for table "{{order}}".
12  *
13  * The followings are the available columns in table '{{order}}':
14  * @property string $id
15  * @property integer $ordered
16  * @property string $user_id
17  * @property string $shipping_info
18  * @property string $payment_info
19  */
20 class Order extends CActiveRecord
21 {
22  private $_userId;
23 
24  const ORDERED = 1;
25  const PROCESS = 0;
26 
27  public $verifyCode;
28 
29  /**
30  * Returns the static model of the specified AR class.
31  * @param string $className active record class name.
32  * @return Order the static model class
33  */
34  public static function model($className=__CLASS__)
35  {
36  return parent::model($className);
37  }
38 
39  /**
40  * @return string the associated database table name
41  */
42  public function tableName()
43  {
44  return '{{order}}';
45  }
46 
47  /**
48  * @return array validation rules for model attributes.
49  */
50  public function rules()
51  {
52  // NOTE: you should only define rules for those attributes that
53  // will receive user inputs.
54  return array(
55  array('ordered', 'numerical', 'integerOnly'=>true),
56  array('shipping_info, payment_info', 'length', 'max'=>1024),
57  array('user_id', 'length', 'max'=>10),
58  // The following rule is used by search().
59  // Please remove those attributes that should not be searched.
60  array('id, ordered, user_id, tmp_user_id, shipping_info, payment_info', 'safe', 'on'=>'search'),
61  array('verifyCode','captcha','allowEmpty'=>!Yii::app()->user->isGuest || !CCaptcha::checkRequirements() || !Yii::app()->getModule('shoppingcart')->captchaEnabled, 'captchaAction' => '/shoppingcart/shoppingcart/captcha'),
62  );
63  }
64 
65  /**
66  * @return array relational rules.
67  */
68  public function relations()
69  {
70  // NOTE: you may need to adjust the relation name and the related
71  // class name for the relations automatically generated below.
72  return array(
73  'order_items' => array(self::HAS_MANY, 'Item', 'order_id'),
74  );
75  }
76  /**
77  * @return array named scopes.
78  */
79  public function scopes()
80  {
81  return array(
82  'ordered'=>array(
83  'condition'=>'ordered='.self::ORDERED,
84  ),
85  'process'=>array(
86  'condition'=>'ordered='.self::PROCESS,
87  ),
88  );
89  }
90 
91  /**
92  * @return array customized attribute labels (name=>label)
93  */
94  public function attributeLabels()
95  {
96  return array(
97  'id' => ShoppingcartModule::t('ID'),
98  'ordered' => ShoppingcartModule::t('Ordered'),
99  'user_id' => ShoppingcartModule::t('User'),
100  'shipping_info' => ShoppingcartModule::t('Shipping Info'),
101  'payment_info' => ShoppingcartModule::t('Payment Info'),
102  'verifyCode' => ShoppingcartModule::t('Verify Code'),
103  );
104  }
105  /*
106  * The method returns the model of actual order or its id
107  * @param integer $id_only bool value that determines what will be returned
108  * @return object | integer
109  */
110  public static function getOrder($id_only = false){
111 
112  $user_id = self::getUser(true);
113  if($user_id === false){
114  return false;
115  }
116  $cacheId = self::getOrderCacheId($user_id);
117  if (!($model = Yii::app()->cache->get($cacheId, false))) {
118  $model = self::model()->process()->find('user_id=:user_id',array(':user_id' =>$user_id));
119  if (!isset($model)) {
120  $model = new Order;
121  $model->user_id = $user_id;
122  $model->save(false);
123  }
124  Yii::app()->cache->set($cacheId, $model, Yii::app()->getModule('shoppingcart')->cacheTime);
125  }
126 
127  return $id_only? $model->id: $model;
128  }
129  /*
130  * The method returns the model od id of user who is owner of actual order
131  * @param integer $id_only bool value that determines what will be returned
132  * @return object | integer
133  */
134  public static function getUser($id_only = false)
135  {
136  $user_id = Yii::app()->user->getState('user_id', null);
137  $cacheId = self::getUserCacheId($user_id);
138  if (!(isset($user_id) && $user = Yii::app()->cache->get($cacheId, false))) {
139  $user = null;
140  if (!isset($user_id) && isset(Yii::app()->user->id)) {
141  $user = TmpUser::model()->with('order')->find('t.parent_user_id=:parent_user_id AND order.ordered=:process',
142  array(':parent_user_id'=>Yii::app()->user->id,':process'=>Order::PROCESS));
143  }
144  if (!isset($user_id) && !isset($user)) {
145  $user_id = (string)Yii::app()->request->cookies['user_id'];
146  }
147  if (isset($user_id) && !isset($user)) {
148  $user = TmpUser::model()->findByPk($user_id);
149  }
150  if (!isset($user)) {
151  if(self::getItemIsAdded()){
152  $user = new TmpUser;
153  if(isset(Yii::app()->user->id)){
154  $user->parent_user_id = Yii::app()->user->id;
155  }
156  $user->save(false);
157  Yii::app()->user->setState('user_id', $user->id);
158  if (!headers_sent()) {
159  $cookie = new CHttpCookie('user_id', $user->id);
160  $cookie->expire = time()+60 * Yii::app()->getModule('shoppingcart')->max_lifetime;
161  $cookie->domain=Yii::app()->getModule('shoppingcart')->cookie_domain;
162  Yii::app()->request->cookies['user_id'] = $cookie;
163  }
164  }else{
165  return false;
166  }
167  }
168  if (isset(Yii::app()->user->id) && !isset($user->parent_user_id)) {
169  $user->parent_user_id = Yii::app()->user->id;
170  $user->save(false, array('parent_user_id'));
171  }
172 
173  $cacheId = self::getUserCacheId($user->id);
174  Yii::app()->cache->set($cacheId, $user, Yii::app()->getModule('shoppingcart')->cacheTime);
175  }
176 
177  return $id_only? $user->id: $user;
178  }
179  /*
180  * The method checks if item is added right now
181  * @return boolean if item is added
182  */
183  public static function getItemIsAdded(){
184  return (Yii::app()->controller->id === 'item'
185  && Yii::app()->controller->action->id === 'add')
186  || Yii::app()->controller->id === 'phpunit';
187  }
188  /*
189  * The method generates cache id for current user
190  * @param integer $user_id current user id
191  * @return string cache id
192  */
193  public static function getUserCacheId($user_id){
194  $cache_id = $user_id;
195  if (isset(Yii::app()->user->id)) {
196  $cache_id .= Yii::app()->user->id;
197  }
198  return "customer_" . md5($cache_id);
199  }
200  /*
201  * The method generates cache id for order
202  * @param integer $user_id current user id
203  * @return string cache id
204  */
205  public static function getOrderCacheId($user_id){
206  return 'order_' . md5($user_id);
207  }
208  /*
209  * The method generates cache id for order items
210  * @param integer $user_id current user id
211  * @return string cache id
212  */
213  public static function getOrderItemsCacheId($user_id){
214  return 'order_items_' . md5($user_id);
215  }
216  /*
217  * The method clears order cache.
218  * It is used when something is changing in the order
219  */
220  public static function flushOrder(){
221  $user_id = self::getUser(true);
222  $idsToFlush = array(
223  self::getOrderCacheId($user_id),
224  self::getOrderItemsCacheId($user_id)
225  );
226  foreach ($idsToFlush as $cacheId) {
227  Yii::app()->cache->delete($cacheId);
228  }
229  }
230  /**
231  * Retrieves a list of models based on the current search/filter conditions.
232  * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
233  */
234  public function search()
235  {
236  // Warning: Please modify the following code to remove attributes that
237  // should not be searched.
238 
239  $criteria=new CDbCriteria;
240 
241  $criteria->compare('id',$this->id,true);
242  $criteria->compare('ordered',$this->ordered);
243  $criteria->compare('user_id',$this->user_id,true);
244  $criteria->compare('shipping_info',$this->shipping_info,true);
245  $criteria->compare('payment_info',$this->order_info,true);
246 
247  return new CActiveDataProvider($this, array(
248  'criteria'=>$criteria,
249  ));
250  }
251 
252  /**
253  * The function that deletes unnecessary items, orders and users
254  * lifetime of which has already gone
255  *
256  * @return void
257  */
258  static function cleaning(){
259 
260  $max_lifetime = Yii::app()->getModule('shoppingcart')->max_lifetime;
261 
262  $connection = Yii::app()->db;
263  //Find all process orders which should be removed
264 
265  Yii::beginProfile('garbageCollector');
266 
267  $ordersToRemove = $command=$connection
268  ->createCommand(
269  "SELECT ProcessOrder.id AS order_id,
270  ProcessOrder.user_id,
271  IF((DATE_ADD(IFNULL(MAX(Item.ordered_at),ProcessOrder.started_at), INTERVAL :max_lifetime MINUTE) < NOW()) , TRUE , FALSE) AS order_to_remove,
272  IF((COUNT(OrderedOrder.id) < 1) , TRUE, FALSE) AS user_to_remove
273  FROM {{order}} AS ProcessOrder
274  LEFT JOIN {{item}} AS Item
275  ON ProcessOrder.id = Item.order_id
276  LEFT JOIN {{order}} AS OrderedOrder
277  ON ProcessOrder.user_id = OrderedOrder.user_id AND OrderedOrder.ordered = :ordered
278  WHERE ProcessOrder.ordered=:process GROUP BY ProcessOrder.id
279  HAVING order_to_remove = TRUE")
280  ->bindValues(array(
281  ':max_lifetime' => $max_lifetime,
282  ':ordered' => Order::ORDERED,
283  ':process' => Order::PROCESS
284  ))->queryAll();
285 
286  $orderIds = array();
287  $userIds = array();
288  foreach($ordersToRemove as $order){
289  $orderIds[] = $order['order_id'];
290  if($order['user_to_remove']){
291  $userIds[] = $order['user_id'];
292  }
293  }
294  $orderIds = array_unique($orderIds);
295  $userIds = array_unique($userIds);
296  //Delete items
297  if(count($orderIds)){
298  $criteria=new CDbCriteria;
299  $criteria->addInCondition('order_id', $orderIds);
300  Item::model()->deleteAll($criteria);
301  }
302  //Delete orders
303  if(count($orderIds)){
304  $criteria=new CDbCriteria;
305  $criteria->addInCondition('id', $orderIds);
306  Order::model()->deleteAll($criteria);
307  }
308  //Delete users
309  if(count($orderIds)){
310  $criteria=new CDbCriteria;
311  $criteria->addInCondition('id', $userIds);
312  TmpUser::model()->deleteAll($criteria);
313  }
314  Yii::endProfile('garbageCollector');
315  }
316  /**
317  * The function performs caching of the order items
318  *
319  * @return array order items
320  */
321  public function getItems(){
322 
323  $user_id = self::getUser(true);
324  $cacheId = self::getOrderItemsCacheId($user_id);
325  if (!($data = Yii::app()->cache->get($cacheId, false))) {
326  $data = $this->order_items;
327  Yii::app()->cache->set($cacheId, $data, Yii::app()->getModule('shoppingcart')->cacheTime);
328  }
329  return $data;
330  }
331 
332 }