guild/backend/modules/card/models/UserCard.php

177 lines
5.4 KiB
PHP
Raw Normal View History

2018-10-11 11:15:09 +03:00
<?php
namespace backend\modules\card\models;
use Yii;
2018-10-12 14:52:08 +03:00
use backend\modules\settings\models\Skill;
2019-07-26 15:31:39 +03:00
use common\classes\Debug;
2018-10-12 14:52:08 +03:00
use common\models\CardSkill;
use common\models\User;
2018-10-11 11:15:09 +03:00
use common\models\FieldsValue;
2019-06-27 15:41:58 +03:00
use common\models\FieldsValueNew;
2018-10-12 14:52:08 +03:00
use yii\helpers\ArrayHelper;
2018-10-11 11:15:09 +03:00
class UserCard extends \common\models\UserCard
{
public $fields;
2018-10-12 14:52:08 +03:00
public $skill;
2018-10-11 11:15:09 +03:00
public function init()
{
parent::init();
2019-06-27 15:41:58 +03:00
$fieldValue = FieldsValueNew::find()->where(
2018-10-11 11:15:09 +03:00
[
2019-06-27 15:41:58 +03:00
'item_id' => \Yii::$app->request->get('id'),
'item_type' => FieldsValueNew::TYPE_PROFILE,
]
)
2018-10-11 11:15:09 +03:00
->all();
$array = [];
if (!empty($fieldValue)) {
foreach ($fieldValue as $item) {
array_push(
$array,
[
'field_id' => $item->field_id,
2019-06-27 15:41:58 +03:00
'value' => $item->value,
'order' => $item->order,
2019-07-26 16:02:35 +03:00
'type_file' => $item->type_file,
'field_name' => $item->field->name
]
);
2018-10-11 11:15:09 +03:00
}
$this->fields = $array;
} else {
2018-10-11 11:15:09 +03:00
$this->fields = [
[
'field_id' => null,
'value' => null,
'order' => null,
2019-06-27 15:41:58 +03:00
'field_name' => null,
2019-07-26 16:02:35 +03:00
'type_file' => null,
2018-10-11 11:15:09 +03:00
],
];
}
2018-10-12 14:52:08 +03:00
$skill = ArrayHelper::getColumn(
CardSkill::find()->where(['card_id' => \Yii::$app->request->get('id')])->all(),
'skill_id'
);
2018-10-12 14:52:08 +03:00
if (!empty($skill)) {
$this->skill = $skill;
}
2018-10-11 11:15:09 +03:00
}
2020-01-24 11:04:48 +03:00
public function beforeSave($insert)
{
$this->salary = str_replace(' ', '', \Yii::$app->request->post('UserCard')['salary']);
return parent::beforeSave($insert); // TODO: Change the autogenerated stub
}
2018-10-11 11:15:09 +03:00
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('UserCard');
if ($post['fields']) {
2019-06-27 15:41:58 +03:00
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROFILE]);
foreach ($post['fields'] as $item) {
2019-12-06 15:39:19 +03:00
$item['value'] = urldecode($item['value']);
2019-12-05 17:20:25 +03:00
$fieldsValue = new FieldsValueNew();
$fieldsValue->field_id = $item['field_id'];
$fieldsValue->value = $item['value'];
$fieldsValue->order = $item['order'];
$fieldsValue->item_id = $this->id;
$fieldsValue->item_type = FieldsValueNew::TYPE_PROFILE;
if(is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])){
$fieldsValue->type_file = 'file';
}else{
$fieldsValue->type_file = 'text';
2019-07-26 15:31:39 +03:00
}
2018-11-06 11:44:27 +03:00
2019-12-05 17:20:25 +03:00
$fieldsValue->save();
2018-11-06 11:44:27 +03:00
}
2018-10-11 11:15:09 +03:00
}
if ($post['skill']) {
2018-11-06 11:44:27 +03:00
CardSkill::deleteAll(['card_id' => $this->id]);
foreach ($post['skill'] as $item) {
2018-11-06 11:44:27 +03:00
$skill = new CardSkill();
$skill->skill_id = $item;
$skill->card_id = $this->id;
$skill->save();
}
2018-10-12 14:52:08 +03:00
}
2018-11-06 11:44:27 +03:00
2018-10-11 11:15:09 +03:00
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
2020-01-17 16:28:25 +03:00
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();
$log = "Логин: " . $email . " Пароль: " . $password . " | ";
2020-01-21 17:33:07 +03:00
file_put_contents("log.txt", $log, FILE_APPEND | LOCK_EX);
return $user->id;
}
2020-01-17 16:28:25 +03:00
public static function genereateLinlkOnUser($user_card, $user_id)
{
$user_card->id_user = $user_id;
$user_card->save();
}
2020-01-17 16:28:25 +03:00
public static function generateUserForUserCard($card_id = null)
{
$userCardQuery = UserCard::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 "Данные успешно сгенерированы";
else return "Нет данных для генерации";
}
2020-01-23 16:53:37 +03:00
public static function getParameter($params, $key)
{
try {
return $params[$key];
} catch (\Exception $e) {
return '';
}
}
}