| Current Path : /var/www/html/project/models/ |
| Current File : /var/www/html/project/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 $file;
/**
* {@inheritdoc}
*/
public static function tableName() {
return 'user';
}
/**
* {@inheritdoc}
*/
public function rules() {
return [
[['name', 'email', 'role', 'dept'], 'required'],
[['status', 'created_by', 'updated_by', 'phone'], 'integer'],
[['created_dt', 'updated_dt', 'auth_key', 'phone', 'img_url', 'file'], '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' => 'Position',
'dept' => 'Department',
'status' => 'Status',
'created_dt' => 'Created Dt',
'created_by' => 'Created By',
'updated_dt' => 'Updated Dt',
'updated_by' => 'Updated By',
'file' => 'User Image',
'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,
'dept' => $user->dept,
];
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 getfirstletter2($name) {
$output = preg_replace('/[^\p{L}\p{N}\s]/u', '', $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 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;
}
public function getuserdetails2($id) {
$ParamList = new ParamList();
$output = '';
if (($model = static::findOne($id)) !== null) {
$img_url = $this->getfirstletter2($model->name);
if (!empty($model->img_url))
$img_url = '<img src="' . $model->img_url . '">';
$output = '<div class="kt-user-card-v2 user-card" data-toggle="tooltip" data-html="true" data-placement="top" title="<b>' . $model->name . '</b><br>'.$ParamList->getlabel(2, $model->role).'">
<div class="kt-user-card-v2__pic">
<div class="kt-badge kt-badge--xl kt-badge--warning">' . $img_url . '</div>
</div>
</div>';
}
return $output;
}
}