PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/admincrud/extensions/bootstrap/widgets/TbButtonGroupColumn.php

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