forked from masihfathi/yii2-drag-drop-forms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormBuilder.php
301 lines (260 loc) · 8.08 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
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
<?php
namespace masihfathi\form;
use Yii;
use yii\base\Widget;
use yii\helpers\{Url, Html, ArrayHelper, Json};
use yii\db\{Query, Exception};
use yii\web\NotFoundHttpException;
use masihfathi\form\{Form, FormBase};
use masihfathi\form\models\FormModel;
class FormBuilder extends Widget {
/**
* @var array preLoaded data of form
*/
public $formData = null;
/**
* @var bool If true FormBuilder set test data on begin
* @since 1.0
*/
public $test_mode = false;
/**
* @var bool If true - only basic options of form and fields
* @since 1.0
*/
public $easy_mode = true;
/**
* @var bool If true - hide options dont need for only generator html|Yii2 code
* @since 1.0
*/
public $generator_mode = false;
/**
* @var bool If true - form can send email response
* @since 1.4
*/
public $email_response = false;
/**
* @var array Configuration data for JavaScript assets
* @since 1.0
*/
public $jsConfig = [];
/**
* @var array configuration data for php render
* @since 1.0
*/
private $options = [];
/**
* @var string DB connections
*/
public $db = 'db';
/**
* @var string The database table storing the forms
*/
public $formTable = 'forms';
/**
* @var string The database table storing the data from forms
*/
public $formDataTable = 'form_';
/**
* @var FormModel Model data
* @since 1.0
*/
public $model;
/**
* @var bool Response from backend to message success
* @since 1.0
*/
public $success = false; // for response
/**
* @var bolean If false - hide button save form
*/
public $hide_button_form_save = false;
/**
* Initializes the object.
* Variables and functions init
* @since 1.0
*
*/
public function init() {
parent::init();
//$this->registerTranslations();
$this->model = new FormModel();
if ($this->formData !== null) {
$this->load($this->formData);
}
$this->options = [
'easy_mode' => $this->easy_mode,
'test_mode' => $this->test_mode,
'generator_mode' => $this->generator_mode,
'email_response' => $this->email_response,
'hide_button_form_save' => $this->hide_button_form_save,
'jsConfig' => $this->jsConfig
];
}
/**
* Executes the widget.
* @since 1.0
* @return void
*/
public function run() {
return $this->formBuilderRender();
}
/**
* Populates the model with input ajax data.
* @since 1.0
* @param object $data Data from request post
* @return null
*/
public function load($form) {
foreach ($form as $key => $value) {
$this->model[$key] = $form[$key];
}
$this->model->author = (isset(Yii::$app->user->identity->id)) ? Yii::$app->user->identity->id : null;
}
/**
* Creates an yii\db\ActiveQueryInterface instance for query purpose.
* @since 1.0
* @param int $id Data from request post
* @return null
*/
public function findModel(int $id) {
$this->model = $this->model->findModel($id);
}
/**
* Saves the current record.
* @since 1.0
* @return array|bool Return message error or true if saved corretly
*/
public function save() {
if (!($this->success = $this->model->save())) {
return $this->success = $this->model->getFirstErrors();
}
return $this->success;
}
/**
* Populates the model with input data.
* @since 1.0
* @see FormBase::tableSchema
* @param string Json form
* @return array Return table shema
*/
public function tableSchema($form_body) {
if (!is_string($form_body)) {
return false;
}
$form_body = Json::decode($form_body);
$schema = FormBase::tableSchema($form_body);
return $schema;
}
/**
* Create table
* Creates a SQL command for creating a new DB table. Execute the SQL statement.
* If table crate table correctly @return array message success
* @since 1.0
* @return object Return json message callback
*/
public function createTable() {
if ($this->success !== true) {
return;
}
$table_name = $this->formDataTable . $this->model->form_id;
$table_schema = $this->tableSchema($this->model->body);
$query = Yii::$app->{$this->db}->createCommand()->createTable($table_name, $table_schema, 'CHARACTER SET utf8 COLLATE utf8_persian_ci');
return $this->execute($query);
}
/**
* Add column
* Creates a SQL command for adding a new DB column and execute.
* @param array $field
* @return object Return json message callback
*/
public function addColumn(array $field) {
if (!isset($field['name'])) {return $this->success = 'empty name';}
$column_name = $field['name'];
$column_type = FormBase::getColumnType($field);
$id = $this->model->form_id;
$query = Yii::$app->{$this->db}->createCommand()->addColumn( $this->formDataTable.$id, $column_name, $column_type );
return $this->execute($query);
}
/**
* Rename column
* Creates a SQL command for renaming a column and execute.
* @since 1.0
* @param array $name Array with old and new name of the column.
* @return object Return json message callback
*/
public function renameColumn(array $name) {
if ( !isset($name['old']) && !isset($name['new']) && $name['old'] === $name['new'] ) {
return $this->success = false;
}
$id = $this->model->form_id;
$query = Yii::$app->db->createCommand()->renameColumn( $this->formDataTable.$id, $name['old'], $name['new']);
return $this->execute($query);
}
/**
* Drop column
* Creates a SQL command for dropping a DB column. and execute.
* @since 1.0
* @param string $column The name of the column to be dropped.
* @return object Return json message callback
*/
public function dropColumn(string $column) {
$id = $this->model->form_id;
$query = Yii::$app->db->createCommand()->dropColumn($this->formDataTable.$id, $column);
return $this->execute($query);
}
/**
* Executes the SQL statement and @return array callback.
* @since 1.0
* @param object $query
* @return object Return json message callback
*/
public function execute($query) {
try {
$query->execute();
return $this->success = true;
} catch (Exception $e) {
return $this->success = $e->getMessage();
}
}
/**
* Function @return array for ajax callback
* @since 1.0
* @param string $format format response
* @return array Return json message callback
*/
public function response(string $format = 'json') {
\Yii::$app->response->format = $format;
$response = ['success' => $this->success];
if ( $this->success === true) {
try {
$response['url'] = Url::to(['user']);
} catch (\yii\base\InvalidParamException $e) {
$response['url'] = '';
}
}
return $response;
}
/**
* Render view
* @since 1.0
* @return void
*/
public function formBuilderRender() {
return $this->render('builder/main', $this->options );
}
/**
* Translates a message to the specified language.
* @since 1.0
* @return null
*/
public function registerTranslations() {
if (!is_object(Yii::$app->i18n->translations['builder'])) {
Yii::$app->i18n->translations['builder'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => '@masihfathi/form/messages',
];
}
}
}
?>