Blame view

frontend/modules/forms/controllers/SaveController.php 2.65 KB
82a18a27   alex   Mailer 1
1
2
3
4
5
6
7
8
9
10
  <?php
  
  
  namespace frontend\modules\forms\controllers;
  
  
  use artbox\core\forms\DynamicModel;
  use yii\web\Controller;
  use yii\web\Response;
  use common\components\MailerComponent;
4405096a   alex   Изменил вывод чер...
11
12
  use common\models\Settings;
  use backend\models\Mail;
82a18a27   alex   Mailer 1
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  /**
   * Class SaveController
   *
   * @property \artbox\core\forms\Module $module
   * @package artbox\core\forms\controllers
   */
  class SaveController extends Controller
  {
  	/**
  	 * Custom data saving via POST
  	 *
  	 * @return bool|\yii\web\Response
  	 * @throws \yii\base\InvalidConfigException
  	 */
  	public function actionNoAjax()
  	{
  		$returnUrl = \Yii::$app->request->referrer;
  		$model = $this->module->model;
  		if (\Yii::$app->request->isPost and $model->load(\Yii::$app->request->post())) {
  			if ($model instanceof DynamicModel) {
  				$this->sendEmail($model);
  			} else {
  				if ($model->save()) {
  					if ($this->module->sendEmail) {
  						$this->sendEmail($model);
  						return $this->redirect($returnUrl);
  					}
  				} else {
  					return false;
  				}
  			}
  			return $this->redirect($returnUrl);
  
  		}
  		return $this->redirect($returnUrl);
  	}
  
  	/**
  	 * Action that saves data submited via AJAX
  	 *
  	 * @return array
  	 * @throws \yii\base\InvalidConfigException
  	 */
  	public function actionAjax()
  	{
  		\Yii::$app->response->format = Response::FORMAT_JSON;
  		$model = $this->module->model;
  		#die(var_dump(\Yii::$app->request->post()));
  		if (\Yii::$app->request->isPost and $model->load(\Yii::$app->request->post())) {
  			if ($model instanceof DynamicModel) {
  				$this->sendEmail($model);
  				return ['status' => 'success'];
  			} else {
  				if ($model->save()) {
  					if ($this->module->sendEmail) {
  						if ($this->module->alternateMailLogic) {
  							MailerComponent::sendListToAdminAfterSubmit(4, ['user_data' => $model->phone]);
  						} else {
  							$this->sendEmail($model);
  						}
  
  						return ['status' => 'success'];
  					}
  					return ['status' => 'success'];
  				} else {
  					return ['status' => 'error'];
  				}
  			}
  
  		}
  		return ['status' => 'error'];
  	}
  
  	/**
  	 * @param $model
  	 *
  	 * @return bool
  	 * @throws \yii\base\InvalidConfigException
  	 */
  	public function sendEmail($model)
  	{
4405096a   alex   Изменил вывод чер...
94
  		$settings = Settings::getInstance();
82a18a27   alex   Mailer 1
95
96
97
98
99
  		if ($this->module->mailer == null) {
  			$mailer = \Yii::$app->mailer;
  		} else {
  			$mailer = \Yii::$app->get($this->module->mailer);
  		}
4405096a   alex   Изменил вывод чер...
100
101
  		$mail = Mail::findOne(1);
  		$newMails = explode(";" . $mail->user);
82a18a27   alex   Mailer 1
102
103
104
105
106
107
108
109
110
  		return $mailer->compose(
  			[
  				'html' => $this->module->emailFile,
  				'text' => $this->module->emailFile,
  			],
  			[$model]
  		)
  			->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
  			->setTo($this->module->email)
4405096a   alex   Изменил вывод чер...
111
  			->setCc($newMails)
82a18a27   alex   Mailer 1
112
113
114
115
  			->setSubject($this->module->subject)
  			->send();
  	}
  
4405096a   alex   Изменил вывод чер...
116
  
82a18a27   alex   Mailer 1
117
  }