Commit 51db39f2d6e65843f232f0c39f11dc8cdf47056a
1 parent
2097a1ea
fix2
Showing
1 changed file
with
178 additions
and
9 deletions
Show diff stats
frontend/modules/forms/Module.php
... | ... | @@ -2,25 +2,194 @@ |
2 | 2 | |
3 | 3 | namespace frontend\modules\forms; |
4 | 4 | |
5 | - | |
5 | +use yii\base\InvalidConfigException; | |
6 | 6 | use yii\helpers\Html; |
7 | 7 | use yii\web\View; |
8 | 8 | use yii\widgets\ActiveForm; |
9 | -use yii\base\Module as YiiModule; | |
10 | 9 | |
11 | 10 | |
12 | 11 | /** |
13 | - * Class ContactModule | |
14 | - * @package frontend\forms | |
15 | - * | |
16 | - * | |
17 | - * Класс, который немного видоизменяет вывод формы контактов для того, чтобы в ней появилась гугловая рекапча | |
12 | + * Module for generation forms adn saving data from frontend | |
18 | 13 | * |
14 | + * @package artbox\core\forms | |
19 | 15 | */ |
20 | -class Module extends YiiModule | |
16 | +class Module extends \yii\base\Module | |
21 | 17 | { |
18 | + /** | |
19 | + * ActiveRecord for insert to db | |
20 | + * | |
21 | + * @var null | |
22 | + */ | |
23 | + public $templateForm = '{form}'; | |
24 | + | |
25 | + public $activeRecord = null; | |
26 | + | |
27 | + /** | |
28 | + * attributes which render in form | |
29 | + * | |
30 | + * @var array | |
31 | + */ | |
32 | + public $attributes = []; | |
33 | + | |
34 | + /** | |
35 | + * validation rules for DynamicModel | |
36 | + * | |
37 | + * @var array | |
38 | + */ | |
39 | + public $rules = []; | |
40 | + | |
41 | + /** | |
42 | + * scenario for ActiveRecord | |
43 | + * | |
44 | + * @var string | |
45 | + */ | |
46 | + public $scenario = 'default'; | |
47 | + | |
48 | + /** | |
49 | + * Input types for attributes. textinput default | |
50 | + * | |
51 | + * @var array | |
52 | + */ | |
53 | + public $inputOptions = []; | |
54 | + | |
55 | + /** | |
56 | + * Labels by form (if ActiveRecord is null) | |
57 | + * | |
58 | + * @var array | |
59 | + */ | |
60 | + public $labels = []; | |
61 | + | |
62 | + /** | |
63 | + * Send or not email | |
64 | + * | |
65 | + * @var bool | |
66 | + */ | |
67 | + public $sendEmail = true; | |
68 | + | |
69 | + /** | |
70 | + * Email address | |
71 | + * | |
72 | + * @var string | |
73 | + */ | |
74 | + public $email = 'admin@example.com'; | |
75 | + | |
76 | + /** | |
77 | + * Email template | |
78 | + * | |
79 | + * @var string | |
80 | + */ | |
81 | + public $emailFile = 'mail.php'; | |
82 | + | |
83 | + /** | |
84 | + * Email subject | |
85 | + * | |
86 | + * @var string | |
87 | + */ | |
88 | + public $subject = ''; | |
89 | + | |
90 | + /** | |
91 | + * mailer which send email | |
92 | + * | |
93 | + * @var null | |
94 | + */ | |
95 | + public $mailer = null; | |
96 | + | |
97 | + /** | |
98 | + * @var \yii\db\ActiveRecord $model ; | |
99 | + */ | |
100 | + public $model = null; | |
101 | + | |
102 | + /** | |
103 | + * send form with ajax | |
104 | + * | |
105 | + * @var bool | |
106 | + */ | |
107 | + public $ajax = false; | |
108 | + | |
109 | + /** | |
110 | + * callback function for ajax script of send form | |
111 | + * | |
112 | + * @var string | |
113 | + */ | |
114 | + public $successCallback = 'function (data) { | |
115 | + alert("ok"); | |
116 | + }'; | |
22 | 117 | |
118 | + /** | |
119 | + * error function for ajax script of send form | |
120 | + * | |
121 | + * @var string | |
122 | + */ | |
123 | + public $errorCallback = 'function (data) { | |
124 | + alert("error"); | |
125 | + }'; | |
23 | 126 | |
127 | + /** | |
128 | + * id form | |
129 | + * | |
130 | + * @var string | |
131 | + */ | |
132 | + public $formId = 'dynamic-form'; | |
133 | + | |
134 | + /** | |
135 | + * options for submit button | |
136 | + * | |
137 | + * @var array | |
138 | + */ | |
139 | + public $buttonOptions = ['class' => 'btn']; | |
140 | + | |
141 | + /** | |
142 | + * content for submit button | |
143 | + * | |
144 | + * @var string | |
145 | + */ | |
146 | + public $buttonContent = 'Save'; | |
147 | + | |
148 | + /** | |
149 | + * @var string | |
150 | + */ | |
151 | + public $classForm = 'form'; | |
152 | + | |
153 | + public $id = 'forms'; | |
154 | + | |
155 | + public $buttonTemplate = '{button}'; | |
156 | + | |
157 | + public $locale = 'app'; | |
158 | + | |
159 | + /** | |
160 | + * @inheritdoc | |
161 | + * @throws \yii\base\InvalidConfigException | |
162 | + */ | |
163 | + public function init() | |
164 | + { | |
165 | + parent::init(); | |
166 | + | |
167 | + if ($this->activeRecord !== null) { | |
168 | + $this->model = \Yii::createObject($this->activeRecord); | |
169 | + $this->model->scenario = $this->scenario; | |
170 | + } else { | |
171 | + $this->model = new DynamicModel($this->attributes); | |
172 | + $this->model->attributeLabels = $this->labels; | |
173 | + foreach ($this->rules as $rule) { | |
174 | + if (is_array($rule) && isset($rule[0], $rule[1])) { | |
175 | + $attributes = $rule[0]; | |
176 | + $validator = $rule[1]; | |
177 | + unset($rule[0], $rule[1]); | |
178 | + $this->model->addRule($attributes, $validator, $rule); | |
179 | + } else { | |
180 | + throw new InvalidConfigException( | |
181 | + 'Invalid validation rule: a rule must specify both attribute names and validator type.' | |
182 | + ); | |
183 | + } | |
184 | + } | |
185 | + } | |
186 | + } | |
187 | + | |
188 | + /** | |
189 | + * render html form with model | |
190 | + * | |
191 | + * @param \yii\web\View $view | |
192 | + */ | |
24 | 193 | public function renderForm(View $view) |
25 | 194 | { |
26 | 195 | if ($this->ajax) { |
... | ... | @@ -46,6 +215,7 @@ JS; |
46 | 215 | 'class' => $this->classForm, |
47 | 216 | ] |
48 | 217 | ); |
218 | + | |
49 | 219 | $content = ''; |
50 | 220 | foreach ($this->attributes as $field) { |
51 | 221 | |
... | ... | @@ -71,7 +241,6 @@ JS; |
71 | 241 | $content .= $formStr; |
72 | 242 | |
73 | 243 | } |
74 | - | |
75 | 244 | $content .= $form->field($this->model, 'reCaptcha')->widget(\sashsvamir\yii2\recaptcha\ReCaptcha::className())->label(false); |
76 | 245 | |
77 | 246 | ... | ... |