Your IP : 216.73.216.79


Current Path : /var/www/html/dyna-hr/models/
Upload File :
Current File : /var/www/html/dyna-hr/models/User.php

<?php

namespace app\models;

use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;
use app\models\ParamList;

/**
 * This is the model class for table "user".
 *
 * @property int $id
 * @property string $uuid
 * @property string $name
 * @property string $pwd
 * @property string $email
 * @property string $phone
 * @property string $role
 * @property int $status
 * @property string $created_dt
 * @property int $created_by
 * @property string $updated_dt
 * @property int $updated_by
 */
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'user';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name', 'email', 'phone_code', 'phone_no', 'role', 'nickname', 'identification_no', 'staff_no', 'staff_start', 'staff_designation', 'staff_department', 'staff_branch', 'staff_location', 'staff_approver'], 'required'],
            [['status', 'created_by', 'updated_by'], 'integer'],
            [['created_dt', 'updated_dt', 'auth_key', 'gender', 'dob', 'nationality', 'religion', 'race', 'identification_no',
            'current_address1', 'current_address2', 'current_address3', 'current_postcode', 'current_city', 'current_state', 'current_country',
            'other_address1', 'other_address2', 'other_address3', 'other_postcode', 'other_city', 'other_state', 'other_country',
            'blood_type', 'tax_disable', 'img_url',
            'marital_status', 'spouse_name', 'spouse_dob', 'spouse_occupation', 'spouse_identification_no', 'spouse_phone', 'spouse_tax_status', 'spouse_tax_disabled'], 'safe'],
            [['name', 'pwd', 'email'], 'string', 'max' => 255],
            [['role'], 'string', 'max' => 10],
            [['uuid', 'email', 'phone_no'], 'unique'],
            ['email', 'email'],
            ['phone_no', 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uuid' => 'Uuid',
            'name' => 'Official Full Name',
            'pwd' => 'Pwd',
            'email' => 'Email Address',
            'phone_no' => 'Mobile Number',
            'role' => 'User Role',
            'status' => 'Status',
            'created_dt' => 'Created Dt',
            'created_by' => 'Created By',
            'updated_dt' => 'Updated Dt',
            'updated_by' => 'Updated By',
            'nickname' => 'Preferred Name',
            'gender' => 'Gender',
            'dob' => 'Date of Birth',
            'nationality' => 'Nationality',
            'religion' => 'Religion',
            'race' => 'Race',
            'identification_no' => 'IC No. or Passport No.',
            'staff_no' => 'Employee Number',
            'staff_start' => 'Start Date',
            'staff_approver' => 'Reports To',
            'staff_designation' => 'Designation',
            'staff_department' => 'Department',
            'staff_branch' => 'Branch',
            'staff_location' => 'Location',
            'current_address1' => 'Current Address',
            'current_address2' => 'Address',
            'current_address3' => 'Address',
            'current_postcode' => 'Postal Code',
            'current_city' => 'City',
            'current_state' => 'State',
            'current_country' => 'Country',
            'other_address1' => 'Other Address',
            'other_address2' => 'Address',
            'other_address3' => 'Address',
            'other_postcode' => 'Postal Code',
            'other_city' => 'City',
            'other_state' => 'State',
            'other_country' => 'Country',
            'blood_type' => 'Blood Type',
            'tax_disable' => 'Tax Disable Person',
            'marital_status' => 'Marital Status',
            'spouse_name' => 'Spouse Name',
            'spouse_dob' => 'Spouse Date of Birth',
            'spouse_occupation' => 'Spouse Occupation',
            'spouse_identification_no' => 'Spouse IC No. or Passport No.',
            'spouse_phone' => 'Spouse Mobile Number',
            'spouse_tax_status' => 'Tax Spouse Status',
            'spouse_tax_disabled' => 'Tax Spouse Disabled',
            'img_url' => 'User Image',
        ];
    }

    public static function findIdentity($id) {
        $user = static::findOne(['id' => $id]);
        if (!$user) {
            return null;
        } else {
            $user_info = [
                'id' => $user->id,
                'email' => $user->email,
                'name' => $user->name,
                'role' => $user->role,
            ];
            return new static($user_info);
        }
    }

    public static function findIdentityByAccessToken($token, $type = null) {
//        foreach (self::$users as $user) {
//            if ($user['accessToken'] === $token) {
//                return new static($user);
//            }
//        }
        return null;
    }

    public static function findByEmail($email) {
        return self::findOne(['email' => $email]);
    }

    public function getId() {
        return $this->id;
    }

    public function getAuthKey() {
        return $this->auth_key;
    }

    public function validateAuthKey($authKey) {
        return $this->auth_key === $authKey;
    }

    public function validatePassword($password) {
        return Yii::$app->security->validatePassword($password, $this->pwd);
    }

    public function checkstatus($email) {
        if (($model = static::findOne(['email' => $email])) !== null) {
            return $model->status;
        }
    }

    public function getuserid($uuid) {
        if (($model = static::findOne(['uuid' => $uuid])) !== null) {
            return $model->id;
        }
    }

    public function getuseruuid($id) {
        if (($model = static::findOne($id)) !== null) {
            return $model->uuid;
        }
    }

    public function getfirstletter() {
        $output = preg_replace('/[^\p{L}\p{N}\s]/u', '', Yii::$app->user->identity->name);
        $output = mb_substr($output, 0, 1, "UTF-8");
        return $output;
    }

    public function getuserdetails($id) {
        $ParamList = new ParamList();
        $output = '';
        if (($model = static::findOne(['id' => $id])) !== null) {
            $output = $model->name . ' (' . $ParamList->getlabel(2, $model->role) . ')';
        }
        return $output;
    }

    public function getusername($id) {
        if (($model = static::findOne($id)) !== null) {
            return $model->name;
        } else {
            return '';
        }
    }

    public function getusernickname($id) {
        if (($model = static::findOne($id)) !== null) {
            return $model->nickname;
        } else {
            return '';
        }
    }

    public function getuserphone($id) {
        if (($model = static::findOne($id)) !== null) {
            return '+601' . $model->phone;
        } else {
            return '';
        }
    }

    public function gettimejoin($id) {
        $output = '';
        if (($model = static::findOne($id)) !== null) {
            $datetime1 = date_create(date("Y-m-d H:i:s"));
            $datetime2 = date_create($model->created_dt);
            $interval = date_diff($datetime1, $datetime2);
            if ($interval->format('%y') != 0)
                $output .= $interval->format('%y years ');
            if ($interval->format('%m') != 0)
                $output .= $interval->format('%m months ');
            if ($interval->format('%d') != 0)
                $output .= $interval->format('%d days ');
            //$output .= $interval->format('%y years %m months and %d days');
        }
        return $output;
    }

}