forked from pceuropa/yii2-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm.php
executable file
·386 lines (313 loc) · 9.64 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php namespace pceuropa\forms;
use Yii;
use yii\base\Widget;
use yii\base\DynamicModel;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use pceuropa\forms\FormBase;
use pceuropa\forms\models\FormModel;
use pceuropa\email\Send as SendEmail;
/**
* FormRender: Render form
* Two method render form : php or js (beta)
*
* @author Rafal Marguzewicz <[email protected]>
* @version 1.4
* @license MIT
*
* https://github.com/pceuropa/yii2-forum
* Please report all issues at GitHub
* https://github.com/pceuropa/yii2-forum/issues
*
* Usage example:
* ~~~
* echo \pceuropa\forms\form::widget([
* 'form' => ',{}'
* ]);
*
* echo \pceuropa\forms\form::widget([
* 'formId' => 1,
* ]);
* ~~~
* echo \pceuropa\forms\Form::widget([
* FormBuilder requires Yii 2
* http://www.yiiframework.com
* https://github.com/yiisoft/yii2
*
*/
class Form extends Widget {
/**
* @var int Id of form. If set, widget take data from FormModel.
* @see pceuropa\models\FormModel
*/
public $formId = null;
/**
* @var array|string JSON Object representing the form body
*/
public $body = '{}';
/**
* @var string Type render js|php
* @since 1.0
*/
public $typeRender = 'php';
/**
* Initializes the object.
*
* @return void
* @see Widget
*/
public function init() {
parent::init();
if (is_int($this->formId)) {
$form = FormModel::FindOne($this->formId);
$this->body = $form->body;
}
$this->body = Json::decode($this->body);
}
/**
* Executes the widget.
* @since 1.0
* @return function
*/
public function run() {
if ($this->typeRender === 'js') {
return $this->jsRender($this->body);
}
return $this->phpRender($this->body);
}
/**
* Create form
* Render form by PHP language.
* @param array $form
* @return View Form
*/
public function phpRender($form) {
$data_fields = FormBase::onlyCorrectDataFields($form);
$DynamicModel = new DynamicModel( ArrayHelper::getColumn($data_fields, 'name') );
foreach ($data_fields as $v) {
if (isset($v["name"]) && $v["name"]) {
if (isset($v["require"]) && $v["require"]) {
$DynamicModel->addRule($v["name"], 'required');
}
$DynamicModel->addRule($v["name"], FormBase::ruleType($v) );
}
}
return $this->render('form_php', [
'form_body' => $form,
'model' => $DynamicModel
]);
}
/**
* Create form
* Render form by JavaScript language.
* @param array $form
* @return View
*/
public function jsRender($form) {
return $this->render('form_js', ['form' => $form]);
}
/**
* Select and return function render field
* Render field in view
* @param yii\bootstrap\ActiveForm $form
* @param DynamicModel $model
* @param array $field
* @return string Return field in div HTML
*/
public static function field($form, $model, $field = null) {
$width = $field['width'];
switch ($field['field']) {
case 'input':
$field = self::input($form, $model, $field);
break;
case 'textarea':
$field = self::textarea($form, $model, $field);
break;
case 'radio':
$field = self::radio($form, $model, $field);
break;
case 'checkbox':
$field = self::checkbox($form, $model, $field);
break;
case 'select':
$field = self::select($form, $model, $field);
break;
case 'description':
$field = self::description($field);
break;
case 'submit':
$field = self::submit($field);
break;
default:
$field = '';
break;
}
return self::div($width, $field);
}
/**
* Return HTML div with field
* @param string $width Class bootstrap
* @param string $field
* @return string
*/
public static function div($width, $field) {
return '<div class="'.$width.'">'. $field .'</div>';
}
/**
* Title description
* @param string $arg
* @return void
*/
public function mergeOptions($options, $field) {
foreach (['placeholder', 'value', 'id', 'class'] as $key => $value) {
if (isset($field[$value])) {
$options[$value] = $field[$value];
}
}
}
/**
* Renders an input tag
* @param yii\bootstrap\ActiveForm $form
* @param DynamicModel $model
* @param array $_data
* @return this The field object itself.
*/
public function input($form, $model, $field) {
$options = [];
if (!isset($field['name'])) return;
foreach (['placeholder', 'value', 'id', 'class'] as $key => $value) {
if (isset($field[$value])) {
$options[$value] = $field[$value];
}
}
$input = $form->field($model, $field['name'] )->input($field['type'], $options);
$input->label($field['label'] ?? false);
return $input;
}
/**
* Renders a text area.
* @param yii\bootstrap\ActiveForm $form
* @param DynamicModel $model
* @param array $field
* @return $this The field object itself.
*/
public static function textarea($form, $model, $field) {
$options = [];
$template ="{label}\n{input}\n{hint}\n{error}";
if (!isset($field['name'])) return;
foreach (['placeholder', 'value', 'id', 'class'] as $key => $value) {
if (isset($field[$value])) {
$options[$value] = $field[$value];
}
}
$text_area = $form->field($model, $field['name'])->textArea($options);
$text_area->label($field['label'] ?? false);
return $text_area;
}
/**
* Renders a list of radio buttons.
* @param yii\bootstrap\ActiveForm $form
* @param DynamicModel $model
* @param array $field
* @return $this The field object itself.
*/
public static function radio($form, $model, $field) {
$items = [];
$checked = [];
foreach ($field['items'] as $key => $value) {
$items[$value['value']] = $value['text'];
if (isset( $value['checked'] )) {
$checked[] = $key+1;
}
}
$model-> {$field['name']} = $checked;
$radio_list = $form->field($model, $field['name'])->radioList($items);
$label = (isset($field['label'])) ? $field['label'] : '';
$radio_list->label($label, ['class' => 'bold']);
return $radio_list;
}
/**
* Renders a list of checkboxes.
* @param yii\bootstrap\ActiveForm $form
* @param DynamicModel $model
* @param array $field
* @return $this The field object itself.
*/
public static function checkbox($form, $model, $field) {
$items = [];
$checked = [];
foreach ($field['items'] as $key => $value) {
$items[$value['value']] = $value['text'];
if (isset($value['checked'])) {
$checked[] = $key+1;
}
}
$items = ArrayHelper::map($field['items'], 'value', 'text');
$model-> {$field['name']} = $checked;
$checkbox_list = $form->field($model, $field['name'])->checkboxList($items);
$label = (isset($field['label'])) ? $field['label'] : '';
$checkbox_list->label($label);
return $checkbox_list;
}
/**
* Renders a drop-down list.
* @param yii\bootstrap\ActiveForm $form
* @param DynamicModel $model
* @param array $field
* @return $this The field object itself.
*/
public static function select($form, $model, $field) {
if (ArrayHelper::keyExists('name', $field) ) {
$items = [];
$checked = [];
foreach ($field['items'] as $key => $value) {
$items[$value['value']] = $value['text'];
if (isset($value['checked'])) {
$checked[] = $key+1;
}
}
$model-> {$field['name']} = $checked;
$select = $form->field($model, $field['name'])->dropDownList($items);
$label = (isset($field['label'])) ? $field['label'] : '';
$select->label($label);
return $select;
}
}
/**
* Renders a description html.
* @param array $v
* @return string
*/
public static function description($v) {
return $v['textdescription'];
}
/**
* Renders a submit buton tag.
* @param array $data
* @return string The generated submit button tag
*/
public static function submit($data) {
return Html::submitButton($data['label'], ['class' => 'btn '.$data['backgroundcolor'] ]);
}
/**
* Send email
* @param string $emailSender
* @param string $to
* @param string $subject
* @param string $response
* @return void
*/
public function sendEmail($emailSender = null, $to = null, $subject = null, $response = null) {
if (!is_string($emailSender) && !is_string($to)) {
return;
}
SendEmail::widget([
'from' => $emailSender,
'to' => $to,
'subject' => $subject,
'textBody' => $response,
]);
}
}
?>