Completed
Push — cmpsrupd ( a9bb94 )
by Richard
07:12
created

ProfileFieldForm::__construct()   F

Complexity

Conditions 42
Paths 18600

Size

Total Lines 210
Code Lines 155

Duplication

Lines 26
Ratio 12.38 %
Metric Value
dl 26
loc 210
rs 2
cc 42
eloc 155
nc 18600
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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