PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/admincrud/extensions/bootstrap/widgets/TbSelect2.php

https://github.com/max-rautkin/yii-admincrud
PHP | 125 lines | 65 code | 17 blank | 43 comment | 8 complexity | 708427f0b607a5c871c8a4ed6ff3ce41 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*## TbSelect2 class file.
  3. *
  4. * @author Antonio Ramirez <antonio@clevertech.biz>
  5. * @copyright Copyright &copy; Clevertech 2012-
  6. * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
  7. * @package bootstrap.widgets.input
  8. */
  9. class TbSelect2 extends CInputWidget
  10. {
  11. /**
  12. * @var TbActiveForm when created via TbActiveForm.
  13. * This attribute is set to the form that renders the widget
  14. * @see TbActionForm->inputRow
  15. */
  16. public $form;
  17. /**
  18. * @var array @param data for generating the list options (value=>display)
  19. */
  20. public $data = array();
  21. /**
  22. * @var string[] the JavaScript event handlers.
  23. */
  24. public $events = array();
  25. /**
  26. * @var bool whether to display a dropdown select box or use it for tagging
  27. */
  28. public $asDropDownList = true;
  29. /**
  30. * @var string the default value.
  31. */
  32. public $val;
  33. /**
  34. * @var
  35. */
  36. public $options;
  37. /**
  38. *### .init()
  39. *
  40. * Initializes the widget.
  41. */
  42. public function init()
  43. {
  44. if (empty($this->data) && $this->asDropDownList === true) {
  45. throw new CException(Yii::t('zii', '"data" attribute cannot be blank'));
  46. }
  47. $this->setDefaultWidthIfEmpty();
  48. }
  49. /**
  50. *### .run()
  51. *
  52. * Runs the widget.
  53. */
  54. public function run()
  55. {
  56. list($name, $id) = $this->resolveNameID();
  57. if ($this->hasModel()) {
  58. if ($this->form) {
  59. echo $this->asDropDownList
  60. ?
  61. $this->form->dropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions)
  62. :
  63. $this->form->hiddenField($this->model, $this->attribute, $this->htmlOptions);
  64. } else {
  65. echo $this->asDropDownList
  66. ?
  67. CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions)
  68. :
  69. CHtml::activeHiddenField($this->model, $this->attribute, $this->htmlOptions);
  70. }
  71. } else {
  72. echo $this->asDropDownList
  73. ?
  74. CHtml::dropDownList($name, $this->value, $this->data, $this->htmlOptions)
  75. :
  76. CHtml::hiddenField($name, $this->value, $this->htmlOptions);
  77. }
  78. $this->registerClientScript($id);
  79. }
  80. /**
  81. *### .registerClientScript()
  82. *
  83. * Registers required client script for bootstrap select2. It is not used through bootstrap->registerPlugin
  84. * in order to attach events if any
  85. */
  86. public function registerClientScript($id)
  87. {
  88. Yii::app()->bootstrap->registerAssetCss('select2.css');
  89. Yii::app()->bootstrap->registerAssetJs('select2.js');
  90. $options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
  91. $defValue = !empty($this->val) ? ".select2('val', '$this->val')" : '';
  92. ob_start();
  93. echo "jQuery('#{$id}').select2({$options})$defValue";
  94. foreach ($this->events as $event => $handler) {
  95. echo ".on('{$event}', " . CJavaScript::encode($handler) . ")";
  96. }
  97. Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $this->getId(), ob_get_clean() . ';');
  98. }
  99. private function setDefaultWidthIfEmpty()
  100. {
  101. if (empty($this->options)) {
  102. $this->options = array();
  103. }
  104. if (empty($this->options['width'])) {
  105. $this->options['width'] = 'resolve';
  106. }
  107. }
  108. }