Modal.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. * Modal renders a modal window that can be toggled by clicking on a button.
  13. *
  14. * The following example will show the content enclosed between the [[begin()]]
  15. * and [[end()]] calls within the modal window:
  16. *
  17. * ~~~php
  18. * Modal::begin([
  19. * 'header' => '<h2>Hello world</h2>',
  20. * 'toggleButton' => ['label' => 'click me'],
  21. * ]);
  22. *
  23. * echo 'Say hello...';
  24. *
  25. * Modal::end();
  26. * ~~~
  27. *
  28. * @see http://getbootstrap.com/javascript/#modals
  29. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  30. * @author Qiang Xue <qiang.xue@gmail.com>
  31. * @since 2.0
  32. */
  33. class Modal extends Widget
  34. {
  35. const SIZE_LARGE = "modal-lg";
  36. const SIZE_SMALL = "modal-sm";
  37. const SIZE_DEFAULT = "";
  38. /**
  39. * @var string the header content in the modal window.
  40. */
  41. public $header;
  42. /**
  43. * @var string the footer content in the modal window.
  44. */
  45. public $footer;
  46. /**
  47. * @var string the modal size. Can be MODAL_LG or MODAL_SM, or empty for default.
  48. */
  49. public $size;
  50. /**
  51. * @var array the options for rendering the close button tag.
  52. * The close button is displayed in the header of the modal window. Clicking
  53. * on the button will hide the modal window. If this is null, no close button will be rendered.
  54. *
  55. * The following special options are supported:
  56. *
  57. * - tag: string, the tag name of the button. Defaults to 'button'.
  58. * - label: string, the label of the button. Defaults to '&times;'.
  59. *
  60. * The rest of the options will be rendered as the HTML attributes of the button tag.
  61. * Please refer to the [Modal plugin help](http://getbootstrap.com/javascript/#modals)
  62. * for the supported HTML attributes.
  63. */
  64. public $closeButton = [];
  65. /**
  66. * @var array the options for rendering the toggle button tag.
  67. * The toggle button is used to toggle the visibility of the modal window.
  68. * If this property is null, no toggle button will be rendered.
  69. *
  70. * The following special options are supported:
  71. *
  72. * - tag: string, the tag name of the button. Defaults to 'button'.
  73. * - label: string, the label of the button. Defaults to 'Show'.
  74. *
  75. * The rest of the options will be rendered as the HTML attributes of the button tag.
  76. * Please refer to the [Modal plugin help](http://getbootstrap.com/javascript/#modals)
  77. * for the supported HTML attributes.
  78. */
  79. public $toggleButton;
  80. /**
  81. * Initializes the widget.
  82. */
  83. public function init()
  84. {
  85. parent::init();
  86. $this->initOptions();
  87. echo $this->renderToggleButton() . "\n";
  88. echo Html::beginTag('div', $this->options) . "\n";
  89. echo Html::beginTag('div', ['class' => 'modal-dialog ' . $this->size]) . "\n";
  90. echo Html::beginTag('div', ['class' => 'modal-content']) . "\n";
  91. echo $this->renderHeader() . "\n";
  92. echo $this->renderBodyBegin() . "\n";
  93. }
  94. /**
  95. * Renders the widget.
  96. */
  97. public function run()
  98. {
  99. echo "\n" . $this->renderBodyEnd();
  100. echo "\n" . $this->renderFooter();
  101. echo "\n" . Html::endTag('div'); // modal-content
  102. echo "\n" . Html::endTag('div'); // modal-dialog
  103. echo "\n" . Html::endTag('div');
  104. $this->registerPlugin('modal');
  105. }
  106. /**
  107. * Renders the header HTML markup of the modal
  108. * @return string the rendering result
  109. */
  110. protected function renderHeader()
  111. {
  112. $button = $this->renderCloseButton();
  113. if ($button !== null) {
  114. $this->header = $button . "\n" . $this->header;
  115. }
  116. if ($this->header !== null) {
  117. return Html::tag('div', "\n" . $this->header . "\n", ['class' => 'modal-header']);
  118. } else {
  119. return null;
  120. }
  121. }
  122. /**
  123. * Renders the opening tag of the modal body.
  124. * @return string the rendering result
  125. */
  126. protected function renderBodyBegin()
  127. {
  128. return Html::beginTag('div', ['class' => 'modal-body']);
  129. }
  130. /**
  131. * Renders the closing tag of the modal body.
  132. * @return string the rendering result
  133. */
  134. protected function renderBodyEnd()
  135. {
  136. return Html::endTag('div');
  137. }
  138. /**
  139. * Renders the HTML markup for the footer of the modal
  140. * @return string the rendering result
  141. */
  142. protected function renderFooter()
  143. {
  144. if ($this->footer !== null) {
  145. return Html::tag('div', "\n" . $this->footer . "\n", ['class' => 'modal-footer']);
  146. } else {
  147. return null;
  148. }
  149. }
  150. /**
  151. * Renders the toggle button.
  152. * @return string the rendering result
  153. */
  154. protected function renderToggleButton()
  155. {
  156. if ($this->toggleButton !== null) {
  157. $tag = ArrayHelper::remove($this->toggleButton, 'tag', 'button');
  158. $label = ArrayHelper::remove($this->toggleButton, 'label', 'Show');
  159. if ($tag === 'button' && !isset($this->toggleButton['type'])) {
  160. $this->toggleButton['type'] = 'button';
  161. }
  162. return Html::tag($tag, $label, $this->toggleButton);
  163. } else {
  164. return null;
  165. }
  166. }
  167. /**
  168. * Renders the close button.
  169. * @return string the rendering result
  170. */
  171. protected function renderCloseButton()
  172. {
  173. if ($this->closeButton !== null) {
  174. $tag = ArrayHelper::remove($this->closeButton, 'tag', 'button');
  175. $label = ArrayHelper::remove($this->closeButton, 'label', '&times;');
  176. if ($tag === 'button' && !isset($this->closeButton['type'])) {
  177. $this->closeButton['type'] = 'button';
  178. }
  179. return Html::tag($tag, $label, $this->closeButton);
  180. } else {
  181. return null;
  182. }
  183. }
  184. /**
  185. * Initializes the widget options.
  186. * This method sets the default values for various options.
  187. */
  188. protected function initOptions()
  189. {
  190. $this->options = array_merge([
  191. 'class' => 'fade',
  192. 'role' => 'dialog',
  193. 'tabindex' => -1,
  194. ], $this->options);
  195. Html::addCssClass($this->options, 'modal');
  196. if ($this->clientOptions !== false) {
  197. $this->clientOptions = array_merge(['show' => false], $this->clientOptions);
  198. }
  199. if ($this->closeButton !== null) {
  200. $this->closeButton = array_merge([
  201. 'data-dismiss' => 'modal',
  202. 'aria-hidden' => 'true',
  203. 'class' => 'close',
  204. ], $this->closeButton);
  205. }
  206. if ($this->toggleButton !== null) {
  207. $this->toggleButton = array_merge([
  208. 'data-toggle' => 'modal',
  209. ], $this->toggleButton);
  210. if (!isset($this->toggleButton['data-target']) && !isset($this->toggleButton['href'])) {
  211. $this->toggleButton['data-target'] = '#' . $this->options['id'];
  212. }
  213. }
  214. }
  215. }