Select2.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: AlexLcDee
  5. * Date: 27.01.2017
  6. * Time: 18:37
  7. */
  8. namespace yiins\components;
  9. use Yii;
  10. use yiins\assets\Select2Asset;
  11. class Select2
  12. {
  13. protected $options = [];
  14. protected $defaultOptions = [];
  15. protected $data = [];
  16. protected $htmlOptions = [
  17. 'class' => 'form-control',
  18. ];
  19. protected $containerOptions = [
  20. 'class' => 'form-group',
  21. ];
  22. protected $labelOptions = [
  23. 'class' => 'control-label',
  24. ];
  25. protected $errorOptions = [
  26. 'class' => 'help-block error',
  27. ];
  28. protected $attribute;
  29. protected $model;
  30. public static function renderActiveSingle(ActiveRecord $model, $attribute, $params = [])
  31. {
  32. $instance = static::instantiate($model, $attribute, $params);
  33. return $instance->renderSingle();
  34. }
  35. protected static function instantiate(ActiveRecord $model, $attribute, $params = [])
  36. {
  37. $instance = new static;
  38. $instance->setModel($model);
  39. $instance->setAttribute($attribute);
  40. foreach ($params as $param => $value) {
  41. $instance->set($param, $value);
  42. }
  43. $instance->init();
  44. return $instance;
  45. }
  46. protected function setModel(ActiveRecord $model)
  47. {
  48. $this->model = $model;
  49. }
  50. protected function setAttribute($attribute)
  51. {
  52. $this->attribute = $attribute;
  53. }
  54. protected function set($key, $value)
  55. {
  56. if (is_array($this->$key) && is_array($value)) {
  57. $this->$key = \CMap::mergeArray($this->$key, $value);
  58. }
  59. }
  60. public function init()
  61. {
  62. Select2Asset::register();
  63. $this->defaultOptions = \CMap::mergeArray($this->defaultOptions, [
  64. 'language' => Yii::app()->language,
  65. ]);
  66. $this->options = \CMap::mergeArray($this->defaultOptions, $this->options);
  67. }
  68. protected function renderSingle()
  69. {
  70. \CHtml::resolveNameID($this->model, $this->attribute, $this->htmlOptions);
  71. $group = \CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
  72. $id = $this->htmlOptions['id'];
  73. $options = \CJavaScript::encode($this->options);
  74. $script = <<<JS
  75. $("#$id").select2($options);
  76. JS;
  77. Yii::web()->clientScript->registerScript('select2-' . (\CHtml::$count++), $script);
  78. return $group;
  79. }
  80. public static function renderActive(ActiveRecord $model, $attribute, $params = [])
  81. {
  82. $instance = static::instantiate($model, $attribute, $params);
  83. return $instance->renderActiveField();
  84. }
  85. protected function renderActiveField()
  86. {
  87. \CHtml::resolveNameID($this->model, $this->attribute, $this->htmlOptions);
  88. $group = \CHtml::openTag('div', $this->containerOptions);
  89. $group .= \CHtml::activeLabelEx($this->model, $this->attribute, $this->labelOptions);
  90. $group .= \CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
  91. $group .= \CHtml::error($this->model, $this->attribute);
  92. $group .= \CHtml::closeTag('div');
  93. $id = $this->htmlOptions['id'];
  94. $options = \CJavaScript::encode($this->options);
  95. $script = <<<JS
  96. $("#$id").select2($options);
  97. JS;
  98. Yii::web()->clientScript->registerScript('select2-' . (\CHtml::$count++), $script);
  99. return $group;
  100. }
  101. }