-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormBuilderWidget.php
172 lines (135 loc) · 4.3 KB
/
FormBuilderWidget.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
<?php
/**
* Created by PhpStorm.
* User: joels
* Date: 31/3/17
* Time: 8:54 PM
*/
namespace enigmatix\dynamicforms;
use yii\bootstrap\Modal;
use yii\bootstrap\Tabs;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use Yii;
use yii\helpers\Json;
use yii\helpers\StringHelper;
use yii\helpers\Url;
use enigmatix\formbuilder\FormBuilder;
class FormBuilderWidget extends Modal
{
var $formModel;
var $configurations = [];
var $formBuilderClass = '\\enigmatix\\formbuilder\\FormBuilder';
var $controller = 'dynamic-forms';
var $saveButtonSelector;
var $saveUrl;
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
foreach ($this->getConfigurationData() as $label => $configuration) {
$tabConfig[] = [
'label' => static::getLabel($label),
'content' => call_user_func([$this->formBuilderClass, 'widget'],
$this->prepareOptions($configuration, $this->getFormId() . '-' . $label))
];
}
if(count($tabConfig) > 1){
echo Tabs::widget([
'items' => $tabConfig,
]);
}else{
$form = array_shift($tabConfig);
echo $form['content'];
}
$this->registerCloseButton();
}
public function getFormId(){
return $this->id . '-form';
}
protected function registerCloseButton(){
$this->view->registerJs('
$("'.$this->saveButtonSelector . '").click(function(){$("#'.$this->id . '").modal("toggle")});
');
}
protected function prepareOptions($formConfigModel, $id){
return [
'id' => $id,
'model' => $this->formModel,
'saveButtonSelector' => $this->saveButtonSelector,
'controller' => $this->controller,
'saveUrl' => $this->getSaveUrl($formConfigModel, $this->controller),
'pluginOptions' => [
'formData' => static::getConfigurationJSON($formConfigModel),
'dataType' => 'json',
'showActionButtons' => false,
'disableFields' => [
'paragraph',
'number',
'autocomplete',
'hidden',
'file',
'button'
]
]
];
}
/**
* @param DynamicForm $formConfigModel
* @param $controller
*
* @return mixed|string
*/
public function getSaveUrl($formConfigModel, $controller){
if(is_callable($this->saveUrl)){
return call_user_func($this->saveUrl, [$formConfigModel, $controller]);
}
if($this->saveUrl !== null){
return $this->saveUrl;
}
if($formConfigModel->isNewRecord){
$url[] = '/'.$controller . '/create';
} else{
$url[] = '/'.$controller . '/update';
$url['id'] = $formConfigModel->uuid;
}
$url['form'] = StringHelper::basename($this->formModel->className());
return Url::to($url);
}
protected function getConfigurationData()
{
if($this->configurations != null){
return $this->configurations;
}
$this->configurations = DynamicForm::findModelConfig(
StringHelper::basename($this->formModel->className()),
$this->getCurrentUser());
return $this->configurations;
}
protected function getCurrentUser()
{
return ArrayHelper::getValue(Yii::$app,'user.id');
}
protected function getConfigurationJSON($formDataModel)
{
if ($formDataModel instanceof DynamicForm){
return $formDataModel->form_data;
}
return Json::decode($formDataModel);
}
public static function getlabel($label){
switch($label){
case '0':
return 'Custom';
break;
case '1':
return 'Default';
break;
default:
if(is_numeric($label)){
return 'Custom ' . $label;
}else {
return $label;
}
}
}
}