| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- /**
- * Created by PhpStorm.
- * User: AlexLcDee
- * Date: 27.01.2017
- * Time: 18:37
- */
- namespace yiins\components;
- use Yii;
- use yiins\assets\Select2Asset;
- class Select2
- {
- protected $options = [];
- protected $defaultOptions = [];
- protected $data = [];
- protected $htmlOptions = [
- 'class' => 'form-control',
- ];
- protected $containerOptions = [
- 'class' => 'form-group',
- ];
- protected $labelOptions = [
- 'class' => 'control-label',
- ];
- protected $errorOptions = [
- 'class' => 'help-block error',
- ];
- protected $attribute;
- protected $model;
- public static function renderActiveSingle(ActiveRecord $model, $attribute, $params = [])
- {
- $instance = static::instantiate($model, $attribute, $params);
- return $instance->renderSingle();
- }
- protected static function instantiate(ActiveRecord $model, $attribute, $params = [])
- {
- $instance = new static;
- $instance->setModel($model);
- $instance->setAttribute($attribute);
- foreach ($params as $param => $value) {
- $instance->set($param, $value);
- }
- $instance->init();
- return $instance;
- }
- protected function setModel(ActiveRecord $model)
- {
- $this->model = $model;
- }
- protected function setAttribute($attribute)
- {
- $this->attribute = $attribute;
- }
- protected function set($key, $value)
- {
- if (is_array($this->$key) && is_array($value)) {
- $this->$key = \CMap::mergeArray($this->$key, $value);
- }
- }
- public function init()
- {
- Select2Asset::register();
- $this->defaultOptions = \CMap::mergeArray($this->defaultOptions, [
- 'language' => Yii::app()->language,
- ]);
- $this->options = \CMap::mergeArray($this->defaultOptions, $this->options);
- }
- protected function renderSingle()
- {
- \CHtml::resolveNameID($this->model, $this->attribute, $this->htmlOptions);
- $group = \CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
- $id = $this->htmlOptions['id'];
- $options = \CJavaScript::encode($this->options);
- $script = <<<JS
- $("#$id").select2($options);
- JS;
- Yii::web()->clientScript->registerScript('select2-' . (\CHtml::$count++), $script);
- return $group;
- }
- public static function renderActive(ActiveRecord $model, $attribute, $params = [])
- {
- $instance = static::instantiate($model, $attribute, $params);
- return $instance->renderActiveField();
- }
- protected function renderActiveField()
- {
- \CHtml::resolveNameID($this->model, $this->attribute, $this->htmlOptions);
- $group = \CHtml::openTag('div', $this->containerOptions);
- $group .= \CHtml::activeLabelEx($this->model, $this->attribute, $this->labelOptions);
- $group .= \CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
- $group .= \CHtml::error($this->model, $this->attribute);
- $group .= \CHtml::closeTag('div');
- $id = $this->htmlOptions['id'];
- $options = \CJavaScript::encode($this->options);
- $script = <<<JS
- $("#$id").select2($options);
- JS;
- Yii::web()->clientScript->registerScript('select2-' . (\CHtml::$count++), $script);
- return $group;
- }
- }
|