Carousel.php 4.91 KB
Newer Older
Antonio Ramirez committed
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\bootstrap;

Qiang Xue committed
10
use yii\base\InvalidConfigException;
Antonio Ramirez committed
11
use yii\helpers\ArrayHelper;
Antonio Ramirez committed
12 13 14 15 16
use yii\helpers\Html;

/**
 * Carousel renders a carousel bootstrap javascript component.
 *
17
 * For example:
Antonio Ramirez committed
18 19
 *
 * ```php
Alexander Makarov committed
20 21
 * echo Carousel::widget([
 *     'items' => [
Qiang Xue committed
22
 *         // the item contains only the image
Antonio Ramirez committed
23
 *         '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg"/>',
Qiang Xue committed
24
 *         // equivalent to the above
Alexander Makarov committed
25
 *         ['content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-02.jpg"/>'],
Qiang Xue committed
26
 *         // the item contains both the image and the caption
Alexander Makarov committed
27
 *         [
Antonio Ramirez committed
28
 *             'content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-03.jpg"/>',
29
 *             'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
Alexander Makarov committed
30 31
 *             'options' => [...],
 *         ],
Antonio Ramirez committed
32 33 34 35 36 37 38 39 40 41 42
 *     )
 * ));
 * ```
 *
 * @see http://twitter.github.io/bootstrap/javascript.html#carousel
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @since 2.0
 */
class Carousel extends Widget
{
	/**
Qiang Xue committed
43 44
	 * @var array|boolean the labels for the previous and the next control buttons.
	 * If false, it means the previous and the next control buttons should not be displayed.
Antonio Ramirez committed
45
	 */
Alexander Makarov committed
46
	public $controls = ['&lsaquo;', '&rsaquo;'];
Antonio Ramirez committed
47
	/**
Qiang Xue committed
48 49
	 * @var array list of slides in the carousel. Each array element represents a single
	 * slide with the following structure:
Antonio Ramirez committed
50 51
	 *
	 * ```php
Alexander Makarov committed
52
	 * [
Qiang Xue committed
53 54 55
	 *     // required, slide content (HTML), such as an image tag
	 *     'content' => '<img src="http://twitter.github.io/bootstrap/assets/img/bootstrap-mdo-sfmoma-01.jpg"/>',
	 *     // optional, the caption (HTML) of the slide
56
	 *     'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
Qiang Xue committed
57
	 *     // optional the HTML attributes of the slide container
Alexander Makarov committed
58 59
	 *     'options' => [],
	 * ]
Antonio Ramirez committed
60 61
	 * ```
	 */
Alexander Makarov committed
62
	public $items = [];
Antonio Ramirez committed
63 64 65 66 67 68 69 70


	/**
	 * Initializes the widget.
	 */
	public function init()
	{
		parent::init();
71
		Html::addCssClass($this->options, 'carousel');
Antonio Ramirez committed
72 73 74 75 76 77 78 79
	}

	/**
	 * Renders the widget.
	 */
	public function run()
	{
		echo Html::beginTag('div', $this->options) . "\n";
80 81
		echo $this->renderIndicators() . "\n";
		echo $this->renderItems() . "\n";
Qiang Xue committed
82
		echo $this->renderControls() . "\n";
Antonio Ramirez committed
83
		echo Html::endTag('div') . "\n";
84
		$this->registerPlugin('carousel');
Antonio Ramirez committed
85 86 87
	}

	/**
Qiang Xue committed
88 89
	 * Renders carousel indicators.
	 * @return string the rendering result
Antonio Ramirez committed
90 91 92
	 */
	public function renderIndicators()
	{
Alexander Makarov committed
93
		$indicators = [];
94
		for ($i = 0, $count = count($this->items); $i < $count; $i++) {
Alexander Makarov committed
95
			$options = ['data-target' => '#' . $this->options['id'], 'data-slide-to' => $i];
Antonio Ramirez committed
96
			if ($i === 0) {
97
				Html::addCssClass($options, 'active');
Antonio Ramirez committed
98
			}
Qiang Xue committed
99
			$indicators[] = Html::tag('li', '', $options);
Antonio Ramirez committed
100
		}
Alexander Makarov committed
101
		return Html::tag('ol', implode("\n", $indicators), ['class' => 'carousel-indicators']);
Antonio Ramirez committed
102 103 104
	}

