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  '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 
114  $model = self::model()
115  ->process()->find(
116  array('condition'=>'user_id=:user_id','params'=> array(':user_id' =>$user_id))
117  );
118 
119  if(!isset($model)){
120  $model = new Order;
121  $model->user_id = $user_id;
122  $model->save(false);
123  }
124 
125  return $id_only? $model->id: $model;
126  }
127  /*
128  * The method returns the model od id of user who is owner of actual order
129  * @param integer $id_only bool value that determines what will be returned
130  * @return object | integer
131  */
132  public static function getUser($id_only = false)
133  {
134  if(!isset($user_id) && isset(Yii::app()->user->id)){
135  $user = TmpUser::model()->with('order')->find('t.parent_user_id=:parent_user_id AND order.ordered=:process',
136  array(':parent_user_id'=>Yii::app()->user->id,':process'=>Order::PROCESS));
137  if(isset($user)){
138  Yii::app()->user->setState('user_id', $user->id);
139  }
140  }
141 
142  $user_id = Yii::app()->user->getState('user_id');
143  if(!isset($user_id)){
144  $user_id = (string)Yii::app()->request->cookies['user_id'];
145  Yii::app()->user->setState('user_id', $user_id);
146  }
147 
148  if(isset($user_id) && !isset($user)) {
149  $user = TmpUser::model()->findByPk($user_id);
150  }
151 
152  if(!isset($user)){
153  $user = new TmpUser;
154  if(isset(Yii::app()->user->id)){
155  $user->parent_user_id = Yii::app()->user->id;
156  }
157  $user->save(false);
158  Yii::app()->user->setState('user_id', $user->id);
159  if (!headers_sent()){
160  $cookie = new CHttpCookie('user_id', $user->id);
161  $cookie->expire = time()+60 * Yii::app()->getModule('shoppingcart')->max_lifetime;
162  $cookie->domain=Yii::app()->getModule('shoppingcart')->cookie_domain;
163  Yii::app()->request->cookies['user_id'] = $cookie;
164  }
165  }
166 
167  if(isset(Yii::app()->user->id) && !isset($user->parent_user_id)){
168  $user->parent_user_id = Yii::app()->user->id;
169  $user->save(false);
170  }
171 
172  return $id_only? $user->id: $user;
173  }
174 
175 
176  /**
177  * Retrieves a list of models based on the current search/filter conditions.
178  * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
179  */
180  public function search()
181  {
182  // Warning: Please modify the following code to remove attributes that
183  // should not be searched.
184 
185  $criteria=new CDbCriteria;
186 
187  $criteria->compare('id',$this->id,true);
188  $criteria->compare('ordered',$this->ordered);
189  $criteria->compare('user_id',$this->user_id,true);
190  $criteria->compare('shipping_info',$this->shipping_info,true);
191  $criteria->compare('payment_info',$this->order_info,true);
192 
193  return new CActiveDataProvider($this, array(
194  'criteria'=>$criteria,
195  ));
196  }
197 }