Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
UWrelBelongsTo.php
1 <?php
2 
4 
5  /**
6  * @var array
7  */
8  public $params = array(
9  'modelName' => '',
10  'optionName' => '',
11  'emptyField' => '',
12  'relationName' => '',
13  );
14 
15  /**
16  * Widget initialization
17  *
18  * @return array
19  */
20  public function init()
21  {
22  return array(
23  'name' => __CLASS__,
24  'label' => UserModule::t('Relation Belongs To', 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  'emptyField' => UserModule::t('Empty item name', array(), __CLASS__),
31  'relationName' => UserModule::t('Profile model relation name', array(), __CLASS__),
32  ),
33  );
34  }
35 
36  /**
37  * Allows to set attributes
38  *
39  * @param $value
40  * @param $model
41  * @param $field_varname
42  *
43  * @return string
44  */
45  public function setAttributes($value, $model, $field_varname)
46  {
47  return $value;
48  }
49 
50  /**
51  * Returns attribute
52  *
53  * @param Profile $model profile model
54  * @param string $field profile fields model item
55  *
56  * @return string
57  */
58  public function viewAttribute($model, $field)
59  {
60  $relation = $model->relations();
61  if ($this->params['relationName'] && isset($relation[$this->params['relationName']])) {
62  $m = $model->__get($this->params['relationName']);
63  } else {
64  $m = CActiveRecord::model($this->params['modelName'])->findByPk($model->getAttribute($field->varname));
65  }
66 
67  if ($m) {
68  return (($this->params['optionName']) ? $m->getAttribute($this->params['optionName']) : $m->id);
69  } else {
70  return $this->params['emptyField'];
71  }
72  }
73 
74  /**
75  * @param $model - profile model
76  * @param $field - profile fields model item
77  * @param $params - htmlOptions
78  *
79  * @return string
80  */
81  public function editAttribute($model, $field, $htmlOptions = array())
82  {
83  $list = array();
84  if ($this->params['emptyField']) {
85  $list[0] = $this->params['emptyField'];
86  }
87 
88  $models = CActiveRecord::model($this->params['modelName'])->findAll();
89  foreach ($models as $m) {
90  $list[$m->id] = (($this->params['optionName']) ? $m->getAttribute($this->params['optionName']) : $m->id);
91  }
92  return CHtml::activeDropDownList($model, $field->varname, $list, $htmlOptions = array());
93  }
94 }