Your IP : 216.73.216.79


Current Path : /var/www/html/agc/backend/controllers/
Upload File :
Current File : /var/www/html/agc/backend/controllers/UserInfoController.php

<?php

namespace backend\controllers;

use backend\models\ProfileForm;
use Yii;
use backend\models\Staff;
use common\components\HelpersFunctions;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use common\models\User;

/**
 * StaffController implements the CRUD actions for Staff model.
 */
class UserInfoController extends Controller {

    /**
     *
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => [
                        'POST'
                    ]
                ]
            ]
        ];
    }

    public function actionIndex() {
        $model = new ProfileForm();
        $id = Yii::$app->user->identity->staff->id ?? null;
        $userid = Yii::$app->user->identity->id;
        $staff = $id ? Staff::findOne($id) : new Staff();
        $user = \common\models\User::findOne($userid);

        // config
        $config = \Yii::$app->params['avatarSetting'];
        $configpath = $config['avatarUploadPath'];

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

            if ($model->validate()) {

                $update_status = 1;

                $model->file = UploadedFile::getInstance($model, 'file');

                // 1. Validate file exists
                if (!empty($model->file)) {

                    $allowedExtensions = ['jpg', 'jpeg', 'png'];
                    $allowedMimeTypes = ['image/jpeg', 'image/png'];

                    if (!in_array($model->file->extension, $allowedExtensions, true)) {
                        // 2. Allow image extension only
                        $update_status = 0;
                        Yii::$app->session->setFlash('error', Yii::t('app', 'Foto must be in png, jpg or jpeg format'));
                    } else if (!in_array($model->file->type, $allowedMimeTypes, true)) {
                        // 3. Validate MIME type
                        $update_status = 0;
                        Yii::$app->session->setFlash('error', Yii::t('app', 'Foto must be in png, jpg or jpeg format'));
                    } else if ($model->file->size > 1024 * 1024) {
                        // 4. Limit file size: 1MB
                        $update_status = 0;
                        Yii::$app->session->setFlash('error', Yii::t('app', 'Foto too large'));
                    } else if (@getimagesize($model->file->tempName) === false) {
                        // 5. Verify actual image content
                        $update_status = 0;
                        Yii::$app->session->setFlash('error', Yii::t('app', 'Invalid image content'));
                    } else {
                        // 6. Generate random filename
                        $filename = Yii::$app->security->generateRandomString(32) . '.' . $model->file->extension;
                        if (empty($_POST['fileBase64'])) {
                            $source = $model->file->tempName;
                        } else {
                            // convert base 64
                            $source = HelpersFunctions::base64ToImage($_POST['fileBase64'], $configpath, $filename);
                        }
                        HelpersFunctions::compressImage($source, $configpath, $filename, 75);
                        $staff->staff_photo = $filename;
                    }
                }



                $staff->staff_name = $model->staff_name;
                //$staff->gender = $model->gender;
                //$staff->phone_no = $model->phone_no;
                //$staff->mobile_no = $model->mobile_no;
                $staff->userid = $userid;
                $staff->save(false);

                $user->email = $model->email;
                $user->fullname = $model->fullname;
                $user->save(false);

                if ($update_status == 1)
                    Yii::$app->session->setFlash('success', Yii::t('app', 'Your profile has been updated'));
            }
        }

        $model->username = $user->username;
        $model->staff_name = $staff->staff_name;
        //$model->gender = $staff->gender;
        $model->email = $user->email;
        $model->fullname = $user->fullname;
        //$model->phone_no = $staff->phone_no;
        //$model->mobile_no = $staff->mobile_no;
        $model->staff_photo = $staff->staff_photo;

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

    public function actionChangePassword() {
        $model = User::findOne(Yii::$app->user->identity->id);
        $model->scenario = 'changePassword';
        $session = \Yii::$app->session;

        if ($model->load(Yii::$app->request->post())) {
            if (Yii::$app->getSecurity()->validatePassword($model->oldPassword, $model->password_hash)) {

                $hash = Yii::$app->getSecurity()->generatePasswordHash($model->newPassword);
                $model->password_hash = $hash;

                if (!$model->save())
                    $session->setFlash('error', \Yii::t('app', 'Error saving password'));
                else
                    $session->setFlash('success', \Yii::t('app', 'Password Succesfully changed'));

                return $this->refresh();
            } else {
                $session->setFlash('warning', \Yii::t('app', 'Incorect old password'));
            }
        }

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

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