FormelementsHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php declare(strict_types=1);
2
3
4
namespace XoopsModules\Wgevents\Forms;
5
6
/*
7
 You may not change or alter any portion of this comment or credits
8
 of supporting developers from this source code or any supporting source code
9
 which is considered copyrighted (c) material of the original comment or credit authors.
10
11
 This program is distributed in the hope that it will be useful,
12
 but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
*/
15
16
/**
17
 * wgEvents module for xoops
18
 *
19
 * @copyright    2021 XOOPS Project (https://xoops.org)
20
 * @license      GPL 2.0 or later
21
 * @package      wgevents
22
 * @since        1.0.0
23
 * @min_xoops    2.5.11 Beta1
24
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
25
 */
26
27
use XoopsModules\Wgevents;
28
use XoopsModules\Wgevents\{
29
    Constants,
30
    Helper
31
};
32
33
34
/**
35
 * Class Object Handler Question
36
 */
37
class FormelementsHandler extends \XoopsPersistableObjectHandler
38
{
39
    /**
40
     * @var int
41
     */
42
    public $type = 0;
43
44
    /**
45
     * @var string
46
     */
47
    public $caption = '';
48
49
    /**
50
     * @var string
51
     */
52
    public $desc = '';
53
54
    /**
55
     * @var string
56
     */
57
    public $name = '';
58
59
    /**
60
     * @var mixed
61
     */
62
    public $value;
63
64
    /**
65
     * @var array
66
     */
67
    public $optionsArr = [];
68
69
    /**
70
     * @var string
71
     */
72
    public $optionsText = '';
73
74
    /**
75
     * @var string
76
     */
77
    public $placeholder = '';
78
79
    /**
80
     * @var int
81
     */
82
    public $size = 20;
83
84
    /**
85
     * @var int
86
     */
87
    public $maxlength = 150;
88
89
    /**
90
     * @var int
91
     */
92
    public $rows = 4;
93
94
    /**
95
     * @var int
96
     */
97
    public $cols = 30;
98
99
    /**
100
     * Constructor
101
     */
102
    public function __construct()
103
    {
104
    }
105
106
    /**
107
     * @return void|\XoopsFormCheckBox|\XoopsFormDateTime|\XoopsFormEditor|\XoopsFormLabel|\XoopsFormRadio|\XoopsFormRadioYN|\XoopsFormSelect|\XoopsFormSelectCountry|\XoopsFormText|\XoopsFormTextArea|\XoopsFormTextDateSelect
108
     */
109
    public function render()
110
    {
111
        switch ($this->type) {
112
            case Constants::FIELD_LABEL:
113
                $field = new \XoopsFormLabel($this->caption, $this->value);
114
                break;
115
            case Constants::FIELD_TEXTBOX:
116
            case Constants::FIELD_NAME:
117
            case Constants::FIELD_EMAIL:
118
                $field = new \XoopsFormText($this->caption, $this->name, $this->size, $this->maxlength, $this->value);
119
                $field->setExtra('placeholder="' . $this->placeholder . '"');
120
                $field->setDescription($this->desc);
121
                break;
122
            case Constants::FIELD_TEXTAREA:
123
                $field = new \XoopsFormTextArea($this->caption, $this->name, $this->value, $this->rows, $this->cols);
124
                $field->setDescription($this->desc);
125
                break;
126
            case Constants::FIELD_TEXTEDITOR:
127
                $editorConfigs = [];
128
                $helper  = Helper::getInstance();
129
                $editor = $helper->getConfig('editor_user');
130
                $editorConfigs['name'] = $this->name;
131
                $editorConfigs['value'] = $this->value;
132
                $editorConfigs['rows'] = $this->rows;
133
                $editorConfigs['cols'] = $this->cols;
134
                $editorConfigs['width'] = '100%';
135
                $editorConfigs['height'] = '400px';
136
                $editorConfigs['editor'] = $editor;
137
                $field = new \XoopsFormEditor($this->caption, $this->name, $editorConfigs);
138
                $field->setDescription($this->desc);
139
                break;
140
            case Constants::FIELD_RADIO:
141
                $field = new \XoopsFormRadio($this->caption, $this->name, $this->value);
142
                $field->addOptionArray($this->optionsArr);
143
                $field->setDescription($this->desc);
144
                break;
145
            case Constants::FIELD_RADIOYN:
146
                $field = new \XoopsFormRadioYN($this->caption, $this->name, $this->value);
147
                $field->setDescription($this->desc);
148
                break;
149
            case Constants::FIELD_SELECTBOX:
150
                $field = new \XoopsFormSelect($this->caption, $this->name, $this->value);
151
                $field->addOptionArray($this->optionsArr);
152
                $field->setDescription($this->desc);
153
                break;
154
            case Constants::FIELD_COMBOBOX:
155
                $field = new \XoopsFormSelect($this->caption, $this->name, $this->value, 5, true);
156
                $field->addOptionArray($this->optionsArr);
157
                $field->setDescription($this->desc);
158
                break;
159
            case Constants::FIELD_DATE:
160
                $field = new \XoopsFormTextDateSelect($this->caption, $this->name, '', $this->value);
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type integer expected by parameter $size of XoopsFormTextDateSelect::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
                $field = new \XoopsFormTextDateSelect($this->caption, $this->name, /** @scrutinizer ignore-type */ '', $this->value);
Loading history...
161
                $field->setDescription($this->desc);
162
                break;
163
            case Constants::FIELD_DATETIME:
164
                $field = new \XoopsFormDateTime($this->caption, $this->name, '', $this->value);
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type integer expected by parameter $size of XoopsFormDateTime::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

164
                $field = new \XoopsFormDateTime($this->caption, $this->name, /** @scrutinizer ignore-type */ '', $this->value);
Loading history...
165
                $field->setDescription($this->desc);
166
                break;
167
            case Constants::FIELD_CHECKBOX:
168
                $field = new \XoopsFormCheckBox($this->caption, $this->name, $this->value);
169
                if (\count($this->optionsArr) > 0) {
170
                    $field->addOptionArray($this->optionsArr);
171
                } else {
172
                    $field->addOption(1, $this->optionsText);
173
                }
174
                $field->setDescription($this->desc);
175
                break;
176
            case Constants::FIELD_COUNTRY:
177
                $field = new \XoopsFormSelectCountry($this->caption, $this->name, $this->value);
178
                $field->setDescription($this->desc);
179
                break;
180
            case 0:
181
            default:
182
                echo 'Error: invalid type in Formelementshandler/render';
183
                die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
184
        }
185
186
        return $field;
187
    }
188
189
    /**
190
     * Returns an array representation of the object
191
     *
192
     * @return array
193
     */
194
    public function getElementsCollection()
195
    {
196
        $ret = [];
197
        $ret[Constants::FIELD_LABEL]      = \_MA_WGEVENTS_FIELD_LABEL;
198
        $ret[Constants::FIELD_TEXTBOX]    = \_MA_WGEVENTS_FIELD_TEXTBOX;
199
        $ret[Constants::FIELD_TEXTAREA]   = \_MA_WGEVENTS_FIELD_TEXTAREA;
200
        $ret[Constants::FIELD_TEXTEDITOR] = \_MA_WGEVENTS_FIELD_TEXTEDITOR;
201
        $ret[Constants::FIELD_RADIO]      = \_MA_WGEVENTS_FIELD_RADIO;
202
        $ret[Constants::FIELD_RADIOYN]    = \_MA_WGEVENTS_FIELD_RADIOYN;
203
        $ret[Constants::FIELD_SELECTBOX]  = \_MA_WGEVENTS_FIELD_SELECTBOX;
204
        $ret[Constants::FIELD_COMBOBOX]   = \_MA_WGEVENTS_FIELD_COMBOBOX;
205
        $ret[Constants::FIELD_CHECKBOX]   = \_MA_WGEVENTS_FIELD_CHECKBOX;
206
        $ret[Constants::FIELD_DATE]       = \_MA_WGEVENTS_FIELD_DATE;
207
        //$ret[Constants::FIELD_DATETIME]  = \_MA_WGEVENTS_FIELD_DATETIME;
208
        $ret[Constants::FIELD_NAME]       = \_MA_WGEVENTS_FIELD_NAME;
209
        $ret[Constants::FIELD_EMAIL]      = \_MA_WGEVENTS_FIELD_EMAIL;
210
        $ret[Constants::FIELD_COUNTRY]    = \_MA_WGEVENTS_FIELD_COUNTRY;
211
212
        return $ret;
213
    }
214
}
215