	/**
Qiang Xue committed
105 106
	 * Renders carousel items as specified on [[items]].
	 * @return string the rendering result
Antonio Ramirez committed
107 108 109
	 */
	public function renderItems()
	{
Alexander Makarov committed
110
		$items = [];
111
		for ($i = 0, $count = count($this->items); $i < $count; $i++) {
Qiang Xue committed
112
			$items[] = $this->renderItem($this->items[$i], $i);
Antonio Ramirez committed
113
		}
Alexander Makarov committed
114
		return Html::tag('div', implode("\n", $items), ['class' => 'carousel-inner']);
Antonio Ramirez committed
115 116 117 118
	}

	/**
	 * Renders a single carousel item
Qiang Xue committed
119
	 * @param string|array $item a single item from [[items]]
Antonio Ramirez committed
120
	 * @param integer $index the item index as the first item should be set to `active`
Qiang Xue committed
121 122
	 * @return string the rendering result
	 * @throws InvalidConfigException if the item is invalid
Antonio Ramirez committed
123 124 125
	 */
	public function renderItem($item, $index)
	{
Antonio Ramirez committed
126
		if (is_string($item)) {
Qiang Xue committed
127 128
			$content = $item;
			$caption = null;
Alexander Makarov committed
129
			$options = [];
Qiang Xue committed
130 131 132 133
		} elseif (isset($item['content'])) {
			$content = $item['content'];
			$caption = ArrayHelper::getValue($item, 'caption');
			if ($caption !== null) {
Alexander Makarov committed
134
				$caption = Html::tag('div', $caption, ['class' => 'carousel-caption']);
Qiang Xue committed
135
			}
Alexander Makarov committed
136
			$options = ArrayHelper::getValue($item, 'options', []);
Antonio Ramirez committed
137
		} else {
Qiang Xue committed
138
			throw new InvalidConfigException('The "content" option is required.');
Antonio Ramirez committed
139 140
		}

141
		Html::addCssClass($options, 'item');
Antonio Ramirez committed
142
		if ($index === 0) {
143
			Html::addCssClass($options, 'active');
Antonio Ramirez committed
144
		}
145

Qiang Xue committed
146
		return Html::tag('div', $content . "\n" . $caption, $options);
Antonio Ramirez committed
147 148 149
	}

	/**
Qiang Xue committed
150 151
	 * Renders previous and next control buttons.
	 * @throws InvalidConfigException if [[controls]] is invalid.
Antonio Ramirez committed
152
	 */
Qiang Xue committed
153
	public function renderControls()
Antonio Ramirez committed
154
	{
Qiang Xue committed
155
		if (isset($this->controls[0], $this->controls[1])) {
Alexander Makarov committed
156
			return Html::a($this->controls[0], '#' . $this->options['id'], [
Antonio Ramirez committed
157
				'class' => 'left carousel-control',
158
				'data-slide' => 'prev',
Alexander Makarov committed
159 160
			]) . "\n"
			. Html::a($this->controls[1], '#' . $this->options['id'], [
Antonio Ramirez committed
161
				'class' => 'right carousel-control',
162
				'data-slide' => 'next',
Alexander Makarov committed
163
			]);
Qiang Xue committed
164 165 166 167 168
		} elseif ($this->controls === false) {
			return '';
		} else {
			throw new InvalidConfigException('The "controls" property must be either false or an array of two elements.');
		}
Antonio Ramirez committed
169
	}
170
}