Commit c7f97c88f045f723f6db5d01b9fb6af07561797e
1 parent
8445ca50
Google recaptcha test variant 1
Showing
6 changed files
with
250 additions
and
2 deletions
Show diff stats
frontend/config/main.php
@@ -137,7 +137,8 @@ | @@ -137,7 +137,8 @@ | ||
137 | }', | 137 | }', |
138 | ], | 138 | ], |
139 | 'contact' => [ | 139 | 'contact' => [ |
140 | - 'class' => 'frontend\views\forms\ContactModule', | 140 | + 'class' => 'frontend\modules\forms\Module', |
141 | + #'class' => 'artbox\core\forms\Module', | ||
141 | 'activeRecord' => "frontend\models\Feedback", | 142 | 'activeRecord' => "frontend\models\Feedback", |
142 | 'templateForm' => '<div class="row">{form}</div>', | 143 | 'templateForm' => '<div class="row">{form}</div>', |
143 | 'attributes' => [ | 144 | 'attributes' => [ |
frontend/models/Feedback.php
1 | +<?php | ||
2 | + | ||
3 | +namespace frontend\modules\forms; | ||
4 | + | ||
5 | +/** | ||
6 | + * Class DynamicModel | ||
7 | + * | ||
8 | + * @package artbox\core\forms | ||
9 | + */ | ||
10 | +class DynamicModel extends \yii\base\DynamicModel | ||
11 | +{ | ||
12 | + /** | ||
13 | + * @var mixed | ||
14 | + */ | ||
15 | + protected $_labels; | ||
16 | + | ||
17 | + /** | ||
18 | + * @param $labels | ||
19 | + */ | ||
20 | + public function setAttributeLabels($labels) | ||
21 | + { | ||
22 | + $this->_labels = $labels; | ||
23 | + } | ||
24 | + | ||
25 | + /** | ||
26 | + * @param string $name | ||
27 | + * | ||
28 | + * @return string | ||
29 | + */ | ||
30 | + public function getAttributeLabel($name) | ||
31 | + { | ||
32 | + return $this->_labels[$name] ?? $name; | ||
33 | + } | ||
34 | +} | ||
0 | \ No newline at end of file | 35 | \ No newline at end of file |
1 | +<?php | ||
2 | + | ||
3 | +namespace frontend\modules\forms; | ||
4 | + | ||
5 | + | ||
6 | +use yii\helpers\Html; | ||
7 | +use yii\web\View; | ||
8 | +use yii\widgets\ActiveForm; | ||
9 | +use artbox\core\forms\Module as CoreModule; | ||
10 | + | ||
11 | + | ||
12 | +/** | ||
13 | + * Class ContactModule | ||
14 | + * @package frontend\forms | ||
15 | + * | ||
16 | + * | ||
17 | + * ะะปะฐัั, ะบะพัะพััะน ะฝะตะผะฝะพะณะพ ะฒะธะดะพะธะทะผะตะฝัะตั ะฒัะฒะพะด ัะพัะผั ะบะพะฝัะฐะบัะพะฒ ะดะปั ัะพะณะพ, ััะพะฑั ะฒ ะฝะตะน ะฟะพัะฒะธะปะฐัั ะณัะณะปะพะฒะฐั ัะตะบะฐะฟัะฐ | ||
18 | + * | ||
19 | + */ | ||
20 | +class Module extends CoreModule | ||
21 | +{ | ||
22 | + | ||
23 | + | ||
24 | + public function renderForm(View $view) | ||
25 | + { | ||
26 | + if ($this->ajax) { | ||
27 | + $js = <<<JS | ||
28 | +$(document).on('submit', '#{$this->formId}', function(e) { | ||
29 | + e.preventDefault(); | ||
30 | + e.stopImmediatePropagation(); | ||
31 | + $.post( | ||
32 | + $(this).attr("action"), $(this).serialize(), {$this->successCallback}).fail({$this->errorCallback}); | ||
33 | +}); | ||
34 | +JS; | ||
35 | + | ||
36 | + $view->registerJs($js, View::POS_READY); | ||
37 | + } | ||
38 | + /** | ||
39 | + * @var ActiveForm $form | ||
40 | + */ | ||
41 | + | ||
42 | + $form = ActiveForm::begin( | ||
43 | + [ | ||
44 | + 'id' => $this->formId, | ||
45 | + 'action' => ($this->ajax) ? '/save/ajax' : '/' . $this->id . '/save/no-ajax', | ||
46 | + 'class' => $this->classForm, | ||
47 | + ] | ||
48 | + ); | ||
49 | + $content = ''; | ||
50 | + foreach ($this->attributes as $field) { | ||
51 | + | ||
52 | + # ะตัะปะธ ะผั ะฟัะธัะฒะพะธะปะธ ััะพะผั ะฟะพะปั ัะธะฟ, ัะพ ะพะฝ ะตะณะพ ะพััะธัะพะฒัะฒะฐะตั | ||
53 | + if (isset($this->inputOptions[$field]['type'])) { | ||
54 | + $function = $this->inputOptions[$field]['type']; | ||
55 | + $formStr = $form->field($this->model, $field) | ||
56 | + ->$function( | ||
57 | + isset($this->inputOptions[$field]['options']) | ||
58 | + ? $this->inputOptions[$field]['options'] : [] | ||
59 | + ); | ||
60 | + } # ะฐ ะตัะปะธ ะฝะตั, ัะพ ะฟัะพััะพ ะดะพะฑะฐะฒะปัะตั ะฟะพะปะต | ||
61 | + else { | ||
62 | + $formStr = $form->field($this->model, $field); | ||
63 | + } | ||
64 | + if (isset($this->labels[$field])) { | ||
65 | + $formStr->label(\Yii::t($this->locale, $this->labels[$field])); | ||
66 | + } | ||
67 | + if (isset($this->inputOptions[$field]['template'])) { | ||
68 | + | ||
69 | + $formStr = str_replace('{input}', $formStr, $this->inputOptions[$field]['template']); | ||
70 | + } | ||
71 | + $content .= $formStr; | ||
72 | + | ||
73 | + } | ||
74 | + | ||
75 | + $content .= $form->field($this->model, 'reCaptcha')->widget(\sashsvamir\yii2\recaptcha\ReCaptcha::className())->label(false); | ||
76 | + | ||
77 | + | ||
78 | + $content .= str_replace('{button}', Html::submitButton(\Yii::t($this->locale, $this->buttonContent), $this->buttonOptions), $this->buttonTemplate); | ||
79 | + $content = str_replace('{form}', $content, $this->templateForm); | ||
80 | + echo $content; | ||
81 | + ActiveForm::end(); | ||
82 | + } | ||
83 | +} | ||
0 | \ No newline at end of file | 84 | \ No newline at end of file |
frontend/modules/forms/controllers/SaveController.php
0 โ 100755
1 | +<?php | ||
2 | + | ||
3 | +namespace frontend\modules\forms\controllers; | ||
4 | + | ||
5 | +use artbox\core\forms\DynamicModel; | ||
6 | +use yii\web\Controller; | ||
7 | +use yii\web\Response; | ||
8 | + | ||
9 | +/** | ||
10 | + * Class SaveController | ||
11 | + * | ||
12 | + * @property \artbox\core\forms\Module $module | ||
13 | + * @package artbox\core\forms\controllers | ||
14 | + */ | ||
15 | +class SaveController extends Controller | ||
16 | +{ | ||
17 | + /** | ||
18 | + * Custom data saving via POST | ||
19 | + * | ||
20 | + * @return bool|\yii\web\Response | ||
21 | + * @throws \yii\base\InvalidConfigException | ||
22 | + */ | ||
23 | + public function actionNoAjax() | ||
24 | + { | ||
25 | + $returnUrl = \Yii::$app->request->referrer; | ||
26 | + $model = $this->module->model; | ||
27 | + if (\Yii::$app->request->isPost and $model->load(\Yii::$app->request->post())) { | ||
28 | + if ($model instanceof DynamicModel) { | ||
29 | + $this->sendEmail($model); | ||
30 | + } else { | ||
31 | + if ($model->save()) { | ||
32 | + if ($this->module->sendEmail) { | ||
33 | + $this->sendEmail($model); | ||
34 | + return $this->redirect($returnUrl); | ||
35 | + } | ||
36 | + } else { | ||
37 | + return false; | ||
38 | + } | ||
39 | + } | ||
40 | + return $this->redirect($returnUrl); | ||
41 | + | ||
42 | + } | ||
43 | + return $this->redirect($returnUrl); | ||
44 | + } | ||
45 | + | ||
46 | + /** | ||
47 | + * Action that saves data submited via AJAX | ||
48 | + * | ||
49 | + * @return array | ||
50 | + * @throws \yii\base\InvalidConfigException | ||
51 | + */ | ||
52 | + public function actionAjax() | ||
53 | + { | ||
54 | + \Yii::$app->response->format = Response::FORMAT_JSON; | ||
55 | + $model = $this->module->model; | ||
56 | + if (\Yii::$app->request->isPost and $model->load(\Yii::$app->request->post())) { | ||
57 | + if ($model instanceof DynamicModel) { | ||
58 | + $this->sendEmail($model); | ||
59 | + return ['status' => 'success']; | ||
60 | + } else { | ||
61 | + if ($model->save()) { | ||
62 | + if ($this->module->sendEmail) { | ||
63 | + $this->sendEmail($model); | ||
64 | + return ['status' => 'success']; | ||
65 | + } | ||
66 | + return ['status' => 'success']; | ||
67 | + } else { | ||
68 | + return ['status' => 'error']; | ||
69 | + } | ||
70 | + } | ||
71 | + | ||
72 | + } | ||
73 | + return ['status' => 'error']; | ||
74 | + } | ||
75 | + | ||
76 | + /** | ||
77 | + * @param $model | ||
78 | + * | ||
79 | + * @return bool | ||
80 | + * @throws \yii\base\InvalidConfigException | ||
81 | + */ | ||
82 | + public function sendEmail($model) | ||
83 | + { | ||
84 | + | ||
85 | + if ($this->module->mailer == null) { | ||
86 | + $mailer = \Yii::$app->mailer; | ||
87 | + } else { | ||
88 | + $mailer = \Yii::$app->get($this->module->mailer); | ||
89 | + } | ||
90 | + return $mailer->compose( | ||
91 | + [ | ||
92 | + 'html' => $this->module->emailFile, | ||
93 | + 'text' => $this->module->emailFile, | ||
94 | + ], | ||
95 | + [$model] | ||
96 | + ) | ||
97 | + ->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot']) | ||
98 | + ->setTo($this->module->email) | ||
99 | + ->setSubject($this->module->subject) | ||
100 | + ->send(); | ||
101 | + } | ||
102 | + | ||
103 | +} | ||
0 | \ No newline at end of file | 104 | \ No newline at end of file |
1 | +<?php | ||
2 | +/** | ||
3 | + * @var \yii\db\ActiveRecord $model | ||
4 | + * @var array $types | ||
5 | + */ | ||
6 | + | ||
7 | +use yii\helpers\Html; | ||
8 | +use yii\widgets\ActiveForm; | ||
9 | + | ||
10 | +$form = ActiveForm::begin(['id' => 'dynamic-form']); | ||
11 | +foreach ($this->context->module->attributes as $field) { | ||
12 | + if (isset($types[$field])) { | ||
13 | + $function = $types[$field]['type']; | ||
14 | + echo $form->field($model, $field) | ||
15 | + ->$function( | ||
16 | + $types[$field]['options'] | ||
17 | + ); | ||
18 | + } else { | ||
19 | + echo $form->field($model, $field); | ||
20 | + } | ||
21 | + | ||
22 | +} | ||
23 | + | ||
24 | +echo Html::submitButton(); | ||
25 | + | ||
26 | +$form = ActiveForm::end(); |