<commit_before><?php

namespace ijony\admin\widgets;

use ijony\admin\assets\DatepickerAsset;
use ijony\admin\assets\AwesomeBootstrapCheckboxAsset;
use ijony\admin\assets\JasnyBootstrapAsset;
use ijony\admin\assets\SelectAsset;
use ijony\admin\assets\SummerNoteAsset;
use ijony\admin\assets\SwitcheryAsset;
use ijony\admin\assets\TagsinputAsset;
use ijony\helpers\Image;
use Yii;
use yii\bootstrap\Html;
use yii\helpers\Url;
use yii\web\View;

/**
 * 动态表单重构类
 *
 * @inheritdoc
 * @package common\widgets
 */
class ActiveField extends \yii\bootstrap\ActiveField
{

    /**
     * @inheritdoc
     */
    public $inline = true;

    /**
     * @inheritdoc
     */
    public $checkboxTemplate = "<div class=\"checkbox\">\n{input}\n{beginLabel}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";
    /**
     * @inheritdoc
     */
    public $radioTemplate = "<div class=\"radio\">\n{input}\n{beginLabel}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";
    /**
     * @inheritdoc
     */
    public $horizontalCheckboxTemplate = "{beginWrapper}\n<div class=\"checkbox\">\n{input}\n{beginLabel}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";
    /**
     * @inheritdoc
     */
    public $horizontalRadioTemplate = "{beginWrapper}\n<div class=\"radio\">\n{input}\n{beginLabel}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";

    /**
     * @inheritdoc
     */
    public function __construct($config = [])
    {
        parent::__construct($config);

        AwesomeBootstrapCheckboxAsset::register(Yii::$app->getView());
    }

    /**
     * 带单位文本框
     *
     * @param       $unit
     * @param array $options
     *
     * @return $this
     */
    public function textUnitInput($unit, $options = [])
    {
        if(!$unit){
            return $this->textInput($options);
        }

        $options = array_merge($this->inputOptions, $options);
        $this->adjustLabelFor($options);
        $this->parts['{input}'] = Html::tag(
            'div',
            Html::activeTextInput($this->model, $this->attribute, $options) .
            Html::tag(
                'div',
                $unit,
                [
                    'class' => 'input-group-addon'
                ]
            ),
            [
                'class' => 'input-group'
            ]
        );

        return $this;
    }

    /**
     * 带按钮文本框
     *
     * @param       $buttonText
     * @param array $options
     *
     * @return $this
     */
    public function textButtonInput($buttonText, $options = [])
    {
        if(!$buttonText){
            return $this->textInput($options);
        }

        $buttonOptions = ['class' => 'btn btn-default'];

        if(isset($options['buttonOptions'])){
            $buttonOptions = array_merge($buttonOptions, $options['buttonOptions']);
            unset($options['buttonOptions']);
        }

        $options = array_merge($this->inputOptions, $options);
        $this->adjustLabelFor($options);
        $this->parts['{input}'] = Html::tag(
            'div',
            Html::activeTextInput($this->model, $this->attribute, $options) .
            Html::tag(
                'div',
                Html::button(
                    $buttonText,
                    $buttonOptions
                ),
                [
                    'class' => 'input-group-btn'
                ]
            ),
            [
                'class' => 'input-group'
            ]
        );

        return $this;
    }

    /**
     * @inheritdoc
     */
    public function dropDownList($items, $options = [])
    {
        if(!isset($options['class'])){
            $options['class'] = 'form-control selectpicker';
        }else{
            $options['class'] .= ' form-control selectpicker';
        }

        $options = array_merge($this->inputOptions, $options);
        $this->addAriaAttributes($options);
        $this->adjustLabelFor($options);
        $this->parts['{input}'] = Html::activeDropDownList($this->model, $this->attribute, $items, $options);

        return $this;
    }

