Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ProfileField.php
1 <?php
2 
3 class ProfileField extends CActiveRecord
4 {
5  const VISIBLE_ALL = 3;
6  const VISIBLE_REGISTER_USER = 2;
7  const VISIBLE_ONLY_OWNER = 1;
8  const VISIBLE_NO = 0;
9 
10  const REQUIRED_NO = 0;
11  const REQUIRED_YES_SHOW_REG = 1;
12  const REQUIRED_NO_SHOW_REG = 2;
13  const REQUIRED_YES_NOT_SHOW_REG = 3;
14 
15  /**
16  * The followings are the available columns in table 'profiles_fields':
17  * @var integer $id
18  * @var string $varname
19  * @var string $title
20  * @var string $field_type
21  * @var integer $field_size
22  * @var integer $field_size_mix
23  * @var integer $required
24  * @var integer $match
25  * @var string $range
26  * @var string $error_message
27  * @var string $other_validator
28  * @var string $default
29  * @var integer $position
30  * @var integer $visible
31  */
32 
33  /**
34  * Returns the static model of the specified AR class.
35  * @return CActiveRecord the static model class
36  */
37  public static function model($className = __CLASS__)
38  {
39  return parent::model($className);
40  }
41 
42  /**
43  * @return string the associated database table name
44  */
45  public function tableName()
46  {
47  return Yii::app()->getModule('user')->tableProfileFields;
48  }
49 
50  /**
51  * @return array validation rules for model attributes.
52  */
53  public function rules()
54  {
55  // NOTE: you should only define rules for those attributes that
56  // will receive user inputs.
57  return array(
58  array('varname, title, field_type', 'required'),
59  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.")),
60  array('varname', 'unique', 'message' => UserModule::t("This field already exists.")),
61  array('varname, widget', 'length', 'max' => 50),
62  array('field_type', 'length', 'max' => 50),
63  array('field_size, field_size_min, required, position, visible', 'numerical', 'integerOnly' => true),
64  array('title, match, error_message, other_validator, default, widget', 'length', 'max' => 255),
65  array('range, widgetparams', 'length', 'max' => 5000),
66  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'),
67  );
68  }
69 
70  /**
71  * @return array relational rules.
72  */
73  public function relations()
74  {
75  // NOTE: you may need to adjust the relation name and the related
76  // class name for the relations automatically generated below.
77  return array(
78  );
79  }
80 
81  /**
82  * @return array customized attribute labels (name=>label)
83  */
84  public function attributeLabels()
85  {
86  return array(
87  'id' => UserModule::t('Id'),
88  'varname' => UserModule::t('Variable name'),
89  'title' => UserModule::t('Title'),
90  'field_type' => UserModule::t('Field Type'),
91  'field_size' => UserModule::t('Field Size'),
92  'field_size_min' => UserModule::t('Field Size min'),
93  'required' => UserModule::t('Required'),
94  'match' => UserModule::t('Match'),
95  'range' => UserModule::t('Range'),
96  'error_message' => UserModule::t('Error Message'),
97  'other_validator' => UserModule::t('Other Validator'),
98  'default' => UserModule::t('Default'),
99  'widget' => UserModule::t('Widget'),
100  'widgetparams' => UserModule::t('Widget parametrs'),
101  'position' => UserModule::t('Position'),
102  'visible' => UserModule::t('Visible'),
103  );
104  }
105 
106  public function scopes()
107  {
108  return array(
109  'forAll' => array(
110  'condition' => 'visible=' . self::VISIBLE_ALL,
111  'order' => 'position',
112  ),
113  'forUser' => array(
114  'condition' => 'visible>=' . self::VISIBLE_REGISTER_USER,
115  'order' => 'position',
116  ),
117  'forOwner' => array(
118  'condition' => 'visible>=' . self::VISIBLE_ONLY_OWNER,
119  'order' => 'position',
120  ),
121  'forRegistration' => array(
122  'condition' => 'required=' . self::REQUIRED_NO_SHOW_REG . ' OR required=' . self::REQUIRED_YES_SHOW_REG,
123  'order' => 'position',
124  ),
125  'sort' => array(
126  'order' => 'position',
127  ),
128  );
129  }
130 
131  /**
132  * @param $value
133  * @return formated value (string)
134  */
135  public function widgetView($model)
136  {
137  if ($this->widget && class_exists($this->widget)) {
138  $widgetClass = new $this->widget;
139 
140  $arr = $this->widgetparams;
141  if ($arr) {
142  $newParams = $widgetClass->params;
143  $arr = (array)CJavaScript::jsonDecode($arr);
144  foreach ($arr as $p => $v) {
145  if (isset($newParams[$p])) $newParams[$p] = $v;
146  }
147  $widgetClass->params = $newParams;
148  }
149 
150  if (method_exists($widgetClass, 'viewAttribute')) {
151  return $widgetClass->viewAttribute($model, $this);
152  }
153  }
154  return false;
155  }
156 
157  public function widgetEdit($model, $params = array())
158  {
159  if ($this->widget && class_exists($this->widget)) {
160  $widgetClass = new $this->widget;
161 
162  $arr = $this->widgetparams;
163  if ($arr) {
164  $newParams = $widgetClass->params;
165  $arr = (array)CJavaScript::jsonDecode($arr);
166  foreach ($arr as $p => $v) {
167  if (isset($newParams[$p])) $newParams[$p] = $v;
168  }
169  $widgetClass->params = $newParams;
170  }
171 
172  if (method_exists($widgetClass, 'editAttribute')) {
173  return $widgetClass->editAttribute($model, $this, $params);
174  }
175  }
176  return false;
177  }
178 
179  public static function itemAlias($type, $code = NULL)
180  {
181  $_items = array(
182  'field_type' => array(
183  'INTEGER' => UserModule::t('INTEGER'),
184  'VARCHAR' => UserModule::t('VARCHAR'),
185  'TEXT' => UserModule::t('TEXT'),
186  'DATE' => UserModule::t('DATE'),
187  'FLOAT' => UserModule::t('FLOAT'),
188  'BOOL' => UserModule::t('BOOL'),
189  'BLOB' => UserModule::t('BLOB'),
190  'BINARY' => UserModule::t('BINARY'),
191  ),
192  'required' => array(
193  self::REQUIRED_NO => UserModule::t('No'),
194  self::REQUIRED_NO_SHOW_REG => UserModule::t('No, but show on registration form'),
195  self::REQUIRED_YES_SHOW_REG => UserModule::t('Yes and show on registration form'),
196  self::REQUIRED_YES_NOT_SHOW_REG => UserModule::t('Yes'),
197  ),
198  'visible' => array(
199  self::VISIBLE_ALL => UserModule::t('For all'),
200  self::VISIBLE_REGISTER_USER => UserModule::t('Registered users'),
201  self::VISIBLE_ONLY_OWNER => UserModule::t('Only owner'),
202  self::VISIBLE_NO => UserModule::t('Hidden'),
203  ),
204  );
205  if (isset($code))
206  return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
207  else
208  return isset($_items[$type]) ? $_items[$type] : false;
209  }
210 
211  /**
212  * Retrieves a list of models based on the current search/filter conditions.
213  * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
214  */
215  public function search()
216  {
217  // Warning: Please modify the following code to remove attributes that
218  // should not be searched.
219 
220  $criteria = new CDbCriteria;
221 
222  $criteria->compare('id', $this->id);
223  $criteria->compare('varname', $this->varname, true);
224  $criteria->compare('title', $this->title, true);
225  $criteria->compare('field_type', $this->field_type, true);
226  $criteria->compare('field_size', $this->field_size);
227  $criteria->compare('field_size_min', $this->field_size_min);
228  $criteria->compare('required', $this->required);
229  $criteria->compare('match', $this->match, true);
230  $criteria->compare('range', $this->range, true);
231  $criteria->compare('error_message', $this->error_message, true);
232  $criteria->compare('other_validator', $this->other_validator, true);
233  $criteria->compare('default', $this->default, true);
234  $criteria->compare('widget', $this->widget, true);
235  $criteria->compare('widgetparams', $this->widgetparams, true);
236  $criteria->compare('position', $this->position);
237  $criteria->compare('visible', $this->visible);
238 
239  return new CActiveDataProvider($this, array(
240  'criteria' => $criteria,
241  'pagination' => array(
242  'pageSize' => Yii::app()->getModule('user')->fields_page_size,
243  ),
244  'sort' => array(
245  'defaultOrder' => 'position',
246  ),
247  ));
248  }
249 
250 }