Carousel.php 5.55 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' => [...],
 *         ],
Alexander Makarov committed
32 33
 *     ]
 * ]);
Antonio Ramirez committed
34 35
 * ```
 *
MarsuBoss committed
36
 * @see http://getbootstrap.com/javascript/#carousel
Antonio Ramirez committed
37 38 39 40 41
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @since 2.0
 */
class Carousel extends Widget
{
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    /**
     * @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.
     */
    public $controls = ['&lsaquo;', '&rsaquo;'];
    /**
     * @var array list of slides in the carousel. Each array element represents a single
     * slide with the following structure:
     *
     * ```php
     * [
     *     // 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
     *     'caption' => '<h4>This is title</h4><p>This is the caption text</p>',
     *     // optional the HTML attributes of the slide container
     *     'options' => [],
     * ]
     * ```
     */
    public $items = [];
Antonio Ramirez committed
63

64 65 66 67 68 69 70 71
    /**
     * Initializes the widget.
     */
    public function init()
    {
        parent::init();
        Html::addCssClass($this->options, 'carousel');
    }
Antonio Ramirez committed
72

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

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

101 102
        return Html::tag('ol', implode("\n", $indicators), ['class' => 'carousel-indicators']);
    }
Antonio Ramirez committed
103

104 105 106 107 108 109 110 111 112 113
    /**
     * Renders carousel items as specified on [[items]].
     * @return string the rendering result
     */
    public function renderItems()
    {
        $items = [];
        for ($i = 0, $count = count($this->items); $i < $count; $i++) {
            $items[] = $this->renderItem($this->items[$i], $i);
        }
Antonio Ramirez committed
114

115 116
        return Html::tag('div', implode("\n", $items), ['class' => 'carousel-inner']);
    }
Antonio Ramirez committed
117

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

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

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        return Html::tag('div', $content . "\n" . $caption, $options);
    }

    /**
     * Renders previous and next control buttons.
     * @throws InvalidConfigException if [[controls]] is invalid.
     */
    public function renderControls()
    {
        if (isset($this->controls[0], $this->controls[1])) {
            return Html::a($this->controls[0], '#' . $this->options['id'], [
                'class' => 'left carousel-control',
                'data-slide' => 'prev',
            ]) . "\n"
            . Html::a($this->controls[1], '#' . $this->options['id'], [
                'class' => 'right carousel-control',
                'data-slide' => 'next',
            ]);
        } elseif ($this->controls === false) {
            return '';
        } else {
            throw new InvalidConfigException('The "controls" property must be either false or an array of two elements.');
        }
    }
171
}