From 7a414ec0d5f3bdd29e8a5f9a436efc67b492e603 Mon Sep 17 00:00:00 2001 From: German Bisurgi Date: Tue, 13 Oct 2020 10:02:11 +0200 Subject: [PATCH 1/7] added captcha column in app_dmstr_contact_template table. Added optsCaptcha to make a select field. Regenerated cruds like described in the README --- ...25_alter_app_dmstr_contact_template_table.php | 16 ++++++++++++++++ src/models/ContactTemplate.php | 11 +++++++++++ src/models/base/ContactTemplate.php | 3 +++ src/views/crud/contact-template/_form.php | 5 +++++ src/views/crud/contact-template/_search.php | 2 ++ src/views/crud/contact-template/index.php | 3 ++- src/views/crud/contact-template/view.php | 1 + 7 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/migrations/m201012_141325_alter_app_dmstr_contact_template_table.php diff --git a/src/migrations/m201012_141325_alter_app_dmstr_contact_template_table.php b/src/migrations/m201012_141325_alter_app_dmstr_contact_template_table.php new file mode 100644 index 0000000..5dd776f --- /dev/null +++ b/src/migrations/m201012_141325_alter_app_dmstr_contact_template_table.php @@ -0,0 +1,16 @@ +addColumn('app_dmstr_contact_template','captcha','SMALLINT(6) NULL AFTER email_subject'); + } + + public function safeDown() + { + $this->dropColumn('app_dmstr_contact_template','captcha'); + } +} diff --git a/src/models/ContactTemplate.php b/src/models/ContactTemplate.php index 5be0f86..c032aab 100644 --- a/src/models/ContactTemplate.php +++ b/src/models/ContactTemplate.php @@ -29,4 +29,15 @@ public function behaviors() ]; return $behaviors; } + + /** + * @return array + */ + public static function optsCaptcha() + { + return [ + 0 => \Yii::t('contact', 'No'), + 1 => \Yii::t('contact', 'Yes'), + ]; + } } diff --git a/src/models/base/ContactTemplate.php b/src/models/base/ContactTemplate.php index 051b754..69e2588 100644 --- a/src/models/base/ContactTemplate.php +++ b/src/models/base/ContactTemplate.php @@ -15,6 +15,7 @@ * @property string $reply_to_email * @property string $to_email * @property string $email_subject + * @property integer $captcha * @property string $form_schema * @property string $created_at * @property string $updated_at @@ -40,6 +41,7 @@ public function rules() { return [ [['name', 'from_email', 'to_email'], 'required'], + [['captcha'], 'integer'], [['form_schema'], 'string'], [['created_at', 'updated_at'], 'safe'], [['name', 'from_email', 'reply_to_email', 'to_email', 'email_subject'], 'string', 'max' => 255], @@ -59,6 +61,7 @@ public function attributeLabels() 'reply_to_email' => Yii::t('models', 'Reply To Email'), 'to_email' => Yii::t('models', 'To Email'), 'email_subject' => Yii::t('models', 'Email Subject'), + 'captcha' => Yii::t('models', 'Captcha'), 'form_schema' => Yii::t('models', 'Form Schema'), 'created_at' => Yii::t('models', 'Created At'), 'updated_at' => Yii::t('models', 'Updated At'), diff --git a/src/views/crud/contact-template/_form.php b/src/views/crud/contact-template/_form.php index 8e9b459..b418ee6 100644 --- a/src/views/crud/contact-template/_form.php +++ b/src/views/crud/contact-template/_form.php @@ -55,6 +55,11 @@ field($model, 'to_email')->textInput(['maxlength' => true]) ?> + + field($model, 'captcha')->dropDownList( + dmstr\modules\contact\models\ContactTemplate::optscaptcha() +); ?> + field($model, 'form_schema')->textarea(['rows' => 6]) ?> diff --git a/src/views/crud/contact-template/_search.php b/src/views/crud/contact-template/_search.php index 62a5e0e..962cabd 100644 --- a/src/views/crud/contact-template/_search.php +++ b/src/views/crud/contact-template/_search.php @@ -36,6 +36,8 @@ field($model, 'email_subject') ?> + field($model, 'captcha') ?> + field($model, 'form_schema') ?> field($model, 'created_at') ?> diff --git a/src/views/crud/contact-template/index.php b/src/views/crud/contact-template/index.php index 50b3952..c054a4e 100644 --- a/src/views/crud/contact-template/index.php +++ b/src/views/crud/contact-template/index.php @@ -115,10 +115,11 @@ 'name', 'from_email:email', 'to_email:email', + 'captcha', 'form_schema:ntext', 'created_at', 'updated_at', - 'reply_to_email:email', + /*'reply_to_email:email',*/ /*'email_subject:email',*/ ], ]); ?> diff --git a/src/views/crud/contact-template/view.php b/src/views/crud/contact-template/view.php index ac7168d..34a9f2e 100644 --- a/src/views/crud/contact-template/view.php +++ b/src/views/crud/contact-template/view.php @@ -82,6 +82,7 @@ 'name', 'from_email:email', 'to_email:email', + 'captcha', 'form_schema:ntext', 'created_at', 'updated_at', From 52428ec06d154950109f8dcb48a586258192e17a Mon Sep 17 00:00:00 2001 From: German Bisurgi Date: Tue, 13 Oct 2020 10:03:10 +0200 Subject: [PATCH 2/7] Added captcha scenario to ContactForm required for captcha validation when scenario = captcha --- src/models/ContactForm.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/models/ContactForm.php b/src/models/ContactForm.php index 089a49c..e5dca3f 100644 --- a/src/models/ContactForm.php +++ b/src/models/ContactForm.php @@ -17,8 +17,11 @@ */ class ContactForm extends Model { + const SCENARIO_CAPTCHA = 'captcha'; + public $contact_template_id; public $json; + public $captcha; public function getSchema() { @@ -48,9 +51,9 @@ function ($attribute) { $this->addError($error['property'], "{$error['property']}: {$error['message']}"); } } - } ]; + $rules [] = ['captcha', 'captcha', 'captchaAction' => 'contact/default/captcha', 'on' => self::SCENARIO_CAPTCHA]; return $rules; } } From c3b63f56badf291f7f285e66d0d884c1af2a9aa8 Mon Sep 17 00:00:00 2001 From: German Bisurgi Date: Tue, 13 Oct 2020 10:04:15 +0200 Subject: [PATCH 3/7] Added captcha acion in controller. Set ContactForm scenario = captchat when active in contact-template --- src/controllers/DefaultController.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/controllers/DefaultController.php b/src/controllers/DefaultController.php index 9ca4feb..b19cf54 100644 --- a/src/controllers/DefaultController.php +++ b/src/controllers/DefaultController.php @@ -8,6 +8,7 @@ use dmstr\modules\contact\models\ContactTemplate; use dmstr\modules\contact\Module; use yii; +use yii\captcha\CaptchaAction; use yii\helpers\Json; use yii\web\Controller; use yii\web\ForbiddenHttpException; @@ -50,6 +51,11 @@ public function actionIndex($schema) 'contact_template_id' => $contactSchema->id ]); + if ($contactSchema->captcha === 1) { + $contactForm->scenario = ContactForm::SCENARIO_CAPTCHA; + } + + if ($contactForm->load(Yii::$app->request->post()) && $contactForm->validate()) { $contactLog = new ContactLog([ @@ -110,4 +116,21 @@ public function actionDone($schema) } + public function actions() + { + $backColor = Yii::$app->settings->getOrSet('backColor', '0x333333', 'captcha', 'string'); + $foreColor = Yii::$app->settings->getOrSet('foreColor', '0xFFFFFF', 'captcha', 'string'); + $actions = parent::actions(); + $actions['captcha'] = [ + 'class' => CaptchaAction::class, + 'testLimit' => 100, + 'width' => 140, + 'height' => 75, + 'offset' => -1, + 'backColor' => hexdec($backColor), + 'foreColor' => hexdec($foreColor) + ]; + return $actions; + } + } From 45785a5a5131901b1ab2ab1515e5238c8f799404 Mon Sep 17 00:00:00 2001 From: German Bisurgi Date: Tue, 13 Oct 2020 10:04:59 +0200 Subject: [PATCH 4/7] Updated README with examples and clearer instructions --- README.md | 161 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 106 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index cd9b719..0c930db 100644 --- a/README.md +++ b/README.md @@ -1,77 +1,125 @@ +## Configuring template -## Configuration +Navigate to `/contact/crud/contact-template` to create a new form template. -Each form has a unique name, e.g. 'reservation'. +* Name: a unique name, e.g. 'reservation' +* From Email: valid email +* To Email: valid email +* Captcha: this tells the contact module that a captcha will be used and that it has to validated against it (sets model scenario to captcha) +* Form Schema: json-schema used to build a form with `dmstr/jsoneditor/JsonEditorWidget` (For more information about schema see examples on: https://github.com/json-editor/json-editor +) + +## conventions: -conventions: * if you have a property reply_to in your schema and send valid email address as value, this will be used as Reply-To: header in message -Each form needs 2 Twig templates: -* `contact:` (template in which the form will be rendered) -* `contact::send` (will be rendered as "thank you page" after message has been send) +## Twig templates (Views) -While must be replaced with your template name +Each form needs 2 Twig templates. Navigate to `/prototype/twig/index` to create them: -For more information about schema see examples on: https://github.com/json-editor/json-editor +* `contact:FORM_NAME`: template in which the form will be rendered +* `contact:FORM_NAME:send`: will be rendered as "thank you page" after message has been send -To enable the export feature add *kartik\grid\Module* to your project modules +While `FORM_NAME` must be replaced with your template name -## Examples +* The form can be seen at `/contact/default/?schema=contact:FORM_NAME` +* The "thank you page" can be seen at `/contact/default/done?schema=contact:FORM_NAME` -### Twig layout ( +### Form Twig layout -Form: ```twig {{ use('dmstr/jsoneditor/JsonEditorWidget') }} {{ use('yii/widgets/ActiveForm') }} -{% set script %} - JSONEditor.defaults.language = "de"; - JSONEditor.defaults.languages.de = { - error_minLength: "Muss mindestens \{\{0\}\} Zeichen enthalten.", - error_notset: "Muss gesetzt sein", - error_notempty: "Pflichtfeld" - }; -{% endset %} -{{ this.registerJs(script) }} -{{ this.registerJs('JSONEditor.plugins.selectize.enable = true;') }} - -
-
- {% set form = active_form_begin({ - 'id': 'contact-form', - 'action' : '', - 'options': { - } - }) %} - - {{ form.errorSummary(model) | raw }} - - {{ json_editor_widget_widget({ - 'model': model, - 'attribute': 'json', - 'options': { - 'id': 'contact-json' - }, - 'clientOptions': { - 'theme': 'bootstrap3', - 'disable_collapse': true, - 'disable_edit_json': true, - 'disable_properties': true, - 'no_additional_properties': true, - 'show_errors': 'always' - }, - 'schema': schema, - }) }} - - - - {{ active_form_end() }} +
+
+
+ {% set form = active_form_begin({ + 'id': 'contact-form', + 'action' : '', + 'options': { + } + }) %} + + {{ form.errorSummary(model) | raw }} + + {{ json_editor_widget_widget({ + 'model': model, + 'attribute': 'json', + 'options': { + 'id': 'contact-json' + }, + 'clientOptions': { + 'theme': 'bootstrap3', + 'disable_collapse': true, + 'disable_edit_json': true, + 'disable_properties': true, + 'no_additional_properties': true, + 'show_errors': 'interaction' + }, + 'schema': schema, + }) }} + + + + {{ active_form_end() }} +
``` -Done: +### Form Twig layout with captcha (requires `captcha` activate in the contact-template) + +```twig +{{ use('dmstr/jsoneditor/JsonEditorWidget') }} +{{ use('yii/widgets/ActiveForm') }} +{{ use('yii/captcha/Captcha') }} + +
+
+
+ {% set form = active_form_begin({ + 'id': 'contact-form', + 'action' : '', + 'options': { + } + }) %} + + {{ form.errorSummary(model) | raw }} + + {{ json_editor_widget_widget({ + 'model': model, + 'attribute': 'json', + 'options': { + 'id': 'contact-json' + }, + 'clientOptions': { + 'theme': 'bootstrap3', + 'disable_collapse': true, + 'disable_edit_json': true, + 'disable_properties': true, + 'no_additional_properties': true, + 'show_errors': 'interaction' + }, + 'schema': schema, + }) }} + + {{ Captcha_widget({ + model: model, + attribute: 'captcha', + captchaAction: '/contact/default/captcha' + }) }} + + + + {{ active_form_end() }} +
+
+
+``` + +### "Thank you page" Twig layout + ```twig
{{ t('twig-widget', 'Thank you for your message') }}
``` @@ -213,6 +261,9 @@ Done: } ``` +## Export + +To enable the export feature add *kartik\grid\Module* to your project modules ## Giiant CRUDs From 2f238b69a86c81f24cadd5f0ade3d8064fa337e3 Mon Sep 17 00:00:00 2001 From: German Bisurgi Date: Tue, 13 Oct 2020 11:39:05 +0200 Subject: [PATCH 5/7] use boolean for table column instead of small int --- .../m201012_141325_alter_app_dmstr_contact_template_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/migrations/m201012_141325_alter_app_dmstr_contact_template_table.php b/src/migrations/m201012_141325_alter_app_dmstr_contact_template_table.php index 5dd776f..379c73f 100644 --- a/src/migrations/m201012_141325_alter_app_dmstr_contact_template_table.php +++ b/src/migrations/m201012_141325_alter_app_dmstr_contact_template_table.php @@ -6,7 +6,7 @@ class m201012_141325_alter_app_dmstr_contact_template_table extends Migration { public function safeUp() { - $this->addColumn('app_dmstr_contact_template','captcha','SMALLINT(6) NULL AFTER email_subject'); + $this->addColumn('app_dmstr_contact_template','captcha', $this->boolean()->notNull()->defaultValue(null)->after('email_subject')); } public function safeDown() From ffae86085e416212b95bccee4226b920f4de7df4 Mon Sep 17 00:00:00 2001 From: German Bisurgi Date: Tue, 13 Oct 2020 11:39:18 +0200 Subject: [PATCH 6/7] updated readme --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0c930db..c4da460 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,8 @@ Each form needs 2 Twig templates. Navigate to `/prototype/twig/index` to create While `FORM_NAME` must be replaced with your template name -* The form can be seen at `/contact/default/?schema=contact:FORM_NAME` -* The "thank you page" can be seen at `/contact/default/done?schema=contact:FORM_NAME` +* The form can be seen at `/contact/default/?schema=FORM_NAME` +* The "thank you page" can be seen at `/contact/default/done?schema=FORM_NAME` ### Form Twig layout @@ -118,6 +118,10 @@ While `FORM_NAME` must be replaced with your template name
``` + +* The background and foreground colors of the captcha can be defined in the settings module under the `captcha` section. +It uses a particular format for colors but the last 6 characters follow the css hex color code(eg 0xff0000 is red and 0x00ff00 is green) + ### "Thank you page" Twig layout ```twig From 47c5ef07099580eca5636dafba65cc83f70c1615 Mon Sep 17 00:00:00 2001 From: German Bisurgi Date: Tue, 13 Oct 2020 12:00:51 +0200 Subject: [PATCH 7/7] added settings for all attributes --- src/controllers/DefaultController.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/controllers/DefaultController.php b/src/controllers/DefaultController.php index b19cf54..447dd27 100644 --- a/src/controllers/DefaultController.php +++ b/src/controllers/DefaultController.php @@ -118,15 +118,19 @@ public function actionDone($schema) public function actions() { + $testLimit = Yii::$app->settings->getOrSet('testLimit', 100, 'captcha', 'integer'); + $width = Yii::$app->settings->getOrSet('width', 140, 'captcha', 'integer'); + $height = Yii::$app->settings->getOrSet('height', 75, 'captcha', 'integer'); + $offset = Yii::$app->settings->getOrSet('offset', -1, 'captcha', 'integer'); $backColor = Yii::$app->settings->getOrSet('backColor', '0x333333', 'captcha', 'string'); $foreColor = Yii::$app->settings->getOrSet('foreColor', '0xFFFFFF', 'captcha', 'string'); $actions = parent::actions(); $actions['captcha'] = [ 'class' => CaptchaAction::class, - 'testLimit' => 100, - 'width' => 140, - 'height' => 75, - 'offset' => -1, + 'testLimit' => $testLimit, + 'width' => $width, + 'height' => $height, + 'offset' => $offset, 'backColor' => hexdec($backColor), 'foreColor' => hexdec($foreColor) ];