Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Profile.php
1 <?php
2 
3 class Profile extends CActiveRecord
4 {
5  /**
6  * The followings are the available columns in table 'profiles':
7  * @var integer $user_id
8  * @var boolean $regMode
9  */
10  public $regMode = false;
11 
12  private $_model;
13  private $_modelReg;
14 
15  /**
16  * Returns the static model of the specified AR class.
17  * @return CActiveRecord the static model class
18  */
19  public static function model($className = __CLASS__)
20  {
21  return parent::model($className);
22  }
23 
24  /**
25  * @return string the associated database table name
26  */
27  public function tableName()
28  {
29  return Yii::app()->getModule('user')->tableProfiles;
30  }
31 
32  /**
33  * @return array validation rules for model attributes.
34  */
35  public function rules()
36  {
37  // NOTE: you should only define rules for those attributes that
38  // will receive user inputs.
39  $required = array();
40  $numerical = array();
41  $rules = array();
42 
43  $model = $this->getFields();
44 
45  foreach ($model as $field) {
46  $field_rule = array();
47  if ($field->required == ProfileField::REQUIRED_YES_NOT_SHOW_REG || $field->required == ProfileField::REQUIRED_YES_SHOW_REG)
48  array_push($required, $field->varname);
49  if ($field->field_type == 'FLOAT' || $field->field_type == 'INTEGER')
50  array_push($numerical, $field->varname);
51  if ($field->field_type == 'VARCHAR' || $field->field_type == 'TEXT') {
52  array_push($rules, array($field->varname, 'common.components.NotContainsTagsValidator'));
53  $field_rule = array($field->varname, 'length', 'max' => $field->field_size, 'min' => $field->field_size_min);
54  if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
55  array_push($rules, $field_rule);
56  }
57  if ($field->other_validator) {
58  if (strpos($field->other_validator, '{') === 0) {
59  $validator = (array)CJavaScript::jsonDecode($field->other_validator);
60  foreach ($validator as $name => $val) {
61  $field_rule = array($field->varname, $name);
62  $field_rule = array_merge($field_rule, (array)$validator[$name]);
63  if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
64  array_push($rules, $field_rule);
65  }
66  } else {
67  $field_rule = array($field->varname, $field->other_validator);
68  if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
69  array_push($rules, $field_rule);
70  }
71  } elseif ($field->field_type == 'DATE') {
72  $field_rule = array($field->varname, 'type', 'type' => 'date', 'dateFormat' => 'yyyy-mm-dd', 'allowEmpty' => true);
73  if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
74  array_push($rules, $field_rule);
75 
76  $field_rule = array($field->varname, 'castToNull');
77  array_push($rules, $field_rule);
78  }
79  if ($field->match) {
80  $field_rule = array($field->varname, 'match', 'pattern' => $field->match);
81  if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
82  array_push($rules, $field_rule);
83  }
84 
85 
86  if ($field->range) {
87  if ($field->required == ProfileField::REQUIRED_YES_NOT_SHOW_REG || $field->required == ProfileField::REQUIRED_YES_SHOW_REG) {
88  $field_rule = array($field->varname, 'in', 'range' => self::rangeRules($field->range));
89  } else {
90  $field_rule = array($field->varname, 'in', 'range' => self::rangeRules($field->range), 'allowEmpty' => true);
91  }
92  if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
93  array_push($rules, $field_rule);
94  }
95  }
96 
97  array_push($rules, array(implode(',', $required), 'required'));
98  array_push($rules, array(implode(',', $numerical), 'numerical', 'integerOnly' => true));
99  return $rules;
100  }
101 
102  public function castToNull($attribute)
103  {
104  if (empty($this->$attribute)) {
105  $this->$attribute = null;
106  }
107  }
108 
109  /**
110  * @return array relational rules.
111  */
112  public function relations()
113  {
114  // NOTE: you may need to adjust the relation name and the related
115  // class name for the relations automatically generated below.
116  $relations = array(
117  'user' => array(self::HAS_ONE, 'User', 'id'),
118  );
119  if (isset(Yii::app()->getModule('user')->profileRelations)) $relations = array_merge($relations, Yii::app()->getModule('user')->profileRelations);
120  return $relations;
121  }
122 
123  /**
124  * @return array customized attribute labels (name=>label)
125  */
126  public function attributeLabels()
127  {
128  $labels = array(
129  'user_id' => UserModule::t('User ID'),
130  );
131  $model = $this->getFields();
132 
133  foreach ($model as $field)
134  $labels[$field->varname] = ((Yii::app()->getModule('user')->fieldsMessage) ? UserModule::t($field->title, array(), Yii::app()->getModule('user')->fieldsMessage) : UserModule::t($field->title));
135 
136  return $labels;
137  }
138 
139  private function rangeRules($str)
140  {
141  $pos = strpos($str, '-');
142  if ($pos !== false) {
143  $str = substr($str, $pos + 1);
144  }
145 
146  $rules = explode(';', $str);
147  for ($i = 0; $i < count($rules); $i++) {
148  $rules[$i] = current(explode("==", $rules[$i]));
149 
150  }
151  return $rules;
152  }
153 
154  static public function range($str, $fieldValue = NULL)
155  {
156  $rules = explode(';', $str);
157  $array = array();
158  for ($i = 0; $i < count($rules); $i++) {
159  $item = explode("==", $rules[$i]);
160  if (isset($item[0]))
161  $array[$item[0]] = isset($item[1]) ? UserModule::t($item[1]) : UserModule::t($item[0]);
162  }
163  if (isset($fieldValue))
164  if (isset($array[$fieldValue])) return $array[$fieldValue]; else return '';
165  else
166  return $array;
167  }
168 
169  public function widgetAttributes()
170  {
171  $data = array();
172  $model = $this->getFields();
173 
174  foreach ($model as $field) {
175  if ($field->widget) $data[$field->varname] = $field->widget;
176  }
177  return $data;
178  }
179 
180  public function widgetParams($fieldName)
181  {
182  $data = array();
183  $model = $this->getFields();
184 
185  foreach ($model as $field) {
186  if ($field->widget) $data[$field->varname] = $field->widgetparams;
187  }
188  return $data[$fieldName];
189  }
190 
191  public function getFields()
192  {
193  if ($this->regMode) {
194  if (!$this->_modelReg)
195  $this->_modelReg = ProfileField::model()->forRegistration()->findAll();
196  return $this->_modelReg;
197  } else {
198  if (!$this->_model)
199  $this->_model = ProfileField::model()->forOwner()->findAll();
200  return $this->_model;
201  }
202  }
203 
204  /**
205  * Extends setAttributes to handle active date fields
206  *
207  * @param $values array
208  * @param $safeOnly boolean
209  */
210  public function setAttributes($values, $safeOnly = true)
211  {
212  foreach ($this->widgetAttributes() as $fieldName => $className) {
213  if (isset($values[$fieldName]) && class_exists($className)) {
214  $class = new $className;
215  $arr = $this->widgetParams($fieldName);
216  if ($arr) {
217  $newParams = $class->params;
218  $arr = (array)CJavaScript::jsonDecode($arr);
219  foreach ($arr as $p => $v) {
220  if (isset($newParams[$p])) $newParams[$p] = $v;
221  }
222  $class->params = $newParams;
223  }
224  if (method_exists($class, 'setAttributes')) {
225  $values[$fieldName] = $class->setAttributes($values[$fieldName], $this, $fieldName);
226  }
227  }
228  }
229  parent::setAttributes($values, $safeOnly);
230  }
231 
232  public function behaviors()
233  {
234  return Yii::app()->getModule('user')->getBehaviorsFor(get_class($this));
235  }
236  /*public function beforeValidate() {
237  var_dump($this->chec);
238  exit;
239  }*/
240 }