PageRenderTime 110ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/yiibooster/widgets/TbButtonGroupColumn.php

https://gitlab.com/zenfork/vektor
PHP | 137 lines | 68 code | 19 blank | 50 comment | 19 complexity | 7e6333341e4b39cc407e0069ecaa5711 MD5 | raw file
  1. <?php
  2. /**
  3. *## TbButtonGroupColumn class file.
  4. *
  5. * @author Topher Kanyuga <kanjoti@gmail.com>
  6. * @copyright
  7. * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
  8. */
  9. Yii::import('booster.widgets.TbButtonColumn');
  10. /**
  11. *## Enhanced bootstrap button column widget.
  12. *
  13. * Renders the buttons as a button group
  14. *
  15. * @package booster.widgets.grids.columns
  16. */
  17. class TbButtonGroupColumn extends TbButtonColumn {
  18. /**
  19. * @var string the button size ('mini','small','normal','large')
  20. */
  21. public $buttonSize = 'mini';
  22. /**
  23. * @var string the view button context ('info','primary','warning','danger','success' defaults to 'info').
  24. */
  25. public $viewButtonContext = 'info';
  26. /**
  27. * @var string the update button context ('info','primary','warning','danger','success' defaults to 'warning').
  28. */
  29. public $updateButtonContext = 'warning';
  30. /**
  31. * @var string the delete button context ('info','primary','warning','danger','success' defaults to 'danger')
  32. */
  33. public $deleteButtonContext = 'danger';
  34. /**
  35. *### .initDefaultButtons()
  36. *
  37. * Initializes the default buttons (view, update and delete).
  38. */
  39. protected function initDefaultButtons() {
  40. parent::initDefaultButtons();
  41. if ($this->viewButtonContext !== false && !isset($this->buttons['view']['context'])) {
  42. $this->buttons['view']['context'] = $this->viewButtonContext;
  43. }
  44. if ($this->updateButtonContext !== false && !isset($this->buttons['update']['context'])) {
  45. $this->buttons['update']['context'] = $this->updateButtonContext;
  46. }
  47. if ($this->deleteButtonContext !== false && !isset($this->buttons['delete']['context'])) {
  48. $this->buttons['delete']['context'] = $this->deleteButtonContext;
  49. }
  50. }
  51. /**
  52. *### .renderButton()
  53. *
  54. * Renders a link button.
  55. *
  56. * @param string $id the ID of the button
  57. * @param array $button the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements.
  58. * @param integer $row the row number (zero-based)
  59. * @param mixed $data the data object associated with the row
  60. */
  61. protected function renderButton($id, $button, $row, $data) {
  62. if (isset($button['visible']) && !$this->evaluateExpression(
  63. $button['visible'],
  64. array('row' => $row, 'data' => $data)
  65. )
  66. ) {
  67. return;
  68. }
  69. $label = isset($button['label']) ? $button['label'] : $id;
  70. $url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row))
  71. : '#';
  72. $options = isset($button['options']) ? $button['options'] : array();
  73. if (!isset($options['title'])) {
  74. $options['title'] = $label;
  75. }
  76. if (!isset($options['data-toggle'])) {
  77. $options['data-toggle'] = 'tooltip';
  78. }
  79. if (!isset($options['class'])) {
  80. $options['class'] = '';
  81. }
  82. $options['class'] .= ' btn btn-' . $this->buttonSize;
  83. if (isset($button['context'])) {
  84. $options['class'] .= ' btn-' . $button['context'];
  85. }
  86. if (isset($button['icon'])) {
  87. if (strpos($button['icon'], 'icon') === false && strpos($button['icon'], 'fa') === false) {
  88. $button['icon'] = 'icon-' . implode(' icon-', explode(' ', $button['icon']));
  89. }
  90. echo CHtml::link('<i class="' . $button['icon'] . '"></i>', $url, $options);
  91. } else if (isset($button['imageUrl']) && is_string($button['imageUrl'])) {
  92. echo CHtml::link(CHtml::image($button['imageUrl'], $label), $url, $options);
  93. } else {
  94. echo CHtml::link($label, $url, $options);
  95. }
  96. }
  97. /**
  98. *### .renderDataCellContent()
  99. *
  100. * Renders the data cell content.
  101. * This method renders the view, update and delete buttons in the data cell.
  102. *
  103. * @param integer $row the row number (zero-based)
  104. * @param mixed $data the data associated with the row
  105. */
  106. protected function renderDataCellContent($row, $data)
  107. {
  108. $tr = array();
  109. ob_start();
  110. foreach ($this->buttons as $id => $button) {
  111. $this->renderButton($id, $button, $row, $data);
  112. $tr['{' . $id . '}'] = ob_get_contents();
  113. ob_clean();
  114. }
  115. ob_end_clean();
  116. echo "<div class='btn-group'>" . strtr($this->template, $tr) . "</div>";
  117. }
  118. }