Your IP : 216.73.216.79


Current Path : /var/www/html/slaas/backend/modules/fruserdynav2/controllers/
Upload File :
Current File : /var/www/html/slaas/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;
use yii\helpers\Url;
use backend\modules\school\models\School;

/**
 * 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', 'updatestatus', 'report'],
                        '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())) {

            $modelSchool = School::findOne(['school_code' => $model->school_code]);
            if (!empty($modelSchool))
                $model->school_id = $modelSchool->school_id;

            $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);
        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';
            if ($model->save()) {

                //update parlimen & dun at table school
                $modelSchool = School::findOne($model->school_id);
                if (!empty($modelSchool)) {
                    $modelSchool->school_parlimen = $model->school_parlimen;
                    $modelSchool->school_dun = $model->school_dun;
                    $modelSchool->save(false);
                }

                $this->getView()->registerJs("swal.fire({
                    title:'Berjaya',
                    icon: 'success', 
                    text: 'Data telah dikemaskini', 
                }).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;
    }

    public function actionUpdatestatus() {
        $output = 'failed';
        $id = $_POST['id'];
        $status = $_POST['status'];
        $model = $this->findModel($id);
        if (!empty($model)) {
            $model->status = $status;
            if ($model->save(false)) {
                $output = 'success';
                $url = 'https://' . $_SERVER['SERVER_NAME'];
                if ($status == 'approved') {

                    $subject_mail = 'Pendaftaran Akaun Pengguna Baru SLAAS';

                    $content_mail = '<p>Selamat Datang ke Program Sekolah Lestari Anugerah Alam Sekitar (SLAAS)!</p>
                        <p>Assalamualaikum wbt dan Salam Sejahtera,</p>
                        <p>Tuan/Puan,</p>
                        <p>
                            Permohonan pendaftaran akaun pengguna baru anda untuk ' . $model->school_name . ' 
                            telah diluluskan seperti maklumat yang telah didaftarkan sebelum ini:
                        </p>
                        <p>
                            Pautan Sistem: ' . $url . '<br>
                            ID Pengguna: ' . $model->username . '<br>
                            Nama Sekolah: ' . $model->school_name . '<br>
                            Kata Laluan: (sila rujuk emel yang diterima semasa pendaftaran)
                        </p>
                        <p>Sila klik <a href="' . $url . '/portal-main/login" target="_blank">di sini</a> untuk log masuk ke dalam sistem.</p>
                        <p><b>PERINGATAN MESRA</b></p>
                        <p>
                            Jangan lupa untuk menghantar laporan dan video yang telah tuan/puan sediakan sebelum tarikh tutup, 
                            bagi melengkapkan penyertaan ini. Semoga berjaya!
                        </p>
                        <p>Sekian, terima kasih.</p>
                        <h2 style="font-family: \'Brush Script MT\', cursive;">Alam Sekitar Tanggungjawab Bersama</h2>
                        <p>Sekretariat SLAAS</p>';

                    if (!empty($model->pic_email1))
                        Yii::$app->emailModule->sendemail($model->pic_email1, $subject_mail, $content_mail);
                    if (!empty($model->pic_email2))
                        Yii::$app->emailModule->sendemail($model->pic_email2, $subject_mail, $content_mail);
                    if (!empty($model->pic_email3))
                        Yii::$app->emailModule->sendemail($model->pic_email3, $subject_mail, $content_mail);
                }
                if ($status == 'rejected') {
                    //$to_mail = $model->email;
                    $subject_mail = 'Pendaftaran Akaun Pengguna Baru SLAAS';
                    /* $content_mail = '<p>Assalamualaikum W.B.T dan Salam Sejahtera,</p>
                      <p>Permohonan pendaftaran akaun pengguna baru anda untuk ' . $model->school_name . ' di ' . $url . ' perlu dikemaskini.
                      Klik di <a href="' . $url . '">sini</a> untuk mendaftar semula Sistem MySLAAS</p>
                      <p>Sekian, terima kasih</p>'; */
                    $content_mail = '<p>Selamat Datang ke Program Sekolah Lestari Anugerah Alam Sekitar (SLAAS)!</p>
                        <p>Assalamualaikum wbt dan Salam Sejahtera,</p>
                        <p>Tuan/Puan,</p>
                        <p>Permohonan pendaftaran akaun pengguna baru anda untuk ' . $model->school_name . ' <b>tidak diluluskan</b>.</p>
                        <p>Pihak tuan/puan boleh mendaftar semula dengan maklumat yang terkini.</p>
                        <p>Sekian, terima kasih.</p>
                        <h2 style="font-family: \'Brush Script MT\', cursive;">Alam Sekitar Tanggungjawab Bersama</h2>
                        <p>Sekretariat SLAAS</p>';

                    if (!empty($model->pic_email1))
                        Yii::$app->emailModule->sendemail($model->pic_email1, $subject_mail, $content_mail);
                    if (!empty($model->pic_email2))
                        Yii::$app->emailModule->sendemail($model->pic_email2, $subject_mail, $content_mail);
                    if (!empty($model->pic_email3))
                        Yii::$app->emailModule->sendemail($model->pic_email3, $subject_mail, $content_mail);
                }
            }
        }
        return $output;
    }

    /**
     * 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 actionReport($search_session = '') {

        if (empty($search_session))
            $search_session = '2';

        $searchModel = new FrontendUserSearch();
        $searchModel->search_session = $search_session;
        $dataProvider = $searchModel->searchreport(Yii::$app->request->queryParams);

        return $this->render('report', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'search_session' => $search_session]);
    }
}