| Total Complexity | 141 |
| Total Lines | 492 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Field 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 Field, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 13 | class Field extends \XoopsObject |
||
| 14 | { |
||
| 15 | public function __construct() |
||
| 16 | { |
||
| 17 | $this->initVar('field_id', \XOBJ_DTYPE_INT, null); |
||
| 18 | $this->initVar('cids', \XOBJ_DTYPE_ARRAY, [0 => '0'], true); |
||
| 19 | $this->initVar('field_type', \XOBJ_DTYPE_TXTBOX); |
||
| 20 | $this->initVar('field_valuetype', \XOBJ_DTYPE_INT, null, true); |
||
| 21 | $this->initVar('field_name', \XOBJ_DTYPE_TXTBOX, null, true); |
||
| 22 | $this->initVar('field_title', \XOBJ_DTYPE_TXTBOX); |
||
| 23 | $this->initVar('field_description', \XOBJ_DTYPE_TXTAREA); |
||
| 24 | $this->initVar('field_required', \XOBJ_DTYPE_INT, 0); //0 = no, 1 = yes |
||
| 25 | $this->initVar('field_maxlength', \XOBJ_DTYPE_INT, 0); |
||
| 26 | $this->initVar('field_weight', \XOBJ_DTYPE_INT, 0); |
||
| 27 | $this->initVar('field_default', \XOBJ_DTYPE_TXTAREA, ''); |
||
| 28 | $this->initVar('field_notnull', \XOBJ_DTYPE_INT, 1); |
||
| 29 | $this->initVar('field_edit', \XOBJ_DTYPE_INT, 0); |
||
| 30 | $this->initVar('field_show', \XOBJ_DTYPE_INT, 0); |
||
| 31 | $this->initVar('field_config', \XOBJ_DTYPE_INT, 0); |
||
| 32 | $this->initVar('field_options', \XOBJ_DTYPE_ARRAY, []); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Extra treatment dealing with non latin encoding |
||
| 37 | * Tricky solution |
||
| 38 | * @param string $key |
||
| 39 | * @param mixed $value |
||
| 40 | * @param bool $not_gpc |
||
| 41 | */ |
||
| 42 | public function setVar($key, $value, $not_gpc = false): void |
||
| 43 | { |
||
| 44 | if ('field_options' === $key && \is_array($value)) { |
||
| 45 | foreach (\array_keys($value) as $idx) { |
||
| 46 | $value[$idx] = base64_encode($value[$idx]); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | parent::setVar($key, $value, $not_gpc); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $key |
||
| 54 | * @param string $format |
||
| 55 | * @return mixed |
||
| 56 | */ |
||
| 57 | public function getVar($key, $format = 's') |
||
| 58 | { |
||
| 59 | $value = parent::getVar($key, $format); |
||
| 60 | if ('field_options' === $key && !empty($value)) { |
||
| 61 | foreach (\array_keys($value) as $idx) { |
||
|
|
|||
| 62 | $value[$idx] = base64_decode($value[$idx], true); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | return $value; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns a {@link XoopsFormElement} for editing the value of this field |
||
| 71 | * |
||
| 72 | * @param \XoopsUser $user {@link XoopsUser} object to edit the value of |
||
| 73 | * @param ObjectsProfile $profile {@link ObjectsProfile} object to edit the value of |
||
| 74 | * |
||
| 75 | * @return \\XoopsFormDhtmlTextArea|\\XoopsFormEditor|\\XoopsFormLabel|\\XoopsFormSelect|\\XoopsFormText|\\XoopsFormTextArea |
||
| 76 | */ |
||
| 77 | public function getEditElement($user, $profile) |
||
| 78 | { |
||
| 79 | $value = \in_array($this->getVar('field_name'), $this->getPostVars(), true) ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e'); |
||
| 80 | if (null === $value) { |
||
| 81 | $value = $this->getVar('field_default'); |
||
| 82 | } |
||
| 83 | $caption = $this->getVar('field_title'); |
||
| 84 | $caption = \defined($caption) ? \constant($caption) : $caption; |
||
| 85 | $name = $this->getVar('field_name', 'e'); |
||
| 86 | $options = $this->getVar('field_options'); |
||
| 87 | if (\is_array($options)) { |
||
| 88 | //asort($options); |
||
| 89 | |||
| 90 | foreach (\array_keys($options) as $key) { |
||
| 91 | $optval = \defined($options[$key]) ? \constant($options[$key]) : $options[$key]; |
||
| 92 | $optkey = \defined((string)$key) ? \constant($key) : $key; |
||
| 93 | unset($options[$key]); |
||
| 94 | $options[$optkey] = $optval; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | require_once $GLOBALS['xoops']->path('class/xoopsformloader.php'); |
||
| 98 | switch ($this->getVar('field_type')) { |
||
| 99 | default: |
||
| 100 | case 'autotext': |
||
| 101 | //autotext is not for editing |
||
| 102 | $element = new \XoopsFormLabel($caption, $this->getOutputValue($user, $profile)); |
||
| 103 | break; |
||
| 104 | case 'textbox': |
||
| 105 | $element = new \XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value); |
||
| 106 | break; |
||
| 107 | case 'textarea': |
||
| 108 | $element = new \XoopsFormTextArea($caption, $name, $value, 4, 30); |
||
| 109 | break; |
||
| 110 | case 'dhtml': |
||
| 111 | $element = new \XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30); |
||
| 112 | break; |
||
| 113 | case 'editor': |
||
| 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 | case 'select': |
||
| 122 | $element = new \XoopsFormSelect($caption, $name, $value); |
||
| 123 | // If options do not include an empty element, then add a blank option to prevent any default selection |
||
| 124 | if (!\array_key_exists('', $options)) { |
||
| 125 | $element->addOption('', _NONE); |
||
| 126 | //trabis |
||
| 127 | if (1 == $this->getVar('field_required')) { |
||
| 128 | $eltmsg = empty($caption) ? \sprintf(_FORM_ENTER, $name) : \sprintf(_FORM_ENTER, $caption); |
||
| 129 | $eltmsg = \str_replace('"', '\"', \stripslashes($eltmsg)); |
||
| 130 | $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};" |
||
| 131 | . "for (i = 0; i < selectBox.options.length; i++ ) { if ( selectBox.options[i].selected === true && selectBox.options[i].value != '' ) { hasSelected = true; break; } }" |
||
| 132 | . "if ( !hasSelected ) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }"; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | $element->addOptionArray($options); |
||
| 136 | break; |
||
| 137 | case 'select_multi': |
||
| 138 | $element = new \XoopsFormSelect($caption, $name, $value, 5, true); |
||
| 139 | $element->addOptionArray($options); |
||
| 140 | break; |
||
| 141 | case 'radio': |
||
| 142 | $element = new \XoopsFormRadio($caption, $name, $value); |
||
| 143 | $element->addOptionArray($options); |
||
| 144 | break; |
||
| 145 | case 'checkbox': |
||
| 146 | $element = new \XoopsFormCheckBox($caption, $name, $value); |
||
| 147 | $element->addOptionArray($options); |
||
| 148 | break; |
||
| 149 | case 'yesno': |
||
| 150 | $element = new \XoopsFormRadioYN($caption, $name, $value); |
||
| 151 | break; |
||
| 152 | case 'group': |
||
| 153 | $element = new \XoopsFormSelectGroup($caption, $name, true, $value); |
||
| 154 | break; |
||
| 155 | case 'group_multi': |
||
| 156 | $element = new \XoopsFormSelectGroup($caption, $name, true, $value, 5, true); |
||
| 157 | break; |
||
| 158 | case 'language': |
||
| 159 | $element = new \XoopsFormSelectLang($caption, $name, $value); |
||
| 160 | break; |
||
| 161 | case 'date': |
||
| 162 | $element = new \XoopsFormTextDateSelect($caption, $name, 15, $value); |
||
| 163 | break; |
||
| 164 | case 'longdate': |
||
| 165 | $element = new \XoopsFormTextDateSelect($caption, $name, 15, \str_replace('-', '/', $value)); |
||
| 166 | break; |
||
| 167 | case 'datetime': |
||
| 168 | $element = new XoopsFormDatetime($caption, $name, 15, $value); |
||
| 169 | break; |
||
| 170 | case 'list': |
||
| 171 | $element = new \XoopsFormSelect($caption, $name, $value, 1, $options[0]); |
||
| 172 | break; |
||
| 173 | case 'timezone': |
||
| 174 | $element = new \XoopsFormSelectTimezone($caption, $name, $value); |
||
| 175 | $element->setExtra("style='width: 280px;'"); |
||
| 176 | break; |
||
| 177 | case 'rank': |
||
| 178 | $element = new \XoopsFormSelect($caption, $name, $value); |
||
| 179 | |||
| 180 | require_once $GLOBALS['xoops']->path('class/xoopslists.php'); |
||
| 181 | $ranks = \XoopsLists::getUserRankList(); |
||
| 182 | $element->addOption(0, '--------------'); |
||
| 183 | $element->addOptionArray($ranks); |
||
| 184 | break; |
||
| 185 | case 'theme': |
||
| 186 | $element = new \XoopsFormSelect($caption, $name, $value); |
||
| 187 | $element->addOption('0', \_AM_SONGLIST_OBJS_MF_SITEDEFAULT); |
||
| 188 | $handle = \opendir(XOOPS_THEME_PATH . '/'); |
||
| 189 | $dirlist = []; |
||
| 190 | while (false !== ($file = \readdir($handle))) { |
||
| 191 | if (\is_dir(XOOPS_THEME_PATH . '/' . $file) && !\preg_match('/^[.]{1,2}$/', $file) && 'cvs' !== \mb_strtolower($file)) { |
||
| 192 | if (\file_exists(XOOPS_THEME_PATH . '/' . $file . '/theme.tpl') && \in_array($file, $GLOBALS['xoopsConfig']['theme_set_allowed'], true)) { |
||
| 193 | $dirlist[$file] = $file; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | \closedir($handle); |
||
| 198 | if (!empty($dirlist)) { |
||
| 199 | \asort($dirlist); |
||
| 200 | $element->addOptionArray($dirlist); |
||
| 201 | } |
||
| 202 | break; |
||
| 203 | } |
||
| 204 | if ('' != $this->getVar('field_description')) { |
||
| 205 | $element->setDescription($this->getVar('field_description')); |
||
| 206 | } |
||
| 207 | |||
| 208 | return $element; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Returns a {@link XoopsFormElement} for editing the value of this field |
||
| 213 | * @return \\XoopsFormLabel|\\XoopsFormSelect|\\XoopsFormText|\\XoopsFormTextArea |
||
| 214 | * @internal param XoopsUser $user <a href='psi_element://XoopsUser'>XoopsUser</a> object to edit the value of object to edit the value of |
||
| 215 | * @internal param ObjectsProfile $profile <a href='psi_element://ObjectsProfile'>ObjectsProfile</a> object to edit the value of object to edit the value of |
||
| 216 | */ |
||
| 217 | public function getSearchElement() |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Returns a value for output of this field |
||
| 345 | * |
||
| 346 | * @param \XoopsUser $user {@link XoopsUser} object to get the value of |
||
| 347 | * @param ObjectsProfile $profile object to get the value of |
||
| 348 | * |
||
| 349 | * @return mixed |
||
| 350 | **/ |
||
| 351 | public function getOutputValue($user, $profile) |
||
| 352 | { |
||
| 353 | \xoops_loadLanguage('modinfo', 'objects'); |
||
| 354 | |||
| 355 | $value = \in_array($this->getVar('field_name'), $this->getPostVars(), true) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name')); |
||
| 356 | |||
| 357 | switch ($this->getVar('field_type')) { |
||
| 358 | default: |
||
| 359 | case 'textbox': |
||
| 360 | if ('url' === $this->getVar('field_name') && '' != $value) { |
||
| 361 | return '<a href="' . \formatURL($value) . '" rel="external">' . $value . '</a>'; |
||
| 362 | } |
||
| 363 | |||
| 364 | return $value; |
||
| 365 | break; |
||
| 366 | case 'editor': |
||
| 367 | case 'textarea': |
||
| 368 | case 'dhtml': |
||
| 369 | case 'theme': |
||
| 370 | case 'language': |
||
| 371 | case 'list': |
||
| 372 | return $value; |
||
| 373 | break; |
||
| 374 | case 'select': |
||
| 375 | case 'radio': |
||
| 376 | $options = $this->getVar('field_options'); |
||
| 377 | if (isset($options[$value])) { |
||
| 378 | $value = \htmlspecialchars(\defined($options[$value]) ? \constant($options[$value]) : $options[$value], \ENT_QUOTES | \ENT_HTML5); |
||
| 379 | } else { |
||
| 380 | $value = ''; |
||
| 381 | } |
||
| 382 | |||
| 383 | return $value; |
||
| 384 | break; |
||
| 385 | case 'select_multi': |
||
| 386 | case 'checkbox': |
||
| 387 | $options = $this->getVar('field_options'); |
||
| 388 | $ret = []; |
||
| 389 | if (\count($options) > 0) { |
||
| 390 | foreach (\array_keys($options) as $key) { |
||
| 391 | if (\in_array($key, $value, true)) { |
||
| 392 | $$ret[$key] = \htmlspecialchars(\defined($options[$key]) ? \constant($options[$key]) : $options[$key], \ENT_QUOTES | \ENT_HTML5); |
||
| 393 | } |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | return $ret; |
||
| 398 | break; |
||
| 399 | case 'group': |
||
| 400 | //change to retrieve groups and return name of group |
||
| 401 | return $value; |
||
| 402 | break; |
||
| 403 | case 'group_multi': |
||
| 404 | //change to retrieve groups and return array of group names |
||
| 405 | return ''; |
||
| 406 | break; |
||
| 407 | case 'longdate': |
||
| 408 | //return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that |
||
| 409 | //when we cannot convert it to a UNIX timestamp? |
||
| 410 | return \str_replace('-', '/', $value); |
||
| 411 | case 'date': |
||
| 412 | return \formatTimestamp($value, 's'); |
||
| 413 | break; |
||
| 414 | case 'datetime': |
||
| 415 | if (!empty($value)) { |
||
| 416 | return \formatTimestamp($value, 'm'); |
||
| 417 | } |
||
| 418 | |||
| 419 | return $value = \_MI_SONGLIST_DATENOTSET; |
||
| 420 | break; |
||
| 421 | case 'autotext': |
||
| 422 | $value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it |
||
| 423 | $value = \str_replace('{X_UID}', $user->getVar('uid'), $value); |
||
| 424 | $value = \str_replace('{X_URL}', XOOPS_URL, $value); |
||
| 425 | $value = \str_replace('{X_UNAME}', $user->getVar('uname'), $value); |
||
| 426 | |||
| 427 | return $value; |
||
| 428 | break; |
||
| 429 | case 'rank': |
||
| 430 | $userrank = $user->rank(); |
||
| 431 | $user_rankimage = ''; |
||
| 432 | if (isset($userrank['image']) && '' != $userrank['image']) { |
||
| 433 | $user_rankimage = '<img src="' . XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '"><br>'; |
||
| 434 | } |
||
| 435 | |||
| 436 | return $user_rankimage . $userrank['title']; |
||
| 437 | break; |
||
| 438 | case 'yesno': |
||
| 439 | return $value ? _YES : _NO; |
||
| 440 | break; |
||
| 441 | case 'timezone': |
||
| 442 | require_once $GLOBALS['xoops']->path('class/xoopslists.php'); |
||
| 443 | $timezones = \XoopsLists::getTimeZoneList(); |
||
| 444 | $value = empty($value) ? '0' : (string)$value; |
||
| 445 | |||
| 446 | return $timezones[\str_replace('.0', '', $value)]; |
||
| 447 | break; |
||
| 448 | } |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Returns a value ready to be saved in the database |
||
| 453 | * |
||
| 454 | * @param mixed $value Value to format |
||
| 455 | * |
||
| 456 | * @return mixed |
||
| 457 | */ |
||
| 458 | public function getValueForSave($value) |
||
| 459 | { |
||
| 460 | switch ($this->getVar('field_type')) { |
||
| 461 | default: |
||
| 462 | case 'textbox': |
||
| 463 | case 'textarea': |
||
| 464 | case 'dhtml': |
||
| 465 | case 'yesno': |
||
| 466 | case 'timezone': |
||
| 467 | case 'theme': |
||
| 468 | case 'language': |
||
| 469 | case 'list': |
||
| 470 | case 'select': |
||
| 471 | case 'radio': |
||
| 472 | case 'select_multi': |
||
| 473 | case 'checkbox': |
||
| 474 | case 'group': |
||
| 475 | case 'group_multi': |
||
| 476 | case 'longdate': |
||
| 477 | return $value; |
||
| 478 | case 'date': |
||
| 479 | if ('' != $value) { |
||
| 480 | return \strtotime((string)$value); |
||
| 481 | } |
||
| 482 | |||
| 483 | return $value; |
||
| 484 | break; |
||
| 485 | case 'datetime': |
||
| 486 | if (!empty($value)) { |
||
| 487 | return \strtotime($value['date']??'') + (int)$value['time']; |
||
| 488 | } |
||
| 489 | |||
| 490 | return $value; |
||
| 491 | break; |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Get names of user variables |
||
| 497 | * |
||
| 498 | * @return array |
||
| 499 | */ |
||
| 500 | public function getPostVars(): array |
||
| 505 | } |
||
| 506 | } |
||
| 507 |