Completed
Pull Request — master (#462)
by Richard
11:38
created

AwesomeButton   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1
Metric Value
wmc 1
lcom 0
cbo 1
dl 0
loc 14
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
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\Form\ElementFactory;
13
use Xoops\Form\ThemeForm;
14
15
/**
16
 * @copyright       XOOPS Project (http://xoops.org)
17
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
18
 * @author          trabis <[email protected]>
19
 */
20
21
include dirname(dirname(__DIR__)) . '/mainfile.php';
22
23
$xoops = Xoops::getInstance();
24
$xoops->header();
25
26
$form = new ThemeForm('Extended Form Element Definitions Example', 'example', '', 'post', true, 'horizontal');
27
28
// establish factory, and set it to add elements directly to our form by default
29
$factory = new ElementFactory();
30
$factory->setContainer($form);
31
32
$factory->create([
33
    ElementFactory::CLASS_KEY => 'Text',
34
    'caption' => 'Code',
35
    'name' => 'code',
36
    'value' => '',
37
    'placeholder' => 'Code...',
38
    'description' => 'Description code',
39
    'pattern' => '^.{3,}$',
40
    ':pattern_description' => 'You need at least 3 characters',
41
    'datalist' => ['Alfa', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot'],
42
    'required' => null,
43
]);
44
45
$factory->create([
46
    ElementFactory::CLASS_KEY => 'Text',
47
    'caption' => 'Password',
48
    'name' => 'password',
49
    'value' => '',
50
    'placeholder' => 'Enter Password',
51
    'description' => 'Description password',
52
    'pattern' => '^.{8,}$',
53
    ':pattern_description' => 'You need at least 8 characters',
54
    'autocomplete' => 'off',
55
    'required' => null,
56
]);
57
58
$factory->create([
59
    ElementFactory::CLASS_KEY => 'TextArea',
60
    'caption' => 'Description',
61
    'name' => 'description',
62
    'value' => '',
63
    'rows' => 5,
64
    'cols' => 64,
65
    'maxlength' => 4096,
66
    'placeholder' => 'Enter the description',
67
    'description' => 'Your description',
68
    'required' => null,
69
]);
70
71
$factory->create([
72
    ElementFactory::CLASS_KEY => 'Radio',
73
    'caption' => 'Radio Inline',
74
    'name' => 'radio_inline',
75
    'value' => 1,
76
    'description' => 'Description of radio',
77
    'option' => [
78
        1 => 'Radio 1',
79
        2 => 'Radio 2',
80
        3 => 'Radio 3',
81
    ],
82
    ':inline' => true,
83
]);
84
85
$factory->create([
86
    ElementFactory::CLASS_KEY => 'Checkbox',
87
    'caption' => 'Checkbox',
88
    'name' => 'checkbox',
89
    'value' => [1, 3],
90
    'description' => 'Description of Checkbox',
91
    'option' => [
92
        1 => 'Checkbox 1',
93
        2 => 'Checkbox 2',
94
        3 => 'Checkbox 3',
95
    ],
96
]);
97
98
$factory->create([
99
    ElementFactory::CLASS_KEY => 'Select',
100
    'caption' => 'Select',
101
    'name' => 'select',
102
    'value' => 1,
103
    'description' => 'Description of Select',
104
    'option' => [
105
        1 => 'Select 1',
106
        2 => 'Select 2',
107
        3 => 'Select 3',
108
    ],
109
]);
110
111
$factory->create([
112
    ElementFactory::CLASS_KEY => 'Select',
113
    'caption' => 'Select Optgroup',
114
    'name' => 'select_optgroup',
115
    'value' => 1,
116
    'description' => 'Description of Select Optgroup',
117
    'option' => [
118
        'Switzerland' => [1 => 'Geneva', 2 => 'Bern', 3 => 'Zurich'],
119
        'France' => [4 => 'Paris', 5 => 'Lyon', 6 => 'Grenoble', 7 => 'Marseille'],
120
    ],
121
]);
122
123
$factory->create([
124
    ElementFactory::CLASS_KEY => 'DateTime',
125
    'caption' => 'Date time',
126
    'name' => 'date_time',
127
    'description' => 'Description Date time',
128
    ':minuteinterval' => 30,
129
]);
130
131
$testTray = $factory->create([
132
    ElementFactory::CLASS_KEY => 'ElementTray',
133
    'caption' => 'Select Tray',
134
    ':joiner' => '',
135
]);
136
137
$factory->create([
138
    ElementFactory::FORM_KEY => $testTray, // override the default, and add to tray instead
139
    ElementFactory::CLASS_KEY => 'Select',
140
    'caption' => 'Category',
141
    'name' => 'select_cat',
142
    'size' => 5,
143
    'value' => 1,
144
    'option' => [
145
        1 => 'Category 1',
146
        2 => 'Category 2',
147
        3 => 'Category 3',
148
        4 => 'Category 4',
149
        5 => 'Category 5',
150
        6 => 'Category 6',
151
        7 => 'Category 7',
152
        8 => 'Category 8',
153
    ],
154
]);
155
156
$factory->create([
157
    ElementFactory::FORM_KEY => $testTray,
158
    ElementFactory::CLASS_KEY => 'Select',
159
    'caption' => 'Sub Cat',
160
    'name' => 'select_subcat',
161
    'size' => 5,
162
    'value' => [2,4,6],
163
    'option' => [
164
        1 => 'Sub 1',
165
        2 => 'Sub 2',
166
        3 => 'Sub 3',
167
        4 => 'Sub 4',
168
        5 => 'Sub 5',
169
        6 => 'Sub 6',
170
        7 => 'Sub 7',
171
        8 => 'Sub 8',
172
    ],
173
    'multiple' => null,
174
]);
175
176
$factory->create([
177
    ElementFactory::CLASS_KEY => 'Button',
178
    'caption' => '',
179
    'name' => 'submit_button',
180
    'type' => 'submit',
181
    'value' => XoopsLocale::A_SUBMIT,
182
]);
183
184
$factory->create([
185
    ElementFactory::CLASS_KEY => 'Button',
186
    'caption' => '',
187
    'name' => 'reset_button',
188
    'type' => 'reset',
189
    'value' => XoopsLocale::A_RESET,
190
    'class' => 'btn btn-danger',
191
    'onclick' => 'return confirm("Are you sure?");',
192
]);
193
194
$factory->create([
195
    ElementFactory::CLASS_KEY => 'ButtonTray',
196
    'caption' => '',
197
    'name' => 'button_tray',
198
    'type' => 'submit',
199
    'class' => 'btn btn-inverse',
200
    'value' => XoopsLocale::A_SUBMIT,
201
    ':showdelete' => true,
202
]);
203
204
205
// example custom element class
206
class AwesomeButton extends Xoops\Form\Button
207
{
208
    /**
209
     * @param array $attributes array of all attributes
210
     */
211
    public function __construct($attributes)
212
    {
213
        parent::__construct($attributes);
214
        $this->set('type', 'button');
215
        $this->setIfNotSet('name', 'awesome');
216
        $this->setIfNotSet('value', 'Awesome!');
217
        $this->add('class', 'btn btn-large btn-success');
218
    }
219
}
220
221
// use the custom element class
222
$factory->create([
223
    ElementFactory::CLASS_KEY => '\AwesomeButton', // Use fully qualified name for class
224
    'caption' => 'A custom form Element',
225
    'onclick' => 'alert("Awesome!");',
226
]);
227
228
$form->display();
229
230
\Xoops\Utils::dumpFile(__FILE__);
231
$xoops->footer();
232