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 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
32
33 34 35 36
37 public static function model($className=__CLASS__)
38 {
39 return parent::model($className);
40 }
41
42 43 44
45 public function tableName()
46 {
47 return Yii::app()->getModule('user')->tableProfileFields;
48 }
49
50 51 52
53 public function rules()
54 {
55
56
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, field_type', 'length', 'max'=>50),
62 array('field_size_min, required, position, visible', 'numerical', 'integerOnly'=>true),
63 array('field_size', 'match', 'pattern' => '/^\s*[-+]?[0-9]*\,*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'),
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 72
73 public function relations()
74 {
75
76
77 return array(
78 );
79 }
80
81 82 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 133 134
135 public function widgetView($model) {
136 if ($this->widget && class_exists($this->widget)) {
137 $widgetClass = new $this->widget;
138
139 $arr = $this->widgetparams;
140 if ($arr) {
141 $newParams = $widgetClass->params;
142 $arr = (array)CJavaScript::jsonDecode($arr);
143 foreach ($arr as $p=>$v) {
144 if (isset($newParams[$p])) $newParams[$p] = $v;
145 }
146 $widgetClass->params = $newParams;
147 }
148
149 if (method_exists($widgetClass,'viewAttribute')) {
150 return $widgetClass->viewAttribute($model,$this);
151 }
152 }
153 return false;
154 }
155
156 public function widgetEdit($model,$params=array()) {
157 if ($this->widget && class_exists($this->widget)) {
158 $widgetClass = new $this->widget;
159
160 $arr = $this->widgetparams;
161 if ($arr) {
162 $newParams = $widgetClass->params;
163 $arr = (array)CJavaScript::jsonDecode($arr);
164 foreach ($arr as $p=>$v) {
165 if (isset($newParams[$p])) $newParams[$p] = $v;
166 }
167 $widgetClass->params = $newParams;
168 }
169
170 if (method_exists($widgetClass,'editAttribute')) {
171 return $widgetClass->editAttribute($model,$this,$params);
172 }
173 }
174 return false;
175 }
176
177 public static function itemAlias($type,$code=NULL) {
178 $_items = array(
179 'field_type' => array(
180 'INTEGER' => UserModule::t('INTEGER'),
181 'VARCHAR' => UserModule::t('VARCHAR'),
182 'TEXT'=> UserModule::t('TEXT'),
183 'DATE'=> UserModule::t('DATE'),
184 'FLOAT'=> UserModule::t('FLOAT'),
185 'DECIMAL'=> UserModule::t('DECIMAL'),
186 'BOOL'=> UserModule::t('BOOL'),
187 'BLOB'=> UserModule::t('BLOB'),
188 'BINARY'=> UserModule::t('BINARY'),
189 ),
190 'required' => array(
191 self::REQUIRED_NO => UserModule::t('No'),
192 self::REQUIRED_NO_SHOW_REG => UserModule::t('No, but show on registration form'),
193 self::REQUIRED_YES_SHOW_REG => UserModule::t('Yes and show on registration form'),
194 self::REQUIRED_YES_NOT_SHOW_REG => UserModule::t('Yes'),
195 ),
196 'visible' => array(
197 self::VISIBLE_ALL => UserModule::t('For all'),
198 self::VISIBLE_REGISTER_USER => UserModule::t('Registered users'),
199 self::VISIBLE_ONLY_OWNER => UserModule::t('Only owner'),
200 self::VISIBLE_NO => UserModule::t('Hidden'),
201 ),
202 );
203 if (isset($code))
204 return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
205 else
206 return isset($_items[$type]) ? $_items[$type] : false;
207 }
208
209 210 211 212
213 public function search()
214 {
215
216
217
218 $criteria=new CDbCriteria;
219
220 $criteria->compare('id',$this->id);
221 $criteria->compare('varname',$this->varname,true);
222 $criteria->compare('title',$this->title,true);
223 $criteria->compare('field_type',$this->field_type,true);
224 $criteria->compare('field_size',$this->field_size);
225 $criteria->compare('field_size_min',$this->field_size_min);
226 $criteria->compare('required',$this->required);
227 $criteria->compare('match',$this->match,true);
228 $criteria->compare('range',$this->range,true);
229 $criteria->compare('error_message',$this->error_message,true);
230 $criteria->compare('other_validator',$this->other_validator,true);
231 $criteria->compare('default',$this->default,true);
232 $criteria->compare('widget',$this->widget,true);
233 $criteria->compare('widgetparams',$this->widgetparams,true);
234 $criteria->compare('position',$this->position);
235 $criteria->compare('visible',$this->visible);
236
237 return new CActiveDataProvider(get_class($this), array(
238 'criteria'=>$criteria,
239 'pagination'=>array(
240 'pageSize'=>Yii::app()->controller->module->fields_page_size,
241 ),
242 'sort'=>array(
243 'defaultOrder'=>'position',
244 ),
245 ));
246 }
247 }