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

98 lines
2.9 KiB
PHP
Raw Normal View History

2018-10-11 11:15:09 +03:00
<?php
namespace backend\modules\card\models;
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;
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){
2019-06-27 15:41:58 +03:00
array_push($array,
['field_id' => $item->field_id,
'value' => $item->value,
'order' => $item->order,
2019-07-26 16:02:35 +03:00
'type_file' => $item->type_file,
2019-06-27 15:41:58 +03:00
'field_name' => $item->field->name]);
2018-10-11 11:15:09 +03:00
}
$this->fields = $array;
}
else{
$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');
if (!empty($skill)) {
$this->skill = $skill;
}
2018-10-11 11:15:09 +03:00
}
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('UserCard');
2018-11-06 11:44:27 +03:00
if($post['fields']){
2019-06-27 15:41:58 +03:00
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROFILE]);
2018-11-06 11:44:27 +03:00
foreach ( $post['fields'] as $item) {
2019-06-27 15:41:58 +03:00
$fildsValue = new FieldsValueNew();
2018-11-06 11:44:27 +03:00
$fildsValue->field_id = $item['field_id'];
$fildsValue->value = $item['value'];
2019-07-26 15:31:39 +03:00
$fildsValue->type_file = 'text';
if(substr($item['value'],0,1) == '/'){
$fildsValue->type_file = 'file';
}
2018-11-06 11:44:27 +03:00
$fildsValue->order = $item['order'];
2019-06-27 15:41:58 +03:00
$fildsValue->item_id = $this->id;
$fildsValue->item_type = FieldsValueNew::TYPE_PROFILE;
2018-11-06 11:44:27 +03:00
$fildsValue->save();
}
2018-10-11 11:15:09 +03:00
}
2018-11-06 11:44:27 +03:00
if($post['skill']){
CardSkill::deleteAll(['card_id' => $this->id]);
foreach ( $post['skill'] as $item) {
$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
}
}