guild/common/models/UserCard.php

257 lines
7.1 KiB
PHP
Raw Normal View History

2018-10-11 11:15:09 +03:00
<?php
namespace common\models;
2019-07-16 11:58:05 +03:00
use common\classes\Debug;
2018-10-11 11:15:09 +03:00
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
2020-01-29 11:26:01 +03:00
use yii\filters\AccessControl;
2019-07-03 13:55:32 +03:00
use yii\helpers\ArrayHelper;
2018-10-11 11:15:09 +03:00
/**
* This is the model class for table "user_card".
*
* @property int $id
* @property int $id_user
2018-10-11 11:15:09 +03:00
* @property string $fio
* @property string $passport
* @property string $photo
* @property string $email
* @property int $gender
* @property string $dob
* @property int $status
* @property string $created_at
* @property string $updated_at
2018-11-06 11:44:27 +03:00
* @property string $deleted_at
2018-10-11 11:15:09 +03:00
* @property string $resume
2018-10-11 17:38:49 +03:00
* @property string $salary
2021-06-25 13:24:32 +03:00
* @property string $vc_text
2018-10-12 14:52:08 +03:00
* @property int $position_id
2019-12-09 10:54:39 +03:00
* @property int $city
2021-06-30 17:30:53 +03:00
* @property int $level
2018-10-11 11:15:09 +03:00
*
2018-10-12 14:52:08 +03:00
* @property FieldsValue[] $fieldsValues
* @property ProjectUser[] $projectUsers
* @property Position $position
2018-10-11 11:15:09 +03:00
* @property Status $status0
*/
class UserCard extends \yii\db\ActiveRecord
{
const GENDER_M = 0;
const GENDER_W = 1;
2021-06-30 17:30:53 +03:00
const LEVEL_JUNIOR = 1;
const LEVEL_MIDDLE = 2;
const LEVEL_MIDDLE_PLUS = 3;
const LEVEL_SENIOR = 4;
/**
* @return string[]
*/
public static function getLevelList(): array
{
return [
self::LEVEL_JUNIOR => 'Junior',
self::LEVEL_MIDDLE => 'Middle',
self::LEVEL_MIDDLE_PLUS => 'Middle+',
self::LEVEL_SENIOR => 'Senior',
];
}
/**
* @param int $level
* @return string
*/
public static function getLevelLabel(int $level): string
{
return self::getLevelList()[$level];
}
2018-10-11 11:15:09 +03:00
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
];
}
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'user_card';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
2020-05-22 11:25:21 +03:00
[['fio', 'status', 'gender', 'email'], 'required'],
2021-06-30 17:30:53 +03:00
[['gender', 'status', 'position_id', 'id_user', 'level'], 'integer'],
2021-06-25 13:24:32 +03:00
[['dob', 'created_at', 'updated_at', 'deleted_at', 'vc_text'], 'safe'],
2020-08-05 15:34:23 +03:00
[['fio', 'passport', 'photo', 'email', 'resume', 'city', 'link_vk', 'link_telegram'], 'string', 'max' => 255],
2018-10-11 17:38:49 +03:00
[['salary'], 'string', 'max' => 100],
2018-10-12 14:52:08 +03:00
[['position_id'], 'exist', 'skipOnError' => true, 'targetClass' => Position::class, 'targetAttribute' => ['position_id' => 'id']],
2018-10-11 11:15:09 +03:00
[['status'], 'exist', 'skipOnError' => true, 'targetClass' => Status::class, 'targetAttribute' => ['status' => 'id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'id_user' => 'ID пользователя',
2018-10-11 11:15:09 +03:00
'fio' => 'ФИО',
'passport' => 'Паспорт',
'photo' => 'Фото',
'email' => 'Email',
'gender' => 'Пол',
'dob' => 'Дата рождения',
'status' => 'Статус',
'created_at' => 'Дата создания',
'updated_at' => 'Дата редактирование',
2018-11-06 11:44:27 +03:00
'deleted_at' => 'Дата удаления',
2018-10-11 11:15:09 +03:00
'resume' => 'Резюме',
2018-10-11 17:38:49 +03:00
'salary' => 'Зарплата',
2018-10-12 14:52:08 +03:00
'position_id' => 'Должность',
2019-12-09 10:54:39 +03:00
'city' => 'Город',
2020-08-05 15:34:23 +03:00
'link_vk' => 'VK',
'link_telegram' => 'Telegram',
2021-06-30 17:30:53 +03:00
'vc_text' => 'Резюме текст',
'level' => 'Уровень'
2018-10-11 11:15:09 +03:00
];
}
2018-10-11 17:38:49 +03:00
/**
* @return \yii\db\ActiveQuery
*/
public function getFieldsValues()
{
2019-06-27 15:41:58 +03:00
return $this->hasMany(FieldsValueNew::class, ['item_id' => 'id'])->where(['item_type' => FieldsValueNew::TYPE_PROFILE])->with('field');
2018-10-11 17:38:49 +03:00
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProjectUsers()
{
return $this->hasMany(ProjectUser::class, ['card_id' => 'id']);
}
2018-10-12 14:52:08 +03:00
/**
* @return \yii\db\ActiveQuery
*/
public function getPosition()
{
return $this->hasOne(Position::class, ['id' => 'position_id']);
}
2018-10-11 11:15:09 +03:00
/**
* @return \yii\db\ActiveQuery
*/
public function getStatus0()
{
return $this->hasOne(Status::class, ['id' => 'status']);
}
public function getGenders()
{
return [
self::GENDER_M => 'Мужчина',
self::GENDER_W => 'Женщина'
];
}
/**
* @return string status text label
*/
public function getGendersText()
{
return $this->genders[$this->gender];
}
2019-07-03 13:55:32 +03:00
public function getSkillValues()
{
return $this->hasMany(CardSkill::class, ['card_id' => 'id'])->with('skill');
}
public static function getNameSkills()
{
return ArrayHelper::map(Skill::find()->all(), 'id', 'name');
2019-07-03 13:55:32 +03:00
}
2019-07-16 11:58:05 +03:00
public static function getUserList()
{
return ArrayHelper::map(self::find()->all(), 'id', 'fio');
}
2020-05-22 16:42:24 +03:00
public static function generateUserForUserCard($card_id = null)
{
$userCardQuery = self::find();
$card_id ? $userCardQuery->where(['id' => $card_id]) : $userCardQuery->where(['id_user' => NULL]);
$user_card_array = $userCardQuery->all();
$user_array = User::find()->all();
foreach ($user_card_array as $user_card_value) {
foreach ($user_array as $user_value)
if ($user_card_value->email == $user_value->email) {
$user_id = $user_value->id;
break;
} else $user_id = NULL;
if ($user_id) {
UserCard::genereateLinlkOnUser($user_card_value, $user_id);
} else {
$user_id = UserCard::generateUser($user_card_value->email, $user_card_value->status);
UserCard::genereateLinlkOnUser($user_card_value, $user_id);
}
}
if ($user_card_array) return "data generated successfully";
else return "no data to generate";
}
public static function generateUser($email, $status)
{
$user = new User();
$auth_key = Yii::$app->security->generateRandomString();
$password = Yii::$app->security->generateRandomString(12);
$password_hash = Yii::$app->security->generatePasswordHash($password);
$user->username = $email;
$user->auth_key = $auth_key;
$user->password_hash = $password_hash;
$user->email = $email;
if ($status == 1) $user->status = 10;
$user->save();
$auth = Yii::$app->authManager;
$authorRole = $auth->getRole('user');
$auth->assign($authorRole, $user->id);
$log = "Логин: " . $email . " Пароль: " . $password . " | ";
file_put_contents("log.txt", $log, FILE_APPEND | LOCK_EX);
return $user->id;
}
public static function genereateLinlkOnUser($user_card, $user_id)
{
$user_card->id_user = $user_id;
$user_card->save();
}
2018-10-11 11:15:09 +03:00
}