Skip to content
This repository
branch: master
Tim Helfensdörfer thelfensdrfer
file 149 lines (118 sloc) 4.316 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
<?php

/**
* Render the bootstrap image carousel
* http://twitter.github.com/bootstrap/javascript.html#carousel
*
* @author Tim Helfensdörfer <tim@visualappeal.de>
* @version 0.3.5
* @package bootstrap.widgets
*/
class EBootstrapCarousel extends EBootstrapWidget {
/**
* Array of images
*
* Each item can have the following options:
* @param bool $active Only one item should be active. This will be the first the user can see
* @param string $src Image src
* @param string $alt Alternative text for the image
* @param string $caption Image caption
* @param string $body Text which will be rendered below the caption
* @param array $htmlOptions
*/
public $items = array();

/**
* Label for the previous control
*/
public $controlPrev = "&lsaquo;";

/**
* Label for the next control
*/
public $controlNext = "&rsaquo;";

/**
* Interval for the next image to appear
*/
public $interval = 5000;

/**
* Unfinit cycle
*/
public $infinite = false;

/**
* JS File to slide the images
*
* If its set to false, no file will be included
*/
public $jsFile = null;

/**
* Init the widget
*/
public function init() {
parent::init();

if (!isset($this->htmlOptions['id']))
$this->htmlOptions['id'] = $this->getId();

Yii::app()->clientScript->registerCoreScript('jquery');
if (is_null($this->jsFile)) {
$jsFile = dirname(__FILE__).'/js/bootstrap.min.js';
$this->jsFile = Yii::app()->getAssetManager()->publish($jsFile);
Yii::app()->clientScript->registerScriptFile($this->jsFile);
}

$jsId = 'carousel_'.$this->htmlOptions['id'];
$js = '';

if ($this->interval !== false) {
$js .= $jsId.' = $("#'.$this->htmlOptions['id'].'").carousel({
interval: '.$this->interval.',
});';
}
else {
$this->interval = 5000;
$js .= $jsId.' $("#'.$this->htmlOptions['id'].'").carousel();';
}

if ($this->infinite) {
$js .= '
'.$jsId.'.bind("slide", function() {
if ($("#'.$this->htmlOptions['id'].' .carousel-inner .item:last-child").hasClass("next")) {
setTimeout("'.$jsId.'.carousel(0)", '.$this->interval.');
}
});';
}

Yii::app()->clientScript->registerScript('ebootstrap-carousel-'.$this->htmlOptions['id'], $js, CClientScript::POS_READY);
}

/**
* Render the carousel
*/
public function run() {
parent::run();

if (is_array($this->items) and count($this->items)) {

EBootstrap::mergeClass($this->htmlOptions, array('carousel'));
echo EBootstrap::openTag('div', $this->htmlOptions)."\n";
echo EBootstrap::openTag('div', array('class' => 'carousel-inner'))."\n";

foreach ($this->items as $item) {
$itemOptions = isset($item['htmlOptions']) ? $item['htmlOptions'] : array();
EBootstrap::mergeClass($itemOptions, array('item'));
if (isset($item['active']) and ($item['active'] == true))
EBootstrap::mergeClass($itemOptions, array('active'));

echo EBootstrap::openTag('div', $itemOptions)."\n";

if (!isset($item['alt']))
$item['alt'] = isset($item['caption']) ? $item['caption'] : '';

if (isset($item['href']))
echo EBootstrap::openTag('a', array('href' => $item['href']));

echo EBootstrap::image($item['src'], $item['alt'], array())."\n";

if ((isset($item['caption']) and !empty($item['caption'])) or (isset($item['body']) and !empty($item['body']))) {
echo EBootstrap::openTag('div', array('class' => 'carousel-caption'));

if (isset($item['caption']) and !empty($item['caption']))
echo EBootstrap::tag('h4', array(), $item['caption'])."\n";

if (isset($item['body']) and !empty($item['body']))
echo EBootstrap::tag('p', array(), $item['body'])."\n";

echo EBootstrap::closeTag('div');
}

if (isset($item['href']))
echo EBootstrap::closeTag('a');

echo EBootstrap::closeTag('div')."\n";
}

echo EBootstrap::closeTag('div')."\n";

echo EBootstrap::link($this->controlPrev, '#'.$this->htmlOptions['id'], array('data-slide' => 'prev', 'class' => 'left carousel-control'))."\n";
echo EBootstrap::link($this->controlNext, '#'.$this->htmlOptions['id'], array('data-slide' => 'next', 'class' => 'right carousel-control'))."\n";

echo EBootstrap::closeTag('div')."\n";
}
}
}

?>
Something went wrong with that request. Please try again.