Carousel.php 5.5 KB

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