Your IP : 216.73.216.79


Current Path : /var/www/html/donate/backend/modules/fruserdynav2/controllers/
Upload File :
Current File : /var/www/html/donate/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 backend\modules\subscription\models\Subscription;
use backend\modules\contentTemplate\models\ContentTemplate;

/**
 * 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 actionCreate() {
        $model = new FrontendUser();
        $model->scenario = 'create';
        if ($model->load(Yii::$app->request->post())) {
            $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->role = 'guest';
            if ($model->save()) {
                return $this->redirect(['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()) {
                return $this->redirect(['index']);
            }
        }
        return $this->render('update', ['model' => $model,]);
    }
    
    public function actionSetretired($id){
        $model = $this->findModel($id);
        if(!empty($model)){
            $model->retired_members = 1;
            if($model->save(false)){
                
                $modelSubscribe = new Subscription();
                $modelSubscribe->subscription_user_id = $model->id;
                $modelSubscribe->subscription_details = 'Member Retired Certificate';
                $modelSubscribe->subscription_year = date("Y");
                $modelSubscribe->subscription_at = date("Y-m-d H:i:s");
                $modelSubscribe->subscription_start_dt = date("Y-m-d");
                
                $member_name = $model->fullname;
                $member_ic = $model->ic_no;
                $member_no = $model->member_no;
                $member_start = date("Y-m-d");
                
                
                $certificate = '';
                if (($modelTemplate = ContentTemplate::findOne(['code' => 'member-retired'])) !== null) {
                    eval("?>" . $modelTemplate->content . "<?");
                }
                $modelSubscribe->subscription_cert = $certificate;
                $modelSubscribe->save(false);
                
                return $this->redirect(['index']);
            }
        }
    }

    public function actionApprovalstatus($id, $approval_status) {
        $model = $this->findModel($id);
        if (!empty($model)) {
            $model->approval_status = $approval_status;
            if ($approval_status == 'approve') {
                //update member no
                if (empty($model->member_no)) {
                    $memberno = 0;
                    $modelmemberno = FrontendUser::find()->where(['!=', 'member_no', ''])->orderBy(['member_no' => SORT_DESC])->one();
                    if (!empty($modelmemberno)) {
                        $membernoarr = explode('/', $modelmemberno->member_no);
                        if (!empty($membernoarr))
                            $memberno = $membernoarr[0];
                    }
                    $memberno = $memberno + 1;
                    $model->member_no = sprintf("%03d", $memberno) . '/' . date('Y');
                }

                $member_name = $model->fullname;
                $member_ic = $model->ic_no;
                $member_no = $model->member_no;
                $member_start = date("Y-m-d");
                $member_end = date("Y") . '-12-31';

                $modelSubscribe = new Subscription();
                $modelSubscribe->subscription_user_id = $model->id;
                $modelSubscribe->subscription_details = 'Member Registration';
                $modelSubscribe->subscription_year = date("Y");
                $modelSubscribe->subscription_at = date("Y-m-d H:i:s");
                $modelSubscribe->subscription_start_dt = $member_start;
                $modelSubscribe->subscription_end_dt = $member_end;

                $certificate = '';
                if (($modelTemplate = ContentTemplate::findOne(['code' => 'member-certificate'])) !== null) {
                    eval("?>" . $modelTemplate->content . "<?");
                }
                $modelSubscribe->subscription_cert = $certificate;
                $modelSubscribe->save(false);
            }
            if ($approval_status == 'reject') {
                $model->refund_status = 'pending';
                $model->role = 'guest';
            }
            if ($model->save(false)) {
                return $this->redirect(['index', 'action' => 'approvalstatus', 'approval_status' => $approval_status]);
            }
        }
    }

    public function actionSuspend($id) {
        $model = $this->findModel($id);
        if (!empty($model)) {
            $model->status = 2;
            if ($model->save(false))
                return $this->redirect(['view', 'id' => $id]);
        }
    }

    public function actionActivate($id) {
        $model = $this->findModel($id);
        if (!empty($model)) {
            $model->status = 1;
            if ($model->save(false))
                return $this->redirect(['view', 'id' => $id]);
        }
    }

    /**
     * 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']);
    }

    /**
     * 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.'));
    }

    public function actionResetpwd($id) {
        $model = $this->findModel($id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->password_hash = Yii::$app->security->generatePasswordHash('123456');
        if ($model->save(false)) {
            return $this->redirect(['index', 'resetpwd' => 'success']);
        } else {
            return $this->redirect(['index', 'resetpwd' => 'failed']);
        }
    }

    public function actionGetintranetdetails($id) {
        $output = json_decode(file_get_contents("https://intranet.ros.gov.my/admin/index.php?r=mobile%2Fmobile-api%2Fviewresult&id=11&user_id=" . $id), true);
        return $output['username'] . '<-->' . $output['fullname'] . '<-->' . $output['password_hash'] . '<-->' . $output['designation'] . '<-->' . $output['department'];
    }

    public function actionRefundstatus($id) {
        $model = $this->findModel($id);
        $model->refund_status = 'complete';
        if ($model->save(false)) {
            return $this->redirect(['index', 'refundstatus' => 'success']);
        } else {
            return $this->redirect(['index', 'refundstatus' => 'failed']);
        }
    }

    /*public function actionMigrate() {
        $model = FrontendUser::find()->where(['=', 'auth_key', ''])->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $auth_key = Yii::$app->security->generateRandomString();
                $password_hash = Yii::$app->security->generatePasswordHash($row->password_hash);

                if (($model2 = FrontendUser::findOne($row->id)) !== null) {
                    $model2->auth_key = $auth_key;
                    $model2->password_hash = $password_hash;
                    $model2->save(false);
                }
            }
        }
    }*/

}