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.
		
		
		
		
		
			
		
			
				
					
					
						
							97 lines
						
					
					
						
							2.4 KiB
						
					
					
				
			
		
		
	
	
							97 lines
						
					
					
						
							2.4 KiB
						
					
					
				<?php | 
						|
 | 
						|
namespace frontend\controllers; | 
						|
 | 
						|
use core\forms\SubscribeForm; | 
						|
use core\services\newsletter\Newsletter; | 
						|
use frontend\components\FrontendController; | 
						|
use yii\filters\VerbFilter; | 
						|
use yii\filters\AccessControl; | 
						|
use frontend\components\SiteAccess; | 
						|
use Yii; | 
						|
 | 
						|
/** | 
						|
 * Site controller | 
						|
 */ | 
						|
class SiteController extends FrontendController | 
						|
{ | 
						|
    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::class, | 
						|
                'only'  => ['subscribe'], | 
						|
                'rules' => [ | 
						|
                    [ | 
						|
                        'actions' => ['subscribe'], | 
						|
                        'allow'   => true, | 
						|
                    ], | 
						|
                ], | 
						|
            ], | 
						|
            'verbs'  => [ | 
						|
                'class'   => VerbFilter::class, | 
						|
                'actions' => [ | 
						|
                    'logout' => ['post'], | 
						|
                ], | 
						|
            ], | 
						|
            SiteAccess::class, | 
						|
        ]; | 
						|
    } | 
						|
 | 
						|
    /** | 
						|
     * @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']; | 
						|
    } | 
						|
}
 | 
						|
 |