HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
Profile.php
Go to the documentation of this file.
1 <?php
2 
3 class Profile extends UActiveRecord
4 {
11  public $regMode = false;
12 
13  private $_model;
14  private $_modelReg;
15  private $_rules = array();
16 
22  public static function model($className = __CLASS__)
23  {
24  return parent::model($className);
25  }
26 
30  public function tableName()
31  {
32  return Yii::app()->getModule('user')->tableProfiles;
33  }
34 
38  public function rules()
39  {
40  if (!$this->_rules) {
41  $required = array();
42  $numerical = array();
43  $float = array();
44  $decimal = array();
45  $rules = array();
46 
47  $model = $this->getFields();
48 
49  foreach ($model as $field) {
50  $field_rule = array();
51  if ($field->required == ProfileField::REQUIRED_YES_NOT_SHOW_REG || $field->required == ProfileField::REQUIRED_YES_SHOW_REG) {
52  array_push($required, $field->varname);
53  }
54  if ($field->field_type == 'FLOAT') {
55  array_push($float, $field->varname);
56  }
57  if ($field->field_type == 'DECIMAL') {
58  array_push($decimal, $field->varname);
59  }
60  if ($field->field_type == 'INTEGER') {
61  array_push($numerical, $field->varname);
62  }
63  if ($field->field_type == 'VARCHAR' || $field->field_type == 'TEXT') {
64  $field_rule = array($field->varname, 'length', 'max' => $field->field_size, 'min' => $field->field_size_min);
65  if ($field->error_message) {
66  $field_rule['message'] = UserModule::t($field->error_message);
67  }
68  array_push($rules, $field_rule);
69  }
70  if ($field->other_validator) {
71  if (strpos($field->other_validator, '{') === 0) {
72  $validator = (array) CJavaScript::jsonDecode($field->other_validator);
73  foreach ($validator as $name => $val) {
74  $field_rule = array($field->varname, $name);
75  $field_rule = array_merge($field_rule, (array) $validator[$name]);
76  if ($field->error_message) {
77  $field_rule['message'] = UserModule::t($field->error_message);
78  }
79  array_push($rules, $field_rule);
80  }
81  } else {
82  $field_rule = array($field->varname, $field->other_validator);
83  if ($field->error_message) {
84  $field_rule['message'] = UserModule::t($field->error_message);
85  }
86  array_push($rules, $field_rule);
87  }
88  } elseif ($field->field_type == 'DATE') {
89  $field_rule = array($field->varname, 'type', 'type' => 'date', 'dateFormat' => 'yyyy-mm-dd', 'allowEmpty' => true);
90  if ($field->error_message) {
91  $field_rule['message'] = UserModule::t($field->error_message);
92  }
93  array_push($rules, $field_rule);
94  }
95  if ($field->match) {
96  $field_rule = array($field->varname, 'match', 'pattern' => $field->match);
97  if ($field->error_message) {
98  $field_rule['message'] = UserModule::t($field->error_message);
99  }
100  array_push($rules, $field_rule);
101  }
102  if ($field->range) {
103  $field_rule = array($field->varname, 'in', 'range' => self::rangeRules($field->range));
104  if ($field->error_message) {
105  $field_rule['message'] = UserModule::t($field->error_message);
106  }
107  array_push($rules, $field_rule);
108  }
109  }
110 
111  array_push($rules, array(implode(',', $required), 'required'));
112  array_push($rules, array(implode(',', $numerical), 'numerical', 'integerOnly' => true));
113  array_push($rules, array(implode(',', $float), 'type', 'type' => 'float'));
114  array_push($rules, array(implode(',', $decimal), 'match', 'pattern' => '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'));
115  $this->_rules = $rules;
116  }
117 
118  return $this->_rules;
119  }
120 
124  public function relations()
125  {
126  // NOTE: you may need to adjust the relation name and the related
127  // class name for the relations automatically generated below.
128  $relations = array(
129  'user' => array(self::HAS_ONE, 'User', 'id'),
130  );
131  if (isset(Yii::app()->getModule('user')->profileRelations)) {
132  $relations = array_merge($relations, Yii::app()->getModule('user')->profileRelations);
133  }
134 
135  return $relations;
136  }
137 
141  public function attributeLabels()
142  {
143  $labels = array(
144  'user_id' => UserModule::t('User ID'),
145  );
146  $model = $this->getFields();
147 
148  foreach ($model as $field) {
149  $labels[$field->varname] = ((Yii::app()->getModule('user')->fieldsMessage) ? UserModule::t($field->title, array(), Yii::app()->getModule('user')->fieldsMessage) : UserModule::t($field->title));
150  }
151 
152  return $labels;
153  }
154 
155  private function rangeRules($str)
156  {
157  $rules = explode(';', $str);
158  for ($i = 0;$i<count($rules);$i++) {
159  $rules[$i] = current(explode("==", $rules[$i]));
160  }
161 
162  return $rules;
163  }
164 
165  public static function range($str, $fieldValue = null)
166  {
167  $rules = explode(';', $str);
168  $array = array();
169  for ($i = 0;$i<count($rules);$i++) {
170  $item = explode("==", $rules[$i]);
171  if (isset($item[0])) {
172  $array[$item[0]] = ((isset($item[1])) ? $item[1] : $item[0]);
173  }
174  }
175  if (isset($fieldValue)) {
176  if (isset($array[$fieldValue])) {
177  return $array[$fieldValue];
178  } else {
179  return '';
180  }
181  } else {
182  return $array;
183  }
184  }
185 
186  public function widgetAttributes()
187  {
188  $data = array();
189  $model = $this->getFields();
190 
191  foreach ($model as $field) {
192  if ($field->widget) {
193  $data[$field->varname] = $field->widget;
194  }
195  }
196 
197  return $data;
198  }
199 
200  public function widgetParams($fieldName)
201  {
202  $data = array();
203  $model = $this->getFields();
204 
205  foreach ($model as $field) {
206  if ($field->widget) {
207  $data[$field->varname] = $field->widgetparams;
208  }
209  }
210 
211  return $data[$fieldName];
212  }
213 
214  public function getFields()
215  {
216  if ($this->regMode) {
217  if (!$this->_modelReg) {
218  $this->_modelReg = ProfileField::model()->forRegistration()->findAll();
219  }
220 
221  return $this->_modelReg;
222  } else {
223  if (!$this->_model) {
224  $this->_model = ProfileField::model()->forOwner()->findAll();
225  }
226 
227  return $this->_model;
228  }
229  }
230 }