Your IP : 216.73.216.79


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

/**
 * 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 = \backend\modules\mimin\models\User::findOne(Yii::$app->user->identity->id);
        $original_img_url = $model->img_url;
        $model->updated_dt = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;

        $roles = [];
        foreach ($model->roles as $role) {
            $roles[] = $role->item_name;
        }
        $model->userroles = implode(', ', $roles);
        
        if ($model->load(Yii::$app->request->post())) {

            $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: 'Profile has been updated', 
                }).then(function(result){
                        window.location = '" . Url::toRoute('/user-info/index') . "';
                });");
            }
        }
        return $this->render('profile', ['model' => $model]);
    }

    /* 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;
      //$model->img_url = $user->img_url;

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

}