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.

100 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;
3 years ago
use RuntimeException;
7 years ago
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use frontend\components\SiteAccess;
use Yii;
3 years ago
use yii\web\Response;
7 years ago
/**
* Site controller
*/
class SiteController extends FrontendController
7 years ago
{
3 years ago
public Newsletter $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.
*
3 years ago
* @return string
7 years ago
*/
3 years ago
public function actionIndex(): string
7 years ago
{
$this->layout = 'home';
7 years ago
return $this->render('index');
}
3 years ago
public function actionSubscribe(): array
{
3 years ago
Yii::$app->response->format = 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'];
3 years ago
} catch (RuntimeException $e) {
return ['result' => 'error', 'message' => $e->getMessage()];
}
}
7 years ago
return ['result' => 'error', 'message' => 'Request error'];
}
7 years ago
}