You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Alert.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\bootstrap;
  8. use Yii;
  9. use yii\helpers\ArrayHelper;
  10. use yii\helpers\Html;
  11. /**
  12. * Alert renders an alert bootstrap component.
  13. *
  14. * For example,
  15. *
  16. * ```php
  17. * echo Alert::widget([
  18. * 'options' => [
  19. * 'class' => 'alert-info',
  20. * ],
  21. * 'body' => 'Say hello...',
  22. * ]);
  23. * ```
  24. *
  25. * The following example will show the content enclosed between the [[begin()]]
  26. * and [[end()]] calls within the alert box:
  27. *
  28. * ```php
  29. * Alert::begin([
  30. * 'options' => [
  31. * 'class' => 'alert-warning',
  32. * ],
  33. * ]);
  34. *
  35. * echo 'Say hello...';
  36. *
  37. * Alert::end();
  38. * ```
  39. *
  40. * @see http://getbootstrap.com/components/#alerts
  41. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  42. * @since 2.0
  43. */
  44. class Alert extends Widget
  45. {
  46. /**
  47. * @var string the body content in the alert component. Note that anything between
  48. * the [[begin()]] and [[end()]] calls of the Alert widget will also be treated
  49. * as the body content, and will be rendered before this.
  50. */
  51. public $body;
  52. /**
  53. * @var array the options for rendering the close button tag.
  54. * The close button is displayed in the header of the modal window. Clicking
  55. * on the button will hide the modal window. If this is false, no close button will be rendered.
  56. *
  57. * The following special options are supported:
  58. *
  59. * - tag: string, the tag name of the button. Defaults to 'button'.
  60. * - label: string, the label of the button. Defaults to '&times;'.
  61. *
  62. * The rest of the options will be rendered as the HTML attributes of the button tag.
  63. * Please refer to the [Alert documentation](http://getbootstrap.com/components/#alerts)
  64. * for the supported HTML attributes.
  65. */
  66. public $closeButton = [];
  67. /**
  68. * Initializes the widget.
  69. */
  70. public function init()
  71. {
  72. parent::init();
  73. $this->initOptions();
  74. echo Html::beginTag('div', $this->options) . "\n";
  75. echo $this->renderBodyBegin() . "\n";
  76. }
  77. /**
  78. * Renders the widget.
  79. */
  80. public function run()
  81. {
  82. echo "\n" . $this->renderBodyEnd();
  83. echo "\n" . Html::endTag('div');
  84. $this->registerPlugin('alert');
  85. }
  86. /**
  87. * Renders the close button if any before rendering the content.
  88. * @return string the rendering result
  89. */
  90. protected function renderBodyBegin()
  91. {
  92. return $this->renderCloseButton();
  93. }
  94. /**
  95. * Renders the alert body (if any).
  96. * @return string the rendering result
  97. */
  98. protected function renderBodyEnd()
  99. {
  100. return $this->body . "\n";
  101. }
  102. /**
  103. * Renders the close button.
  104. * @return string the rendering result
  105. */
  106. protected function renderCloseButton()
  107. {
  108. if ($this->closeButton !== false) {
  109. $tag = ArrayHelper::remove($this->closeButton, 'tag', 'button');
  110. $label = ArrayHelper::remove($this->closeButton, 'label', '&times;');
  111. if ($tag === 'button' && !isset($this->closeButton['type'])) {
  112. $this->closeButton['type'] = 'button';
  113. }
  114. return Html::tag($tag, $label, $this->closeButton);
  115. } else {
  116. return null;
  117. }
  118. }
  119. /**
  120. * Initializes the widget options.
  121. * This method sets the default values for various options.
  122. */
  123. protected function initOptions()
  124. {
  125. Html::addCssClass($this->options, 'alert');
  126. Html::addCssClass($this->options, 'fade');
  127. Html::addCssClass($this->options, 'in');
  128. if ($this->closeButton !== false) {
  129. $this->closeButton = array_merge([
  130. 'data-dismiss' => 'alert',
  131. 'aria-hidden' => 'true',
  132. 'class' => 'close',
  133. ], $this->closeButton);
  134. }
  135. }
  136. }