HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
AccountUsers.php
Go to the documentation of this file.
1 <?php
2 
11 class AccountUsers extends CActiveRecord
12 {
18  const DEFAULT_ACCOUNT_TYPE = 'default';
19 
25  const DEFAULT_TEMP_ACCOUNT_TYPE = 'default_temp';
26 
30  public function tableName()
31  {
32  return 'account_users';
33  }
34 
38  public function rules()
39  {
40  // NOTE: you should only define rules for those attributes that
41  // will receive user inputs.
42  return array(
43  array('UserId, AccountTypeId', 'required'),
44  array('UserId, AccountTypeId', 'length', 'max' => 20),
45  // The following rule is used by search().
46  // @todo Please remove those attributes that should not be searched.
47  array('UserId, AccountTypeId', 'safe', 'on' => 'search'),
48  );
49  }
50 
54  public function relations()
55  {
56  // NOTE: you may need to adjust the relation name and the related
57  // class name for the relations automatically generated below.
58  return array(
59  'AccountTypes' => array(self::BELONGS_TO, 'AccountTypes', 'AccountTypeId'),
60  'User' => array(self::BELONGS_TO, 'User', 'UserId'),
61  );
62  }
63 
67  public function attributeLabels()
68  {
69  return array(
70  'UserId' => 'User',
71  'AccountTypeId' => 'Account Type',
72  );
73  }
74 
87  public function search()
88  {
89  // @todo Please modify the following code to remove attributes that should not be searched.
90 
91  $criteria = new CDbCriteria();
92 
93  $criteria->compare('UserId', $this->UserId, true);
94  $criteria->compare('AccountTypeId', $this->AccountTypeId, true);
95 
96  return new CActiveDataProvider($this, array(
97  'criteria' => $criteria,
98  ));
99  }
100 
109  public static function model($className = __CLASS__)
110  {
111  return parent::model($className);
112  }
113 
119  public function setUserDefaultAccountType($userId, $defaultType = self::DEFAULT_ACCOUNT_TYPE)
120  {
121  if (empty($userId)) {
122  return false;
123  }
124 
125  return $this->setUserAccountType($userId, $defaultType);
126  }
127 
136  public function setUserAccountType($userId, $accountType)
137  {
138  if (empty($userId) || empty($accountType)) {
139  return false;
140  }
141  $AccountTypes = AccountTypes::model()->findByAttributes(array(
142  'Type' => $accountType,
143  ));
144  if (empty($AccountTypes)) {
145  return false;
146  }
147 
148  $AccountUsers = AccountUsers::model()->findByPk($userId);
149  if (empty($AccountUsers)) {
150  $AccountUsers = new AccountUsers();
151  }
152 
153  $AccountUsers->UserId = $userId;
154  $AccountUsers->AccountTypeId = $AccountTypes->Id;
155 
156  return $AccountUsers->save();
157  }
158 
163  public function setAllUsersDefaultAccountTypeByAccountTypeId($accountTypeId)
164  {
165  $AccountUsers = AccountUsers::model()->findAllByAttributes(array(
166  'AccountTypeId' => $accountTypeId,
167  ));
168  foreach ($AccountUsers as $AccountUser) {
169  VarDumper::dump($AccountUser->UserId);
170  AccountUsers::model()->setUserDefaultAccountType($AccountUser->UserId);
171  }
172  }
173 }