forked from wbraganca/yii2-dynamicform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicFormWidget.php
215 lines (197 loc) · 7.39 KB
/
DynamicFormWidget.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
<?php
/**
* @link https://github.com/wbraganca/yii2-dynamicform
* @copyright Copyright (c) 2014 Wanderson Bragança
* @license https://github.com/wbraganca/yii2-dynamicform/blob/master/LICENSE
*/
namespace wbraganca\dynamicform;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\CssSelector\CssSelector;
use yii\helpers\Json;
use yii\helpers\Html;
use yii\base\InvalidConfigException;
/**
* yii2-dynamicform is widget to yii2 framework to clone form elements in a nested manner, maintaining accessibility.
*
* @author Wanderson Bragança <[email protected]>
*/
class DynamicFormWidget extends \yii\base\Widget
{
const HASH_VAR_BASE_NAME = 'dynamicform_';
/**
* @var string
*/
public $widgetContainer;
/**
* @var string
*/
public $widgetBody;
/**
* @var string
*/
public $widgetItem;
/**
* @var string
*/
public $limit = 999;
/**
* @var string
*/
public $insertButton;
/**
* @var string
*/
public $deleteButton;
/**
* @var string 'bottom' or 'top';
*/
public $insertPosition = 'bottom';
/**
* @var Model|ActiveRecord the model used for the form
*/
public $model;
/**
* @var string form ID
*/
public $formId;
/**
* @var array fields to be validated.
*/
public $formFields;
/**
* @var integer
*/
public $min = 1;
/**
* @var string
*/
private $_options;
/**
* @var string
*/
private $_insertPositions = ['bottom', 'top'];
/**
* @var string the hashed global variable name storing the pluginOptions
*/
private $_hashVar;
/**
* Initializes the widget
*
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
parent::init();
if (empty($this->widgetContainer) || (preg_match('/^\w{1,}$/', $this->widgetContainer) === 0)) {
throw new InvalidConfigException('Invalid configuration to property "widgetContainer".
Allowed only alphanumeric characters plus underline: [A-Za-z0-9_]');
}
if (empty($this->widgetBody)) {
throw new InvalidConfigException("The 'widgetBody' property must be set.");
}
if (empty($this->widgetItem)) {
throw new InvalidConfigException("The 'widgetItem' property must be set.");
}
if (empty($this->model) || !$this->model instanceof \yii\base\Model) {
throw new InvalidConfigException("The 'model' property must be set and must extend from '\\yii\\base\\Model'.");
}
if (empty($this->formId)) {
throw new InvalidConfigException("The 'formId' property must be set.");
}
if (empty($this->insertPosition) || ! in_array($this->insertPosition, $this->_insertPositions)) {
throw new InvalidConfigException("Invalid configuration to property 'insertPosition' (allowed values: 'bottom' or 'top')");
}
if (empty($this->formFields) || !is_array($this->formFields)) {
throw new InvalidConfigException("The 'formFields' property must be set.");
}
$this->initOptions();
}
/**
* Initializes the widget options
*/
protected function initOptions()
{
$this->_options['widgetContainer'] = $this->widgetContainer;
$this->_options['widgetBody'] = $this->widgetBody;
$this->_options['widgetItem'] = $this->widgetItem;
$this->_options['limit'] = $this->limit;
$this->_options['insertButton'] = $this->insertButton;
$this->_options['deleteButton'] = $this->deleteButton;
$this->_options['insertPosition'] = $this->insertPosition;
$this->_options['formId'] = $this->formId;
$this->_options['min'] = $this->min;
$this->_options['fields'] = [];
foreach ($this->formFields as $field) {
$this->_options['fields'][] = [
'id' => Html::getInputId($this->model, '[{}]' . $field),
'name' => Html::getInputName($this->model, '[{}]' . $field)
];
}
ob_start();
ob_implicit_flush(false);
}
protected function registerOptions($view)
{
$encOptions = Json::encode($this->_options);
$this->_hashVar = DynamicFormWidget::HASH_VAR_BASE_NAME . hash('crc32', $encOptions);
$view->registerJs("var {$this->_hashVar} = {$encOptions};\n", $view::POS_HEAD);
}
/**
* Registers the needed assets
*/
public function registerAssets()
{
$view = $this->getView();
DynamicFormAsset::register($view);
$options = Json::encode($this->_options);
$this->registerOptions($view);
$js = 'jQuery("#' . $this->formId . '").yiiDynamicForm(' . $this->_hashVar .');' . "\n";
$view->registerJs($js, $view::POS_READY);
// add a click handler for the clone button
$js = 'jQuery("#' . $this->formId . '").on("click", "' . $this->insertButton . '", function(e) {'. "\n";
$js .= " e.preventDefault();\n";
$js .= ' jQuery(".' . $this->widgetContainer . '").triggerHandler("beforeInsert", [jQuery(this)]);' . "\n";
$js .= ' jQuery(".' . $this->widgetContainer . '").yiiDynamicForm("addItem", '. $this->_hashVar . ", e, jQuery(this));\n";
$js .= "});\n";
$view->registerJs($js, $view::POS_READY);
// add a click handler for the remove button
$js = 'jQuery("#' . $this->formId . '").on("click", "' . $this->deleteButton . '", function(e) {'. "\n";
$js .= " e.preventDefault();\n";
$js .= ' jQuery(".' . $this->widgetContainer . '").yiiDynamicForm("deleteItem", '. $this->_hashVar . ", e, jQuery(this));\n";
$js .= "});\n";
$view->registerJs($js, $view::POS_READY);
}
public function run()
{
$content = ob_get_clean();
$crawler = new Crawler();
$crawler->addHTMLContent($content, \Yii::$app->charset);
$results = $crawler->filter($this->widgetItem);
$document = new \DOMDocument('1.0', \Yii::$app->charset);
$document->appendChild($document->importNode($results->first()->getNode(0), true));
$this->_options['template'] = trim($document->saveHTML());
if (isset($this->_options['min']) && $this->_options['min'] === 0 && $this->model->isNewRecord) {
$content = $this->removeItems($content);
}
$this->registerAssets();
echo Html::tag('div', $content, ['class' => $this->widgetContainer, 'data-dynamicform' => $this->_hashVar]);
}
private function removeItems($content)
{
$document = new \DOMDocument('1.0', \Yii::$app->charset);
$crawler = new Crawler();
$crawler->addHTMLContent($content, \Yii::$app->charset);
$root = $document->appendChild($document->createElement('_root'));
$crawler->rewind();
$root->appendChild($document->importNode($crawler->current(), true));
$domxpath = new \DOMXPath($document);
$crawlerInverse = $domxpath->query(CssSelector::toXPath($this->widgetItem));
foreach ($crawlerInverse as $elementToRemove) {
$parent = $elementToRemove->parentNode;
$parent->removeChild($elementToRemove);
}
$crawler->clear();
$crawler->add($document);
return $crawler->filter('body')->eq(0)->html();
}
}