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