92 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			92 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace backend\modules\card\models;
 | 
						|
 | 
						|
use backend\modules\settings\models\Skill;
 | 
						|
use common\models\CardSkill;
 | 
						|
use common\models\FieldsValue;
 | 
						|
use common\models\FieldsValueNew;
 | 
						|
use yii\helpers\ArrayHelper;
 | 
						|
 | 
						|
class UserCard extends \common\models\UserCard
 | 
						|
{
 | 
						|
    public $fields;
 | 
						|
    public $skill;
 | 
						|
 | 
						|
    public function init()
 | 
						|
    {
 | 
						|
        parent::init();
 | 
						|
 | 
						|
        $fieldValue = FieldsValueNew::find()->where(
 | 
						|
            [
 | 
						|
                'item_id' => \Yii::$app->request->get('id'),
 | 
						|
                'item_type' => FieldsValueNew::TYPE_PROFILE,
 | 
						|
            ])
 | 
						|
            ->all();
 | 
						|
        $array = [];
 | 
						|
        if(!empty($fieldValue)){
 | 
						|
            foreach ($fieldValue as $item){
 | 
						|
                array_push($array,
 | 
						|
                    ['field_id' => $item->field_id,
 | 
						|
                        'value' => $item->value,
 | 
						|
                        'order' => $item->order,
 | 
						|
                        'field_name' => $item->field->name]);
 | 
						|
            }
 | 
						|
            $this->fields = $array;
 | 
						|
        }
 | 
						|
        else{
 | 
						|
            $this->fields = [
 | 
						|
                [
 | 
						|
                    'field_id'   => null,
 | 
						|
                    'value'  => null,
 | 
						|
                    'order' => null,
 | 
						|
                    'field_name' => null,
 | 
						|
                ],
 | 
						|
            ];
 | 
						|
        }
 | 
						|
 | 
						|
        $skill = ArrayHelper::getColumn(CardSkill::find()->where(['card_id' => \Yii::$app->request->get('id')])->all(),
 | 
						|
            'skill_id');
 | 
						|
 | 
						|
        if (!empty($skill)) {
 | 
						|
            $this->skill = $skill;
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    public function afterSave($insert, $changedAttributes)
 | 
						|
    {
 | 
						|
        $post = \Yii::$app->request->post('UserCard');
 | 
						|
 | 
						|
        if($post['fields']){
 | 
						|
            FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROFILE]);
 | 
						|
 | 
						|
            foreach ( $post['fields'] as $item) {
 | 
						|
                $fildsValue = new FieldsValueNew();
 | 
						|
                $fildsValue->field_id = $item['field_id'];
 | 
						|
                $fildsValue->value = $item['value'];
 | 
						|
                $fildsValue->order = $item['order'];
 | 
						|
                $fildsValue->item_id = $this->id;
 | 
						|
                $fildsValue->item_type = FieldsValueNew::TYPE_PROFILE;
 | 
						|
 | 
						|
                $fildsValue->save();
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        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();
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
 | 
						|
    }
 | 
						|
} |