Completed
Push — tp71 ( 8607f4 )
by Richard
06:09
created

ProfileFieldForm   C

Complexity

Total Complexity 42

Size/Duplication

Total Lines 224
Duplicated Lines 15.18 %

Coupling/Cohesion

Components 0
Dependencies 17

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 34
loc 224
rs 6.5405
wmc 42
lcom 0
cbo 17

1 Method

Rating   Name   Duplication   Size   Complexity  
F __construct() 34 218 42

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ProfileFieldForm 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 ProfileFieldForm, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
use Xoops\Core\Kernel\Dtype;
13
14
/**
15
 * @copyright       2000-2016 XOOPS Project (http://xoops.org)
16
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
17
 * @author          trabis <[email protected]>
18
 */
19
20
class ProfileFieldForm extends Xoops\Form\ThemeForm
21
{
22
    /**
23
     * @param ProfileField|XoopsObject $obj
24
     */
25
    public function __construct(ProfileField $obj)
26
    {
27
        $xoops = Xoops::getInstance();
28
29
        $title = $obj->isNew() ? sprintf(_PROFILE_AM_ADD, _PROFILE_AM_FIELD)
30
                : sprintf(_PROFILE_AM_EDIT, _PROFILE_AM_FIELD);
31
32
        parent::__construct($title, 'form', '', 'post', true);
33
34
        $this->addElement(new Xoops\Form\Text(_PROFILE_AM_TITLE, 'field_title', 5, 255, $obj->getVar('field_title', 'e')), true);
35
        $this->addElement(new Xoops\Form\TextArea(_PROFILE_AM_DESCRIPTION, 'field_description', $obj->getVar('field_description', 'e'), 5, 5));
36
37
        if (!$obj->isNew()) {
38
            $fieldcat_id = $obj->getVar('cat_id');
39
        } else {
40
            $fieldcat_id = 0;
41
        }
42
        $category_handler = \Xoops::getModuleHelper('profile')->getHandler('category');
43
        $cat_select = new Xoops\Form\Select(_PROFILE_AM_CATEGORY, 'field_category', $fieldcat_id);
44
        $cat_select->addOption(0, _PROFILE_AM_DEFAULT);
45
        $cat_select->addOptionArray($category_handler->getList());
46
        $this->addElement($cat_select);
47
        $weight = new Xoops\Form\Text(_PROFILE_AM_WEIGHT, 'field_weight', 1, 10, $obj->getVar('field_weight', 'e'), '');
48
        $weight->setPattern('^\d+$', _PROFILE_AM_ERROR_WEIGHT);
49
        $this->addElement($weight, true);
50
        if ($obj->getVar('field_config') || $obj->isNew()) {
51
            if (!$obj->isNew()) {
52
                $this->addElement(new Xoops\Form\Label(_PROFILE_AM_NAME, $obj->getVar('field_name')));
53
                $this->addElement(new Xoops\Form\Hidden('id', $obj->getVar('field_id')));
54
            } else {
55
                $this->addElement(new Xoops\Form\Text(_PROFILE_AM_NAME, 'field_name', 5, 255, $obj->getVar('field_name', 'e')), true);
56
            }
57
58
            //autotext and theme left out of this one as fields of that type should never be changed (valid assumption, I think)
59
            $fieldtypes = array(
60
                'checkbox' => _PROFILE_AM_CHECKBOX, 'date' => _PROFILE_AM_DATE, 'datetime' => _PROFILE_AM_DATETIME,
61
                'longdate' => _PROFILE_AM_LONGDATE, 'group' => _PROFILE_AM_GROUP,
62
                'group_multi' => _PROFILE_AM_GROUPMULTI, 'language' => _PROFILE_AM_LANGUAGE,
63
                'radio' => _PROFILE_AM_RADIO, 'select' => _PROFILE_AM_SELECT, 'select_multi' => _PROFILE_AM_SELECTMULTI,
64
                'textarea' => _PROFILE_AM_TEXTAREA, 'dhtml' => _PROFILE_AM_DHTMLTEXTAREA,
65
                'textbox' => _PROFILE_AM_TEXTBOX, 'timezone' => _PROFILE_AM_TIMEZONE, 'yesno' => _PROFILE_AM_YESNO
66
            );
67
68
            $element_select = new Xoops\Form\Select(_PROFILE_AM_TYPE, 'field_type', $obj->getVar('field_type', 'e'));
69
            $element_select->addOptionArray($fieldtypes);
70
71
            $this->addElement($element_select);
72
73
            switch ($obj->getVar('field_type')) {
74 View Code Duplication
                case "textbox":
75
                    $valuetypes = array(
76
                        Dtype::TYPE_ARRAY => _PROFILE_AM_ARRAY,
77
                        Dtype::TYPE_EMAIL => _PROFILE_AM_EMAIL,
78
                        Dtype::TYPE_INTEGER => _PROFILE_AM_INT,
79
                        Dtype::TYPE_FLOAT => _PROFILE_AM_FLOAT,
80
                        Dtype::TYPE_DECIMAL => _PROFILE_AM_DECIMAL,
81
                        Dtype::TYPE_TEXT_AREA => _PROFILE_AM_TXTAREA,
82
                        Dtype::TYPE_TEXT_BOX => _PROFILE_AM_TXTBOX,
83
                        Dtype::TYPE__URL => _PROFILE_AM_URL,
84
                        Dtype::TYPE_OTHER => _PROFILE_AM_OTHER
85
                    );
86
87
                    $type_select = new Xoops\Form\Select(_PROFILE_AM_VALUETYPE, 'field_valuetype', $obj->getVar('field_valuetype', 'e'), 5, 5);
88
                    $type_select->addOptionArray($valuetypes);
89
                    $this->addElement($type_select);
90
                    break;
91
92
                case "select":
93 View Code Duplication
                case "radio":
94
                    $valuetypes = array(
95
                        Dtype::TYPE__ARRAY => _PROFILE_AM_ARRAY,
96
                        Dtype::TYPE__EMAIL => _PROFILE_AM_EMAIL,
97
                        Dtype::TYPE_INTEGER => _PROFILE_AM_INT,
98
                        Dtype::TYPE_FLOAT => _PROFILE_AM_FLOAT,
99
                        Dtype::TYPE_DECIMAL => _PROFILE_AM_DECIMAL,
100
                        Dtype::TYPE_TEXT_AREA => _PROFILE_AM_TXTAREA,
101
                        Dtype::TYPE_TEXT_BOX => _PROFILE_AM_TXTBOX,
102
                        Dtype::TYPE_URL => _PROFILE_AM_URL,
103
                        Dtype::TYPE_OTHER => _PROFILE_AM_OTHER
104
                    );
105
106
                    $type_select = new Xoops\Form\Select(_PROFILE_AM_VALUETYPE, 'field_valuetype', $obj->getVar('field_valuetype', 'e'));
107
                    $type_select->addOptionArray($valuetypes);
108
                    $this->addElement($type_select);
109
                    break;
110
            }
111
112
            //$this->addElement(new Xoops\Form\RadioYesNo(_PROFILE_AM_NOTNULL, 'field_notnull', $obj->getVar('field_notnull', 'e') ));
113
114
            if ($obj->getVar('field_type') === "select" || $obj->getVar('field_type') === "select_multi" || $obj->getVar('field_type') === "radio" || $obj->getVar('field_type') === "checkbox") {
115
                $options = $obj->getVar('field_options');
116
                if (count($options) > 0) {
117
                    $remove_options = new Xoops\Form\Checkbox(_PROFILE_AM_REMOVEOPTIONS, 'removeOptions');
118
                    //$remove_options->columns = 3;
119
                    asort($options);
120
                    foreach (array_keys($options) as $key) {
121
                        $options[$key] .= "[{$key}]";
122
                    }
123
                    $remove_options->addOptionArray($options);
124
                    $this->addElement($remove_options);
125
                }
126
127
                $option_text = "<table  cellspacing='1'><tr><td class='width20'>" . _PROFILE_AM_KEY . "</td><td>" . _PROFILE_AM_VALUE . "</td></tr>";
128
                for ($i = 0; $i < 3; ++$i) {
129
                    $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>";
130
                    $option_text .= "<tr height='3px'><td colspan='2'> </td></tr>";
131
                }
132
                $option_text .= "</table>";
133
                $this->addElement(new Xoops\Form\Label(_PROFILE_AM_ADDOPTION, $option_text));
134
            }
135
        }
136
137
        if ($obj->getVar('field_edit')) {
138
            switch ($obj->getVar('field_type')) {
139
                case "textbox":
140
                case "textarea":
141
                case "dhtml":
142
                    $this->addElement(new Xoops\Form\Text(_PROFILE_AM_MAXLENGTH, 'field_maxlength', 5, 5, $obj->getVar('field_maxlength', 'e')));
143
                    $this->addElement(new Xoops\Form\TextArea(_PROFILE_AM_DEFAULT, 'field_default', $obj->getVar('field_default', 'e')));
144
                    break;
145
146
                case "checkbox":
147
                case "select_multi":
148
                    $def_value = $obj->getVar('field_default', 'e') != null
149
                            ? unserialize($obj->getVar('field_default', 'n')) : null;
150
                    $element = new Xoops\Form\Select(_PROFILE_AM_DEFAULT, 'field_default', $def_value, 8, true);
151
                    $options = $obj->getVar('field_options');
152
                    asort($options);
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
                        $element->addOption('', XoopsLocale::NONE);
156
                    }
157
                    $element->addOptionArray($options);
158
                    $this->addElement($element);
159
                    break;
160
161
                case "select":
162
                case "radio":
163
                    $def_value = $obj->getVar('field_default', 'e') != null ? $obj->getVar('field_default') : null;
164
                    $element = new Xoops\Form\Select(_PROFILE_AM_DEFAULT, 'field_default', $def_value);
165
                    $options = $obj->getVar('field_options');
166
                    asort($options);
167
                    // If options do not include an empty element, then add a blank option to prevent any default selection
168
                    if (!in_array('', array_keys($options))) {
169
                        $element->addOption('', XoopsLocale::NONE);
170
                    }
171
                    $element->addOptionArray($options);
172
                    $this->addElement($element);
173
                    break;
174
175
                case "date":
176
                    $this->addElement(new Xoops\Form\DateSelect(_PROFILE_AM_DEFAULT, 'field_default', $obj->getVar('field_default', 'e')));
177
                    break;
178
179
                case "longdate":
180
                    $this->addElement(new Xoops\Form\DateSelect(_PROFILE_AM_DEFAULT, 'field_default', strtotime($obj->getVar('field_default', 'e'))));
181
                    break;
182
183
                case "datetime":
184
                    $this->addElement(new Xoops\Form\DateTime(_PROFILE_AM_DEFAULT, 'field_default', $obj->getVar('field_default', 'e')));
185
                    break;
186
187
                case "yesno":
188
                    $this->addElement(new Xoops\Form\RadioYesNo(_PROFILE_AM_DEFAULT, 'field_default', $obj->getVar('field_default', 'e')));
189
                    break;
190
191
                case "timezone":
192
                    $this->addElement(new Xoops\Form\SelectTimeZone(_PROFILE_AM_DEFAULT, 'field_default', $obj->getVar('field_default', 'e')));
193
                    break;
194
195
                case "language":
196
                    $this->addElement(new Xoops\Form\SelectLanguage(_PROFILE_AM_DEFAULT, 'field_default', $obj->getVar('field_default', 'e')));
197
                    break;
198
199
                case "group":
200
                    $this->addElement(new Xoops\Form\SelectGroup(_PROFILE_AM_DEFAULT, 'field_default', true, $obj->getVar('field_default', 'e')));
201
                    break;
202
203
                case "group_multi":
204
                    $this->addElement(new Xoops\Form\SelectGroup(_PROFILE_AM_DEFAULT, 'field_default', true, unserialize($obj->getVar('field_default', 'n')), 5, true));
205
                    break;
206
207
                case "theme":
208
                    $this->addElement(new Xoops\Form\SelectTheme(_PROFILE_AM_DEFAULT, 'field_default', $obj->getVar('field_default', 'e')));
209
                    break;
210
211
                case "autotext":
212
                    $this->addElement(new Xoops\Form\TextArea(_PROFILE_AM_DEFAULT, 'field_default', $obj->getVar('field_default', 'e')));
213
                    break;
214
            }
215
        }
216
217
        $groupperm_handler = $xoops->getHandlerGroupPermission();
218
        $searchable_types = array(
219
            'textbox', 'select', 'radio', 'yesno', 'date', 'datetime', 'timezone', 'language'
220
        );
221
        if (in_array($obj->getVar('field_type'), $searchable_types)) {
222
            $search_groups = $groupperm_handler->getGroupIds('profile_search', $obj->getVar('field_id'), $xoops->module->getVar('mid'));
223
            $this->addElement(new Xoops\Form\SelectGroup(_PROFILE_AM_PROF_SEARCH, 'profile_search', true, $search_groups, 5, true));
224
        }
225
        if ($obj->getVar('field_edit') || $obj->isNew()) {
226
            if (!$obj->isNew()) {
227
                //Load groups
228
                $editable_groups = $groupperm_handler->getGroupIds('profile_edit', $obj->getVar('field_id'), $xoops->module->getVar('mid'));
229
            } else {
230
                $editable_groups = array();
231
            }
232
            $this->addElement(new Xoops\Form\SelectGroup(_PROFILE_AM_PROF_EDITABLE, 'profile_edit', false, $editable_groups, 5, true));
233
            $this->addElement(new Xoops\Form\RadioYesNo(_PROFILE_AM_REQUIRED, 'field_required', $obj->getVar('field_required', 'e')));
234
            $regstep_select = new Xoops\Form\Select(_PROFILE_AM_PROF_REGISTER, 'step_id', $obj->getVar('step_id', 'e'));
235
            $regstep_select->addOption(0, XoopsLocale::NO);
236
            $regstep_handler = \Xoops::getModuleHelper('profile')->getHandler('regstep');
237
            $regstep_select->addOptionArray($regstep_handler->getList());
238
            $this->addElement($regstep_select);
239
        }
240
        $this->addElement(new Xoops\Form\Hidden('op', 'save'));
241
        $this->addElement(new Xoops\Form\Button('', 'submit', XoopsLocale::A_SUBMIT, 'submit'));
242
    }
243
}
244