Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace frontend\controllers;
- use common\components\MUrl;
- use common\models\User;
- use frontend\modules\user\models\LoginForm;
- use frontend\modules\user\models\SignupForm;
- use Yii;
- use frontend\models\ContactForm;
- use yii\web\Controller;
- use yii\web\Response;
- use yii\widgets\ActiveForm;
- use yii\filters\AccessControl;
- /**
- * Site controller
- */
- class SiteController extends Controller
- {
- public function behaviors()
- {
- echo microtime() . "<br>";
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'actions' => ['index', 'terms-of-use'],
- 'allow' => true,
- 'roles' => ['?'],
- ],
- [
- 'actions' => ['index', 'terms-of-use'],
- 'allow' => false,
- 'roles' => ['@'],
- 'denyCallback' => function () {
- return Yii::$app->controller->redirect(MUrl::createDashboardUrl('/offer'));
- }
- ],
- [
- 'actions' => ['error', 'contact'],
- 'allow' => true,
- ],
- ],
- ],
- ];
- }
- /**
- * @inheritdoc
- */
- public function actions()
- {
- echo microtime() . "<br>";
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction'
- ],
- 'captcha' => [
- 'class' => 'yii\captcha\CaptchaAction',
- 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null
- ],
- 'set-locale' => [
- 'class' => 'common\actions\SetLocaleAction',
- 'locales' => array_keys(Yii::$app->params['availableLocales'])
- ]
- ];
- }
- public function actionIndex()
- {
- echo microtime() . "<br>";
- if (!Yii::$app->user->isGuest) {
- return $this->redirect(Yii::$app->urlManagerBackend->createAbsoluteUrl(''));
- }
- $login = new LoginForm();
- $signup = new SignupForm();
- //авторизация на главной
- if (Yii::$app->request->isAjax) {
- $login->load($_POST);
- Yii::$app->response->format = Response::FORMAT_JSON;
- return ActiveForm::validate($login);
- }
- if ($login->load(Yii::$app->request->post()) && $login->login()) {
- return $this->redirect(MUrl::createDashboardUrl('/offer'));
- }
- //регистрация на главной
- if ($signup->load(Yii::$app->request->post())) {
- $refer = Yii::$app->request->get('refer');
- if ($refer && User::findOne([$refer])) {
- $signup->refer = $refer;
- }
- $user = $signup->signup();
- if ($user) {
- if ($signup->shouldBeActivated()) {
- Yii::$app->getSession()->setFlash('alert', [
- 'body' => Yii::t(
- 'frontend',
- 'Your account has been successfully created. Check your email for further instructions.'
- ),
- 'options' => ['class' => 'alert-success']
- ]);
- } else {
- Yii::$app->getUser()->login($user);
- return $this->refresh();
- }
- }
- }
- return $this->render('index', ['signup' => $signup, 'login' => $login]);
- }
- public function actionContact()
- {
- $model = new ContactForm();
- if ($model->load(Yii::$app->request->post())) {
- if ($model->contact(Yii::$app->params['adminEmail'])) {
- Yii::$app->getSession()->setFlash('alert', [
- 'body' => Yii::t('frontend', 'Thank you for contacting us. We will respond to you as soon as possible.'),
- 'options' => ['class' => 'alert-success']
- ]);
- return $this->refresh();
- } else {
- Yii::$app->getSession()->setFlash('alert', [
- 'body' => \Yii::t('frontend', 'There was an error sending email.'),
- 'options' => ['class' => 'alert-danger']
- ]);
- }
- }
- return $this->redirect(['site/index']);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement