Skip to content

Commit 13b573e

Browse files
authored
Merge pull request #13 from dmstr/feature/marriage-2-x-3
Feature/marriage 2 x 3
2 parents a30e0f4 + 114c117 commit 13b573e

20 files changed

+508
-138
lines changed

README.md

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22

33
Navigate to `/contact/crud/contact-template` to create a new form template.
44

5-
* Name: a unique name, e.g. 'reservation'
6-
* From Email: valid email
7-
* To Email: valid email
8-
* 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)
9-
* 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
10-
)
5+
| property | required | description |
6+
| ---------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
7+
| Name | yes | a unique name, e.g. 'reservation' |
8+
| From Email | yes | valid email used as 'From:' header |
9+
| To Email | yes | one or more valid email addresses (comma separated) used as 'To:' header |
10+
| Reply To Email | no | optional, valid email. Use this if you want to set 'Reply-To:' header to a fixed address for ALL mails. If set 'Reply to Schema Property' will be irgnored! |
11+
| Reply to Schema Property | no | can be used to define which property from schema should be used (if valid mail!) as 'Reply-To:' header. If 'Reply To Email' is set to a fixed address, this will be ignored |
12+
| Return Path | no | if set and is a valid email, this will be used as 'Return-Path:' header where bounce Mails will be send to. Handle with care |
13+
| Captcha | no | this tells the contact module that a captcha will be used and that it has to validated against it (sets model scenario to captcha). Captcha widget is required in twig! |
14+
| Form Schema | yes | 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) |
15+
16+
## Upgrade hints:
17+
18+
- If the form was build with version <= 1.0.0 there was the convention, that property reply_to in your schema was used as 'Reply-To:' header in message.
19+
- this "magick" is removed! You must now set 'Reply to Schema Property' to 'reply_to' to get the same behavior!
1120

12-
## conventions:
1321

14-
* 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
1522

1623
## Twig templates (Views)
1724

