c7f97c88
alex
Google recaptcha ...
|
1
2
3
4
|
<?php
namespace frontend\modules\forms;
|
51db39f2
alex
fix2
|
5
|
use yii\base\InvalidConfigException;
|
c7f97c88
alex
Google recaptcha ...
|
6
7
8
|
use yii\helpers\Html;
use yii\web\View;
use yii\widgets\ActiveForm;
|
c7f97c88
alex
Google recaptcha ...
|
9
10
11
|
/**
|
51db39f2
alex
fix2
|
12
|
* Module for generation forms adn saving data from frontend
|
c7f97c88
alex
Google recaptcha ...
|
13
|
*
|
51db39f2
alex
fix2
|
14
|
* @package artbox\core\forms
|
c7f97c88
alex
Google recaptcha ...
|
15
|
*/
|
51db39f2
alex
fix2
|
16
|
class Module extends \yii\base\Module
|
c7f97c88
alex
Google recaptcha ...
|
17
|
{
|
51db39f2
alex
fix2
|
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
|
/**
* ActiveRecord for insert to db
*
* @var null
*/
public $templateForm = '{form}';
public $activeRecord = null;
/**
* attributes which render in form
*
* @var array
*/
public $attributes = [];
/**
* validation rules for DynamicModel
*
* @var array
*/
public $rules = [];
/**
* scenario for ActiveRecord
*
* @var string
*/
public $scenario = 'default';
/**
* Input types for attributes. textinput default
*
* @var array
*/
public $inputOptions = [];
/**
* Labels by form (if ActiveRecord is null)
*
* @var array
*/
public $labels = [];
/**
* Send or not email
*
* @var bool
*/
public $sendEmail = true;
/**
* Email address
*
* @var string
*/
public $email = 'admin@example.com';
/**
* Email template
*
* @var string
*/
public $emailFile = 'mail.php';
/**
* Email subject
*
* @var string
*/
public $subject = '';
/**
* mailer which send email
*
* @var null
*/
public $mailer = null;
/**
* @var \yii\db\ActiveRecord $model ;
*/
public $model = null;
/**
* send form with ajax
*
* @var bool
*/
public $ajax = false;
/**
* callback function for ajax script of send form
*
* @var string
*/
public $successCallback = 'function (data) {
alert("ok");
}';
|
c7f97c88
alex
Google recaptcha ...
|
117
|
|
51db39f2
alex
fix2
|
118
119
120
121
122
123
124
125
|
/**
* error function for ajax script of send form
*
* @var string
*/
public $errorCallback = 'function (data) {
alert("error");
}';
|
c7f97c88
alex
Google recaptcha ...
|
126
|
|
51db39f2
alex
fix2
|
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
|
/**
* id form
*
* @var string
*/
public $formId = 'dynamic-form';
/**
* options for submit button
*
* @var array
*/
public $buttonOptions = ['class' => 'btn'];
/**
* content for submit button
*
* @var string
*/
public $buttonContent = 'Save';
/**
* @var string
*/
public $classForm = 'form';
public $id = 'forms';
public $buttonTemplate = '{button}';
public $locale = 'app';
/**
* @inheritdoc
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
parent::init();
if ($this->activeRecord !== null) {
$this->model = \Yii::createObject($this->activeRecord);
$this->model->scenario = $this->scenario;
} else {
$this->model = new DynamicModel($this->attributes);
$this->model->attributeLabels = $this->labels;
foreach ($this->rules as $rule) {
if (is_array($rule) && isset($rule[0], $rule[1])) {
$attributes = $rule[0];
$validator = $rule[1];
unset($rule[0], $rule[1]);
$this->model->addRule($attributes, $validator, $rule);
} else {
throw new InvalidConfigException(
'Invalid validation rule: a rule must specify both attribute names and validator type.'
);
}
}
}
}
/**
* render html form with model
*
* @param \yii\web\View $view
*/
|
c7f97c88
alex
Google recaptcha ...
|
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
public function renderForm(View $view)
{
if ($this->ajax) {
$js = <<<JS
$(document).on('submit', '#{$this->formId}', function(e) {
e.preventDefault();
e.stopImmediatePropagation();
$.post(
$(this).attr("action"), $(this).serialize(), {$this->successCallback}).fail({$this->errorCallback});
});
JS;
$view->registerJs($js, View::POS_READY);
}
/**
* @var ActiveForm $form
*/
$form = ActiveForm::begin(
[
'id' => $this->formId,
|
2097a1ea
alex
fix1
|
214
|
'action' => ($this->ajax) ? '/' . $this->id . '/save/ajax' : '/' . $this->id . '/save/no-ajax',
|
c7f97c88
alex
Google recaptcha ...
|
215
216
217
|
'class' => $this->classForm,
]
);
|
51db39f2
alex
fix2
|
218
|
|
c7f97c88
alex
Google recaptcha ...
|
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
|
$content = '';
foreach ($this->attributes as $field) {
# если мы присвоили этому полю тип, то он его отрисовывает
if (isset($this->inputOptions[$field]['type'])) {
$function = $this->inputOptions[$field]['type'];
$formStr = $form->field($this->model, $field)
->$function(
isset($this->inputOptions[$field]['options'])
? $this->inputOptions[$field]['options'] : []
);
} # а если нет, то просто добавляет поле
else {
$formStr = $form->field($this->model, $field);
}
if (isset($this->labels[$field])) {
$formStr->label(\Yii::t($this->locale, $this->labels[$field]));
}
if (isset($this->inputOptions[$field]['template'])) {
$formStr = str_replace('{input}', $formStr, $this->inputOptions[$field]['template']);
}
$content .= $formStr;
}
|
c7f97c88
alex
Google recaptcha ...
|
244
245
246
247
248
249
250
251
252
|
$content .= $form->field($this->model, 'reCaptcha')->widget(\sashsvamir\yii2\recaptcha\ReCaptcha::className())->label(false);
$content .= str_replace('{button}', Html::submitButton(\Yii::t($this->locale, $this->buttonContent), $this->buttonOptions), $this->buttonTemplate);
$content = str_replace('{form}', $content, $this->templateForm);
echo $content;
ActiveForm::end();
}
}
|