<commit_before><?php

/**
 * Wrapper Widget to use jQuery Select2 in Yii application.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright &copy; 2014 <EMAIL>
 * @package extensions
 * @subpackage select2
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 * @version 3.4.3 rev.0
 *
 * @see https://github.com/ivaynberg/select2 jQuery Select2
 */
class Select2 extends CInputWidget
{

    /** @var string Path to assets directory published in init() */
    private $assetsDir;

    /** @var array Chosen script settings passed to $.fn.chosen() */
    private $settings = array();

    /** @var bool Multiple or single item should be selected */
    public $multiple = false;

    /** @var array See CHtml::listData() */
    public $data;
    public $onTrigger=array();

    /** Initiialize method to publish assets */
    public function init()
    {
        $dir = dirname(__FILE__) . '/assets';
        $this->assetsDir = Yii::app()->assetManager->publish($dir);

        if ($this->multiple) {
            $this->htmlOptions['multiple'] = true;
        } elseif (isset($this->htmlOptions['multiple'])) {
            $this->multiple = true;
        }
        
        if (isset($this->htmlOptions['placeholder'])) {
            $this->settings['placeholder'] = $this->htmlOptions['placeholder'];
        } elseif (isset($this->htmlOptions['data-placeholder'])) {
            $this->settings['placeholder'] = $this->htmlOptions['data-placeholder'];
        }

        if (isset($this->htmlOptions['select2Options'])) {
            if(array_key_exists("onTrigger",$this->htmlOptions['select2Options'])){
                    $this->onTrigger    =   CMap::mergeArray($this->onTrigger, $this->htmlOptions['select2Options']['onTrigger']);
                    unset($this->htmlOptions['select2Options']['onTrigger']);
            }
            $this->settings = CMap::mergeArray($this->settings, $this->htmlOptions['select2Options']);
            unset($this->htmlOptions['select2Options']);
        }
        
    }

    /**Update scripts and bind events */
    public function run()
    {
        list($name, $id) = $this->resolveNameID();
        
        if (isset($this->htmlOptions['id'])) {
            $id = $this->htmlOptions['id'];
        } else {
            $this->htmlOptions['id'] = $id;
        }

        if (isset($this->htmlOptions['name'])) {
            $name = $this->htmlOptions['name'];
        }
        
        if (isset($this->settings['ajax'])) {
            if (isset($this->model)) {
                echo CHtml::textField($name, $this->model->{$this->attribute}, $this->htmlOptions);
            } else {
                echo CHtml::textField($name, $this->value, $this->htmlOptions);
            }
        } else {
            if (isset($this->model)) {
                echo CHtml::dropDownList($name, $this->model->{$this->attribute}, $this->data, $this->htmlOptions);
            } else {
                echo CHtml::dropDownList($name, $this->value, $this->data, $this->htmlOptions);
            }
        }

        $this->registerScripts($id);
    }

    /** Register client scripts */
    private function registerScripts($id)
    {
        $cs = Yii::app()->getClientScript();

        $cs->registerCssFile($this->assetsDir . '/select2.min.css');
        $cs->registerScriptFile($this->assetsDir . '/select2.full.min.js');

        $lang = strtoupper(str_replace('_', '-', Yii::app()->language));
        $lang[0] = strtolower($lang[0]);
        $lang[1] = strtolower($lang[1]);

        $cs->registerScriptFile($this->assetsDir . '/i18n/' . $lang . '.js');
        
        $settings = CJavaScript::encode($this->settings);
        
        
        $onTriggers='';
        if(!empty($this->onTrigger)){
            foreach($this->onTrigger as $k=>$v){
                $onTriggers .='.on(\''.$k.'\','.CJavaScript::encode($v).')';
            }
        }
        
        $cs->registerScript("{$id}_select2", ""
                . "$('#{$id}').select2({$settings})".$onTriggers.";"
            );
            
    }

    
    public static function dropDownList($name, $select, $data, $htmlOptions = array())
    {
        return Yii::app()->getController()->widget(__CLASS__, array(
            'name' => $name,
            'value' => $select,
            'data' => $data,
            'htmlOptions' => $htmlOptions,
        ), true);
    }

    public static function activeDropDownList($model, $attribute, $data, $htmlOptions = array())
    {
        return self::dropDownList(CHtml::activeName($model, $attribute), CHtml::value($model, $attribute), $data, $htmlOptions);
    }

    /** Multiple elect */
    public static function multiSelect($name, $select, $data, $htmlOptions = array())
    {
        return Yii::app()->getController()->widget(__CLASS__, array(
            'name' => $name,
            'value' => $select,
            'data' => $data,
            'htmlOptions' => $htmlOptions,
            'multiple' => true,
        ), true);
    }

    public static function activeMultiSelect($model, $attribute, $data, $htmlOptions = array())
    {
        return self::multiSelect(CHtml::activeName($model, $attribute) . '[]', CHtml::value($model, $attribute), $data, $htmlOptions);
    }

}<commit_msg>Language and compatibility with select2 4.0.0<commit_after><?php

