Total Complexity | 143 |
Total Lines | 540 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SonglistField 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 SonglistField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class SonglistField extends XoopsObject |
||
11 | { |
||
12 | function __construct() |
||
|
|||
13 | { |
||
14 | $this->initVar('field_id', XOBJ_DTYPE_INT, null); |
||
15 | $this->initVar('cids', XOBJ_DTYPE_ARRAY, array(0=>'0'), true); |
||
16 | $this->initVar('field_type', XOBJ_DTYPE_TXTBOX); |
||
17 | $this->initVar('field_valuetype', XOBJ_DTYPE_INT, null, true); |
||
18 | $this->initVar('field_name', XOBJ_DTYPE_TXTBOX, null, true); |
||
19 | $this->initVar('field_title', XOBJ_DTYPE_TXTBOX); |
||
20 | $this->initVar('field_description', XOBJ_DTYPE_TXTAREA); |
||
21 | $this->initVar('field_required', XOBJ_DTYPE_INT, 0); //0 = no, 1 = yes |
||
22 | $this->initVar('field_maxlength', XOBJ_DTYPE_INT, 0); |
||
23 | $this->initVar('field_weight', XOBJ_DTYPE_INT, 0); |
||
24 | $this->initVar('field_default', XOBJ_DTYPE_TXTAREA, ""); |
||
25 | $this->initVar('field_notnull', XOBJ_DTYPE_INT, 1); |
||
26 | $this->initVar('field_edit', XOBJ_DTYPE_INT, 0); |
||
27 | $this->initVar('field_show', XOBJ_DTYPE_INT, 0); |
||
28 | $this->initVar('field_config', XOBJ_DTYPE_INT, 0); |
||
29 | $this->initVar('field_options', XOBJ_DTYPE_ARRAY, array() ); |
||
30 | } |
||
31 | |||
32 | |||
33 | |||
34 | function SonglistField() |
||
35 | { |
||
36 | $this->__construct(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Extra treatment dealing with non latin encoding |
||
41 | * Tricky solution |
||
42 | */ |
||
43 | function setVar($key, $value, $not_gpc = false) |
||
44 | { |
||
45 | if ($key == 'field_options' && is_array($value)) { |
||
46 | foreach (array_keys($value) as $idx ) { |
||
47 | $value[$idx] = base64_encode($value[$idx]); |
||
48 | } |
||
49 | } |
||
50 | parent::setVar($key, $value, $not_gpc); |
||
51 | } |
||
52 | |||
53 | function getVar($key, $format = 's') |
||
54 | { |
||
55 | $value = parent::getVar($key, $format); |
||
56 | if ($key == 'field_options' && !empty($value)) { |
||
57 | foreach (array_keys($value) as $idx) { |
||
58 | $value[$idx] = base64_decode($value[$idx]); |
||
59 | } |
||
60 | } |
||
61 | return $value; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Returns a {@link XoopsFormElement} for editing the value of this field |
||
66 | * |
||
67 | * @param XoopsUser $user {@link XoopsUser} object to edit the value of |
||
68 | * @param ObjectsProfile $profile {@link ObjectsProfile} object to edit the value of |
||
69 | * |
||
70 | * @return XoopsFormElement |
||
71 | **/ |
||
72 | function getEditElement($user, $profile) |
||
73 | { |
||
74 | $value = in_array($this->getVar('field_name'), $this->getPostVars() ) ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e'); |
||
75 | if (is_null($value)) { |
||
76 | $value = $this->getVar('field_default'); |
||
77 | } |
||
78 | $caption = $this->getVar('field_title'); |
||
79 | $caption = defined($caption) ? constant($caption) : $caption; |
||
80 | $name = $this->getVar('field_name', 'e'); |
||
81 | $options = $this->getVar('field_options'); |
||
82 | if (is_array($options)) { |
||
83 | //asort($options); |
||
84 | |||
85 | foreach (array_keys($options) as $key) { |
||
86 | $optval = defined($options[$key]) ? constant($options[$key]) : $options[$key]; |
||
87 | $optkey = defined($key) ? constant($key) : $key; |
||
88 | unset($options[$key]); |
||
89 | $options[$optkey] = $optval; |
||
90 | } |
||
91 | } |
||
92 | include_once $GLOBALS['xoops']->path('class/xoopsformloader.php'); |
||
93 | switch ($this->getVar('field_type') ) { |
||
94 | default: |
||
95 | case "autotext": |
||
96 | //autotext is not for editing |
||
97 | $element = new XoopsFormLabel($caption, $this->getOutputValue($user, $profile)); |
||
98 | break; |
||
99 | |||
100 | case "textbox": |
||
101 | $element = new XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value); |
||
102 | break; |
||
103 | |||
104 | case "textarea": |
||
105 | $element = new XoopsFormTextArea($caption, $name, $value, 4, 30); |
||
106 | break; |
||
107 | |||
108 | case "dhtml": |
||
109 | $element = new XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30); |
||
110 | break; |
||
111 | |||
112 | case "editor": |
||
113 | |||
114 | $editor_config['name'] = $name; |
||
115 | $editor_config['editor'] = $GLOBALS['songlistModuleConfig']['editor']; |
||
116 | $editor_config['value'] = $value; |
||
117 | $editor_config['width'] = $GLOBALS['songlistModuleConfig']['editor_width']; |
||
118 | $editor_config['height'] = $GLOBALS['songlistModuleConfig']['editor_height']; |
||
119 | $element = new XoopsFormEditor($caption, $name, $editor_config); |
||
120 | break; |
||
121 | |||
122 | case "select": |
||
123 | $element = new XoopsFormSelect($caption, $name, $value); |
||
124 | // If options do not include an empty element, then add a blank option to prevent any default selection |
||
125 | if (!in_array('', array_keys($options))) { |
||
126 | $element->addOption('', _NONE); |
||
127 | //trabis |
||
128 | if ($this->getVar('field_required') == 1) { |
||
129 | $eltmsg = empty($caption) ? sprintf(_FORM_ENTER, $name) : sprintf( _FORM_ENTER, $caption); |
||
130 | $eltmsg = str_replace('"', '\"', stripslashes($eltmsg)); |
||
131 | $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};" . |
||
132 | "for (i = 0; i < selectBox.options.length; i++ ) { if ( selectBox.options[i].selected == true && selectBox.options[i].value != '' ) { hasSelected = true; break; } }" . |
||
133 | "if ( !hasSelected ) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }"; |
||
134 | } |
||
135 | } |
||
136 | $element->addOptionArray($options); |
||
137 | break; |
||
138 | |||
139 | case "select_multi": |
||
140 | $element = new XoopsFormSelect($caption, $name, $value, 5, true); |
||
141 | $element->addOptionArray($options); |
||
142 | break; |
||
143 | |||
144 | case "radio": |
||
145 | $element = new XoopsFormRadio($caption, $name, $value); |
||
146 | $element->addOptionArray($options); |
||
147 | break; |
||
148 | |||
149 | case "checkbox": |
||
150 | $element = new XoopsFormCheckBox($caption, $name, $value); |
||
151 | $element->addOptionArray($options); |
||
152 | break; |
||
153 | |||
154 | case "yesno": |
||
155 | $element = new XoopsFormRadioYN($caption, $name, $value); |
||
156 | break; |
||
157 | |||
158 | case "group": |
||
159 | $element = new XoopsFormSelectGroup($caption, $name, true, $value); |
||
160 | break; |
||
161 | |||
162 | case "group_multi": |
||
163 | $element = new XoopsFormSelectGroup($caption, $name, true, $value, 5, true); |
||
164 | break; |
||
165 | |||
166 | case "language": |
||
167 | $element = new XoopsFormSelectLang($caption, $name, $value); |
||
168 | break; |
||
169 | |||
170 | case "date": |
||
171 | $element = new XoopsFormTextDateSelect($caption, $name, 15, $value); |
||
172 | break; |
||
173 | |||
174 | case "longdate": |
||
175 | $element = new XoopsFormTextDateSelect($caption, $name, 15, str_replace("-", "/", $value) ); |
||
176 | break; |
||
177 | |||
178 | case "datetime": |
||
179 | $element = new XoopsFormDatetime($caption, $name, 15, $value); |
||
180 | break; |
||
181 | |||
182 | case "list": |
||
183 | $element = new XoopsFormSelectList($caption, $name, $value, 1, $options[0]); |
||
184 | break; |
||
185 | |||
186 | case "timezone": |
||
187 | $element = new XoopsFormSelectTimezone($caption, $name, $value); |
||
188 | $element->setExtra("style='width: 280px;'"); |
||
189 | break; |
||
190 | |||
191 | case "rank": |
||
192 | $element = new XoopsFormSelect($caption, $name, $value); |
||
193 | |||
194 | include_once $GLOBALS['xoops']->path('class/xoopslists.php'); |
||
195 | $ranks = XoopsLists::getUserRankList(); |
||
196 | $element->addOption(0, "--------------"); |
||
197 | $element->addOptionArray($ranks); |
||
198 | break; |
||
199 | |||
200 | case 'theme': |
||
201 | $element = new XoopsFormSelect($caption, $name, $value); |
||
202 | $element->addOption("0", _OBJS_MF_SITEDEFAULT); |
||
203 | $handle = opendir(XOOPS_THEME_PATH . '/'); |
||
204 | $dirlist = array(); |
||
205 | while (false !== ($file = readdir($handle) ) ) { |
||
206 | if (is_dir(XOOPS_THEME_PATH . '/' . $file) && !preg_match("/^[.]{1,2}$/", $file) && strtolower($file) != 'cvs' ) { |
||
207 | if (file_exists(XOOPS_THEME_PATH . "/" . $file . "/theme.html") && in_array($file, $GLOBALS['xoopsConfig']['theme_set_allowed'])) { |
||
208 | $dirlist[$file] = $file; |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | closedir($handle); |
||
213 | if (!empty($dirlist)) { |
||
214 | asort($dirlist); |
||
215 | $element->addOptionArray($dirlist); |
||
216 | } |
||
217 | break; |
||
218 | } |
||
219 | if ($this->getVar('field_description') != "") { |
||
220 | $element->setDescription($this->getVar('field_description') ); |
||
221 | } |
||
222 | return $element; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Returns a {@link XoopsFormElement} for editing the value of this field |
||
227 | * |
||
228 | * @param XoopsUser $user {@link XoopsUser} object to edit the value of |
||
229 | * @param ObjectsProfile $profile {@link ObjectsProfile} object to edit the value of |
||
230 | * |
||
231 | * @return XoopsFormElement |
||
232 | **/ |
||
233 | function getSearchElement() |
||
234 | { |
||
235 | $caption = $this->getVar('field_title'); |
||
236 | $caption = defined($caption) ? constant($caption) : $caption; |
||
237 | $name = $this->getVar('field_name', 'e'); |
||
238 | $options = $this->getVar('field_options'); |
||
239 | if (is_array($options)) { |
||
240 | //asort($options); |
||
241 | |||
242 | foreach (array_keys($options) as $key) { |
||
243 | $optval = defined($options[$key]) ? constant($options[$key]) : $options[$key]; |
||
244 | $optkey = defined($key) ? constant($key) : $key; |
||
245 | unset($options[$key]); |
||
246 | $options[$optkey] = $optval; |
||
247 | } |
||
248 | } |
||
249 | include_once $GLOBALS['xoops']->path('class/xoopsformloader.php'); |
||
250 | switch ($this->getVar('field_type') ) { |
||
251 | default: |
||
252 | case "autotext": |
||
253 | //autotext is not for editing |
||
254 | $element = new XoopsFormLabel($caption, $this->getOutputValue($user, $profile)); |
||
255 | break; |
||
256 | |||
257 | case "textbox": |
||
258 | $element = new XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value); |
||
259 | break; |
||
260 | |||
261 | case "textarea": |
||
262 | $element = new XoopsFormTextArea($caption, $name, $value, 4, 30); |
||
263 | break; |
||
264 | |||
265 | case "dhtml": |
||
266 | $element = new XoopsFormText($caption, $name, 35, 255, $value); |
||
267 | break; |
||
268 | |||
269 | case "select": |
||
270 | $element = new XoopsFormSelect($caption, $name, $value); |
||
271 | // If options do not include an empty element, then add a blank option to prevent any default selection |
||
272 | if (!in_array('', array_keys($options))) { |
||
273 | $element->addOption('', _NONE); |
||
274 | //trabis |
||
275 | if ($this->getVar('field_required') == 1) { |
||
276 | $eltmsg = empty($caption) ? sprintf(_FORM_ENTER, $name) : sprintf( _FORM_ENTER, $caption); |
||
277 | $eltmsg = str_replace('"', '\"', stripslashes($eltmsg)); |
||
278 | $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};" . |
||
279 | "for (i = 0; i < selectBox.options.length; i++ ) { if ( selectBox.options[i].selected == true && selectBox.options[i].value != '' ) { hasSelected = true; break; } }" . |
||
280 | "if ( !hasSelected ) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }"; |
||
281 | } |
||
282 | //end |
||
283 | } |
||
284 | $element->addOptionArray($options); |
||
285 | break; |
||
286 | |||
287 | |||
288 | case "editor": |
||
289 | |||
290 | $element = new XoopsFormText($caption, $name, 35, 255, $value); |
||
291 | break; |
||
292 | |||
293 | case "select_multi": |
||
294 | $element = new XoopsFormSelect($caption, $name, $value, 5, true); |
||
295 | $element->addOptionArray($options); |
||
296 | break; |
||
297 | |||
298 | case "radio": |
||
299 | $element = new XoopsFormRadio($caption, $name, $value); |
||
300 | $element->addOptionArray($options); |
||
301 | break; |
||
302 | |||
303 | case "checkbox": |
||
304 | $element = new XoopsFormCheckBox($caption, $name, $value); |
||
305 | $element->addOptionArray($options); |
||
306 | break; |
||
307 | |||
308 | case "yesno": |
||
309 | $element = new XoopsFormRadioYN($caption, $name, $value); |
||
310 | break; |
||
311 | |||
312 | case "group": |
||
313 | $element = new XoopsFormSelectGroup($caption, $name, true, $value); |
||
314 | break; |
||
315 | |||
316 | case "group_multi": |
||
317 | $element = new XoopsFormSelectGroup($caption, $name, true, $value, 5, true); |
||
318 | break; |
||
319 | |||
320 | case "language": |
||
321 | $element = new XoopsFormSelectLang($caption, $name, $value); |
||
322 | break; |
||
323 | |||
324 | case "date": |
||
325 | $element = new XoopsFormTextDateSelect($caption, $name, 15, $value); |
||
326 | break; |
||
327 | |||
328 | case "longdate": |
||
329 | $element = new XoopsFormTextDateSelect($caption, $name, 15, str_replace("-", "/", $value) ); |
||
330 | break; |
||
331 | |||
332 | case "datetime": |
||
333 | $element = new XoopsFormDatetime($caption, $name, 15, $value); |
||
334 | break; |
||
335 | |||
336 | case "list": |
||
337 | $element = new XoopsFormSelectList($caption, $name, $value, 1, $options[0]); |
||
338 | break; |
||
339 | |||
340 | case "timezone": |
||
341 | $element = new XoopsFormSelectTimezone($caption, $name, $value); |
||
342 | $element->setExtra("style='width: 280px;'"); |
||
343 | break; |
||
344 | |||
345 | case "rank": |
||
346 | $element = new XoopsFormSelect($caption, $name, $value); |
||
347 | |||
348 | include_once $GLOBALS['xoops']->path('class/xoopslists.php'); |
||
349 | $ranks = XoopsLists::getUserRankList(); |
||
350 | $element->addOption(0, "--------------"); |
||
351 | $element->addOptionArray($ranks); |
||
352 | break; |
||
353 | |||
354 | case 'theme': |
||
355 | $element = new XoopsFormSelect($caption, $name, $value); |
||
356 | $element->addOption("0", _OBJS_MF_SITEDEFAULT); |
||
357 | $handle = opendir(XOOPS_THEME_PATH . '/'); |
||
358 | $dirlist = array(); |
||
359 | while (false !== ($file = readdir($handle) ) ) { |
||
360 | if (is_dir(XOOPS_THEME_PATH . '/' . $file) && !preg_match("/^[.]{1,2}$/", $file) && strtolower($file) != 'cvs' ) { |
||
361 | if (file_exists(XOOPS_THEME_PATH . "/" . $file . "/theme.html") && in_array($file, $GLOBALS['xoopsConfig']['theme_set_allowed'])) { |
||
362 | $dirlist[$file] = $file; |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | closedir($handle); |
||
367 | if (!empty($dirlist)) { |
||
368 | asort($dirlist); |
||
369 | $element->addOptionArray($dirlist); |
||
370 | } |
||
371 | break; |
||
372 | } |
||
373 | if ($this->getVar('field_description') != "") { |
||
374 | $element->setDescription($this->getVar('field_description') ); |
||
375 | } |
||
376 | return $element; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Returns a value for output of this field |
||
381 | * |
||
382 | * @param XoopsUser $user {@link XoopsUser} object to get the value of |
||
383 | * @param ObjectsProfile $profile object to get the value of |
||
384 | * |
||
385 | * @return mixed |
||
386 | **/ |
||
387 | function getOutputValue($user, $profile) |
||
388 | { |
||
389 | if (file_exists($file = $GLOBALS['xoops']->path('modules/objects/language/' . $GLOBALS['xoopsConfig']['language'] . '/modinfo.php'))) { |
||
390 | include_once $file; |
||
391 | } else { |
||
392 | include_once $GLOBALS['xoops']->path('modules/objects/language/english/modinfo.php'); |
||
393 | } |
||
394 | |||
395 | $value = in_array($this->getVar('field_name'), $this->getPostVars() ) ? $user->getVar($this->getVar('field_name') ) : $profile->getVar($this->getVar('field_name')); |
||
396 | |||
397 | switch ($this->getVar('field_type') ) { |
||
398 | default: |
||
399 | case "textbox": |
||
400 | if ( $this->getVar('field_name') == 'url' && $value != '') { |
||
401 | return '<a href="' . formatURL($value) . '" rel="external">' . $value . '</a>'; |
||
402 | } else { |
||
403 | return $value; |
||
404 | } |
||
405 | break; |
||
406 | case "editor": |
||
407 | case "textarea": |
||
408 | case "dhtml": |
||
409 | case 'theme': |
||
410 | case "language": |
||
411 | case "list": |
||
412 | return $value; |
||
413 | break; |
||
414 | |||
415 | case "select": |
||
416 | case "radio": |
||
417 | $options = $this->getVar('field_options'); |
||
418 | if (isset($options[$value])) { |
||
419 | $value = htmlspecialchars( defined($options[$value]) ? constant($options[$value]) : $options[$value]); |
||
420 | } else { |
||
421 | $value = ""; |
||
422 | } |
||
423 | return $value; |
||
424 | break; |
||
425 | |||
426 | case "select_multi": |
||
427 | case "checkbox": |
||
428 | $options = $this->getVar('field_options'); |
||
429 | $ret = array(); |
||
430 | if (count($options) > 0) { |
||
431 | foreach (array_keys($options) as $key) { |
||
432 | if (in_array($key, $value)) { |
||
433 | $$ret[$key] = htmlspecialchars( defined($options[$key]) ? constant($options[$key]) : $options[$key]); |
||
434 | } |
||
435 | } |
||
436 | } |
||
437 | return $ret; |
||
438 | break; |
||
439 | |||
440 | case "group": |
||
441 | //change to retrieve groups and return name of group |
||
442 | return $value; |
||
443 | break; |
||
444 | |||
445 | case "group_multi": |
||
446 | //change to retrieve groups and return array of group names |
||
447 | return ""; |
||
448 | break; |
||
449 | |||
450 | case "longdate": |
||
451 | //return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that |
||
452 | //when we cannot convert it to a UNIX timestamp? |
||
453 | return str_replace("-", "/", $value); |
||
454 | |||
455 | case "date": |
||
456 | return formatTimestamp($value, 's'); |
||
457 | break; |
||
458 | |||
459 | case "datetime": |
||
460 | if (!empty($value)) { |
||
461 | return formatTimestamp($value, 'm'); |
||
462 | } else { |
||
463 | return $value = _XFORUM_MI_DATENOTSET; |
||
464 | } |
||
465 | break; |
||
466 | |||
467 | case "autotext": |
||
468 | $value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it |
||
469 | $value = str_replace("{X_UID}", $user->getVar("uid"), $value); |
||
470 | $value = str_replace("{X_URL}", XOOPS_URL, $value ); |
||
471 | $value = str_replace("{X_UNAME}", $user->getVar("uname"), $value); |
||
472 | return $value; |
||
473 | break; |
||
474 | |||
475 | case "rank": |
||
476 | $userrank = $user->rank(); |
||
477 | $user_rankimage = ""; |
||
478 | if (isset($userrank['image']) && $userrank['image'] != "") { |
||
479 | $user_rankimage = '<img src="'.XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /><br />'; |
||
480 | } |
||
481 | return $user_rankimage.$userrank['title']; |
||
482 | break; |
||
483 | |||
484 | case "yesno": |
||
485 | return $value ? _YES : _NO; |
||
486 | break; |
||
487 | |||
488 | case "timezone": |
||
489 | include_once $GLOBALS['xoops']->path('class/xoopslists.php'); |
||
490 | $timezones = XoopsLists::getTimeZoneList(); |
||
491 | $value = empty($value) ? "0" : strval($value); |
||
492 | return $timezones[str_replace('.0', '', $value)]; |
||
493 | break; |
||
494 | } |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Returns a value ready to be saved in the database |
||
499 | * |
||
500 | * @param mixed $value Value to format |
||
501 | * |
||
502 | * @return mixed |
||
503 | */ |
||
504 | function getValueForSave($value) |
||
538 | } |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * Get names of user variables |
||
543 | * |
||
544 | * @return array |
||
545 | */ |
||
546 | function getPostVars() |
||
550 | } |
||
551 | } |
||
552 | |||
553 | /** |
||
784 | ?> |
||
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.