@@ -128,6 +135,14 @@ It uses a particular format for colors but the last 6 characters follow the css
128135
<div class="alert alert-success">{{ t('twig-widget', 'Thank you for your message') }}</div>
129136
```
130137

138+
### Contact form widgets twig example
139+
140+
```twig
141+
{{ use('dmstr/modules/contact/widgets/ContactForm') }}
142+
143+
{{ contact_form_widget({schemaName: schemaName}) }}
144+
```
145+
131146
### Settings schema
132147

133148
```json
@@ -136,16 +151,6 @@ It uses a particular format for colors but the last 6 characters follow the css
136151
"type": "object",
137152
"format": "grid",
138153
"properties": {
139-
"reply_to": {
140-
"type": "string",
141-
"template": "{{email}}",
142-
"options": {
143-
"hidden": true
144-
},
145-
"watch": {
146-
"email": "Email"
147-
}
148-
},
149154
"Company": {
150155
"type": "string",
151156
"minLength": 3,
@@ -269,17 +274,3 @@ It uses a particular format for colors but the last 6 characters follow the css
269274

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

272-
## Giiant CRUDs
273-
274-
```bash
275-
yii giiant-batch \
276-
--tables=app_dmstr_contact_template,app_dmstr_contact_log \
277-
--tablePrefix=app_dmstr_ \
278-
--modelNamespace=dmstr\\modules\\contact\\models \
279-
--modelQueryNamespace=dmstr\\modules\\contact\\models\\query \
280-
--crudViewPath=@dmstr/modules/contact/views/crud \
281-
--crudControllerNamespace=dmstr\\modules\\contact\\controllers\\crud \
282-
--crudSearchModelNamespace=dmstr\\modules\\contact\\models\\search \
283-
--useTimestampBehavior=0 \
284-
--interactive=0
285-
```

src/controllers/DefaultController.php

Lines changed: 24 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22

33
namespace dmstr\modules\contact\controllers;
44

5-
use dmstr\modules\contact\events\MessageEvent;
6-
use dmstr\modules\contact\models\ContactForm;
5+
use dmstr\modules\contact\controllers\actions\SubmitAction;
76
use dmstr\modules\contact\models\ContactLog;
8-
use dmstr\modules\contact\models\ContactTemplate;
97
use dmstr\modules\contact\Module;
108
use yii;
119
use yii\captcha\CaptchaAction;
1210
use yii\helpers\Json;
1311
use yii\web\Controller;
14-
use yii\web\ForbiddenHttpException;
15-
use yii\web\HttpException;
16-
use yii\web\NotFoundHttpException;
1712

1813
/**
1914
* @property mixed $settings
@@ -25,87 +20,19 @@ class DefaultController extends Controller
2520

2621
public function init()
2722
{
28-
$this->layout = $this->module->frontendLayout;
2923
parent::init();
30-
}
31-
32-
/**
33-
* Renders the index view for the module
34-
*
35-
* @param $schema
36-
*
37-
* @return string
38-
* @throws HttpException
39-
* @throws yii\base\Exception
40-
* @throws yii\base\InvalidConfigException
41-
*/
42-
public function actionIndex($schema)
43-
{
44-
$contactSchema = ContactTemplate::findOne(['name' => $schema]);
45-
46-
if ($contactSchema === null) {
47-
throw new NotFoundHttpException('Page not found.');
48-
}
49-
50-
$contactForm = new ContactForm([
51-
'contact_template_id' => $contactSchema->id
52-
]);
53-
54-
if ($contactSchema->captcha === 1) {
55-
$contactForm->scenario = ContactForm::SCENARIO_CAPTCHA;
56-
}
57-
58-
59-
if ($contactForm->load(Yii::$app->request->post()) && $contactForm->validate()) {
60-
61-
$contactLog = new ContactLog([
62-
'contact_template_id' => $contactSchema->id,
63-
'json' => $contactForm->json,
64-
]);
65-
66-
if (!$contactLog->save()) {
67-
throw new HttpException(500, 'Your message could not be sent.');
68-
}
69-
70-
Yii::$app->session->set(self::CONTACT_FORM_ID_KEY, $contactLog->id);
71-
72-
$event = new MessageEvent();
73-
$event->model = $contactLog;
74-
$this->trigger(MessageEvent::EVENT_BEFORE_MESSAGE_SENT, $event);
75-
76-
if ($contactLog->sendMessage()) {
77-
Yii::$app->session->addFlash('success', Yii::t('contact', 'Your message was successfully sent.'));
78-
$this->redirect(['done', 'schema' => $schema]);
79-
80-
$this->trigger(MessageEvent::EVENT_AFTER_MESSAGE_SENT, $event);
81-
} else {
82-
Yii::$app->session->addFlash('error', Yii::t('contact', 'Your message could not be sent.'));
83-
$this->redirect(['index', 'schema' => $schema]);
84-
85-
$this->trigger(MessageEvent::EVENT_SENT_MESSAGE_ERROR, $event);
86-
}
87-
}
88-
89-
return $this->render(
90-
'index',
91-
[
92-
'model' => $contactForm,
93-
'schema' => $schema,
94-
'schemaData' => Json::decode($contactForm->schema)
95-
]
96-
);
24+
$this->layout = $this->module->frontendLayout;
9725
}
9826

9927
public function actionDone($schema)
10028
{
10129
$model = ContactLog::findOne(Yii::$app->session->get(self::CONTACT_FORM_ID_KEY));
30+
Yii::$app->session->remove(self::CONTACT_FORM_ID_KEY);
10231

10332
if ($model === null) {
104-
throw new ForbiddenHttpException(Yii::t('contact', 'You are not allowed to access this page directly.'));
33+
return $this->render('done-expired');
10534
}
10635

107-
Yii::$app->session->remove(self::CONTACT_FORM_ID_KEY);
108-
10936
return $this->render(
11037
'done',
11138
[
@@ -116,6 +43,18 @@ public function actionDone($schema)
11643

11744
}
11845

46+
/**
47+
* @param string $schema
48+
*
49+
* @return string
50+
*/
51+
public function actionForm($schema)
52+
{
53+
return $this->render('form', [
54+
'schema' => $schema
55+
]);
56+
}
57+
11958
public function actions()
12059
{
12160
$testLimit = Yii::$app->settings->getOrSet('testLimit', 100, 'captcha', 'integer');
@@ -131,9 +70,16 @@ public function actions()
13170
'width' => $width,
13271
'height' => $height,
13372
'offset' => $offset,
134-
'backColor' => hexdec($backColor),
73+
'backColor' => hexdec($backColor),
13574
'foreColor' => hexdec($foreColor)
13675
];
76+
$actions['index'] = [
77+
'class' => SubmitAction::class
78+
];
79+
$actions['form'] = [
80+
'class' => SubmitAction::class,
81+
'viewFile' => 'form'
82+
];
13783
return $actions;
13884
}
13985

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace dmstr\modules\contact\controllers\actions;
4+
5+
use dmstr\modules\contact\controllers\DefaultController;
6+
use dmstr\modules\contact\events\MessageEvent;
7+
use dmstr\modules\contact\models\ContactForm;
8+
use dmstr\modules\contact\models\ContactLog;
9+
use dmstr\modules\contact\models\ContactTemplate;
10+
use yii\base\Action;
11+
use yii\helpers\Json;
12+
use yii\web\HttpException;
13+
use yii\web\NotFoundHttpException;
14+
use Yii;
15+
16+
class SubmitAction extends Action
17+
{
18+
19+
/**
20+
* @var string
21+
*/
22+
public $viewFile = 'index';
23+
24+
/**
25+
* Submit a form and handle errors
26+
*
27+
* @param $schema
28+
*
29+
* @throws HttpException
30+
* @throws NotFoundHttpException
31+
* @return string
32+
*/
33+
public function run($schema)
34+
{
35+
$contactSchema = ContactTemplate::findOne(['name' => $schema]);
36+
37+
if ($contactSchema === null) {
38+
throw new NotFoundHttpException(Yii::t('contact', 'Page not found.'));
39+
}
40+
41+
$contactForm = new ContactForm([
42+
'contact_template_id' => $contactSchema->id
43+
]);
44+
45+
if ($contactSchema->captcha === 1) {
46+
$contactForm->scenario = ContactForm::SCENARIO_CAPTCHA;
47+
}
48+
49+
if ($contactForm->load($this->controller->request->post()) && $contactForm->validate()) {
50+
51+
$contactLog = new ContactLog([
52+
'contact_template_id' => $contactSchema->id,
53+
'json' => $contactForm->json,
54+
]);
55+
56+
if (!$contactLog->save()) {
57+
throw new HttpException(500, Yii::t('contact', 'Your message could not be sent.'));
58+
}
59+
60+
Yii::$app->session->set(DefaultController::CONTACT_FORM_ID_KEY, $contactLog->id);
61+
62+
$event = new MessageEvent();
63+
$event->model = $contactLog;
64+
$this->controller->trigger(MessageEvent::EVENT_BEFORE_MESSAGE_SENT, $event);
65+
66+
if ($contactLog->sendMessage()) {
67+
Yii::$app->session->addFlash('success', Yii::t('contact', 'Your message was successfully sent.'));
68+
$this->controller->redirect(['done', 'schema' => $schema]);
69+
70+
$this->controller->trigger(MessageEvent::EVENT_AFTER_MESSAGE_SENT, $event);
71+
} else {
72+
Yii::$app->session->addFlash('error', Yii::t('contact', 'Your message could not be sent.'));
73+
$this->controller->redirect([$this->viewFile, 'schema' => $schema]);
74+
75+
$this->controller->trigger(MessageEvent::EVENT_SENT_MESSAGE_ERROR, $event);
76+
}
77+
}
78+
79+
return $this->controller->render(
80+
$this->viewFile,
81+
[
82+
'model' => $contactForm,
83+
'schema' => $schema,
84+
'schemaData' => Json::decode($contactSchema->form_schema)
85+
]
86+
);
87+
}
88+
}

src/migrations/m201012_141325_alter_app_dmstr_contact_template_table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ class m201012_141325_alter_app_dmstr_contact_template_table extends Migration
66
{
77
public function safeUp()
88
{
9-
$this->addColumn('app_dmstr_contact_template','captcha', $this->boolean()->notNull()->defaultValue(null)->after('email_subject'));
9+
$this->addColumn('{{%dmstr_contact_template}}','captcha', $this->boolean()->notNull()->defaultValue(null)->after('email_subject'));
1010
}
1111

1212
public function safeDown()
1313
{
14-
$this->dropColumn('app_dmstr_contact_template','captcha');
14+
$this->dropColumn('{{%dmstr_contact_template}}','captcha');
1515
}
1616
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
class m230214_071325_add_return_path_2_app_dmstr_contact_template_table extends Migration
6+
{
7+
public function safeUp()
8+
{
9+
10+
$this->addColumn('{{%dmstr_contact_template}}','return_path', $this->string()->null()->defaultValue(null)->after('to_email'));
11+
}
12+
13+
public function safeDown()
14+
{
15+
$this->dropColumn('{{%dmstr_contact_template}}','return_path');
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
class m230214_081325_add_reply_to_schema_property_2_app_dmstr_contact_template_table extends Migration
6+
{
7+
public function safeUp()
8+
{
9+
10+
$this->addColumn('{{%dmstr_contact_template}}','reply_to_schema_property', $this->string()->null()->defaultValue(null)->after('return_path'));
11+
}
12+
13+
public function safeDown()
14+
{
15+
$this->dropColumn('{{%dmstr_contact_template}}','reply_to_schema_property');
16+
}
17+
}

0 commit comments

Comments
 (0)