<commit_before><?php
namespace common\models;
use yii\db\Query;

use Yii;
use yii\helpers\Html;

class Profile extends \yii\db\ActiveRecord
{
    
    public $skills;
    public $country_id;
    public $user_id;
    public $imageDisplay;
    
    const IMAGE_PLACEHOLDER = '/frontend/web/images/default_user.jpg';
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username','state_id','city_id','country_id','email'], 'required'], 
            [['skills','avatar'], 'safe'], 
            [['avatar'], 'file', 'extensions'=>'jpg'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'user_name' => 'Username',
            'email' => 'Email',
            'country_id' => 'Country',
            'state_id' => 'State',
            'city_id' => 'City',
            'skills'=>'Select Skills',
        ];
    }
    
    public function saveUserSkills($skills,$user_id)
    {
        $arraySkills = explode(',', strtolower(strip_tags($this->skills)));
        foreach ($arraySkills as $skill) {
                $query = new Query;
                $skillModel = $query->select("name")->from('skill')->where(['name'=>trim($skill)])->scalar();                  
                if (!$skillModel) {
                        $skillModel = new Skill();
                        $skillModel->name = trim($skill);
                        $skillModel->save();
                }                     
        }    
        $sql = "DELETE FROM rel_user_skills WHERE user_id=$user_id";            
        Yii::$app->db->createCommand($sql)->execute();

        foreach ($arraySkills as $skill) {
                $skillId = $query->select("id")->from('skill')->where(['name'=>trim($skill)])->scalar();                  
                Yii::$app->db->createCommand()->insert('rel_user_skills',[
                        'user_id' => $user_id,
                        'skill_id' => $skillId,
                ])->execute();                    
        }
    }
    
    /**
     * get User skills
     * @return array
     */
    
    public static function getUserSkills($user_id = '')
    {
            $str = null;
            if (!empty($user_id)) {

                    $modeluserskills = RelUserSkills::find(['user_id' => $user_id])->all();
                    if ($modeluserskills) {
                        $arrayModels = \yii\helpers\ArrayHelper::map($modeluserskills, 'skill_id','skill.name'); //id = your ID model, name = your caption                          
                        $str = implode(',', $arrayModels);
                    }
            }
            return $str;
    } 

    /**
     * get User skills
     * @return string
     */
    public function getUserSkillsString()
    {
            $skills = $this->userSkills;
            $result = array();
            foreach ($skills as $skill) {
                    $result[] = $skill['name'];
            }
            return implode(',', $result);
    }   
    
    /**
     * @return boolean
     */<commit_msg>Testing and code clean<commit_after><?php
namespace common\models;
use yii\db\Query;

use Yii;
use yii\helpers\Html;

class Profile extends \yii\db\ActiveRecord
{
    
    public $skills;
    public $country_id;
    public $user_id;
    public $imageDisplay;
    
    const IMAGE_PLACEHOLDER = '/frontend/web/images/default_user.jpg';
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username','state_id','city_id','country_id','email'], 'required'], 
            [['skills','avatar'], 'safe'], 
            [['avatar'], 'file', 'extensions'=>'jpg'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'user_name' => 'Username',
            'email' => 'Email',
            'country_id' => 'Country',
            'state_id' => 'State',
            'city_id' => 'City',
            'skills'=>'Select Skills',
        ];
    }
    
    public function saveUserSkills($skills,$user_id)
    {
        $arraySkills = array_filter(explode(',', strtolower(strip_tags($this->skills))));
        //print_r($arraySkills);
        //print_r(array_filter($arraySkills));exit;
        foreach ($arraySkills as $skill) {
            $query = new Query;
            $skillModel = $query->select("name")->from('skill')->where(['name'=>trim($skill)])->scalar();                  
            if (!$skillModel) {
                    $skillModel = new Skill();
                    $skillModel->name = trim($skill);
                    $skillModel->save();
            }     
        }  
        $sql = "DELETE FROM rel_user_skills WHERE user_id=$user_id";            
        Yii::$app->db->createCommand($sql)->execute();
        foreach ($arraySkills as $skill) {
                $skillId = $query->select("id")->from('skill')->where(['name'=>trim($skill)])->scalar();                  
                Yii::$app->db->createCommand()->insert('rel_user_skills',[
                        'user_id' => $user_id,
                        'skill_id' => $skillId,
                ])->execute();                    
        }
    }
    
    /**
     * get User skills
     * @return array
     */
    
    public static function getUserSkills($user_id = '')
    {
            $str = null;
            if (!empty($user_id)) {

                    $modeluserskills = RelUserSkills::find()->where(['user_id' => $user_id])->all();
                    if ($modeluserskills) {
                        $arrayModels = \yii\helpers\ArrayHelper::map($modeluserskills, 'skill_id','skill.name'); //id = your ID model, name = your caption                          
                        $str = implode(',', $arrayModels);
                    }
            }
            return $str;
    } 

    /**
     * get User skills
     * @return string
     */
    public function getUserSkillsString()
    {
            $skills = $this->userSkills;
            $result = array();
            foreach ($skills as $skill) {
                    $result[] = $skill['name'];
            }
            return implode(',', $result);
    }   
    
    /**
     * @return boolean
     */
{'fraction_non_alphanumeric': 0.15632716049382717, 'fraction_numerical': 0.0, 'mean_word_length': 1.4946112394149345, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5751976', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1859, 'n_words': 321}