Browse Source

Project information

master
Egorka 6 years ago
parent
commit
17838b1f23
  1. 204
      common/modules/forms/controllers/FormController.php
  2. 15
      composer.json

204
common/modules/forms/controllers/FormController.php

@ -6,7 +6,6 @@
namespace common\modules\forms\controllers; namespace common\modules\forms\controllers;
use common\modules\forms\entities\Form; use common\modules\forms\entities\Form;
use common\modules\forms\services\FormMessageManageService; use common\modules\forms\services\FormMessageManageService;
use frontend\components\FrontendController; use frontend\components\FrontendController;
@ -19,97 +18,114 @@ use Yii;
class FormController extends FrontendController class FormController extends FrontendController
{ {
public $mailer; public $mailer;
public $message_service; public $message_service;
public function __construct( string $id, $module, MailerInterface $mailer, FormMessageManageService $message_service, array $config = [] ) { public function __construct(
parent::__construct( $id, $module, $config ); string $id,
$this->mailer = $mailer; $module,
$this->message_service = $message_service; MailerInterface $mailer,
} FormMessageManageService $message_service,
array $config = []
public function actionSend($id) ) {
{ parent::__construct($id, $module, $config);
$form = $this->findModel($id); $this->mailer = $mailer;
$this->message_service = $message_service;
$className = 'DynaForm' . $id; }
$classPath = '\\common\\modules\\forms\\runtime\\' . $className;
public function actionSend($id)
/* @var $dynaForm Model */ {
$dynaForm = new $classPath(); $form = $this->findModel($id);
if ($dynaForm->load(Yii::$app->request->post()) && $dynaForm->validate()) { $className = 'DynaForm' . $id;
// send message $classPath = '\\common\\modules\\forms\\runtime\\' . $className;
$this->sendMessage($form->data, $form, $dynaForm);
/* @var $dynaForm Model */
if ($form->complete_page_id) { $dynaForm = new $classPath();
return $this->redirect(['/pages/page/view', 'id' => $form->complete_page_id]);
} if ($dynaForm->load(Yii::$app->request->post()) && $dynaForm->validate()) {
//return $form->complete_text; // send message
return $this->render('view', ['text' => $form->complete_text]); $this->sendMessage($form->data, $form, $dynaForm);
}
return $this->redirect(Yii::$app->request->referrer); if ($form->complete_page_id) {
} return $this->redirect(['/pages/page/view', 'id' => $form->complete_page_id]);
}
private function sendMessage($json_string, Form $form, $dynaForm)
{ //return $form->complete_text;
$messageItems = $this->prepareItems($json_string, $dynaForm); return $this->render('view', ['text' => $form->complete_text]);
// save message }
$this->message_service->create($form->id, Json::encode($messageItems, JSON_UNESCAPED_UNICODE));
//prepare e-mail message return $this->redirect(Yii::$app->request->referrer);
$to = isset($form->return) && $form->return ? explode(',',$form->return) : [Yii::$app->params['adminEmail']]; }
$from = isset($form->from) && $form->from ? explode(',',$form->from) : [Yii::$app->params['adminEmail']];
$reply = isset($form->reply) && $form->reply ? explode(',',$form->reply) : $from; private function sendMessage($json_string, Form $form, $dynaForm)
{
$sent = $this->mailer->compose( $messageItems = $this->prepareItems($json_string, $dynaForm);
['html' => '@common/modules/forms/mail/form-html', 'text' => '@common/modules/forms/mail/form-text'], // save message
['items' => $messageItems] $this->message_service->create($form->id, Json::encode($messageItems, JSON_UNESCAPED_UNICODE));
) //prepare e-mail message
->setTo($to) $to = isset($form->return) && $form->return ? explode(',', $form->return) : [Yii::$app->params['adminEmail']];
->setFrom($from) $from = isset($form->from) && $form->from ? explode(',', $form->from) : [Yii::$app->params['adminEmail']];
->setReplyTo($reply) $reply = isset($form->reply) && $form->reply ? explode(',', $form->reply) : $from;
->setSubject($form->subject)
->send(); $sent = $this->mailer->compose(
if (!$sent) { ['html' => '@common/modules/forms/mail/form-html', 'text' => '@common/modules/forms/mail/form-text'],
throw new \RuntimeException('Sending error.'); ['items' => $messageItems]
} )
} ->setTo($to)
->setFrom($from)
private function prepareItems($json_string, $dynaForm): array ->setReplyTo($reply)
{ ->setSubject($form->subject)
$items = []; ->send();
$json = Json::decode($json_string, true); if (!$sent) {
foreach ($json as $item) { throw new \RuntimeException('Sending error.');
if ($item['type'] == 'button') {continue;} }
if ($item['type'] == 'paragraph') {continue;} }
if ($item['type'] == 'header') {continue;}
private function prepareItems($json_string, $dynaForm): array
$item['name'] = str_replace( '-', '_', $item['name'] ); {
$items[] = [ $items = [];
'key' => isset($item['label']) ? $item['label'] : '', $json = Json::decode($json_string, true);
'value' => is_array($dynaForm->{$item['name']}) ? implode(', ', array_map(function($el){return Html::encode($el);}, $dynaForm->{$item['name']})) : Html::encode($dynaForm->{$item['name']}), foreach ($json as $item) {
]; if ($item['type'] == 'button') {
} continue;
return $items; }
} if ($item['type'] == 'paragraph') {
continue;
/*private function prepareMessage($json_string, $dynaForm): string }
{ if ($item['type'] == 'header') {
$message = ''; continue;
$json = Json::decode($json_string, true); }
foreach ($json as $item) {
if ($item['type'] == 'button') {continue;} $item['name'] = str_replace('-', '_', $item['name']);
$item['name'] = str_replace('-', '_', $item['name']); $items[] = [
$message.=$item['label'] . ': ' . Html::encode($dynaForm->{$item['name']}) . "\n"; 'key' => isset($item['label']) ? $item['label'] : '',
} 'value' => is_array($dynaForm->{$item['name']}) ? implode(', ', array_map(function ($el) {
return $message; return Html::encode($el);
}*/ }, $dynaForm->{$item['name']})) : Html::encode($dynaForm->{$item['name']}),
];
protected function findModel($id): Form }
{
if (($model = Form::findOne($id)) !== null) { return $items;
return $model; }
}
throw new NotFoundHttpException('The requested form does not exist.'); /*private function prepareMessage($json_string, $dynaForm): string
} {
$message = '';
$json = Json::decode($json_string, true);
foreach ($json as $item) {
if ($item['type'] == 'button') {continue;}
$item['name'] = str_replace('-', '_', $item['name']);
$message.=$item['label'] . ': ' . Html::encode($dynaForm->{$item['name']}) . "\n";
}
return $message;
}*/
protected function findModel($id): Form
{
if (($model = Form::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested form does not exist.');
}
} }

15
composer.json

@ -1,16 +1,13 @@
{ {
"name": "yiisoft/yii2-app-advanced", "name": "zertex/zxcms",
"description": "Yii 2 Advanced Project Template", "description": "Zertex CMS",
"keywords": ["yii2", "framework", "advanced", "project template"], "keywords": ["Zertex", "CMS", "yii2", "framework", "advanced"],
"homepage": "http://www.yiiframework.com/", "homepage": "https://cms.zertex.ru/",
"type": "project", "type": "project",
"license": "BSD-3-Clause", "license": "BSD-3-Clause",
"support": { "support": {
"issues": "https://github.com/yiisoft/yii2/issues?state=open", "issues": "https://gitlab.com/zertex/zxcms/issues",
"forum": "http://www.yiiframework.com/forum/", "source": "https://gitlab.com/zertex/zxcms"
"wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2"
}, },
"minimum-stability": "stable", "minimum-stability": "stable",
"require": { "require": {

Loading…
Cancel
Save