Carousel.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\base\InvalidConfigException;
  9. use yii\helpers\ArrayHelper;
  10. /**
  11. * Carousel renders a carousel bootstrap javascript component.
  12. *
  13. * For example:
  14. *
  15. * ```php
  16. * echo Carousel::widget([
  17. * 'items' => [
  18. * // the item contains only the image
  19. * '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg"/>',
  20. * // equivalent to the above
  21. * ['content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-02.jpg"/>'],
  22. * // the item contains both the image and the caption
  23. * [
  24. * 'content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-03.jpg"/>',
  25. * 'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
  26. * 'options' => [...],
  27. * ],
  28. * ]
  29. * ]);
  30. * ```
  31. *
  32. * @see http://getbootstrap.com/javascript/#carousel
  33. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  34. * @since 2.0
  35. */
  36. class Carousel extends Widget
  37. {
  38. /**
  39. * @var array|bool the labels for the previous and the next control buttons.
  40. * If false, it means the previous and the next control buttons should not be displayed.
  41. */
  42. public $controls = ['&lsaquo;', '&rsaquo;'];
  43. /**
  44. * @var bool whether carousel indicators (<ol> tag with anchors to items) should be displayed or not.
  45. */
  46. public $showIndicators = true;
  47. /**
  48. * @var array list of slides in the carousel. Each array element represents a single
  49. * slide with the following structure:
  50. *
  51. * ```php
  52. * [
  53. * // required, slide content (HTML), such as an image tag
  54. * 'content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg"/>',
  55. * // optional, the caption (HTML) of the slide
  56. * 'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
  57. * // optional the HTML attributes of the slide container
  58. * 'options' => [],
  59. * ]
  60. * ```
  61. */
  62. public $items = [];
  63. /**
  64. * Initializes the widget.
  65. */
  66. public function init()
  67. {
  68. parent::init();
  69. Html::addCssClass($this->options, ['widget' => 'carousel']);
  70. }
  71. /**
  72. * Renders the widget.
  73. */
  74. public function run()
  75. {
  76. $this->registerPlugin('carousel');
  77. return implode("\n", [
  78. Html::beginTag('div', $this->options),
  79. $this->renderIndicators(),
  80. $this->renderItems(),
  81. $this->renderControls(),
  82. Html::endTag('div')
  83. ]) . "\n";
  84. }
  85. /**
  86. * Renders carousel indicators.
  87. * @return string the rendering result
  88. */
  89. public function renderIndicators()
  90. {
  91. if ($this->showIndicators === false) {
  92. return '';
  93. }
  94. $indicators = [];
  95. for ($i = 0, $count = count($this->items); $i < $count; $i++) {
  96. $options = ['data-target' => '#' . $this->options['id'], 'data-slide-to' => $i];
  97. if ($i === 0) {
  98. Html::addCssClass($options, 'active');
  99. }
  100. $indicators[] = Html::tag('li', '', $options);
  101. }
  102. return Html::tag('ol', implode("\n", $indicators), ['class' => 'carousel-indicators']);
  103. }
  104. /**
  105. * Renders carousel items as specified on [[items]].
  106. * @return string the rendering result
  107. */
  108. public function renderItems()
  109. {
  110. $items = [];
  111. for ($i = 0, $count = count($this->items); $i < $count; $i++) {
  112. $items[] = $this->renderItem($this->items[$i], $i);
  113. }
  114. return Html::tag('div', implode("\n", $items), ['class' => 'carousel-inner']);
  115. }
  116. /**
  117. * Renders a single carousel item
  118. * @param string|array $item a single item from [[items]]
  119. * @param int $index the item index as the first item should be set to `active`
  120. * @return string the rendering result
  121. * @throws InvalidConfigException if the item is invalid
  122. */
  123. public function renderItem($item, $index)
  124. {
  125. if (is_string($item)) {
  126. $content = $item;
  127. $caption = null;
  128. $options = [];
  129. } elseif (isset($item['content'])) {
  130. $content = $item['content'];
  131. $caption = ArrayHelper::getValue($item, 'caption');
  132. if ($caption !== null) {
  133. $caption = Html::tag('div', $caption, ['class' => 'carousel-caption']);
  134. }
  135. $options = ArrayHelper::getValue($item, 'options', []);
  136. } else {
  137. throw new InvalidConfigException('The "content" option is required.');
  138. }
  139. Html::addCssClass($options, ['widget' => 'item']);
  140. if ($index === 0) {
  141. Html::addCssClass($options, 'active');
  142. }
  143. return Html::tag('div', $content . "\n" . $caption, $options);
  144. }
  145. /**
  146. * Renders previous and next control buttons.
  147. * @throws InvalidConfigException if [[controls]] is invalid.
  148. */
  149. public function renderControls()
  150. {
  151. if (isset($this->controls[0], $this->controls[1])) {
  152. return Html::a($this->controls[0], '#' . $this->options['id'], [
  153. 'class' => 'left carousel-control',
  154. 'data-slide' => 'prev',
  155. ]) . "\n"
  156. . Html::a($this->controls[1], '#' . $this->options['id'], [
  157. 'class' => 'right carousel-control',
  158. 'data-slide' => 'next',
  159. ]);
  160. } elseif ($this->controls === false) {
  161. return '';
  162. } else {
  163. throw new InvalidConfigException('The "controls" property must be either false or an array of two elements.');
  164. }
  165. }
  166. }