Your IP : 216.73.216.79


Current Path : /var/www/html/audit/backend/modules/fruserdynav2/controllers/
Upload File :
Current File : /var/www/html/audit/backend/modules/fruserdynav2/controllers/FrontendUserController.php

<?php

namespace backend\modules\fruserdynav2\controllers;

use Yii;
use backend\modules\fruserdynav2\models\FrontendUser;
use backend\modules\fruserdynav2\models\FrontendUserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\components\AccessRule;
use yii\web\UploadedFile;
use yii\helpers\Url;

/**
 * FrontendUserController implements the CRUD actions for FrontendUser model.
 */
class FrontendUserController extends Controller {

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'ruleConfig' => [
                    'class' => AccessRule::className(),
                ],
                'rules' => [
                    [
                        'actions' => ['index', 'create', 'update', 'view', 'delete', 'resetpwd', 'getintranetdetails',
                            'suspend', 'activate', 'approvalstatus', 'refundstatus', 'migrate', 'setretired'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all FrontendUser models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new FrontendUserSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider,]);
    }

    /**
     * Displays a single FrontendUser model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', ['model' => $this->findModel($id),]);
    }

    /**
     * Creates a new FrontendUser model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionResetpwd() {
        $output = "failed";
        if (($model = FrontendUser::findOne($_POST['id'])) !== null) {
            $pwd = Yii::$app->security->generateRandomString(10);
            $model->password_hash = \Yii::$app->security->generatePasswordHash($pwd);
            if ($model->save(false)) {

                $url = 'https://' . $_SERVER['SERVER_NAME'] . '/portal-main/login';
                $to_mail = $model->email;
                $subject_mail = 'Portal Login Details';
                $content_mail = '<p>Hi ' . $model->fullname . ',</p>
                            <p>You now can access the portal as user using below details:</p>
                            <p>URL: <a href="' . $url . '" target="_blank">' . $url . '</a></p>
                            <p>Username: ' . $model->username . '</p>
                            <p>Password: ' . $pwd . '</p>
                            <p>- Admin -</p>';

                if (!empty($to_mail))
                    Yii::$app->emailModule->sendemail($to_mail, $subject_mail, $content_mail);

                $output = 'success';
            }
        }
        return $output;
    }

    public function actionCreate() {
        $model = new FrontendUser();
        $model->scenario = 'create';
        if ($model->load(Yii::$app->request->post())) {
            $pwd = $model->password;
            $model->created_at = date("Y-m-d H:i:s");
            $model->created_by = Yii::$app->user->identity->id;
            $model->created_user_type = 'backend_user';
            $model->updated_at = date("Y-m-d H:i:s");
            $model->updated_by = Yii::$app->user->identity->id;
            $model->updated_user_type = 'backend_user';
            $model->auth_key = Yii::$app->security->generateRandomString();
            $model->password_hash = Yii::$app->security->generatePasswordHash($model->password);
            $model->img_url = UploadedFile::getInstance($model, 'img_url');
            if (!empty($model->img_url)) {
                $img_url = 'user_' . date("YmdHis") . '.' . $model->img_url->extension;
                if ($model->img_url->saveAs('../uploads/' . $img_url))
                    $model->img_url = $img_url;
            }
            if ($model->save()) {
                $url = 'https://' . $_SERVER['SERVER_NAME'] . '/portal-main/login';
                $to_mail = $model->email;
                $subject_mail = 'Portal Login Details';
                $content_mail = '<p>Hi ' . $model->fullname . ',</p>
                            <p>You now can access the portal as user using below details:</p>
                            <p>URL: <a href="' . $url . '" target="_blank">' . $url . '</a></p>
                            <p>Username: ' . $model->username . '</p>
                            <p>Password: ' . $pwd . '</p>
                            <p>- Admin -</p>';

                if (!empty($to_mail))
                    Yii::$app->emailModule->sendemail($to_mail, $subject_mail, $content_mail);

                $this->getView()->registerJs("swal.fire({
                    title:'User has been added',
                    icon: 'success', 
                    text: 'Login details have been emailed to the user', 
                }).then(function(result){
                        window.location = '" . Url::toRoute('index') . "';
                });");
            }
        }
        return $this->render('create', ['model' => $model,]);
    }

    /**
     * Updates an existing FrontendUser model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        $original_img_url = $model->img_url;

        if ($model->load(Yii::$app->request->post())) {
            $model->updated_at = date("Y-m-d H:i:s");
            $model->updated_by = Yii::$app->user->identity->id;
            $model->updated_user_type = 'backend_user';

            $model->img_url = UploadedFile::getInstance($model, 'img_url');
            if (!empty($model->img_url)) {
                $img_url = 'user_' . date("YmdHis") . '.' . $model->img_url->extension;
                if ($model->img_url->saveAs('../uploads/' . $img_url))
                    $model->img_url = $img_url;
            } else {
                $model->img_url = $original_img_url;
            }

            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing FrontendUser model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
//    public function actionDelete($id) {
//        $this->findModel($id)->delete();
//        return $this->redirect(['index']);
//    }

    public function actionDelete() {
        $id = $_POST['id'];
        $status = 'failed';
        if ($this->findModel($id)->delete())
            $status = 'success';
        return $status;
    }

    /**
     * Finds the FrontendUser model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return FrontendUser the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = FrontendUser::findOne($id)) !== null) {
            return $model;
        }
        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }

}