/
ActiveForm.php
294 lines (236 loc) · 10.2 KB
/
ActiveForm.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
<?php
namespace x1\knockout;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;
use yii\web\JsExpression;
use yii\web\View;
use yii\helpers\Url;
class ActiveForm extends \yii\base\Widget {
const DATE = 'date';
const DECIMAL_SEPARATOR = 'decimalSeparator';
const THOUSANDS_SEPARATOR = 'thousandsSeparator';
const DECIMALS = 'decimals';
public static $autoIdPrefix = 'knockoutActiveForm';
public $fieldClass = 'x1\knockout\FormField';
public $fieldConfig = [];
public $action = null;
public $namespace = 'mapping';
public $structure = [];
public $structurePath = [];
public $element = null;
public $data = null;
public $errors = null;
public $options = null;
public $encodeErrorSummary = true;
public $errorSummaryCssClass = 'error-summary';
public $defaults = [
// self::DATE => 'YYYY-MM-DD',
// self::DECIMAL_SEPARATOR => '.',
// self::THOUSANDS_SEPARATOR => ',',
// self::DECIMALS => 2,
];
public $method = 'POST';
public $viewModelName = 'viewModel';
public $validateOnSubmit = true;
public $bind = true;
private $relationTag = [];
public static function begin($config = [])
{
$defaults = ArrayHelper::remove($config, 'defaults', []);
$w = parent::begin($config);
$w->defaults = ArrayHelper::merge($w->defaults, $defaults);
$view = $w->getView();
echo Html::beginForm($w->action, $w->method, ArrayHelper::merge(ArrayHelper::getValue($config, 'options', []), ['id' => $w->id, 'method' => 'POST', 'enctype' => 'multipart/form-data']));
KnockoutAsset::register($view);
// client side validate on submit
if ($w->validateOnSubmit) {
$view->registerJs(sprintf(<<<EOD
$('#%1\$s').submit(function(e) {
var vm = ko.dataFor(e.target);
if (vm && vm.validate) {
vm.validate();
}
if (vm && vm.isValid && vm.isValid()) {
return true;
} else {
e.preventDefault();
return false;
}
})
EOD
, $w->id, Json::encode($w->defaults)), View::POS_END);
}
// $view->registerJs(sprintf(<<<EOD
// var %1\$s = $.extend({}, x1.config, %2\$s);
// EOD
// , lcfirst(\yii\helpers\Inflector::camelize($w->id)), Json::encode($w->defaults)));
return $w;
}
public static function end() {
$w = self::$stack[count(self::$stack) - 1];
$view = $w->getView();
if ($c = count($w->structurePath) > 0) {
throw new \yii\base\Exception(sprintf("%d unclosed relations", $c));
}
echo Html::endForm();
$w->createMappings();
// if we don't have any data, initialize empty attributes, otherwise attributes are not created by mapping
if (empty($w->data)) {
foreach ($w->structure as $key => $value) {
foreach ($value as $key2 => $value2) {
$w->data[$key2] = null;
}
}
}
if ($w->bind) {
$w->bind($w->data, $w->errors);
}
parent::end();
}
public static function &arrayPath(&$array, $path) {
$offset =& $array;
if ($path) foreach ($path as $index) {
$offset =& $offset[$index];
}
return $offset;
}
public function field($model, $attribute, $options = []) {
$config = $this->fieldConfig;
if ($config instanceof \Closure) {
$config = call_user_func($config, $model, $attribute);
}
if (!isset($config['class'])) {
$config['class'] = $this->fieldClass;
}
return Yii::createObject(ArrayHelper::merge($config, ['inputOptions' => ['class' => 'form-control']], $options, [
'model' => $model,
'attribute' => $attribute,
'form' => $this,
]));
}
public function init() {
parent::init();
if ($this->action == null)
$this->action = Url::current();
}
public function createMappings() {
$mapping = [];
// $mapping['prototype'] = new JsExpression($this->render('active-form/fn-prototype'));
$mapping['prototype'] = new JsExpression('ko.proto');
// generate create function for 1st level vs prototype
$models = [];
foreach ($this->structure as $key => $value) {
$models[] = sprintf('ko.mapping.fromJS(options.data, %s.%s, self);', $this->namespace, strtolower($key));
}
$mapping['create'] = new JsExpression(sprintf('function(options) {
var self = new %1$s.prototype(options);
%3$s
if (self.init) { self.init(%1$s._arrays); }
if (%1$s.%2$s) {
self = %1$s.%2$s(self);
}
return self;
}', $this->namespace, $this->viewModelName, implode('', $models)));
$mapping['_defaults'] = new JsExpression(Json::encode($this->defaults));
$this->walkLevel($this->structure, [], $mapping);
$this->view->registerJs(sprintf('var %s=%s;', $this->namespace, Json::encode($mapping)), View::POS_END);
}
public function walkLevel($structure, $path = [], &$mapping = []) {
// walk through models
foreach ($structure as $key => $value) {
$key = strtolower($key);
$path[] = $key;
$m = & $mapping[$key];
$relations = ArrayHelper::remove($value, '_relations', []);
$arrays = ArrayHelper::remove($value, '_arrays', []);
foreach ($value as $key => $v2) {
$m[$key]['create'] = new JsExpression(sprintf('function(options) { return ko.observable(options.data).extend(%s); }', Json::encode($v2)));
}
// walk through relations
if (!empty($relations)) {
foreach ($relations as $relation => &$level) {
$path[] = $relation;
$p = $path;
$firstModel = key(reset($relations));
$p[] = strtolower($firstModel);
$subrelations = isset($level[$firstModel]['_relations']) ? array_keys($level[$firstModel]['_relations']) : [];
$m['_arrays'] = new JsExpression(Json::encode($arrays));
$m[$relation]['create'] = new JsExpression(sprintf('function(options) {
var self = new %1$s.prototype(options);
var x = ko.mapping.fromJS(options.data, %1$s.%3$s, self);
if (self.init) { self.init(%1$s.%6$s._arrays); }
if (%1$s.%4$s.%2$s) {
self = %1$s.%4$s.%2$s(self);
}
return self;
}', $this->namespace, $this->viewModelName, implode('.', $p), implode('.', $path), Json::encode($subrelations), implode('.', array_slice($path, 0, count($path) - 1))));
$this->walkLevel($level, $path, $m[$relation]);
array_pop($path);
}
}
array_pop($path);
}
}
public function bind($data = null, $errors = null) {
$this->view->registerJs(sprintf('
(function(ko, data, namespace, element, errors) {
if (element !== undefined && element !== null && element !== \'\') {
ko.applyBindings(vm = ko.mapping.fromJS(data, namespace), document.getElementById(element));
} else {
ko.applyBindings(vm = ko.mapping.fromJS(data, namespace));
}
if (errors !== null) {
vm.setErrors(errors);
}
})(ko, %1$s, %2$s, "%3$s", %4$s)
', Json::encode($data), $this->namespace, ($this->element == null) ? $this->id : $this->element, Json::encode($errors)), View::POS_READY);
}
public function beginRelation($model, $relation, $tag = null, $options = []) {
$rel = $model->getRelation($relation);
$this->structurePath[] = ['formName' => $model->formName(), 'relation' => $relation, 'multiple' => $rel->multiple];
if ($tag === null) {
$tag = $rel->multiple ? $tag = 'tbody' : 'div';
}
$path = [];
foreach ($this->structurePath as $key => $value) {
$path[] = $value['formName'];
$path[] = '_relations';
$path[] = $value['relation'];
}
$structure =& $this->arrayPath($this->structure, $path);
if ($rel !== null && $rel->multiple) {
array_pop($path);
array_pop($path);
$path[] = '_arrays';
$structure =& $this->arrayPath($this->structure, $path);
$structure[] = $relation;
}
$options['data-bind'] = sprintf('%s: %s', ($rel->multiple) ? 'foreach' : 'with', $relation);
$this->relationTag[] = $tag;
if ($tag !== false)
return Html::beginTag($tag, $options);
else
return null;
return Html::beginTag('div', ['data-bind' => sprintf('%s: %s', ($rel->multiple) ? 'foreach' : 'with', $relation)]);
}
public function endRelation() {
array_pop($this->structurePath);
$tag = array_pop($this->relationTag);
if ($tag !== false)
return Html::endTag($tag);
else
return null;
}
public function errorSummary($models, $options = []) {
Html::addCssClass($options, $this->errorSummaryCssClass);
$options['encode'] = $this->encodeErrorSummary;
return Html::errorSummary($models, $options);
}
public function submitButton($options = []) {
$text = ArrayHelper::getValue($options, 'text', Yii::t('app', 'Submit'));
return Html::submitButton('<i class="glyphicon" data-bind="css: {\'glyphicon-ok\': !hasError(), \'glyphicon-warning-sign\': hasError(), }"></i> ' . $text, ['class' => 'btn btn-primary', 'data-bind' => 'css: { \'btn-danger\': hasError() }, enable: !hasError()']);
}
}
?>