Your IP : 216.73.216.79


Current Path : /var/www/html/ebook/backend/controllers/
Upload File :
Current File : /var/www/html/ebook/backend/controllers/SiteController.php

<?php

namespace backend\controllers;

use Yii;
use backend\modules\mimin\components\AccessControl;
use common\models\User;
use yii\web\Controller;
use common\models\LoginForm;
use backend\models\PasswordResetRequestForm;
use backend\models\User as UserValidate;
// use yii\filters\AccessControl;
use yii\filters\VerbFilter;
//use backend\models\FileSurvey;

//use yii\web\AssetBundle;
/**
 * Site controller
 */
class SiteController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
//                 'rules' => [
//                     [
//                         'actions' => ['login', 'error'],
//                         'allow' => true,
//                     ],
//                     [
//                         'actions' => ['logout', 'index'],
//                         'allow' => true,
//                         'roles' => ['@'],
//                     ],
//                 ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex() {
        
        //if module dashboard has been setting to specific module 
        // config in backend/params
        $config = \Yii::$app->params['dashboardModule'];

        if(!empty($config)) {
            return Yii::$app->runAction($config['route'], $config['param']);
        }

        return $this->render('index');
    }

    public function actionLogin() {

        $this->layout = 'empty';

        if (!Yii::$app->user->isGuest) {
            $this->goBack();
        }
            $model = new LoginForm();
            $modelUser = new User();
            $modelForgotPass = new PasswordResetRequestForm;
            $modelValidate = new UserValidate();
            

                if ($model->load(Yii::$app->request->post()) && $model->login()) {

                    return $this->goBack();

                }else{
            
                    return $this->render('login', [
                            'model' => $model,
                            'modelUser' => $modelUser,
                            'modelForgotPass' => $modelForgotPass,
                            'validate' => $modelValidate,
                        ]);
                }
    }

    public function actionLogout() {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    public function actionRequestPasswordReset() {

        $model = new PasswordResetRequestForm();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
                Yii::$app->session->setFlash('success', Yii::t('app', 'Sila periksa emel anda untuk tindakan seterusnya.'));
                return $this->goHome();
            }
            return $this->refresh();
        } else {

            return $this->render('requestPasswordResetToken', [
                        'model' => $model,
            ]);
        }
    }

    public function actionForgotPassword() {

        if (isset($_POST['email'])) {
            $mailTo = $_POST['email'];

            $checkUser = User::find()->where("email='$mailTo'")->one();
            if (count($checkUser) > 0) {
                        Yii::$app->mailer->compose('email')
                        ->setFrom(Yii::$app->params['adminEmail'])
                        ->setTo($mailTo) //replace email sini untuk testing
                        ->setSubject('Admin - Terlupa kata laluan')
                        ->setHtmlBody('Kata Laluan baru: 12345qwer')
                        ->send();

                $checkUser->password_hash = Yii::$app->security->generatePasswordHash("12345qwer");
                $checkUser->update();
                return "BERJAYA\nAn emel telah dihantar.";
            } else {
                return "GAGAL\nEmel ini tidak wujud di dalam sistemxxxxx";
            }
        }
    }
    
    public function actionStub(){
        return $this->render('stub');
    }
}