PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/modules/user/models/ProfileField.php

https://bitbucket.org/mkwiek/hairdresser
PHP | 205 lines | 148 code | 16 blank | 41 comment | 11 complexity | d80e7f5d72379bf785f618b4364ec4d8 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause, LGPL-2.1
  1. <?php
  2. class ProfileField extends CActiveRecord
  3. {
  4. const VISIBLE_ALL=3;
  5. const VISIBLE_REGISTER_USER=2;
  6. const VISIBLE_ONLY_OWNER=1;
  7. const VISIBLE_NO=0;
  8. const REQUIRED_NO = 0;
  9. const REQUIRED_YES_SHOW_REG = 1;
  10. const REQUIRED_NO_SHOW_REG = 2;
  11. const REQUIRED_YES_NOT_SHOW_REG = 3;
  12. /**
  13. * The followings are the available columns in table 'profiles_fields':
  14. * @var integer $id
  15. * @var string $varname
  16. * @var string $title
  17. * @var string $field_type
  18. * @var integer $field_size
  19. * @var integer $field_size_mix
  20. * @var integer $required
  21. * @var integer $match
  22. * @var string $range
  23. * @var string $error_message
  24. * @var string $other_validator
  25. * @var string $default
  26. * @var integer $position
  27. * @var integer $visible
  28. */
  29. /**
  30. * Returns the static model of the specified AR class.
  31. * @return CActiveRecord the static model class
  32. */
  33. public static function model($className=__CLASS__)
  34. {
  35. return parent::model($className);
  36. }
  37. /**
  38. * @return string the associated database table name
  39. */
  40. public function tableName()
  41. {
  42. return Yii::app()->getModule('user')->tableProfileFields;
  43. }
  44. /**
  45. * @return array validation rules for model attributes.
  46. */
  47. public function rules()
  48. {
  49. // NOTE: you should only define rules for those attributes that
  50. // will receive user inputs.
  51. return array(
  52. array('varname, title, field_type', 'required'),
  53. array('varname', 'match', 'pattern' => '/^[A-Za-z_0-9]+$/u','message' => UserModule::t("Variable name may consist of A-z, 0-9, underscores, begin with a letter.")),
  54. array('varname', 'unique', 'message' => UserModule::t("This field already exists.")),
  55. array('varname, field_type', 'length', 'max'=>50),
  56. array('field_size, field_size_min, required, position, visible', 'numerical', 'integerOnly'=>true),
  57. array('title, match, error_message, other_validator, default, widget', 'length', 'max'=>255),
  58. array('range, widgetparams', 'length', 'max'=>5000),
  59. );
  60. }
  61. /**
  62. * @return array relational rules.
  63. */
  64. public function relations()
  65. {
  66. // NOTE: you may need to adjust the relation name and the related
  67. // class name for the relations automatically generated below.
  68. return array(
  69. );
  70. }
  71. /**
  72. * @return array customized attribute labels (name=>label)
  73. */
  74. public function attributeLabels()
  75. {
  76. return array(
  77. 'id' => UserModule::t('Id'),
  78. 'varname' => UserModule::t('Variable name'),
  79. 'title' => UserModule::t('Title'),
  80. 'field_type' => UserModule::t('Field Type'),
  81. 'field_size' => UserModule::t('Field Size'),
  82. 'field_size_min' => UserModule::t('Field Size min'),
  83. 'required' => UserModule::t('Required'),
  84. 'match' => UserModule::t('Match'),
  85. 'range' => UserModule::t('Range'),
  86. 'error_message' => UserModule::t('Error Message'),
  87. 'other_validator' => UserModule::t('Other Validator'),
  88. 'default' => UserModule::t('Default'),
  89. 'widget' => UserModule::t('Widget'),
  90. 'widgetparams' => UserModule::t('Widget parametrs'),
  91. 'position' => UserModule::t('Position'),
  92. 'visible' => UserModule::t('Visible'),
  93. );
  94. }
  95. public function scopes()
  96. {
  97. return array(
  98. 'forAll'=>array(
  99. 'condition'=>'visible='.self::VISIBLE_ALL,
  100. 'order'=>'position',
  101. ),
  102. 'forUser'=>array(
  103. 'condition'=>'visible>='.self::VISIBLE_REGISTER_USER,
  104. 'order'=>'position',
  105. ),
  106. 'forOwner'=>array(
  107. 'condition'=>'visible>='.self::VISIBLE_ONLY_OWNER,
  108. 'order'=>'position',
  109. ),
  110. 'forRegistration'=>array(
  111. 'condition'=>'required='.self::REQUIRED_NO_SHOW_REG.' OR required='.self::REQUIRED_YES_SHOW_REG,
  112. 'order'=>'position',
  113. ),
  114. 'sort'=>array(
  115. 'order'=>'position',
  116. ),
  117. );
  118. }
  119. /**
  120. * @param $value
  121. * @return formated value (string)
  122. */
  123. public function widgetView($model) {
  124. if ($this->widget && class_exists($this->widget)) {
  125. $widgetClass = new $this->widget;
  126. $arr = $this->widgetparams;
  127. if ($arr) {
  128. $newParams = $widgetClass->params;
  129. $arr = (array)CJavaScript::jsonDecode($arr);
  130. foreach ($arr as $p=>$v) {
  131. if (isset($newParams[$p])) $newParams[$p] = $v;
  132. }
  133. $widgetClass->params = $newParams;
  134. }
  135. if (method_exists($widgetClass,'viewAttribute')) {
  136. return $widgetClass->viewAttribute($model,$this);
  137. }
  138. }
  139. return false;
  140. }
  141. public function widgetEdit($model,$params=array()) {
  142. if ($this->widget && class_exists($this->widget)) {
  143. $widgetClass = new $this->widget;
  144. $arr = $this->widgetparams;
  145. if ($arr) {
  146. $newParams = $widgetClass->params;
  147. $arr = (array)CJavaScript::jsonDecode($arr);
  148. foreach ($arr as $p=>$v) {
  149. if (isset($newParams[$p])) $newParams[$p] = $v;
  150. }
  151. $widgetClass->params = $newParams;
  152. }
  153. if (method_exists($widgetClass,'editAttribute')) {
  154. return $widgetClass->editAttribute($model,$this,$params);
  155. }
  156. }
  157. return false;
  158. }
  159. public static function itemAlias($type,$code=NULL) {
  160. $_items = array(
  161. 'field_type' => array(
  162. 'INTEGER' => UserModule::t('INTEGER'),
  163. 'VARCHAR' => UserModule::t('VARCHAR'),
  164. 'TEXT'=> UserModule::t('TEXT'),
  165. 'DATE'=> UserModule::t('DATE'),
  166. 'FLOAT'=> UserModule::t('FLOAT'),
  167. 'BOOL'=> UserModule::t('BOOL'),
  168. 'BLOB'=> UserModule::t('BLOB'),
  169. 'BINARY'=> UserModule::t('BINARY'),
  170. ),
  171. 'required' => array(
  172. self::REQUIRED_NO => UserModule::t('No'),
  173. self::REQUIRED_NO_SHOW_REG => UserModule::t('No, but show on registration form'),
  174. self::REQUIRED_YES_SHOW_REG => UserModule::t('Yes and show on registration form'),
  175. self::REQUIRED_YES_NOT_SHOW_REG => UserModule::t('Yes'),
  176. ),
  177. 'visible' => array(
  178. self::VISIBLE_ALL => UserModule::t('For all'),
  179. self::VISIBLE_REGISTER_USER => UserModule::t('Registered users'),
  180. self::VISIBLE_ONLY_OWNER => UserModule::t('Only owner'),
  181. self::VISIBLE_NO => UserModule::t('Hidden'),
  182. ),
  183. );
  184. if (isset($code))
  185. return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
  186. else
  187. return isset($_items[$type]) ? $_items[$type] : false;
  188. }
  189. }