HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
UWrelBelongsTo.php
Go to the documentation of this file.
1 <?php
2 
4 {
5  public $params = array(
6  'modelName' => '',
7  'optionName' => '',
8  'emptyField' => '',
9  'relationName' => '',
10  );
11 
17  public function init()
18  {
19  return array(
20  'name' => __CLASS__,
21  'label' => UserModule::t('Relation Belongs To', array(), __CLASS__),
22  'fieldType' => array('INTEGER'),
23  'params' => $this->params,
24  'paramsLabels' => array(
25  'modelName' => UserModule::t('Model Name', array(), __CLASS__),
26  'optionName' => UserModule::t('Lable field name', array(), __CLASS__),
27  'emptyField' => UserModule::t('Empty item name', array(), __CLASS__),
28  'relationName' => UserModule::t('Profile model relation name', array(), __CLASS__),
29  ),
30  );
31  }
32 
40  public function setAttributes($value, $model, $field_varname)
41  {
42  return $value;
43  }
44 
51  public function viewAttribute($model, $field)
52  {
53  $relation = $model->relations();
54  if ($this->params['relationName'] && isset($relation[$this->params['relationName']])) {
55  $m = $model->__get($this->params['relationName']);
56  } else {
57  $m = CActiveRecord::model($this->params['modelName'])->findByPk($model->getAttribute($field->varname));
58  }
59 
60  if ($m) {
61  return (($this->params['optionName']) ? $m->getAttribute($this->params['optionName']) : $m->id);
62  } else {
63  return $this->params['emptyField'];
64  }
65  }
66 
74  public function editAttribute($model, $field, $htmlOptions = array())
75  {
76  $list = array();
77  if ($this->params['emptyField']) {
78  $list[0] = $this->params['emptyField'];
79  }
80 
81  $models = CActiveRecord::model($this->params['modelName'])->findAll();
82  foreach ($models as $m) {
83  $list[$m->id] = (($this->params['optionName']) ? $m->getAttribute($this->params['optionName']) : $m->id);
84  }
85 
86  return CHtml::activeDropDownList($model, $field->varname, $list, $htmlOptions = array());
87  }
88 }