Commit e62d1f9133aa091438a761d5d418aaab1fbff2ce

Authored by Alexey Boroda
1 parent 44ff20b4

-Emails on feedback and in settings ready

backend/controllers/SettingsController.php
1 <?php 1 <?php
2 namespace backend\controllers; 2 namespace backend\controllers;
3 3
  4 + use common\models\Mail;
4 use common\models\Settings; 5 use common\models\Settings;
5 use yii\base\InvalidConfigException; 6 use yii\base\InvalidConfigException;
6 use yii\filters\AccessControl; 7 use yii\filters\AccessControl;
@@ -47,6 +48,11 @@ @@ -47,6 +48,11 @@
47 public function actionIndex() 48 public function actionIndex()
48 { 49 {
49 $model = $this->findSettings(); 50 $model = $this->findSettings();
  51 + $mail = Mail::findOne(1);
  52 +
  53 + if ($mail->load(\Yii::$app->request->post())) {
  54 + $mail->save();
  55 + }
50 56
51 if ($model->load(Yii::$app->request->post()) && $model->save()) { 57 if ($model->load(Yii::$app->request->post()) && $model->save()) {
52 Yii::$app->session->setFlash('success', \Yii::t('core', 'Settings saved')); 58 Yii::$app->session->setFlash('success', \Yii::t('core', 'Settings saved'));
@@ -58,6 +64,7 @@ @@ -58,6 +64,7 @@
58 'settings', 64 'settings',
59 [ 65 [
60 'model' => $model, 66 'model' => $model,
  67 + 'mail' => $mail,
61 ] 68 ]
62 ); 69 );
63 } 70 }
backend/views/settings/_mail_tab.php 0 → 100644
  1 +<?php
  2 +
  3 + use common\models\Mail;
  4 + use yii\bootstrap\ActiveForm;
  5 + use yii\web\View;
  6 +
  7 + /**
  8 + * @var View $this
  9 + * @var Mail $mail
  10 + * @var ActiveForm $form
  11 + */
  12 +
  13 + echo $form->field($mail, 'host')
  14 + ->textInput();
  15 +
  16 + echo $form->field($mail, 'user')
  17 + ->textInput();
  18 +
  19 + echo $form->field($mail, 'pass')
  20 + ->textInput();
0 \ No newline at end of file 21 \ No newline at end of file
backend/views/settings/settings.php
@@ -2,8 +2,10 @@ @@ -2,8 +2,10 @@
2 /** 2 /**
3 * @var View $this 3 * @var View $this
4 * @var Settings $model 4 * @var Settings $model
  5 + * @var Mail $mail
5 */ 6 */
6 7
  8 + use common\models\Mail;
7 use common\models\Settings; 9 use common\models\Settings;
8 use yii\bootstrap\ActiveForm; 10 use yii\bootstrap\ActiveForm;
9 use yii\bootstrap\Html; 11 use yii\bootstrap\Html;
@@ -26,7 +28,7 @@ @@ -26,7 +28,7 @@
26 28
27 <?php 29 <?php
28 $form = ActiveForm::begin(); 30 $form = ActiveForm::begin();
29 - 31 +
30 echo Tabs::widget( 32 echo Tabs::widget(
31 [ 33 [
32 'options' => [ 34 'options' => [
@@ -73,10 +75,20 @@ @@ -73,10 +75,20 @@
73 ] 75 ]
74 ), 76 ),
75 ], 77 ],
  78 + [
  79 + 'label' => \Yii::t('core', 'Email'),
  80 + 'content' => $this->render(
  81 + '_mail_tab',
  82 + [
  83 + 'form' => $form,
  84 + 'mail' => $mail,
  85 + ]
  86 + ),
  87 + ],
