PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/models/UserDetails.php

http://web3cms.googlecode.com/
PHP | 247 lines | 144 code | 13 blank | 90 comment | 8 complexity | cf0a0dbce8000353cc7b66e277b41600 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. class UserDetails extends _CActiveRecord
  3. {
  4. /**
  5. * The followings are the available columns in table 'w3_user_details':
  6. * @var integer $userId
  7. * @var string $passwordHint
  8. * @var string $isEmailConfirmed
  9. * @var string $emailConfirmationKey
  10. * @var string $isEmailVisible
  11. * @var string $isScreenNameEditable
  12. * @var integer $deactivationTime
  13. * @var string $firstName
  14. * @var string $middleName
  15. * @var string $lastName
  16. * @var string $initials
  17. * @var string $occupation
  18. * @var string $gender
  19. * @var string $birthDate
  20. * @var string $textStatus
  21. * @var integer $lastLoginTime
  22. * @var integer $lastVisitTime
  23. * @var integer $totalTimeLoggedIn
  24. * @var string $secretQuestion
  25. * @var string $secretAnswer
  26. * @var string $administratorNote
  27. * @var integer $updateTime
  28. */
  29. const EMAIL_IS_CONFIRMED='1';
  30. const EMAIL_IS_NOT_CONFIRMED='0';
  31. const EMAIL_IS_VISIBLE='1';
  32. const EMAIL_IS_NOT_VISIBLE='0';
  33. /**
  34. * Returns the static model of the specified AR class.
  35. * @return CActiveRecord the static model class
  36. */
  37. public static function model($className=__CLASS__)
  38. {
  39. return parent::model($className);
  40. }
  41. /**
  42. * @return string the associated database table name (without prefix)
  43. */
  44. protected function _tableName()
  45. {
  46. return 'user_details';
  47. }
  48. /**
  49. * @return array validation rules for model attributes.
  50. */
  51. public function rules()
  52. {
  53. $retval=array();
  54. if(Yii::app()->user->checkAccess(User::ADMINISTRATOR))
  55. {
  56. // administratorNote is safe
  57. $retval[]=array('administratorNote', 'safe', 'on'=>'not sure');
  58. // deactivationTime has to be integer
  59. $retval[]=array('deactivationTime', 'numerical', 'integerOnly'=>true, 'on'=>'not sure');
  60. // deactivationTime has to be 10 characters length max
  61. $retval[]=array('deactivationTime', 'length', 'max'=>10, 'on'=>'not sure');
  62. }
  63. // emailConfirmationKey has to be 32 characters length max
  64. $retval[]=array('emailConfirmationKey', 'length', 'max'=>32, 'on'=>'not sure');
  65. // firstName has to be 128 characters length max
  66. $retval[]=array('firstName', 'length', 'max'=>128, 'on'=>'not sure');
  67. // initials has to be 16 characters length max on update
  68. $retval[]=array('initials', 'length', 'max'=>16, 'on'=>'update');
  69. // isEmailConfirmed is in range
  70. $retval[]=array('isEmailConfirmed', 'in', 'range'=>array(null,self::EMAIL_IS_CONFIRMED,self::EMAIL_IS_NOT_CONFIRMED), 'strict'=>true, 'allowEmpty'=>false, 'on'=>'not sure');
  71. // isEmailVisible is in range
  72. $retval[]=array('isEmailVisible', 'in', 'range'=>array(null,self::EMAIL_IS_VISIBLE,self::EMAIL_IS_NOT_VISIBLE), 'strict'=>true, 'allowEmpty'=>false, 'on'=>'not sure');
  73. if(Yii::app()->user->checkAccess(User::ADMINISTRATOR))
  74. // isScreenNameEditable needs to be a boolean
  75. $retval[]=array('isScreenNameEditable', 'boolean', 'on'=>'not sure');//in
  76. // lastLoginTime has to be 10 characters length max
  77. $retval[]=array('lastLoginTime', 'length', 'max'=>10, 'on'=>'not sure');
  78. // lastVisitTime has to be 10 characters length max
  79. $retval[]=array('lastVisitTime', 'length', 'max'=>10, 'on'=>'not sure');
  80. // lastName has to be 128 characters length max
  81. $retval[]=array('lastName', 'length', 'max'=>128, 'on'=>'not sure');
  82. // middleName has to be 128 characters length max
  83. $retval[]=array('middleName', 'length', 'max'=>128, 'on'=>'not sure');
  84. // occupation has to be 128 characters length max on update
  85. $retval[]=array('occupation', 'length', 'max'=>128, 'on'=>'update');
  86. // secretAnswer has to be 255 characters length max
  87. $retval[]=array('secretAnswer', 'length', 'max'=>255, 'on'=>'not sure');
  88. // totalTimeLoggedIn has to be integer
  89. $retval[]=array('totalTimeLoggedIn', 'numerical', 'integerOnly'=>true, 'on'=>'not sure');
  90. // totalTimeLoggedIn has to be 9 characters length max
  91. $retval[]=array('totalTimeLoggedIn', 'length', 'max'=>9, 'on'=>'not sure');
  92. return $retval;
  93. }
  94. /**
  95. * @return array relational rules.
  96. */
  97. public function relations()
  98. {
  99. return array(
  100. // user details belongs to an 'user' record associated
  101. 'user' => array(self::BELONGS_TO,'User','userId',
  102. 'alias'=>'UserDetails_User'
  103. ),
  104. );
  105. }
  106. /**
  107. * @return array customized attribute labels (name=>label)
  108. */
  109. public function attributeLabels()
  110. {
  111. return array(
  112. 'deactivationTime'=>Yii::t('t','Deactivation date'),
  113. 'Deact'=>Yii::t('t','Deact.[Deactivation]'),
  114. 'isEmailConfirmed'=>Yii::t('t','Email is confirmed'),
  115. 'isEmailVisible'=>Yii::t('t','Email is visible'),
  116. 'initials'=>Yii::t('t','Initials'),
  117. 'occupation'=>Yii::t('t','Occupation'),
  118. 'updateTime'=>Yii::t('t','Update date'),
  119. 'userId'=>'User',
  120. 'passwordHint'=>'Password Hint',
  121. 'emailConfirmationKey'=>'Email Confirmation Key',
  122. 'isScreenNameEditable'=>'Is Screen Name Editable',
  123. 'firstName'=>'First Name',
  124. 'middleName'=>'Middle Name',
  125. 'lastName'=>'Last Name',
  126. 'gender'=>'Gender',
  127. 'birthDate'=>'Birth Date',
  128. 'textStatus'=>'Text Status',
  129. 'lastLoginTime'=>'Last login date',
  130. 'lastVisitTime'=>'Last visit date',
  131. 'totalTimeLoggedIn'=>'Total Time Logged In',
  132. 'secretQuestion'=>'Secret Question',
  133. 'secretAnswer'=>'Secret Answer',
  134. 'administratorNote'=>'Administrator note',
  135. );
  136. }
  137. /**
  138. * Prepares attributes before performing validation.
  139. */
  140. protected function beforeValidate()
  141. {
  142. $scenario=$this->getScenario();
  143. if(isset($_POST[__CLASS__]['isEmailConfirmed']) && $this->isEmailConfirmed!==self::EMAIL_IS_CONFIRMED && $this->isEmailConfirmed!==self::EMAIL_IS_NOT_CONFIRMED)
  144. // enum('0','1') null
  145. $this->isEmailConfirmed=null;
  146. if(isset($_POST[__CLASS__]['isEmailVisible']) && $this->isEmailVisible!==self::EMAIL_IS_VISIBLE && $this->isEmailVisible!==self::EMAIL_IS_NOT_VISIBLE)
  147. // enum('0','1') null
  148. $this->isEmailVisible=null;
  149. // parent does all common work
  150. return parent::beforeValidate();
  151. }
  152. /**
  153. * Last model processing before save in db.
  154. */
  155. /*protected function beforeSave()
  156. {
  157. return true;
  158. }*/
  159. /**
  160. * Returns i18n (translated) representation of the attribute value for view.
  161. * @param string the attribute name
  162. * @return string the attribute value's translation
  163. */
  164. public function getAttributeView($attribute)
  165. {
  166. switch($attribute)
  167. {
  168. case 'isEmailConfirmed':
  169. switch($this->isEmailConfirmed)
  170. {
  171. case self::EMAIL_IS_CONFIRMED:
  172. return Yii::t('attr','Yes (Member has confirmed indicated email)');
  173. case self::EMAIL_IS_NOT_CONFIRMED:
  174. return Yii::t('attr','No (Member has not yet confirmed indicated email)');
  175. default:
  176. return $this->isEmailConfirmed;
  177. }
  178. case 'isEmailVisible':
  179. switch($this->isEmailVisible)
  180. {
  181. case self::EMAIL_IS_VISIBLE:
  182. return Yii::t('attr','Yes (Email is visible by all members)');
  183. case self::EMAIL_IS_NOT_VISIBLE:
  184. return Yii::t('attr','No (Email is not visible by other members)');
  185. case null:
  186. return Yii::t('attr','By default (Email is not visible by other members)');
  187. default:
  188. return $this->isEmailVisible;
  189. }
  190. default:
  191. return $this->$attribute;
  192. }
  193. }
  194. /**
  195. * Returns data array of the attribute for create/update.
  196. * @param string the attribute name
  197. * @return array the attribute's data
  198. */
  199. public function getAttributeData($attribute)
  200. {
  201. switch($attribute)
  202. {
  203. case 'isEmailConfirmed':
  204. return array(
  205. self::EMAIL_IS_NOT_CONFIRMED=>Yii::t('attr','No (Member has not yet confirmed indicated email)'),
  206. self::EMAIL_IS_CONFIRMED=>Yii::t('attr','Yes (Member has confirmed indicated email)'),
  207. );
  208. case 'isEmailVisible':
  209. return array(
  210. null=>Yii::t('attr','By default (Email is not visible by other members)'),
  211. self::EMAIL_IS_NOT_VISIBLE=>Yii::t('attr','No (Email is not visible by other members)'),
  212. self::EMAIL_IS_VISIBLE=>Yii::t('attr','Yes (Email is visible by all members)'),
  213. );
  214. default:
  215. return $this->$attribute;
  216. }
  217. }
  218. /**
  219. * Generates the email confirmation key.
  220. * @return string key
  221. */
  222. public function generateConfirmationKey()
  223. {
  224. return md5(uniqid(rand(),true));
  225. }
  226. /**
  227. * Whether user email is visible by all members.
  228. * @return bool
  229. */
  230. public function isEmailVisible()
  231. {
  232. return $this->isEmailVisible===self::EMAIL_IS_VISIBLE;
  233. }
  234. }