Total Complexity | 95 |
Total Lines | 397 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ProfileField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProfileField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ProfileField extends XoopsObject |
||
27 | { |
||
28 | public $field_id; |
||
29 | public $cat_id; |
||
30 | public $field_type; |
||
31 | public $field_valuetype; |
||
32 | public $field_name; |
||
33 | public $field_title; |
||
34 | public $field_description; |
||
35 | public $field_required; //0 = no, 1 = yes |
||
36 | public $field_maxlength; |
||
37 | public $field_weight; |
||
38 | public $field_default; |
||
39 | public $field_notnull; |
||
40 | public $field_edit; |
||
41 | public $field_show; |
||
42 | public $field_config; |
||
43 | public $field_options; |
||
44 | public $step_id; |
||
45 | |||
46 | /** |
||
47 | * |
||
48 | */ |
||
49 | public function __construct() |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Extra treatment dealing with non latin encoding |
||
72 | * Tricky solution |
||
73 | * @param string $key |
||
74 | * @param mixed $value |
||
75 | * @param bool $not_gpc |
||
76 | */ |
||
77 | public function setVar($key, $value, $not_gpc = false) |
||
78 | { |
||
79 | if ($key === 'field_options' && \is_array($value)) { |
||
80 | foreach (array_keys($value) as $idx) { |
||
81 | $value[$idx] = base64_encode($value[$idx]); |
||
82 | } |
||
83 | } |
||
84 | parent::setVar($key, $value, $not_gpc); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param string $key |
||
89 | * @param string $format |
||
90 | * |
||
91 | * @return mixed |
||
92 | */ |
||
93 | public function getVar($key, $format = 's') |
||
94 | { |
||
95 | $value = parent::getVar($key, $format); |
||
96 | if ($key === 'field_options' && !empty($value)) { |
||
97 | foreach (array_keys($value) as $idx) { |
||
|
|||
98 | $value[$idx] = base64_decode($value[$idx]); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | return $value; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Returns a {@link XoopsFormElement} for editing the value of this field |
||
107 | * |
||
108 | * @param XoopsUser $user {@link XoopsUser} object to edit the value of |
||
109 | * @param ProfileProfile $profile {@link ProfileProfile} object to edit the value of |
||
110 | * |
||
111 | * @return XoopsFormElement |
||
112 | **/ |
||
113 | public function getEditElement($user, $profile) |
||
114 | { |
||
115 | $value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e'); |
||
116 | |||
117 | $caption = $this->getVar('field_title'); |
||
118 | $caption = defined($caption) ? constant($caption) : $caption; |
||
119 | $name = $this->getVar('field_name', 'e'); |
||
120 | $options = $this->getVar('field_options'); |
||
121 | if (is_array($options)) { |
||
122 | //asort($options); |
||
123 | |||
124 | foreach (array_keys($options) as $key) { |
||
125 | $optval = defined($options[$key]) ? constant($options[$key]) : $options[$key]; |
||
126 | $optkey = defined($key) ? constant($key) : $key; |
||
127 | unset($options[$key]); |
||
128 | $options[$optkey] = $optval; |
||
129 | } |
||
130 | } |
||
131 | include_once $GLOBALS['xoops']->path('class/xoopsformloader.php'); |
||
132 | switch ($this->getVar('field_type')) { |
||
133 | default: |
||
134 | case 'autotext': |
||
135 | //autotext is not for editing |
||
136 | $element = new XoopsFormLabel($caption, $this->getOutputValue($user, $profile)); |
||
137 | break; |
||
138 | |||
139 | case 'textbox': |
||
140 | $element = new XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value); |
||
141 | break; |
||
142 | |||
143 | case 'textarea': |
||
144 | $element = new XoopsFormTextArea($caption, $name, $value, 4, 30); |
||
145 | break; |
||
146 | |||
147 | case 'dhtml': |
||
148 | $element = new XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30); |
||
149 | break; |
||
150 | |||
151 | case 'select': |
||
152 | $element = new XoopsFormSelect($caption, $name, $value); |
||
153 | // If options do not include an empty element, then add a blank option to prevent any default selection |
||
154 | // if (!in_array('', array_keys($options))) { |
||
155 | if (!array_key_exists('', $options)) { |
||
156 | $element->addOption('', _NONE); |
||
157 | |||
158 | $eltmsg = empty($caption) ? sprintf(_FORM_ENTER, $name) : sprintf(_FORM_ENTER, $caption); |
||
159 | $eltmsg = str_replace('"', '\"', stripslashes($eltmsg)); |
||
160 | $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};" . "for (i = 0; i < selectBox.options.length; i++) { if (selectBox.options[i].selected == true && selectBox.options[i].value != '') { hasSelected = true; break; } }" . "if (!hasSelected) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }"; |
||
161 | } |
||
162 | $element->addOptionArray($options); |
||
163 | break; |
||
164 | |||
165 | case 'select_multi': |
||
166 | $element = new XoopsFormSelect($caption, $name, $value, 5, true); |
||
167 | $element->addOptionArray($options); |
||
168 | break; |
||
169 | |||
170 | case 'radio': |
||
171 | $element = new XoopsFormRadio($caption, $name, (string)$value); |
||
172 | $element->addOptionArray($options); |
||
173 | break; |
||
174 | |||
175 | case 'checkbox': |
||
176 | $element = new XoopsFormCheckBox($caption, $name, $value); |
||
177 | $element->addOptionArray($options); |
||
178 | break; |
||
179 | |||
180 | case 'yesno': |
||
181 | $element = new XoopsFormRadioYN($caption, $name, $value); |
||
182 | break; |
||
183 | |||
184 | case 'group': |
||
185 | $element = new XoopsFormSelectGroup($caption, $name, true, $value); |
||
186 | break; |
||
187 | |||
188 | case 'group_multi': |
||
189 | $element = new XoopsFormSelectGroup($caption, $name, true, $value, 5, true); |
||
190 | break; |
||
191 | |||
192 | case 'language': |
||
193 | $element = new XoopsFormSelectLang($caption, $name, $value); |
||
194 | break; |
||
195 | |||
196 | case 'date': |
||
197 | $element = new XoopsFormTextDateSelect($caption, $name, 15, $value); |
||
198 | break; |
||
199 | |||
200 | case 'longdate': |
||
201 | $element = new XoopsFormTextDateSelect($caption, $name, 15, str_replace('-', '/', $value)); |
||
202 | break; |
||
203 | |||
204 | case 'datetime': |
||
205 | $element = new XoopsFormDatetime($caption, $name, 15, $value); |
||
206 | break; |
||
207 | |||
208 | case 'timezone': |
||
209 | $element = new XoopsFormSelectTimezone($caption, $name, $value); |
||
210 | //$element->setExtra("style='width: 280px;'"); |
||
211 | break; |
||
212 | |||
213 | case 'rank': |
||
214 | $element = new XoopsFormSelect($caption, $name, $value); |
||
215 | |||
216 | include_once $GLOBALS['xoops']->path('class/xoopslists.php'); |
||
217 | $ranks = XoopsLists::getUserRankList(); |
||
218 | $element->addOption(0, '--------------'); |
||
219 | $element->addOptionArray($ranks); |
||
220 | break; |
||
221 | |||
222 | case 'theme': |
||
223 | $element = new XoopsFormSelectTheme($caption, $name, $value, 1, true); |
||
224 | break; |
||
225 | } |
||
226 | if ($this->getVar('field_description') != '') { |
||
227 | $element->setDescription($this->getVar('field_description')); |
||
228 | } |
||
229 | |||
230 | return $element; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Returns a value for output of this field |
||
235 | * |
||
236 | * @param XoopsUser $user {@link XoopsUser} object to get the value of |
||
237 | * @param profileProfile $profile object to get the value of |
||
238 | * |
||
239 | * @return mixed |
||
240 | **/ |
||
241 | public function getOutputValue(&$user, $profile) |
||
362 | } |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Returns a value ready to be saved in the database |
||
367 | * |
||
368 | * @param mixed $value Value to format |
||
369 | * |
||
370 | * @return mixed |
||
371 | */ |
||
372 | public function getValueForSave($value) |
||
373 | { |
||
374 | switch ($this->getVar('field_type')) { |
||
375 | default: |
||
376 | case 'textbox': |
||
377 | case 'textarea': |
||
378 | case 'dhtml': |
||
379 | case 'yesno': |
||
380 | case 'timezone': |
||
381 | case 'theme': |
||
382 | case 'language': |
||
383 | case 'select': |
||
384 | case 'radio': |
||
385 | case 'select_multi': |
||
386 | case 'group': |
||
387 | case 'group_multi': |
||
388 | case 'longdate': |
||
389 | return $value; |
||
390 | |||
391 | case 'checkbox': |
||
392 | return (array)$value; |
||
393 | |||
394 | case 'date': |
||
395 | if ($value !== '') { |
||
396 | return strtotime($value); |
||
397 | } |
||
398 | |||
399 | return $value; |
||
400 | break; |
||
401 | |||
402 | case 'datetime': |
||
403 | if (!empty($value)) { |
||
404 | return strtotime($value['date']) + (int)$value['time']; |
||
405 | } |
||
406 | |||
407 | return $value; |
||
408 | break; |
||
409 | } |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Get names of user variables |
||
414 | * |
||
415 | * @return array |
||
416 | */ |
||
417 | public function getUserVars() |
||
423 | } |
||
424 | } |
||
425 | |||
691 |