HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
UWjuiAutoComplete.php
Go to the documentation of this file.
1 <?php
2 
4 {
5  public $params = array(
6  'ui-theme' => 'base',
7  'modelName' => '',
8  'optionName' => '',
9  'emptyFieldLabel' => 'Not found',
10  'emptyFieldValue' => 0,
11  'relationName' => '',
12  'minLength' => '',
13  );
14 
20  public function init()
21  {
22  return array(
23  'name' => __CLASS__,
24  'label' => UserModule::t('jQueryUI autocomplete', array(), __CLASS__),
25  'fieldType' => array('INTEGER'),
26  'params' => $this->params,
27  'paramsLabels' => array(
28  'modelName' => UserModule::t('Model Name', array(), __CLASS__),
29  'optionName' => UserModule::t('Lable field name', array(), __CLASS__),
30  'emptyFieldLabel' => UserModule::t('Empty item name', array(), __CLASS__),
31  'emptyFieldValue' => UserModule::t('Empty item value', array(), __CLASS__),
32  'relationName' => UserModule::t('Profile model relation name', array(), __CLASS__),
33  'minLength' => UserModule::t('minimal start research length', array(), __CLASS__),
34  ),
35  );
36  }
37 
45  public function setAttributes($value, $model, $field_varname)
46  {
47  return $value;
48  }
49 
56  public function viewAttribute($model, $field)
57  {
58  $relation = $model->relations();
59  if ($this->params['relationName'] && isset($relation[$this->params['relationName']])) {
60  $m = $model->__get($this->params['relationName']);
61  } else {
62  $m = CActiveRecord::model($this->params['modelName'])->findByPk($model->getAttribute($field->varname));
63  }
64 
65  if ($m) {
66  return (($this->params['optionName']) ? $m->getAttribute($this->params['optionName']) : $m->id);
67  } else {
68  return $this->params['emptyFieldLabel'];
69  }
70  }
71 
79  public function editAttribute($model, $field, $htmlOptions = array())
80  {
81  $list = array();
82  if (isset($this->params['emptyFieldValue'])) {
83  $list[] = array('id' => $this->params['emptyFieldValue'],'label' => $this->params['emptyFieldLabel']);
84  }
85  $models = CActiveRecord::model($this->params['modelName'])->findAll();
86  foreach ($models as $m) {
87  $list[] = (($this->params['optionName']) ? array('label' => $m->getAttribute($this->params['optionName']), 'id' => $m->id) : array('label' => $m->id, 'id' => $m->id));
88  }
89 
90  if (!isset($htmlOptions['id'])) {
91  $htmlOptions['id'] = $field->varname;
92  }
93  $id = $htmlOptions['id'];
94 
95  $relation = $model->relations();
96  if ($this->params['relationName'] && isset($relation[$this->params['relationName']])) {
97  $m = $model->__get($this->params['relationName']);
98  } else {
99  $m = CActiveRecord::model($this->params['modelName'])->findByPk($model->getAttribute($field->varname));
100  }
101 
102  if ($m) {
103  $default_value = (($this->params['optionName']) ? $m->getAttribute($this->params['optionName']) : $m->id);
104  } else {
105  $default_value = '';
106  }
107 
108  $htmlOptions['value'] = $default_value;
109  $options['source'] = $list;
110  $options['minLength'] = $this->params['minLength'];
111  $options['showAnim'] = 'fold';
112  $options['select'] = "js:function(event, ui) { $('#".get_class($model)."_".$field->varname."').val(ui.item.id);}";
113  $options = CJavaScript::encode($options);
114  //$basePath=Yii::getPathOfAlias('application.views.asset');
115  $basePath = Yii::getPathOfAlias('application.modules.user.views.asset');
116  $baseUrl = Yii::app()->getAssetManager()->publish($basePath);
117  $cs = Yii::app()->getClientScript();
118  $cs->registerCssFile($baseUrl.'/css/'.$this->params['ui-theme'].'/jquery-ui.css');
119  $cs->registerScriptFile($baseUrl.'/js/jquery-ui.min.js');
120  $js = "jQuery('#{$id}').autocomplete({$options});";
121  $cs->registerScript('Autocomplete'.'#'.$id, $js);
122 
123  return CHtml::activeTextField($model, $field->varname, $htmlOptions).CHtml::activehiddenField($model, $field->varname);
124  }
125 }