<commit_before><?php
/**
 * Created by PhpStorm.
 * User: <NAME>
 * Date: 7/16/2016
 * Time: 7:32 PM
 */

namespace Cobonto\Classes\Traits;


trait HelperForm
{
    /** @var  array $fields form */
    protected $fields_form = [];
    /** @var  array $fields form */
    protected $fields_values = [];
    /** @var array $tpl_form */
    protected $tpl_form = 'admin.helpers.form.main';
    /** @var array available plugin for form */
    private $available_plugins = [
        'selecttwo',
        'colorpicker',
        'datepicker',
        'ckeditor',
        'tree',
    ];
    /** @var array list of switchers */
    protected $switchers = ['active'];
    /**
     * create form with helper form
     */
    protected function generateForm()
    {
        // check fields_value for edit it
        $dataFormDb = true;
        if(count($this->fields_values))
            $dataFormDb = false;
        if (count($this->fields_form))
        {
            // add plugins
            foreach ($this->fields_form as &$form)
            {
                foreach ($form['input'] as &$field)
                {
                    // add field values
                    if($dataFormDb)
                    {
                        if(is_object($this->model) && $this->model->id)
                        {
                            $this->fields_values[$field['name']]=$this->model->{$field['name']};
                        }
                        else
                        {
                            $this->fields_values[$field['name']] = (isset($field['default_value'])?$field['default_value']:null);
                        }
                    }

                    if (in_array($field['type'], $this->available_plugins))
                        $this->assign->addPlugin($field['type']);
                    if ($field['type'] == 'selecttwo')
                    {
                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").select2({';
                        $this->addJqueryOptions($field);
                    }
                    elseif ($field['type'] == 'inputmask')
                    {
                        $this->assign->addJS('plugins/inputmask/jquery.inputmask.js',true);
                        // check has extenstions
                        if (isset($field['extensions']))
                        {
                            $this->assign->addJS([
                                'plugins/inputmask/jquery.inputmask.' . $field['extensions'] . '.extensions.js',
                                'plugins/inputmask/jquery.inputmask.extensions.js',
                            ],true);
                        }

                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").inputmask()';

                    }
                    elseif ($field['type'] == 'colorpicker')
                    {
                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").colorpicker({';
                        $this->addJqueryOptions($field);
                    }
                    elseif ($field['type'] == 'datepicker')
                    {
                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").datepicker({';
                        // add options
                        $this->addJqueryOptions($field);

                    }
                    elseif($field['type']=='tree')
                    {
                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").treeview({
                        data:'.$field['data'].',';
                        // add options
                        $this->assign->addJSVars([
                            'tree_id'=>$id,
                            'multiSelect'=>$field['multiSelect'],
                            'tree_name'=>$field['name'],
                        ]);
                        $this->addJqueryOptions($field);
                    }
                    elseif ($field['type'] == 'textarea' && isset($field['class']) && $field['class'] == 'ckeditor')
                    {
                        $this->assign->addJS('plugins/ckeditor/ckeditor.js',true);
                        $this->assign->addJSVars(['eldifnder_url'=>route('elfinder.ckeditor')]);

                    }
                    elseif ($field['type'] == 'switch')
                    {
                        $this->assign->addPlugin('bootstrap-switch');
                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").bootstrapSwitch({';
                        $this->addJqueryOptions($field);
                    }
                }
                // add id to field_value
            }
            // add more values to array before pass to view<commit_msg>fix multi language for ckeditor<commit_after><?php
/**
 * Created by PhpStorm.
 * User: <NAME>
 * Date: 7/16/2016
 * Time: 7:32 PM
 */

namespace Cobonto\Classes\Traits;


trait HelperForm
{
    /** @var  array $fields form */
    protected $fields_form = [];
    /** @var  array $fields form */
    protected $fields_values = [];
    /** @var array $tpl_form */
    protected $tpl_form = 'admin.helpers.form.main';
    /** @var array available plugin for form */
    private $available_plugins = [
        'selecttwo',
        'colorpicker',
        'datepicker',
        'ckeditor',
        'tree',
    ];
    /** @var array list of switchers */
    protected $switchers = ['active'];
    /**
     * create form with helper form
     */
    protected function generateForm()
    {
        // check fields_value for edit it
        $dataFormDb = true;
        if(count($this->fields_values))
            $dataFormDb = false;
        if (count($this->fields_form))
        {
            // add plugins
            foreach ($this->fields_form as &$form)
            {
                foreach ($form['input'] as &$field)
                {
                    // add field values
                    if($dataFormDb)
                    {
                        if(is_object($this->model) && $this->model->id)
                        {
                            $this->fields_values[$field['name']]=$this->model->{$field['name']};
                        }
                        else
                        {
                            $this->fields_values[$field['name']] = (isset($field['default_value'])?$field['default_value']:null);
                        }
                    }

                    if (in_array($field['type'], $this->available_plugins))
                        $this->assign->addPlugin($field['type']);
                    if ($field['type'] == 'selecttwo')
                    {
                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").select2({';
                        $this->addJqueryOptions($field);
                    }
                    elseif ($field['type'] == 'inputmask')
                    {
                        $this->assign->addJS('plugins/inputmask/jquery.inputmask.js',true);
                        // check has extenstions
                        if (isset($field['extensions']))
                        {
                            $this->assign->addJS([
                                'plugins/inputmask/jquery.inputmask.' . $field['extensions'] . '.extensions.js',
                                'plugins/inputmask/jquery.inputmask.extensions.js',
                            ],true);
                        }

                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").inputmask()';

                    }
                    elseif ($field['type'] == 'colorpicker')
                    {
                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").colorpicker({';
                        $this->addJqueryOptions($field);
                    }
                    elseif ($field['type'] == 'datepicker')
                    {
                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").datepicker({';
                        // add options
                        $this->addJqueryOptions($field);

                    }
                    elseif($field['type']=='tree')
                    {
                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").treeview({
                        data:'.$field['data'].',';
                        // add options
                        $this->assign->addJSVars([
                            'tree_id'=>$id,
                            'multiSelect'=>$field['multiSelect'],
                            'tree_name'=>$field['name'],
                        ]);
                        $this->addJqueryOptions($field);
                    }
                    elseif ($field['type'] == 'textarea' && isset($field['class']) && $field['class'] == 'ckeditor')
                    {
                        $contentsLangDirection='ltr';
                        $cke_lang = 'en';
                        $this->assign->addJS('plugins/ckeditor/ckeditor.js',true);
                        if(config('app.rtl'))
                        {
                            $contentsLangDirection='rtl';
                            $cke_lang ='fa';
                        }
                        $this->assign->addJSVars(
                            [
                                'eldifnder_url'=>route('elfinder.ckeditor'),
                                'contentsLangDirection'=>$contentsLangDirection,
                                'cke_lang'=>$cke_lang,
                            ]
                            );

                    }
                    elseif ($field['type'] == 'switch')
                    {
                        $this->assign->addPlugin('bootstrap-switch');
                        $id = (isset($field['id']) ? $field['id'] : $field['name']);
                        $field['javascript'] = '$("#' . $id . '").bootstrapSwitch({';
                        $this->addJqueryOptions($field);
                    }
                }
                // add id to field_value
            }
            // add more values to array before pass to view
{'fraction_non_alphanumeric': 0.16078647556045572, 'fraction_numerical': 0.002021315692760015, 'mean_word_length': 1.1013513513513513, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 75, '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': '2866814', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2572, 'n_words': 421}