This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
FormBuilder.php
208 lines (167 loc) · 5.48 KB
/
FormBuilder.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
<?php
namespace meysampg\formbuilder;
use yii\base\Widget;
use yii\helpers\Inflector;
use yii\base\InvalidConfigException;
class FormBuilder extends Widget
{
/**
* @property string HTML tag for form builder constructor
*/
public $elementType = 'div';
/**
* @property string language of form builder
*/
public $language;
/**
* @property array list of elements for rendering as default elements of form builder
*/
public $data = [];
/**
* @property string indicates that input and output data must be XML or JSON
*/
public $dataType = 'xml';
/**
* @property array list of label strings on a desired language.
*/
public $messages = [];
/**
* @property array list of plugin options, see [FormBuilder Documentations](http://formbuilder.readthedocs.io/en/latest/)
*/
public $options = [];
/**
* @property string JavaScript variable name for accessing to formbuilder contents on development
*/
public $accessVariableName = 'formBuilderJsVariable';
/**
* @property boolean indicates that control buttons be showed or not
*/
public $showActionButtons = false;
private $view;
private $arrayToTypeFunction;
public function init()
{
parent::init();
$this->view = $this->getView();
if ($this->dataType == 'json') {
$this->arrayToTypeFunction = 'arrayJsonEncode';
} elseif ($this->dataType == 'xml') {
$this->arrayToTypeFunction = 'arrayXmlEncode';
} else {
throw new InvalidConfigException('Property $dataType must be "xml" or "json".');
}
}
public function run()
{
FormBuilderAsset::register($this->view);
$this->view->registerJs($this->getFBJs());
return '<' . $this->elementType . ' id="' . $this->fBId . '"></' . $this->elementType . '>';
}
/**
* @return string unique id of form builder
*/
public function getFBId()
{
return $this->id . '-fb-element';
}
/**
* @return string initialize form builder javascript code
*/
private function getFBJs()
{
$str = "var {$this->getFBJsVariableElement()} = $('#{$this->getFBId()}');\n"
. "var {$this->accessVariableName} = $({$this->getFBJsVariableElement()}).formBuilder({$this->getFBOptions()});\n";
return $str;
}
private function getFBJsVariableElement()
{
return lcfirst(Inflector::id2camel($this->getFBId() . '-' . 'variable'));
}
private function getFBOptions()
{
$this->options = array_merge(
[
'dataType' => $this->dataType,
'formData' => $this->{$this->arrayToTypeFunction}($this->data),
'showActionButtons' => $this->showActionButtons,
],
$this->options
);
if ($this->language) {
$this->options['i18n'] = [
'locale' => $this->language,
'preloaded' => [
$this->language => $this->getMessages()
],
];
}
return json_encode($this->options);
}
private function getMessages()
{
$messages = [];
if ($this->language) {
$fileName = __DIR__ . '/messages/' . $this->language . '.php';
if (file_exists($fileName)) {
$messages = require($fileName);
}
}
return array_merge($messages, $this->messages);
}
private function getLanguage()
{
return $this->language ? $this->language : 'en-US';
}
private function arrayJsonEncode($options)
{
return json_encode($options);
}
private function arrayXmlEncode($options, $tag = 'field', $generateHeaderAndFooter = true)
{
$xmlString = '';
if ($generateHeaderAndFooter) {
$xmlString .= "<form-template>"
. "<fields>";
}
if (is_array($options)) {
if (!empty($options)) {
foreach ($options as $key => $option) {
$label = isset($option['label']) ? $option['label'] : '';
$attributes = $this->arrayToAttribute($option);
if (strlen($attributes)) {
$xmlString .= "<$tag"
. $attributes
. ">";
}
if (is_array($option)) {
$xmlString .= $this->arrayXmlEncode($option, Inflector::singularize($key), false);
}
if ('option' == $tag) {
$xmlString .= $label;
}
if (strlen($attributes)) {
$xmlString .= "</$tag>";
}
}
}
} else {
throw new InvalidConfigException('Property $data must be an array.');
}
if ($generateHeaderAndFooter) {
$xmlString .= "</fields>"
. "</form-template>";
}
return $xmlString;
}
private function arrayToAttribute(&$options)
{
$str = '';
foreach ($options as $key => $value) {
if (is_scalar($value)) {
$str .= " $key=" . (is_bool($value) ? "'" . var_export($value, 1) . "'" : var_export($value, 1));
unset($options[$key]);
}
}
return $str;
}
}