You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.4 KiB

7 years ago
<?php
7 years ago
namespace frontend\controllers;
use core\forms\SubscribeForm;
use core\services\newsletter\Newsletter;
use frontend\components\FrontendController;
7 years ago
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use frontend\components\SiteAccess;
use Yii;
/**
* Site controller
*/
class SiteController extends FrontendController
7 years ago
{
public $newletter;
7 years ago
public function __construct(string $id, $module, Newsletter $newsletter, array $config = [])
{
parent::__construct($id, $module, $config);
$this->newletter = $newsletter;
}
7 years ago
/**
7 years ago
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'only' => ['subscribe'],
7 years ago
'rules' => [
[
'actions' => ['subscribe'],
'allow' => true,
],
7 years ago
],
],
'verbs' => [
'class' => VerbFilter::class,
7 years ago
'actions' => [
'logout' => ['post'],
],
],
SiteAccess::class,
7 years ago
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
7 years ago
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
7 years ago
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
'transparent' => true,
7 years ago
],
];
}
/**
* Displays homepage.
*
* @return mixed
*/
public function actionIndex()
{
$this->layout = 'home';
7 years ago
return $this->render('index');
}
public function actionSubscribe()
{
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$form = new SubscribeForm();
if (Yii::$app->request->isAjax && $form->load(Yii::$app->request->post())) {
try {
$this->newletter->subscribe($form->email);
return ['result' => 'success'];
} catch (\RuntimeException $e) {
return ['result' => 'error', 'message' => $e->getMessage()];
}
}
7 years ago
return ['result' => 'error', 'message' => 'Request error'];
}
7 years ago
}