/**
 * Wrapper Widget to use jQuery Select2 in Yii application.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright &copy; 2014 omaraslam.com
 * @package extensions
 * @subpackage select2
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 * @version 3.4.3 rev.0
 *
 * @see https://github.com/ivaynberg/select2 jQuery Select2
 */
class Select2 extends CInputWidget
{

    /** @var string Path to assets directory published in init() */
    private $assetsDir;

    /** @var array Chosen script settings passed to $.fn.chosen() */
    private $settings = array();

    /** @var bool Multiple or single item should be selected */
    public $multiple = false;

    /** @var array See CHtml::listData() */
    public $data;
    public $onTrigger = array();

    /** Initiialize method to publish assets */
    public function init()
    {
        $dir = dirname(__FILE__) . '/assets';
        $this->assetsDir = Yii::app()->assetManager->publish($dir);

        if ($this->multiple) {
            $this->htmlOptions['multiple'] = true;
        } elseif (isset($this->htmlOptions['multiple'])) {
            $this->multiple = true;
        }

        if (isset($this->htmlOptions['placeholder'])) {
            $this->settings['placeholder'] = $this->htmlOptions['placeholder'];
        } elseif (isset($this->htmlOptions['data-placeholder'])) {
            $this->settings['placeholder'] = $this->htmlOptions['data-placeholder'];
        }

        if (isset($this->htmlOptions['select2Options'])) {
            if (array_key_exists("onTrigger", $this->htmlOptions['select2Options'])) {
                $this->onTrigger = CMap::mergeArray($this->onTrigger,
                    $this->htmlOptions['select2Options']['onTrigger']);
                unset($this->htmlOptions['select2Options']['onTrigger']);
            }
            $this->settings = CMap::mergeArray($this->settings, $this->htmlOptions['select2Options']);
            unset($this->htmlOptions['select2Options']);
        }

    }

    /**Update scripts and bind events */
    public function run()
    {
        list($name, $id) = $this->resolveNameID();

        if (isset($this->htmlOptions['id'])) {
            $id = $this->htmlOptions['id'];
        } else {
            $this->htmlOptions['id'] = $id;
        }

        if (isset($this->htmlOptions['name'])) {
            $name = $this->htmlOptions['name'];
        }

        if (isset($this->model)) {
            echo CHtml::dropDownList($name, $this->model->{$this->attribute}, $this->data, $this->htmlOptions);
        } else {
            echo CHtml::dropDownList($name, $this->value, $this->data, $this->htmlOptions);
        }

        $this->registerScripts($id);
    }

    /** Register client scripts */
    private function registerScripts($id)
    {
        $cs = Yii::app()->getClientScript();

        $cs->registerCssFile($this->assetsDir . '/select2.min.css');
        $cs->registerScriptFile($this->assetsDir . '/select2.full.min.js');

        $lang = substr(str_replace('_', '-', strtolower(Yii::app()->language)), 0, 2);

        $cs->registerScriptFile($this->assetsDir . '/i18n/' . $lang . '.js');

        $settings = CJavaScript::encode($this->settings);


        $onTriggers = '';
        if (!empty($this->onTrigger)) {
            foreach ($this->onTrigger as $k => $v) {
                $onTriggers .= '.on(\'' . $k . '\',' . CJavaScript::encode($v) . ')';
            }
        }

        $cs->registerScript("{$id}_select2", ""
            . "$('#{$id}').select2({$settings})" . $onTriggers . ";"
        );

    }


    public static function dropDownList($name, $select, $data, $htmlOptions = array())
    {
        return Yii::app()->getController()->widget(__CLASS__, array(
            'name' => $name,
            'value' => $select,
            'data' => $data,
            'htmlOptions' => $htmlOptions,
        ), true);
    }

    public static function activeDropDownList($model, $attribute, $data, $htmlOptions = array())
    {
        return self::dropDownList(CHtml::activeName($model, $attribute), CHtml::value($model, $attribute), $data,
            $htmlOptions);
    }

    /** Multiple elect */
    public static function multiSelect($name, $select, $data, $htmlOptions = array())
    {
        return Yii::app()->getController()->widget(__CLASS__, array(
            'name' => $name,
            'value' => $select,
            'data' => $data,
            'htmlOptions' => $htmlOptions,
            'multiple' => true,
        ), true);
    }

    public static function activeMultiSelect($model, $attribute, $data, $htmlOptions = array())
    {
        return self::multiSelect(CHtml::activeName($model, $attribute) . '[]', CHtml::value($model, $attribute), $data,
            $htmlOptions);
    }

}
{'fraction_non_alphanumeric': 0.18007889546351086, 'fraction_numerical': 0.005917159763313609, 'mean_word_length': 2.409885675857431, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 148, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1005531', 'n_tokens_mistral': 2931, 'n_tokens_neox': 2869, 'n_words': 552}