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

163 lines
5.0 KiB
PHP
Raw Normal View History

2018-10-11 11:15:09 +03:00
<?php
namespace backend\modules\card\models;
use backend\modules\achievements\models\Achievement;
2020-08-04 10:47:53 +03:00
use Common\Behaviors\LogBehavior;
use common\models\AchievementUserCard;
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;
public $achievements;
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;
}
$achievements = ArrayHelper::getColumn(AchievementUserCard::find()
->where(['user_card_id' => \Yii::$app->request->get('id')])
->innerJoinWith(['achievement' => function($query) {
$query->andWhere(['status' => \common\models\Achievement::STATUS_ACTIVE]);
}])
->all(),
'achievement_id'
);
if (!empty($achievements)) {
$this->achievements = $achievements;
}
2018-10-11 11:15:09 +03:00
}
2020-08-04 10:47:53 +03:00
public function behaviors()
{
return [
'log' => [
'class' => \common\behaviors\LogBehavior::class,
]
];
}
2020-01-24 11:04:48 +03:00
public function beforeSave($insert)
{
2021-08-12 14:10:28 +03:00
if(isset(\Yii::$app->request->post('UserCard')['salary']))
2020-08-04 10:47:53 +03:00
{
$this->salary = str_replace(' ', '', \Yii::$app->request->post('UserCard')['salary']);
}
2020-01-24 11:04:48 +03:00
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');
2020-08-04 10:47:53 +03:00
if($post) {
if (isset($post['fields'])) {
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROFILE]);
foreach ($post['fields'] as $item) {
$item['value'] = urldecode($item['value']);
$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';
}
$fieldsValue->save();
2019-07-26 15:31:39 +03:00
}
2018-11-06 11:44:27 +03:00
}
2020-08-04 10:47:53 +03:00
if (is_array($post['fields'])) {
CardSkill::deleteAll(['card_id' => $this->id]);
if (is_array($post['skill']))
foreach ($post['skill'] as $item) {
$skill = new CardSkill();
$skill->skill_id = $item;
$skill->card_id = $this->id;
$skill->save();
}
2018-11-06 11:44:27 +03:00
}
if(is_array($post['achievements'])){
AchievementUserCard::deleteAll(['user_card_id' => $this->id]);
foreach ($post['achievements'] as $item) {
$achCard = new AchievementUserCard();
$achCard->user_card_id = $this->id;
$achCard->achievement_id = $item;
$achCard->save();
}
}
2020-08-04 10:47:53 +03:00
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
2018-10-12 14:52:08 +03:00
}
2018-10-11 11:15:09 +03:00
}
2020-01-23 16:53:37 +03:00
public static function getParameter($params, $key)
{
try {
return $params[$key];
} catch (\Exception $e) {
return '';
}
}
}