From 8d88a55247207f976f7f487cebc637b5fb181828 Mon Sep 17 00:00:00 2001 From: dozer111 Date: Wed, 25 Jul 2018 12:17:35 +0300 Subject: [PATCH] Add Google Recaptcha to site/contact **** Нужно композером на сервере установить новую либу --- common/config/main.php | 7 +++++++ composer.json | 3 ++- composer.lock | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- frontend/config/main.php | 6 ++++-- frontend/controllers/SiteController.php | 6 +++--- frontend/models/Feedback.php | 17 +++++++++++++++-- frontend/views/forms/ContactModule.php | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ frontend/views/site/contact.php | 5 ++++- frontend/web/css/site.css | 8 ++++++++ frontend/web/js/script.js | 3 +++ 10 files changed, 182 insertions(+), 16 deletions(-) create mode 100644 frontend/views/forms/ContactModule.php diff --git a/common/config/main.php b/common/config/main.php index 756f265..7bc58c3 100755 --- a/common/config/main.php +++ b/common/config/main.php @@ -26,6 +26,13 @@ ] ], 'components' => [ + + 'reCaptcha' => [ + 'name' => 'reCaptcha', + 'class' => 'sashsvamir\yii2\recaptcha\ReCaptcha', + 'siteKey' => '6LcZcmQUAAAAANF1TashIYAvYtTBCscpTlPe1q6T', + 'secret' => '6LcZcmQUAAAAAFH4Ru4S6QnmB3An3PJWglmV', + ], 'cache' => [ 'class' => 'yii\caching\FileCache', ], diff --git a/composer.json b/composer.json index e965b8b..7713c33 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "minimum-stability": "RC", "require": { "php": ">=7.0", - "artweb/artbox-core" : ">0.0.10" + "artweb/artbox-core": ">0.0.10", + "sashsvamir/yii2-recaptcha-widget": "dev-master" }, "require-dev": { "yiisoft/yii2-debug": "~2.0.0", diff --git a/composer.lock b/composer.lock index 32ce367..0c84db3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "d1c4ebdcace0dbe46fef8e15f5de2d7f", - "content-hash": "20c2db805fdbbc718dc7c57c39800648", + "hash": "9a6e92d2cba1e4da99f27abd0a2ae4f3", + "content-hash": "f335aaff8ec5703aad48c3b2fd89b0c1", "packages": [ { "name": "2amigos/yii2-tinymce-widget", @@ -226,7 +226,7 @@ "version": "1.4.0", "source": { "type": "git", - "url": "https://github.com/puikinsh/gentelella.git", + "url": "git@github.com:puikinsh/gentelella.git", "reference": "3448fdfdf16a41707d6e2a8154d592a88d85c41c" }, "dist": { @@ -245,12 +245,12 @@ "version": "1.0.2", "source": { "type": "git", - "url": "https://github.com/fronteed/icheck.git", + "url": "git@github.com:fronteed/iCheck.git", "reference": "8a6eb37bd7dab1e843c1b630c91c6398ff409d05" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/fronteed/icheck/zipball/8a6eb37bd7dab1e843c1b630c91c6398ff409d05", + "url": "https://api.github.com/repos/fronteed/iCheck/zipball/8a6eb37bd7dab1e843c1b630c91c6398ff409d05", "reference": "8a6eb37bd7dab1e843c1b630c91c6398ff409d05", "shasum": null }, @@ -370,7 +370,7 @@ "version": "v1.3.2", "source": { "type": "git", - "url": "https://github.com/bestiejs/punycode.js.git", + "url": "git@github.com:bestiejs/punycode.js.git", "reference": "38c8d3131a82567bfef18da09f7f4db68c84f8a3" }, "dist": { @@ -2485,6 +2485,50 @@ "time": "2017-01-11 14:05:47" }, { + "name": "sashsvamir/yii2-recaptcha-widget", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/sashsvamir/yii2-recaptcha-widget.git", + "reference": "3daa1b48d99839807c0bf520bb2660d43ed83446" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sashsvamir/yii2-recaptcha-widget/zipball/3daa1b48d99839807c0bf520bb2660d43ed83446", + "reference": "3daa1b48d99839807c0bf520bb2660d43ed83446", + "shasum": "" + }, + "require": { + "himiklab/yii2-recaptcha-widget": "1.0.6", + "yiisoft/yii2": "*" + }, + "type": "yii2-extension", + "autoload": { + "psr-4": { + "sashsvamir\\yii2\\recaptcha\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "sashsvamir", + "homepage": "https://github.com/sashsvamir/" + } + ], + "description": "Yii2 Google reCAPTCHA widget", + "keywords": [ + "captcha", + "google", + "recaptcha", + "widget", + "yii2" + ], + "time": "2018-01-24 10:27:21" + }, + { "name": "skeeks/yii2-assets-auto-compress", "version": "1.4.0", "source": { @@ -5899,7 +5943,9 @@ ], "aliases": [], "minimum-stability": "RC", - "stability-flags": [], + "stability-flags": { + "sashsvamir/yii2-recaptcha-widget": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/frontend/config/main.php b/frontend/config/main.php index 026ca2c..731e9c9 100755 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -137,7 +137,7 @@ }', ], 'contact' => [ - 'class' => 'artbox\core\forms\Module', + 'class' => 'frontend\views\forms\ContactModule', 'activeRecord' => "frontend\models\Feedback", 'templateForm' => '
{form}
', 'attributes' => [ @@ -145,6 +145,7 @@ 'email', 'phone', 'message', + ], 'rules' => [ [ @@ -158,7 +159,8 @@ 'phone', ], 'required', - ] + ], + ], 'labels' => [ 'email' => \Yii::t('app','Email'), diff --git a/frontend/controllers/SiteController.php b/frontend/controllers/SiteController.php index 15dda2f..ec8b8a9 100755 --- a/frontend/controllers/SiteController.php +++ b/frontend/controllers/SiteController.php @@ -1,7 +1,7 @@ F::SCENARIO_DEFAULT]); return $this->render( 'contact', [ - 'contact' => $contact, + 'model' => $contact, ] ); } diff --git a/frontend/models/Feedback.php b/frontend/models/Feedback.php index 2454786..7a5590c 100644 --- a/frontend/models/Feedback.php +++ b/frontend/models/Feedback.php @@ -7,13 +7,18 @@ namespace frontend\models; use artbox\core\models\Feedback as ArtboxCoreFeedback; - +use sashsvamir\yii2\recaptcha\ReCaptchaValidator; class Feedback extends ArtboxCoreFeedback { + public $reCaptcha; + + public function rules() { - return [ + + + return [ [ [ 'name', @@ -24,6 +29,14 @@ class Feedback extends ArtboxCoreFeedback 'on' => self::SCENARIO_DEFAULT, 'message' => \Yii::t('app', 'Empty field'), ], + ['reCaptcha', ReCaptchaValidator::className(), 'uncheckedMessage' => 'Please confirm that you are not a bot.', + // add follow lines to prevent checking recaptcha when from has errors + 'when' => function ($model) { + return !$model->hasErrors(); + }, + 'on' => self::SCENARIO_DEFAULT, + + ], [ [ 'name', diff --git a/frontend/views/forms/ContactModule.php b/frontend/views/forms/ContactModule.php new file mode 100644 index 0000000..7bd7194 --- /dev/null +++ b/frontend/views/forms/ContactModule.php @@ -0,0 +1,83 @@ +ajax) { + $js = <<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, + 'action' => ($this->ajax) ? '/' . $this->id . '/save/ajax' : '/' . $this->id . '/save/no-ajax', + 'class' => $this->classForm, + ] + ); + $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; + + } + + $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(); + } +} \ No newline at end of file diff --git a/frontend/views/site/contact.php b/frontend/views/site/contact.php index bf2eecb..6d4aff5 100755 --- a/frontend/views/site/contact.php +++ b/frontend/views/site/contact.php @@ -11,6 +11,7 @@ use frontend\assets\MapAsset; use yii\helpers\Html; use yii\web\View; +use yii\widgets\ActiveForm; MapAsset::register($this); $settings = Settings::getInstance(); @@ -119,7 +120,9 @@ JS;
- renderForm($this)?> + + + renderForm($this) ?>
diff --git a/frontend/web/css/site.css b/frontend/web/css/site.css index fc08e85..e52dfbb 100644 --- a/frontend/web/css/site.css +++ b/frontend/web/css/site.css @@ -133,3 +133,11 @@ a.desc:after { .background-white { background-color: white; } + +/* +Класс для выравнивания по центру формы инпута google ReCaptcha +*/ +.f_center { + margin-left: 80%; + transform: translateX(-50%); +} \ No newline at end of file diff --git a/frontend/web/js/script.js b/frontend/web/js/script.js index 03585bb..b7fc64c 100755 --- a/frontend/web/js/script.js +++ b/frontend/web/js/script.js @@ -67,6 +67,9 @@ $( // } // ); + + $('.field-feedback-recaptcha').addClass('col-sm-12').addClass('f_center') + /** * Button UP code */ -- libgit2 0.21.4