diff --git a/README.md b/README.md index cd9b719..c4da460 100644 --- a/README.md +++ b/README.md @@ -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:` (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=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;') }} - -
-
- {% 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() }} +
+
+
+``` + + +* 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
{{ t('twig-widget', 'Thank you for your message') }}
``` @@ -213,6 +265,9 @@ Done: } ``` +## Export + +To enable the export feature add *kartik\grid\Module* to your project modules ## Giiant CRUDs diff --git a/src/controllers/DefaultController.php b/src/controllers/DefaultController.php index 9ca4feb..447dd27 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,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; + } + } 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..379c73f --- /dev/null +++ b/src/migrations/m201012_141325_alter_app_dmstr_contact_template_table.php @@ -0,0 +1,16 @@ +addColumn('app_dmstr_contact_template','captcha', $this->boolean()->notNull()->defaultValue(null)->after('email_subject')); + } + + public function safeDown() + { + $this->dropColumn('app_dmstr_contact_template','captcha'); + } +} 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; } } 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',