Skip to content

Commit

Permalink
Merge pull request #2 from germanbisurgi/feature/captcha
Browse files Browse the repository at this point in the history
Feature/captcha
  • Loading branch information
schmunk42 authored Oct 13, 2020
2 parents 024f082 + 47c5ef0 commit 2218de9
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 57 deletions.
165 changes: 110 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,129 @@
## 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:<formname>` (template in which the form will be rendered)
* `contact:<formname>:send` (will be rendered as "thank you page" after message has been send)
## Twig templates (Views)

While <formname> 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=FORM_NAME`
* The "thank you page" can be seen at `/contact/default/done?schema=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;') }}
<div class="row">
<div class="col-md-12">
{% 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,
}) }}
<button type="submit" class="btn btn-primary">{{ t('twig-widget', 'Send') }}</button>
{{ active_form_end() }}
<div class="container">
<div class="row">
<div class="col-md-12">
{% 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,
}) }}
<button type="submit" class="btn btn-primary">{{ t('twig-widget', 'Send') }}</button>
{{ active_form_end() }}
</div>
</div>
</div>
```

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') }}
<div class="container">
<div class="row">
<div class="col-md-12">
{% 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'
}) }}
<button type="submit" class="btn btn-primary">{{ t('twig-widget', 'Send') }}</button>
{{ active_form_end() }}
</div>
</div>
</div>
```


* 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
<div class="alert alert-success">{{ t('twig-widget', 'Thank you for your message') }}</div>
```
Expand Down Expand Up @@ -213,6 +265,9 @@ Done:
}
```

## Export

To enable the export feature add *kartik\grid\Module* to your project modules

## Giiant CRUDs

Expand Down
27 changes: 27 additions & 0 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -110,4 +116,25 @@ 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' => $testLimit,
'width' => $width,
'height' => $height,
'offset' => $offset,
'backColor' => hexdec($backColor),
'foreColor' => hexdec($foreColor)
];
return $actions;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use yii\db\Migration;

class m201012_141325_alter_app_dmstr_contact_template_table extends Migration
{
public function safeUp()
{
$this->addColumn('app_dmstr_contact_template','captcha', $this->boolean()->notNull()->defaultValue(null)->after('email_subject'));
}

public function safeDown()
{
$this->dropColumn('app_dmstr_contact_template','captcha');
}
}
5 changes: 4 additions & 1 deletion src/models/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/
class ContactForm extends Model
{
const SCENARIO_CAPTCHA = 'captcha';

public $contact_template_id;
public $json;
public $captcha;

public function getSchema()
{
Expand Down Expand Up @@ -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;
}
}
11 changes: 11 additions & 0 deletions src/models/ContactTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
];
}
}
3 changes: 3 additions & 0 deletions src/models/base/ContactTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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],
Expand All @@ -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'),
Expand Down
5 changes: 5 additions & 0 deletions src/views/crud/contact-template/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<!-- attribute to_email -->
<?php echo $form->field($model, 'to_email')->textInput(['maxlength' => true]) ?>

<!-- attribute captcha -->
<?php echo $form->field($model, 'captcha')->dropDownList(
dmstr\modules\contact\models\ContactTemplate::optscaptcha()
); ?>

<!-- attribute form_schema -->
<?php echo $form->field($model, 'form_schema')->textarea(['rows' => 6]) ?>

Expand Down
2 changes: 2 additions & 0 deletions src/views/crud/contact-template/_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

<?php // echo $form->field($model, 'email_subject') ?>

<?php // echo $form->field($model, 'captcha') ?>

<?php // echo $form->field($model, 'form_schema') ?>

<?php // echo $form->field($model, 'created_at') ?>
Expand Down
3 changes: 2 additions & 1 deletion src/views/crud/contact-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',*/
],
]); ?>
Expand Down
1 change: 1 addition & 0 deletions src/views/crud/contact-template/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
'name',
'from_email:email',
'to_email:email',
'captcha',
'form_schema:ntext',
'created_at',
'updated_at',
Expand Down

0 comments on commit 2218de9

Please sign in to comment.