76 ], 88 ],
77 ] 89 ]
78 ); 90 );
79 - 91 +
80 echo Html::submitButton( 92 echo Html::submitButton(
81 'Save', 93 'Save',
82 [ 94 [
common/config/.gitignore
@@ -2,4 +2,5 @@ main-local.php @@ -2,4 +2,5 @@ main-local.php
2 db* 2 db*
3 params-local.php 3 params-local.php
4 test-local.php 4 test-local.php
5 -settings.php  
6 \ No newline at end of file 5 \ No newline at end of file
  6 +settings.php
  7 +mail.php
7 \ No newline at end of file 8 \ No newline at end of file
common/config/mail.php 0 → 100755
  1 +<?php
  2 +
  3 +return [
  4 + 1 => [
  5 + 'host' => 'smtp.gmail.com',
  6 + 'user' => 'kennen.md@gmail.com',
  7 + 'pass' => 'nobeernoparty',
  8 + 'id' => 1,
  9 + ],
  10 +];
0 \ No newline at end of file 11 \ No newline at end of file
common/config/main.php
1 <?php 1 <?php
2 use artbox\core\components\SeoComponent; 2 use artbox\core\components\SeoComponent;
3 3
  4 + $mail = require( 'mail.php' );
  5 +
4 return [ 6 return [
5 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', 7 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
6 'modules' => [ 8 'modules' => [
@@ -22,11 +24,11 @@ @@ -22,11 +24,11 @@
22 ], 24 ],
23 'i18n' => [ 25 'i18n' => [
24 'translations' => [ 26 'translations' => [
25 - 'core' => [ 27 + 'core' => [
26 'class' => 'yii\i18n\PhpMessageSource', 28 'class' => 'yii\i18n\PhpMessageSource',
27 'basePath' => '@artbox/core/messages', 29 'basePath' => '@artbox/core/messages',
28 ], 30 ],
29 - 'app' => [ 31 + 'app' => [
30 'class' => 'yii\i18n\PhpMessageSource', 32 'class' => 'yii\i18n\PhpMessageSource',
31 'basePath' => '@common/messages', 33 'basePath' => '@common/messages',
32 ], 34 ],
@@ -51,5 +53,17 @@ @@ -51,5 +53,17 @@
51 'useFilename' => true, 53 'useFilename' => true,
52 'absoluteUrl' => false, 54 'absoluteUrl' => false,
53 ], 55 ],
  56 + 'smtpmailer' => [
  57 + 'class' => 'yii\swiftmailer\Mailer',
  58 + 'transport' => [
  59 + 'class' => 'Swift_SmtpTransport',
  60 + 'host' => $mail[ 1 ][ 'host' ],
  61 + 'username' => $mail[ 1 ][ 'user' ],
  62 + 'password' => $mail[ 1 ][ 'pass' ],
  63 + 'port' => '587',
  64 + 'encryption' => 'tls',
  65 + ],
  66 + 'viewPath' => '@common/mail'
  67 + ],
54 ], 68 ],
55 ]; 69 ];
common/config/settings.php
@@ -2,32 +2,32 @@ @@ -2,32 +2,32 @@
2 2
3 return [ 3 return [
4 1 => [ 4 1 => [
5 - 'phone' => '+38 (044) 593-73-76',  
6 - 'phone2' => '+38 (098) 468-07-64',  
7 - 'skype' => 'artwebstudio',  
8 - 'email' => 'artweb.ua@gmail.com',  
9 - 'house' => '1-М',  
10 - 'street' => 'пр. М. Бажана',  
11 - 'office' => '25',  
12 - 'city' => 'Киев',  
13 - 'country' => 'Украина',  
14 - 'lat' => '50.403696',  
15 - 'lon' => '30.641481', 5 + 'phone' => '+38 (044) 593-73-76',
  6 + 'phone2' => '+38 (098) 468-07-64',
  7 + 'skype' => 'artwebstudio',
  8 + 'email' => 'kennen.md@gmail.com',
  9 + 'house' => '1-М',
  10 + 'street' => 'пр. М. Бажана',
  11 + 'office' => '25',
  12 + 'city' => 'Киев',
  13 + 'country' => 'Украина',
  14 + 'lat' => '50.403696',
  15 + 'lon' => '30.641481',
16 'facebook' => 'https://www.facebook.com/ArtWeb.ua/', 16 'facebook' => 'https://www.facebook.com/ArtWeb.ua/',
17 - 'vk' => 'https://vk.com/artwebua',  
18 - 'ok' => 'https://ok.ru/artwebua',  
19 - 'google' => 'https://plus.google.com/+ArtwebUaAgency',  
20 - 'twitter' => 'https://twitter.com/ArtWeb_ua',  
21 - 'name' => 'Artweb',  
22 - 'logo' => '11',  
23 - 'about' => 'Строим бизнес в онлайне', 17 + 'vk' => 'https://vk.com/artwebua',
  18 + 'ok' => 'https://ok.ru/artwebua',
  19 + 'google' => 'https://plus.google.com/+ArtwebUaAgency',
  20 + 'twitter' => 'https://twitter.com/ArtWeb_ua',
  21 + 'name' => 'Artweb',
  22 + 'logo' => null,
  23 + 'about' => 'Строим бизнес в онлайне',
24 'analytics_key' => '119240817', 24 'analytics_key' => '119240817',
25 - 'robots' => 'User-agent: Google 25 + 'robots' => 'User-agent: Google
26 Disallow: 26 Disallow:
27 ', 27 ',
28 - 'ga_code' => '796967',  
29 - 'ya_code' => '08908908',  
30 - 'tag_manager' => '',  
31 - 'id' => 1, 28 + 'ga_code' => '796967',
  29 + 'ya_code' => '08908908',
  30 + 'tag_manager' => '',
  31 + 'id' => 1,
32 ], 32 ],
33 ]; 33 ];
34 \ No newline at end of file 34 \ No newline at end of file
common/mail/feedback.php 0 → 100644
  1 +<?php
  2 + use artbox\core\models\Feedback;
  3 + use yii\web\View;
  4 +
  5 + /**
  6 + * @var View $this
  7 + * @var Feedback $model
  8 + */
  9 +
  10 +?>
  11 +
  12 +<table>
  13 + <tbody>
  14 + <tr>
  15 + <td><b><?= \Yii::t('app', 'Name: ') ?></b></td>
  16 + <td><?= $model->name ?></td>
  17 + </tr>
  18 + <tr>
  19 + <td><b><?= \Yii::t('app', 'Phone: ') ?></b></td>
  20 + <td><?= $model->phone ?></td>
  21 + </tr>
  22 + <tr>
  23 + <td><b><?= \Yii::t('app', 'Email: ') ?></b></td>
  24 + <td><?= $model->email ?></td>
  25 + </tr>
  26 + <tr>
  27 + <td><b><?= \Yii::t('app', 'Message: ') ?></b></td>
  28 + <td><?= $model->message ?></td>
  29 + </tr>
  30 + </tbody>
  31 +</table>
common/models/Mail.php 0 → 100644
  1 +<?php
  2 +
  3 + namespace common\models;
  4 +
  5 + use yii2tech\filedb\ActiveRecord;
  6 +
  7 + /**
  8 + * Class Mail
  9 + *
  10 + * @property string $host
  11 + * @property string $user
  12 + * @property string $pass
  13 + * @package common\models
  14 + */
  15 + class Mail extends ActiveRecord
  16 + {
  17 + /**
  18 + * @inheritdoc
  19 + */
  20 + public static function fileName()
  21 + {
  22 + return 'mail';
  23 + }
  24 +
  25 + public function rules()
  26 + {
  27 + return [
  28 + [
  29 + [
  30 + 'user',
  31 + 'host',
  32 + 'pass',
  33 + ],
  34 + 'string',
  35 + ],
  36 + ];
  37 + }
  38 + }
0 \ No newline at end of file 39 \ No newline at end of file
frontend/controllers/SiteController.php
@@ -4,9 +4,10 @@ @@ -4,9 +4,10 @@
4 use artbox\core\models\Feedback; 4 use artbox\core\models\Feedback;
5 use common\models\Settings; 5 use common\models\Settings;
6 use Yii; 6 use Yii;
  7 + use yii\filters\VerbFilter;
  8 + use yii\swiftmailer\Mailer;
7 use yii\web\BadRequestHttpException; 9 use yii\web\BadRequestHttpException;
8 use yii\web\Controller; 10 use yii\web\Controller;
9 - use yii\filters\VerbFilter;  
10 use yii\web\Response; 11 use yii\web\Response;
11 12
12 /** 13 /**
@@ -101,11 +102,30 @@ @@ -101,11 +102,30 @@
101 public function actionFeedback() 102 public function actionFeedback()
102 { 103 {
103 Yii::$app->response->format = Response::FORMAT_JSON; 104 Yii::$app->response->format = Response::FORMAT_JSON;
104 - if (empty( Yii::$app->request->post() )) { 105 +
  106 + /**
  107 + * @var Mailer $mailer
  108 + */
  109 + $mailer = \Yii::$app->get('smtpmailer');
  110 + $settings = Settings::getInstance();
  111 +
  112 + if (empty(Yii::$app->request->post())) {
105 throw new BadRequestHttpException(); 113 throw new BadRequestHttpException();
106 } else { 114 } else {
107 $model = new Feedback(); 115 $model = new Feedback();
108 if ($model->load(Yii::$app->request->post()) && $model->save()) { 116 if ($model->load(Yii::$app->request->post()) && $model->save()) {
  117 +
  118 + $mailer->compose(
  119 + 'feedback',
  120 + [
  121 + 'model' => $model,
  122 + ]
  123 + )
  124 + ->setFrom('artbox@domain.com')
  125 + ->setTo($settings->email)
  126 + ->setSubject(\Yii::t('app', 'Feedback'))
  127 + ->send();
  128 +
109 return [ 129 return [
110 'success' => true, 130 'success' => true,
111 'message' => 'Success message', 131 'message' => 'Success message',