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.
		
		
		
		
			
				
					95 lines
				
				2.2 KiB
			
		
		
			
		
	
	
					95 lines
				
				2.2 KiB
			| 
											8 years ago
										 | <?php
 | ||
|  | namespace frontend\controllers;
 | ||
|  | 
 | ||
|  | use core\forms\SubscribeForm;
 | ||
|  | use core\services\newsletter\Newsletter;
 | ||
|  | use yii\web\Controller;
 | ||
|  | use yii\filters\VerbFilter;
 | ||
|  | use yii\filters\AccessControl;
 | ||
|  | use frontend\components\SiteAccess;
 | ||
|  | use Yii;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * Site controller
 | ||
|  |  */
 | ||
|  | class SiteController extends Controller
 | ||
|  | {
 | ||
|  | 	public $newletter;
 | ||
|  | 
 | ||
|  | 	public function __construct( string $id, $module, Newsletter $newsletter, array $config = [] ) {
 | ||
|  | 		parent::__construct( $id, $module, $config );
 | ||
|  | 		$this->newletter = $newsletter;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  |      * @inheritdoc
 | ||
|  |      */
 | ||
|  |     public function behaviors()
 | ||
|  |     {
 | ||
|  |         return [
 | ||
|  |             'access' => [
 | ||
|  |                 'class' => AccessControl::className(),
 | ||
|  |                 'only' => ['subscribe'],
 | ||
|  |                 'rules' => [
 | ||
|  | 	                [
 | ||
|  | 		                'actions' => ['subscribe'],
 | ||
|  | 		                'allow' => true,
 | ||
|  | 	                ],
 | ||
|  |                 ],
 | ||
|  |             ],
 | ||
|  |             'verbs' => [
 | ||
|  |                 'class' => VerbFilter::className(),
 | ||
|  |                 'actions' => [
 | ||
|  |                     'logout' => ['post'],
 | ||
|  |                 ],
 | ||
|  |             ],
 | ||
|  | 	        SiteAccess::className(),
 | ||
|  |         ];
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * @inheritdoc
 | ||
|  |      */
 | ||
|  |     public function actions()
 | ||
|  |     {
 | ||
|  |         return [
 | ||
|  |             'error' => [
 | ||
|  |                 'class' => 'yii\web\ErrorAction',
 | ||
|  |             ],
 | ||
|  |             'captcha' => [
 | ||
|  |                 'class' => 'yii\captcha\CaptchaAction',
 | ||
|  |                 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
 | ||
|  | 	            'transparent' => true,
 | ||
|  |             ],
 | ||
|  |         ];
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * Displays homepage.
 | ||
|  |      *
 | ||
|  |      * @return mixed
 | ||
|  |      */
 | ||
|  |     public function actionIndex()
 | ||
|  |     {
 | ||
|  |     	$this->layout = 'home';
 | ||
|  |         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() ];
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		return ['result' => 'error', 'message' => 'Request error'];
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | }
 |