    /**
     * 文件上传
     *
     * @return $this
     */
    public function file()
    {
        $inputName = Html::getInputName($this->model, $this->attribute);

        $this->parts['{input}'] = <<<HTML
<div class="fileinput fileinput-new input-group" data-provides="fileinput">
    <div class="form-control" data-trigger="fileinput">
        <i class="glyphicon glyphicon-file fileinput-exists"></i>
        <span class="fileinput-filename"></span>
    </div>
    <span class="input-group-addon btn btn-default btn-file">
        <span class="fileinput-new">选择文件</span>
        <span class="fileinput-exists">更换</span>
        <input type="file" name="$inputName"/>
    </span>
    <a href="#" class="input-group-addon btn btn-default fileinput-exists" data-dismiss="fileinput">移除</a>
</div>
HTML;

        JasnyBootstrapAsset::register(Yii::$app->getView());

        return $this;
    }

    /**
     * 单图上传
     *
     * @return $this
     */
    public function image()
    {
        $inputId = Html::getInputId($this->model, $this->attribute);
        $inputName = Html::getInputName($this->model, $this->attribute);
        $inputValue = Html::getAttributeValue($this->model, $this->attribute);

        $preview = Image::getImg($inputValue, 300);

        $data = Html::img($preview, ['data-default' => $preview]);

        if($inputValue){
            $data .= Html::hiddenInput($inputName, $inputValue, ['id' => $inputId]);
            $data .= Html::fileInput($inputName, $inputValue, ['class' => 'image-upload-input']);
        }else{
            $data .= Html::fileInput($inputName, $inputValue, ['id' => $inputId, 'class' => 'image-upload-input']);
        }

        $this->parts['{input}'] = Html::tag('div', $data, ['class' => 'image-upload']);

        $js = <<<JS
$('.image-upload-input').change(function(){
    var imgUrl = null;
    var objUrl = this.files[0];
    var filename = $(this).val();
    var previewImg = $(this).parent().find('img');
    
    fileExt = filename.substr(filename.lastIndexOf(".")).toLowerCase();
    
    $(this).closest('.form-group').removeClass('has-error');
    $(this).closest('.form-group').find('.help-block-error').text('');
    previewImg.attr('src', previewImg.attr('data-default'));
    
    if(fileExt != '.jpg' && fileExt != '.png' && fileExt != '.gif'){
        $(this).closest('.form-group').addClass('has-error');
        $(this).closest('.form-group').find('.help-block-error').text('图片格式不对，只能上传 jpg、png 和 gif 格式！');
    }else{
        if(window.createObjectURL != undefined){
            imgUrl = window.createObjectURL(objUrl) ;
        }else if (window.URL != undefined){
            imgUrl = window.URL.createObjectURL(objUrl) ;
        }else if (window.webkitURL != undefined){
            imgUrl = window.webkitURL.createObjectURL(objUrl) ;
        }
        
        if(imgUrl){
            previewImg.attr('src', imgUrl);
        }
    }
});
JS;

        Yii::$app->view->registerJs($js, View::POS_READY, 'image-upload');

        return $this;
    }

    /**
     * 级联下拉表单
     *
     * @param array $options
     *
     * @return $this
     */
    public function select($options = [])
    {
        $model = $this->model;
        $model = $model::className();
        $inputName = Html::getInputName($this->model, $this->attribute);
        $valueId = Html::getAttributeValue($this->model, $this->attribute);

        if(isset($options['class'])){
            $model = $options['class'];
            $modelData = $model::findOne($valueId);
            $ids = [0];
            if($modelData){
                $ids = $modelData->getParentIds();
            }
            $exclude = 0;
        }else{
            $primaryKey = $model::primaryKey();
            $primaryKey = current($primaryKey);
            $ids = $this->model->getParentIds();
            $exclude = $this->model->$primaryKey ? $this->model->$primaryKey : 0;
        }

        $selects = [];

        foreach($ids as $index => $parentId){
            $id = 0;
            if(isset($ids[$index + 1])){
                $id = $ids[$index + 1];
            }else{
                $id = $valueId;
            }

            $datas = $model::getSelectData($parentId, $exclude);
            if($datas){
                $selects[] = Html::dropDownList($inputName, $id, $datas, [
                    'class' => 'form-control form-control-inline',
                    'ajax-select' => Url::to(['ajax/select', 'model' => $model, 'input' => $inputName, 'exclude' => $exclude]),
                ]);
            }
        }

        if(!$selects){
            $selects[] = Html::dropDownList($inputName, '', [], [
                'class' => 'form-control form-control-inline',
                'prompt' => '请选择',
            ]);
        }

        $this->parts['{input}'] = join("", $selects);

        return $this;
    }

