Your IP : 216.73.216.79


Current Path : /var/www/html/crm/models/
Upload File :
Current File : /var/www/html/crm/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 {

    public $user_access;

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

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name', 'email', 'phone', 'role', 'user_access', 'nickname'], 'required'],
            [['status', 'created_by', 'updated_by', 'phone'], 'integer'],
            [['designation', 'created_dt', 'updated_dt', 'auth_key', 'account_bank', 'account_name', 'account_number', 'address1', 'address2', 'postcode', 'city', 'state', 'redirect_url'], 'safe'],
            [['name', 'pwd', 'email'], 'string', 'max' => 255],
            [['phone'], 'string', 'max' => 20],
            [['role'], 'string', 'max' => 10],
            [['uuid', 'email'], 'unique'],
            ['email', 'email'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uuid' => 'Uuid',
            'name' => 'Name',
            'pwd' => 'Pwd',
            'email' => 'Email Address',
            'phone' => 'Mobile Number',
            'role' => 'User Role',
            'status' => 'Status',
            'created_dt' => 'Created Dt',
            'created_by' => 'Created By',
            'updated_dt' => 'Updated Dt',
            'updated_by' => 'Updated By',
            'account_bank' => 'Account Bank',
            'account_name' => 'Account Holder Name',
            'account_number' => 'Account Number',
            'nickname' => 'Nickname',
            'address1' => 'Address',
            'address2' => 'Address2',
            'postcode' => 'Postcode',
            'city' => 'City',
            'state' => 'State',
            'designation' => 'Designation',
            'redirect_url' => 'Redirect URL',
        ];
    }

    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 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;
    }

}