Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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