Passed
Push — master ( 95e0c4...fc8ab0 )
by
unknown
44s queued 15s
created

FieldForm   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 43
eloc 186
c 1
b 0
f 0
dl 0
loc 221
rs 8.96

1 Method

Rating   Name   Duplication   Size   Complexity  
F __construct() 0 215 43

How to fix   Complexity   

Complex Class

Complex classes like FieldForm 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 FieldForm, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace XoopsModules\Suico\Form;
4
5
use XoopsModules\Suico;
6
use XoopsModules\Suico\Field;
7
use XoopsThemeForm;
8
use XoopsFormButton;
9
use XoopsFormText;
10
use XoopsFormTextArea;
11
use XoopsFormHidden;
12
use XoopsFormSelect;
13
use XoopsFormLabel;
14
use XoopsFormTextDateSelect;
15
16
class FieldForm extends XoopsThemeForm
17
{
18
    /**
19
     * @param Suico\Field $field  {@link Suico\Field} object to get edit form for
20
     * @param mixed       $action URL to submit to - or false for $_SERVER['REQUEST_URI']
21
     */
22
    function __construct(Suico\Field $field, $action = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
    {
24
        if (!$action) {
25
            $action = $_SERVER['REQUEST_URI'];
26
        }
27
        $title = $field->isNew() ? sprintf(_AM_SUICO_ADD, _AM_SUICO_FIELD) : sprintf(_AM_SUICO_EDIT, _AM_SUICO_FIELD);
28
        include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
29
        parent::__construct($title, 'form', $action, 'post', true);
30
        $this->addElement(new XoopsFormText(_AM_SUICO_TITLE, 'field_title', 35, 255, $field->getVar('field_title', 'e')));
31
        $this->addElement(new XoopsFormTextArea(_AM_SUICO_DESCRIPTION, 'field_description', $field->getVar('field_description', 'e')));
32
        $fieldcat_id = 0;
33
        if (!$field->isNew()) {
34
            $fieldcat_id = $field->getVar('cat_id');
35
        }
36
        $categoryHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Category');
37
        $cat_select      = new XoopsFormSelect(_AM_SUICO_CATEGORY, 'field_category', $fieldcat_id);
38
        $cat_select->addOption(0, _AM_SUICO_DEFAULT);
39
        $cat_select->addOptionArray($categoryHandler->getList());
40
        $this->addElement($cat_select);
41
        $this->addElement(new XoopsFormText(_AM_SUICO_WEIGHT, 'field_weight', 10, 10, $field->getVar('field_weight', 'e')));
42
        if ($field->getVar('field_config') || $field->isNew()) {
43
            if (!$field->isNew()) {
44
                $this->addElement(new XoopsFormLabel(_AM_SUICO_NAME, $field->getVar('field_name')));
45
                $this->addElement(new XoopsFormHidden('id', $field->getVar('field_id')));
46
            } else {
47
                $this->addElement(new XoopsFormText(_AM_SUICO_NAME, 'field_name', 35, 255, $field->getVar('field_name', 'e')));
48
            }
49
            //autotext and theme left out of this one as fields of that type should never be changed (valid assumption, I think)
50
            $fieldtypes     = [
51
                'checkbox'     => _AM_SUICO_CHECKBOX,
52
                'date'         => _AM_SUICO_DATE,
53
                'datetime'     => _AM_SUICO_DATETIME,
54
                'longdate'     => _AM_SUICO_LONGDATE,
55
                'group'        => _AM_SUICO_GROUP,
56
                'group_multi'  => _AM_SUICO_GROUPMULTI,
57
                'language'     => _AM_SUICO_LANGUAGE,
58
                'radio'        => _AM_SUICO_RADIO,
59
                'select'       => _AM_SUICO_SELECT,
60
                'select_multi' => _AM_SUICO_SELECTMULTI,
61
                'textarea'     => _AM_SUICO_TEXTAREA,
62
                'dhtml'        => _AM_SUICO_DHTMLTEXTAREA,
63
                'textbox'      => _AM_SUICO_TEXTBOX,
64
                'timezone'     => _AM_SUICO_TIMEZONE,
65
                'yesno'        => _AM_SUICO_YESNO,
66
            ];
67
            $element_select = new XoopsFormSelect(_AM_SUICO_TYPE, 'field_type', $field->getVar('field_type', 'e'));
68
            $element_select->addOptionArray($fieldtypes);
69
            $this->addElement($element_select);
70
            switch ($field->getVar('field_type')) {
71
                case 'textbox':
72
                    $valuetypes  = [
73
                        XOBJ_DTYPE_TXTBOX          => _AM_SUICO_TXTBOX,
74
                        XOBJ_DTYPE_EMAIL           => _AM_SUICO_EMAIL,
75
                        XOBJ_DTYPE_INT             => _AM_SUICO_INT,
76
                        XOBJ_DTYPE_FLOAT           => _AM_SUICO_FLOAT,
77
                        XOBJ_DTYPE_DECIMAL         => _AM_SUICO_DECIMAL,
78
                        XOBJ_DTYPE_TXTAREA         => _AM_SUICO_TXTAREA,
79
                        XOBJ_DTYPE_URL             => _AM_SUICO_URL,
80
                        XOBJ_DTYPE_OTHER           => _AM_SUICO_OTHER,
81
                        XOBJ_DTYPE_ARRAY           => _AM_SUICO_ARRAY,
82
                        XOBJ_DTYPE_UNICODE_ARRAY   => _AM_SUICO_UNICODE_ARRAY,
83
                        XOBJ_DTYPE_UNICODE_TXTBOX  => _AM_SUICO_UNICODE_TXTBOX,
84
                        XOBJ_DTYPE_UNICODE_TXTAREA => _AM_SUICO_UNICODE_TXTAREA,
85
                        XOBJ_DTYPE_UNICODE_EMAIL   => _AM_SUICO_UNICODE_EMAIL,
86
                        XOBJ_DTYPE_UNICODE_URL     => _AM_SUICO_UNICODE_URL,
87
                    ];
88
                    $type_select = new XoopsFormSelect(_AM_SUICO_VALUETYPE, 'field_valuetype', $field->getVar('field_valuetype', 'e'));
89
                    $type_select->addOptionArray($valuetypes);
90
                    $this->addElement($type_select);
91
                    break;
92
                case 'select':
93
                case 'radio':
94
                    $valuetypes  = [
95
                        XOBJ_DTYPE_TXTBOX          => _AM_SUICO_TXTBOX,
96
                        XOBJ_DTYPE_EMAIL           => _AM_SUICO_EMAIL,
97
                        XOBJ_DTYPE_INT             => _AM_SUICO_INT,
98
                        XOBJ_DTYPE_FLOAT           => _AM_SUICO_FLOAT,
99
                        XOBJ_DTYPE_DECIMAL         => _AM_SUICO_DECIMAL,
100
                        XOBJ_DTYPE_TXTAREA         => _AM_SUICO_TXTAREA,
101
                        XOBJ_DTYPE_URL             => _AM_SUICO_URL,
102
                        XOBJ_DTYPE_OTHER           => _AM_SUICO_OTHER,
103
                        XOBJ_DTYPE_ARRAY           => _AM_SUICO_ARRAY,
104
                        XOBJ_DTYPE_UNICODE_ARRAY   => _AM_SUICO_UNICODE_ARRAY,
105
                        XOBJ_DTYPE_UNICODE_TXTBOX  => _AM_SUICO_UNICODE_TXTBOX,
106
                        XOBJ_DTYPE_UNICODE_TXTAREA => _AM_SUICO_UNICODE_TXTAREA,
107
                        XOBJ_DTYPE_UNICODE_EMAIL   => _AM_SUICO_UNICODE_EMAIL,
108
                        XOBJ_DTYPE_UNICODE_URL     => _AM_SUICO_UNICODE_URL,
109
                    ];
110
                    $type_select = new XoopsFormSelect(_AM_SUICO_VALUETYPE, 'field_valuetype', $field->getVar('field_valuetype', 'e'));
111
                    $type_select->addOptionArray($valuetypes);
112
                    $this->addElement($type_select);
113
                    break;
114
            }
115
            //$this->addElement(new XoopsFormRadioYN(_AM_SUICO_NOTNULL, 'field_notnull', $field->getVar('field_notnull', 'e') ));
116
            if ('select' === $field->getVar('field_type') || 'select_multi' === $field->getVar('field_type') || 'radio' === $field->getVar('field_type') || 'checkbox' === $field->getVar('field_type')) {
117
                $options = $field->getVar('field_options');
118
                if (count($options) > 0) {
119
                    $remove_options          = new \XoopsFormCheckBox(_AM_SUICO_REMOVEOPTIONS, 'removeOptions');
120
                    $remove_options->columns = 3;
121
                    asort($options);
122
                    foreach (array_keys($options) as $key) {
123
                        $options[$key] .= "[{$key}]";
124
                    }
125
                    $remove_options->addOptionArray($options);
126
                    $this->addElement($remove_options);
127
                }
128
                $option_text = "<table  cellspacing='1'><tr><td class='width20'>" . _AM_SUICO_KEY . '</td><td>' . _AM_SUICO_VALUE . '</td></tr>';
129
                for ($i = 0; $i < 3; ++$i) {
130
                    $option_text .= "<tr><td><input type='text' name='addOption[{$i}][key]' id='addOption[{$i}][key]' size='15' /></td><td><input type='text' name='addOption[{$i}][value]' id='addOption[{$i}][value]' size='35' /></td></tr>";
131
                    $option_text .= "<tr height='3px'><td colspan='2'> </td></tr>";
132
                }
133
                $option_text .= '</table>';
134
                $this->addElement(new XoopsFormLabel(_AM_SUICO_ADDOPTION, $option_text));
135
            }
136
        }
137
        if ($field->getVar('field_edit')) {
138
            switch ($field->getVar('field_type')) {
139
                case 'textbox':
140
                case 'textarea':
141
                case 'dhtml':
142
                    $this->addElement(new XoopsFormText(_AM_SUICO_MAXLENGTH, 'field_maxlength', 35, 35, $field->getVar('field_maxlength', 'e')));
143
                    $this->addElement(new XoopsFormTextArea(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
144
                    break;
145
                case 'checkbox':
146
                case 'select_multi':
147
                    $def_value = null != $field->getVar('field_default', 'e') ? unserialize($field->getVar('field_default', 'n')) : null;
148
                    $element   = new XoopsFormSelect(_AM_SUICO_DEFAULT, 'field_default', $def_value, 8, true);
149
                    $options   = $field->getVar('field_options');
150
                    asort($options);
151
                    // If options do not include an empty element, then add a blank option to prevent any default selection
152
                    //                if (!in_array('', array_keys($options))) {
153
                    if (!array_key_exists('', $options)) {
154
                        $element->addOption('', _NONE);
155
                    }
156
                    $element->addOptionArray($options);
157
                    $this->addElement($element);
158
                    break;
159
                case 'select':
160
                case 'radio':
161
                    $def_value = null != $field->getVar('field_default', 'e') ? $field->getVar('field_default') : null;
162
                    $element   = new XoopsFormSelect(_AM_SUICO_DEFAULT, 'field_default', $def_value);
163
                    $options   = $field->getVar('field_options');
164
                    asort($options);
165
                    // If options do not include an empty element, then add a blank option to prevent any default selection
166
                    //                if (!in_array('', array_keys($options))) {
167
                    if (!array_key_exists('', $options)) {
168
                        $element->addOption('', _NONE);
169
                    }
170
                    $element->addOptionArray($options);
171
                    $this->addElement($element);
172
                    break;
173
                case 'date':
174
                    $this->addElement(new XoopsFormTextDateSelect(_AM_SUICO_DEFAULT, 'field_default', 15, $field->getVar('field_default', 'e')));
175
                    break;
176
                case 'longdate':
177
                    $this->addElement(new XoopsFormTextDateSelect(_AM_SUICO_DEFAULT, 'field_default', 15, strtotime($field->getVar('field_default', 'e'))));
178
                    break;
179
                case 'datetime':
180
                    $this->addElement(new \XoopsFormDateTime(_AM_SUICO_DEFAULT, 'field_default', 15, $field->getVar('field_default', 'e')));
181
                    break;
182
                case 'yesno':
183
                    $this->addElement(new \XoopsFormRadioYN(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
184
                    break;
185
                case 'timezone':
186
                    $this->addElement(new \XoopsFormSelectTimezone(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
187
                    break;
188
                case 'language':
189
                    $this->addElement(new \XoopsFormSelectLang(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
190
                    break;
191
                case 'group':
192
                    $this->addElement(new \XoopsFormSelectGroup(_AM_SUICO_DEFAULT, 'field_default', true, $field->getVar('field_default', 'e')));
193
                    break;
194
                case 'group_multi':
195
                    $this->addElement(new \XoopsFormSelectGroup(_AM_SUICO_DEFAULT, 'field_default', true, unserialize($field->getVar('field_default', 'n')), 5, true));
196
                    break;
197
                case 'theme':
198
                    $this->addElement(new \XoopsFormSelectTheme(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
199
                    break;
200
                case 'autotext':
201
                    $this->addElement(new XoopsFormTextArea(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
202
                    break;
203
            }
204
        }
205
        /* @var \XoopsGroupPermHandler $grouppermHandler */
206
        $grouppermHandler = xoops_getHandler('groupperm');
207
        $searchable_types = [
208
            'textbox',
209
            'select',
210
            'radio',
211
            'yesno',
212
            'date',
213
            'datetime',
214
            'timezone',
215
            'language',
216
        ];
217
        if (in_array($field->getVar('field_type'), $searchable_types)) {
218
            $search_groups = $grouppermHandler->getGroupIds('profile_search', $field->getVar('field_id'), $GLOBALS['xoopsModule']->getVar('mid'));
219
            $this->addElement(new \XoopsFormSelectGroup(_AM_SUICO_PROF_SEARCH, 'profile_search', true, $search_groups, 5, true));
220
        }
221
        if ($field->getVar('field_edit') || $field->isNew()) {
222
            $editable_groups = [];
223
            if (!$field->isNew()) {
224
                //Load groups
225
                $editable_groups = $grouppermHandler->getGroupIds('profile_edit', $field->getVar('field_id'), $GLOBALS['xoopsModule']->getVar('mid'));
226
            }
227
            $this->addElement(new \XoopsFormSelectGroup(_AM_SUICO_PROF_EDITABLE, 'profile_edit', false, $editable_groups, 5, true));
228
            $this->addElement(new \XoopsFormRadioYN(_AM_SUICO_REQUIRED, 'field_required', $field->getVar('field_required', 'e')));
229
            $regstep_select = new XoopsFormSelect(_AM_SUICO_PROF_REGISTER, 'step_id', $field->getVar('step_id', 'e'));
230
            $regstep_select->addOption(0, _NO);
231
            $regstepHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Regstep');
232
            $regstep_select->addOptionArray($regstepHandler->getList());
233
            $this->addElement($regstep_select);
234
        }
235
        $this->addElement(new XoopsFormHidden('op', 'save'));
236
        $this->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
237
    }
238
}
239