Your IP : 216.73.216.124


Current Path : /var/www/html/affiliate/backend/controllers/
Upload File :
Current File : /var/www/html/affiliate/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'];
        $filename = $id . '.jpg';

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

            if ($model->validate()) {

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

                if (! empty($model->file)) {

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