Advertisement
alpa_s

Untitled

Jun 15th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.54 KB | None | 0 0
  1. <?php
  2.  
  3. namespace frontend\controllers;
  4.  
  5. use common\components\MUrl;
  6. use common\models\User;
  7. use frontend\modules\user\models\LoginForm;
  8. use frontend\modules\user\models\SignupForm;
  9. use Yii;
  10. use frontend\models\ContactForm;
  11. use yii\web\Controller;
  12. use yii\web\Response;
  13. use yii\widgets\ActiveForm;
  14. use yii\filters\AccessControl;
  15.  
  16. /**
  17.  * Site controller
  18.  */
  19. class SiteController extends Controller
  20. {
  21.     public function behaviors()
  22.     {
  23.         echo microtime() . "<br>";
  24.         return [
  25.             'access' => [
  26.                 'class' => AccessControl::className(),
  27.                 'rules' => [
  28.                     [
  29.                         'actions' => ['index', 'terms-of-use'],
  30.                         'allow' => true,
  31.                         'roles' => ['?'],
  32.                     ],
  33.                     [
  34.                         'actions' => ['index', 'terms-of-use'],
  35.                         'allow' => false,
  36.                         'roles' => ['@'],
  37.                         'denyCallback' => function () {
  38.                             return Yii::$app->controller->redirect(MUrl::createDashboardUrl('/offer'));
  39.                         }
  40.                     ],
  41.                     [
  42.                         'actions' => ['error', 'contact'],
  43.                         'allow' => true,
  44.                     ],
  45.                 ],
  46.             ],
  47.         ];
  48.     }
  49.  
  50.  
  51.     /**
  52.      * @inheritdoc
  53.      */
  54.     public function actions()
  55.     {
  56.         echo microtime() . "<br>";
  57.         return [
  58.             'error' => [
  59.                 'class' => 'yii\web\ErrorAction'
  60.             ],
  61.             'captcha' => [
  62.                 'class' => 'yii\captcha\CaptchaAction',
  63.                 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null
  64.             ],
  65.             'set-locale' => [
  66.                 'class' => 'common\actions\SetLocaleAction',
  67.                 'locales' => array_keys(Yii::$app->params['availableLocales'])
  68.             ]
  69.         ];
  70.     }
  71.  
  72.     public function actionIndex()
  73.     {  
  74.         echo microtime() . "<br>";
  75.         if (!Yii::$app->user->isGuest) {
  76.             return $this->redirect(Yii::$app->urlManagerBackend->createAbsoluteUrl(''));
  77.         }
  78.  
  79.         $login = new LoginForm();
  80.         $signup = new SignupForm();
  81.  
  82.         //авторизация на главной
  83.         if (Yii::$app->request->isAjax) {
  84.             $login->load($_POST);
  85.             Yii::$app->response->format = Response::FORMAT_JSON;
  86.             return ActiveForm::validate($login);
  87.         }
  88.  
  89.         if ($login->load(Yii::$app->request->post()) && $login->login()) {
  90.             return $this->redirect(MUrl::createDashboardUrl('/offer'));
  91.         }
  92.  
  93.         //регистрация на главной
  94.         if ($signup->load(Yii::$app->request->post())) {
  95.             $refer = Yii::$app->request->get('refer');
  96.             if ($refer && User::findOne([$refer])) {
  97.                 $signup->refer = $refer;
  98.             }
  99.             $user = $signup->signup();
  100.             if ($user) {
  101.                 if ($signup->shouldBeActivated()) {
  102.                     Yii::$app->getSession()->setFlash('alert', [
  103.                         'body' => Yii::t(
  104.                             'frontend',
  105.                             'Your account has been successfully created. Check your email for further instructions.'
  106.                         ),
  107.                         'options' => ['class' => 'alert-success']
  108.                     ]);
  109.                 } else {
  110.                     Yii::$app->getUser()->login($user);
  111.                     return $this->refresh();
  112.                 }
  113.             }
  114.         }
  115.         return $this->render('index', ['signup' => $signup, 'login' => $login]);
  116.     }
  117.  
  118.     public function actionContact()
  119.     {
  120.         $model = new ContactForm();
  121.         if ($model->load(Yii::$app->request->post())) {
  122.             if ($model->contact(Yii::$app->params['adminEmail'])) {
  123.                 Yii::$app->getSession()->setFlash('alert', [
  124.                     'body' => Yii::t('frontend', 'Thank you for contacting us. We will respond to you as soon as possible.'),
  125.                     'options' => ['class' => 'alert-success']
  126.                 ]);
  127.                 return $this->refresh();
  128.             } else {
  129.                 Yii::$app->getSession()->setFlash('alert', [
  130.                     'body' => \Yii::t('frontend', 'There was an error sending email.'),
  131.                     'options' => ['class' => 'alert-danger']
  132.                 ]);
  133.             }
  134.         }
  135.         return $this->redirect(['site/index']);
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement