HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
ProfileField.php
Go to the documentation of this file.
1 <?php
2 
3 class ProfileField extends CActiveRecord
4 {
5  const VISIBLE_ALL = 3;
7  const VISIBLE_ONLY_OWNER = 1;
8  const VISIBLE_NO = 0;
9 
10  const REQUIRED_NO = 0;
14 
39  public static function model($className = __CLASS__)
40  {
41  return parent::model($className);
42  }
43 
47  public function tableName()
48  {
49  return Yii::app()->getModule('user')->tableProfileFields;
50  }
51 
55  public function rules()
56  {
57  // NOTE: you should only define rules for those attributes that
58  // will receive user inputs.
59  return array(
60  array('varname, title, field_type', 'required'),
61  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.")),
62  array('varname', 'unique', 'message' => UserModule::t("This field already exists.")),
63  array('varname, field_type', 'length', 'max' => 50),
64  array('field_size_min, required, position, visible', 'numerical', 'integerOnly' => true),
65  array('field_size', 'match', 'pattern' => '/^\s*[-+]?[0-9]*\,*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'),
66  array('title, match, error_message, other_validator, default, widget', 'length', 'max' => 255),
67  array('range, widgetparams', 'length', 'max' => 5000),
68  array('id, varname, title, field_type, field_size, field_size_min, required, match, range, error_message, other_validator, default, widget, widgetparams, position, visible', 'safe', 'on' => 'search'),
69  );
70  }
71 
75  public function relations()
76  {
77  // NOTE: you may need to adjust the relation name and the related
78  // class name for the relations automatically generated below.
79  return array(
80  );
81  }
82 
86  public function attributeLabels()
87  {
88  return array(
89  'id' => UserModule::t('Id'),
90  'varname' => UserModule::t('Variable name'),
91  'title' => UserModule::t('Title'),
92  'field_type' => UserModule::t('Field Type'),
93  'field_size' => UserModule::t('Field Size'),
94  'field_size_min' => UserModule::t('Field Size min'),
95  'required' => UserModule::t('Required'),
96  'match' => UserModule::t('Match'),
97  'range' => UserModule::t('Range'),
98  'error_message' => UserModule::t('Error Message'),
99  'other_validator' => UserModule::t('Other Validator'),
100  'default' => UserModule::t('Default'),
101  'widget' => UserModule::t('Widget'),
102  'widgetparams' => UserModule::t('Widget parametrs'),
103  'position' => UserModule::t('Position'),
104  'visible' => UserModule::t('Visible'),
105  );
106  }
107 
108  public function scopes()
109  {
110  return array(
111  'forAll' => array(
112  'condition' => 'visible='.self::VISIBLE_ALL,
113  'order' => 'position',
114  ),
115  'forUser' => array(
116  'condition' => 'visible>='.self::VISIBLE_REGISTER_USER,
117  'order' => 'position',
118  ),
119  'forOwner' => array(
120  'condition' => 'visible>='.self::VISIBLE_ONLY_OWNER,
121  'order' => 'position',
122  ),
123  'forRegistration' => array(
124  'condition' => 'required='.self::REQUIRED_NO_SHOW_REG.' OR required='.self::REQUIRED_YES_SHOW_REG,
125  'order' => 'position',
126  ),
127  'sort' => array(
128  'order' => 'position',
129  ),
130  );
131  }
132 
138  public function widgetView($model)
139  {
140  if ($this->widget && class_exists($this->widget)) {
141  $widgetClass = new $this->widget();
142 
143  $arr = $this->widgetparams;
144  if ($arr) {
145  $newParams = $widgetClass->params;
146  $arr = (array) CJavaScript::jsonDecode($arr);
147  foreach ($arr as $p => $v) {
148  if (isset($newParams[$p])) {
149  $newParams[$p] = $v;
150  }
151  }
152  $widgetClass->params = $newParams;
153  }
154 
155  if (method_exists($widgetClass, 'viewAttribute')) {
156  return $widgetClass->viewAttribute($model, $this);
157  }
158  }
159 
160  return false;
161  }
162 
163  public function widgetEdit($model, $params = array())
164  {
165  if ($this->widget && class_exists($this->widget)) {
166  $widgetClass = new $this->widget();
167 
168  $arr = $this->widgetparams;
169  if ($arr) {
170  $newParams = $widgetClass->params;
171  $arr = (array) CJavaScript::jsonDecode($arr);
172  foreach ($arr as $p => $v) {
173  if (isset($newParams[$p])) {
174  $newParams[$p] = $v;
175  }
176  }
177  $widgetClass->params = $newParams;
178  }
179 
180  if (method_exists($widgetClass, 'editAttribute')) {
181  return $widgetClass->editAttribute($model, $this, $params);
182  }
183  }
184 
185  return false;
186  }
187 
188  public static function itemAlias($type, $code = null)
189  {
190  $_items = array(
191  'field_type' => array(
192  'INTEGER' => UserModule::t('INTEGER'),
193  'VARCHAR' => UserModule::t('VARCHAR'),
194  'TEXT' => UserModule::t('TEXT'),
195  'DATE' => UserModule::t('DATE'),
196  'FLOAT' => UserModule::t('FLOAT'),
197  'DECIMAL' => UserModule::t('DECIMAL'),
198  'BOOL' => UserModule::t('BOOL'),
199  'BLOB' => UserModule::t('BLOB'),
200  'BINARY' => UserModule::t('BINARY'),
201  ),
202  'required' => array(
203  self::REQUIRED_NO => UserModule::t('No'),
204  self::REQUIRED_NO_SHOW_REG => UserModule::t('No, but show on registration form'),
205  self::REQUIRED_YES_SHOW_REG => UserModule::t('Yes and show on registration form'),
206  self::REQUIRED_YES_NOT_SHOW_REG => UserModule::t('Yes'),
207  ),
208  'visible' => array(
209  self::VISIBLE_ALL => UserModule::t('For all'),
210  self::VISIBLE_REGISTER_USER => UserModule::t('Registered users'),
211  self::VISIBLE_ONLY_OWNER => UserModule::t('Only owner'),
212  self::VISIBLE_NO => UserModule::t('Hidden'),
213  ),
214  );
215  if (isset($code)) {
216  return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
217  } else {
218  return isset($_items[$type]) ? $_items[$type] : false;
219  }
220  }
221 
227  public function search()
228  {
229  // Warning: Please modify the following code to remove attributes that
230  // should not be searched.
231 
232  $criteria = new CDbCriteria();
233 
234  $criteria->compare('id', $this->id);
235  $criteria->compare('varname', $this->varname, true);
236  $criteria->compare('title', $this->title, true);
237  $criteria->compare('field_type', $this->field_type, true);
238  $criteria->compare('field_size', $this->field_size);
239  $criteria->compare('field_size_min', $this->field_size_min);
240  $criteria->compare('required', $this->required);
241  $criteria->compare('match', $this->match, true);
242  $criteria->compare('range', $this->range, true);
243  $criteria->compare('error_message', $this->error_message, true);
244  $criteria->compare('other_validator', $this->other_validator, true);
245  $criteria->compare('default', $this->default, true);
246  $criteria->compare('widget', $this->widget, true);
247  $criteria->compare('widgetparams', $this->widgetparams, true);
248  $criteria->compare('position', $this->position);
249  $criteria->compare('visible', $this->visible);
250 
251  return new CActiveDataProvider(get_class($this), array(
252  'criteria' => $criteria,
253  'pagination' => array(
254  'pageSize' => Yii::app()->controller->module->fields_page_size,
255  ),
256  'sort' => array(
257  'defaultOrder' => 'position',
258  ),
259  ));
260  }
261 }