    /**
     * 起止输入表单
     *
     * @param       $endAttr
     * @param array $options
     *
     * @return $this
     */
    public function between($endAttr, $options = [])
    {
        $hasTime = true;

        $beginInputId = Html::getInputId($this->model, $this->attribute);
        $endInputId = Html::getInputId($this->model, $endAttr);

        $beginInputName = Html::getInputName($this->model, $this->attribute);
        $endInputName = Html::getInputName($this->model, $endAttr);

        $beginValue = Html::getAttributeValue($this->model, $this->attribute);
        $endValue = Html::getAttributeValue($this->model, $endAttr);

        $template = '%s &nbsp; 到 &nbsp; %s';

        if($this->form->layout == 'default'){
            $template = '<div>%s &nbsp; 到 &nbsp; %s</div>';
        }<commit_msg>修复 Select<commit_after><?php

namespace ijony\admin\widgets;

use ijony\admin\assets\DatepickerAsset;
use ijony\admin\assets\AwesomeBootstrapCheckboxAsset;
use ijony\admin\assets\JasnyBootstrapAsset;
use ijony\admin\assets\SelectAsset;
use ijony\admin\assets\SummerNoteAsset;
use ijony\admin\assets\SwitcheryAsset;
use ijony\admin\assets\TagsinputAsset;
use ijony\helpers\Image;
use Yii;
use yii\bootstrap\Html;
use yii\helpers\Url;
use yii\web\View;

/**
 * 动态表单重构类
 *
 * @inheritdoc
 * @package common\widgets
 */
class ActiveField extends \yii\bootstrap\ActiveField
{

    /**
     * @inheritdoc
     */
    public $inline = true;

    /**
     * @inheritdoc
     */
    public $checkboxTemplate = "<div class=\"checkbox\">\n{input}\n{beginLabel}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";
    /**
     * @inheritdoc
     */
    public $radioTemplate = "<div class=\"radio\">\n{input}\n{beginLabel}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";
    /**
     * @inheritdoc
     */
    public $horizontalCheckboxTemplate = "{beginWrapper}\n<div class=\"checkbox\">\n{input}\n{beginLabel}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";
    /**
     * @inheritdoc
     */
    public $horizontalRadioTemplate = "{beginWrapper}\n<div class=\"radio\">\n{input}\n{beginLabel}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";

    /**
     * @inheritdoc
     */
    public function __construct($config = [])
    {
        parent::__construct($config);

        AwesomeBootstrapCheckboxAsset::register(Yii::$app->getView());
    }

    /**
     * 带单位文本框
     *
     * @param       $unit
     * @param array $options
     *
     * @return $this
     */
    public function textUnitInput($unit, $options = [])
    {
        if(!$unit){
            return $this->textInput($options);
        }

        $options = array_merge($this->inputOptions, $options);
        $this->adjustLabelFor($options);
        $this->parts['{input}'] = Html::tag(
            'div',
            Html::activeTextInput($this->model, $this->attribute, $options) .
            Html::tag(
                'div',
                $unit,
                [
                    'class' => 'input-group-addon'
                ]
            ),
            [
                'class' => 'input-group'
            ]
        );

        return $this;
    }

    /**
     * 带按钮文本框
     *
     * @param       $buttonText
     * @param array $options
     *
     * @return $this
     */
    public function textButtonInput($buttonText, $options = [])
    {
        if(!$buttonText){
            return $this->textInput($options);
        }

        $buttonOptions = ['class' => 'btn btn-default'];

        if(isset($options['buttonOptions'])){
            $buttonOptions = array_merge($buttonOptions, $options['buttonOptions']);
            unset($options['buttonOptions']);
        }

        $options = array_merge($this->inputOptions, $options);
        $this->adjustLabelFor($options);
        $this->parts['{input}'] = Html::tag(
            'div',
            Html::activeTextInput($this->model, $this->attribute, $options) .
            Html::tag(
                'div',
                Html::button(
                    $buttonText,
                    $buttonOptions
                ),
                [
                    'class' => 'input-group-btn'
                ]
            ),
            [
                'class' => 'input-group'
            ]
        );

        return $this;
    }

    /**
     * @inheritdoc
     */
    public function dropDownList($items, $options = [])
    {
        if(!isset($options['class'])){
            $options['class'] = 'form-control selectpicker';
        }else{
            $options['class'] .= ' form-control selectpicker';
        }

        $options = array_merge($this->inputOptions, $options);
        $this->addAriaAttributes($options);
        $this->adjustLabelFor($options);
        $this->parts['{input}'] = Html::activeDropDownList($this->model, $this->attribute, $items, $options);

        return $this;
    }

    /**
     * 文件上传
     *
     * @return $this
     */
    public function file()
    {
        $inputName = Html::getInputName($this->model, $this->attribute);

        $this->parts['{input}'] = <<<HTML
<div class="fileinput fileinput-new input-group" data-provides="fileinput">
    <div class="form-control" data-trigger="fileinput">
        <i class="glyphicon glyphicon-file fileinput-exists"></i>
        <span class="fileinput-filename"></span>
    </div>
    <span class="input-group-addon btn btn-default btn-file">
        <span class="fileinput-new">选择文件</span>
        <span class="fileinput-exists">更换</span>
        <input type="file" name="$inputName"/>
    </span>
    <a href="#" class="input-group-addon btn btn-default fileinput-exists" data-dismiss="fileinput">移除</a>
</div>
HTML;

        JasnyBootstrapAsset::register(Yii::$app->getView());

        return $this;
    }

    /**
     * 单图上传
     *
     * @return $this
     */
    public function image()
    {
        $inputId = Html::getInputId($this->model, $this->attribute);
        $inputName = Html::getInputName($this->model, $this->attribute);
        $inputValue = Html::getAttributeValue($this->model, $this->attribute);

        $preview = Image::getImg($inputValue, 300);

        $data = Html::img($preview, ['data-default' => $preview]);

        if($inputValue){
            $data .= Html::hiddenInput($inputName, $inputValue, ['id' => $inputId]);
            $data .= Html::fileInput($inputName, $inputValue, ['class' => 'image-upload-input']);
        }else{
            $data .= Html::fileInput($inputName, $inputValue, ['id' => $inputId, 'class' => 'image-upload-input']);
        }

        $this->parts['{input}'] = Html::tag('div', $data, ['class' => 'image-upload']);

        $js = <<<JS
$('.image-upload-input').change(function(){
    var imgUrl = null;
    var objUrl = this.files[0];
    var filename = $(this).val();
    var previewImg = $(this).parent().find('img');
    
    fileExt = filename.substr(filename.lastIndexOf(".")).toLowerCase();
    
    $(this).closest('.form-group').removeClass('has-error');
    $(this).closest('.form-group').find('.help-block-error').text('');
    previewImg.attr('src', previewImg.attr('data-default'));
    
    if(fileExt != '.jpg' && fileExt != '.png' && fileExt != '.gif'){
        $(this).closest('.form-group').addClass('has-error');
        $(this).closest('.form-group').find('.help-block-error').text('图片格式不对，只能上传 jpg、png 和 gif 格式！');
    }else{
        if(window.createObjectURL != undefined){
            imgUrl = window.createObjectURL(objUrl) ;
        }else if (window.URL != undefined){
            imgUrl = window.URL.createObjectURL(objUrl) ;
        }else if (window.webkitURL != undefined){
            imgUrl = window.webkitURL.createObjectURL(objUrl) ;
        }
        
        if(imgUrl){
            previewImg.attr('src', imgUrl);
        }
    }
});
JS;

        Yii::$app->view->registerJs($js, View::POS_READY, 'image-upload');

        return $this;
    }

    /**
     * 级联下拉表单
     *
     * @param array $options
     *
     * @return $this
     */
    public function select($options = [])
    {
        $model = $this->model;
        $model = $model::className();
        $inputName = Html::getInputName($this->model, $this->attribute);
        $valueId = Html::getAttributeValue($this->model, $this->attribute);

        if(isset($options['class'])){
            $model = $options['class'];
            $modelData = $model::findOne($valueId);
            $ids = [0];
            if($modelData){
                $ids = $modelData->getParentIds();
            }
            $exclude = 0;
        }else{
            $primaryKey = $model::primaryKey();
            $primaryKey = current($primaryKey);
            $ids = $this->model->getParentIds();
            $exclude = $this->model->$primaryKey ? $this->model->$primaryKey : 0;
        }

        $selects = [];

        foreach($ids as $index => $parentId){
            $id = 0;
            if(isset($ids[$index + 1])){
                $id = $ids[$index + 1];
            }else{
                $id = $valueId;
            }

            $datas = $model::getSelectData($parentId, $exclude);
            if($datas){
                $selects[] = Html::dropDownList($inputName, $id, $datas, [
                    'class' => 'form-control form-control-inline',
                    'ajax-select' => Url::to(['ajax/select', 'model' => $model, 'input' => $inputName, 'exclude' => $exclude]),
                ]);
            }
        }

        if(!$selects){
            $selects[] = Html::dropDownList($inputName, '', [], [
                'class' => 'form-control form-control-inline',
                'prompt' => '请选择',
            ]);
        }

        $this->parts['{input}'] = join("", $selects);

        $js = <<<JS
        
$(document).off('change', 'select[ajax-select]');
$(document).on('change', 'select[ajax-select]', function(){
    var select = $(this);
    var url = $(this).attr('ajax-select');
    var parent_id = $(this).val();
    
    select.nextAll('select').remove();
    
    if(parent_id == $(this).children('option').eq(0).val()){
        return false;
    }
    
    $.post(url, {parent_id: parent_id}, function(datas){
        if(datas.html){
            select.after(datas.html);
        }
    }, 'json');
});

JS;

        Yii::$app->view->registerJs($js, View::POS_READY, 'ajax-select');
        SelectAsset::register(Yii::$app->getView());
        SelectFixAsset::register(Yii::$app->getView());

        return $this;
    }

    /**
     * 起止输入表单
     *
     * @param       $endAttr
     * @param array $options
     *
     * @return $this
     */
    public function between($endAttr, $options = [])
    {
        $hasTime = true;

        $beginInputId = Html::getInputId($this->model, $this->attribute);
        $endInputId = Html::getInputId($this->model, $endAttr);

        $beginInputName = Html::getInputName($this->model, $this->attribute);
        $endInputName = Html::getInputName($this->model, $endAttr);

        $beginValue = Html::getAttributeValue($this->model, $this->attribute);
        $endValue = Html::getAttributeValue($this->model, $endAttr);

        $template = '%s &nbsp; 到 &nbsp; %s';

        if($this->form->layout == 'default'){
            $template = '<div>%s &nbsp; 到 &nbsp; %s</div>';
        }
{'fraction_non_alphanumeric': 0.18261858831676311, 'fraction_numerical': 0.0010387297818667458, 'mean_word_length': 2.141880341880342, 'pattern_counts': {'":': 0, '<': 74, '<?xml version=': 0, '>': 211, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3591124', 'n_tokens_mistral': 6385, 'n_tokens_neox': 6275, 'n_words': 955}