1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* TbCarousel class file.
|
4
|
|
|
* @author Christoffer Niska <[email protected]>
|
5
|
|
|
* @copyright Copyright © Christoffer Niska 2011-
|
6
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
|
7
|
|
|
* @package bootstrap.widgets
|
8
|
|
|
* @since 0.9.10
|
9
|
|
|
*/
|
10
|
|
|
|
11
|
|
|
/**
|
12
|
|
|
* Bootstrap carousel widget.
|
13
|
|
|
* @see http://twitter.github.com/bootstrap/javascript.html#carousel
|
14
|
|
|
*/
|
15
|
|
|
class TbCarousel extends CWidget
|
16
|
|
|
{
|
17
|
|
|
/**
|
18
|
|
|
* @var string the previous button label. Defaults to '‹'.
|
19
|
|
|
*/
|
20
|
|
|
public $prevLabel = '‹';
|
21
|
|
|
/**
|
22
|
|
|
* @var string the next button label. Defaults to '›'.
|
23
|
|
|
*/
|
24
|
|
|
public $nextLabel = '›';
|
25
|
|
|
/**
|
26
|
|
|
* @var boolean indicates whether the carousel should slide items.
|
27
|
|
|
*/
|
28
|
|
|
public $slide = true;
|
29
|
|
|
/**
|
30
|
|
|
* @var boolean indicates whether to display the previous and next links.
|
31
|
|
|
*/
|
32
|
|
|
public $displayPrevAndNext = true;
|
33
|
|
|
/**
|
34
|
|
|
* @var array the carousel items configuration.
|
35
|
|
|
*/
|
36
|
|
|
public $items = array();
|
37
|
|
|
/**
|
38
|
|
|
* @var array the options for the Bootstrap Javascript plugin.
|
39
|
|
|
*/
|
40
|
|
|
public $options = array();
|
41
|
|
|
/**
|
42
|
|
|
* @var string[] the Javascript event handlers.
|
43
|
|
|
*/
|
44
|
|
|
public $events = array();
|
45
|
|
|
/**
|
46
|
|
|
* @var array the HTML attributes for the widget container.
|
47
|
|
|
*/
|
48
|
|
|
public $htmlOptions = array();
|
49
|
|
|
|
50
|
|
|
/**
|
51
|
|
|
* Initializes the widget.
|
52
|
|
|
*/
|
53
|
|
|
public function init()
|
54
|
|
|
{
|
55
|
|
|
if (!isset($this->htmlOptions['id']))
|
56
|
|
|
$this->htmlOptions['id'] = $this->getId();
|
57
|
|
|
|
58
|
|
|
$classes = array('carousel');
|
59
|
|
|
|
60
|
|
|
if ($this->slide === true)
|
61
|
|
|
$classes[] = 'slide';
|
62
|
|
|
|
63
|
|
|
if (!empty($classes))
|
64
|
|
|
{
|
65
|
|
|
$classes = implode(' ', $classes);
|
66
|
|
|
if (isset($this->htmlOptions['class']))
|
67
|
|
|
$this->htmlOptions['class'] .= ' '.$classes;
|
68
|
|
|
else
|
69
|
|
|
$this->htmlOptions['class'] = $classes;
|
70
|
|
|
}
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* Runs the widget.
|
75
|
|
|
*/
|
76
|
|
|
public function run()
|
77
|
|
|
{
|
78
|
|
|
$id = $this->htmlOptions['id'];
|
79
|
|
|
|
80
|
|
|
echo CHtml::openTag('div', $this->htmlOptions);
|
81
|
|
|
echo '<div class="carousel-inner">';
|
82
|
|
|
$this->renderItems($this->items);
|
83
|
|
|
echo '</div>';
|
84
|
|
|
|
85
|
|
|
if ($this->displayPrevAndNext)
|
86
|
|
|
{
|
87
|
|
|
echo '<a class="carousel-control left" href="#'.$id.'" data-slide="prev">'.$this->prevLabel.'</a>';
|
88
|
|
|
echo '<a class="carousel-control right" href="#'.$id.'" data-slide="next">'.$this->nextLabel.'</a>';
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
echo '</div>';
|
92
|
|
|
|
93
|
|
|
/** @var CClientScript $cs */
|
94
|
|
|
$cs = Yii::app()->getClientScript();
|
95
|
|
|
$options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
|
96
|
|
|
$cs->registerScript(__CLASS__.'#'.$id, "jQuery('#{$id}').carousel({$options});");
|
97
|
|
|
|
98
|
|
|
foreach ($this->events as $name => $handler)
|
99
|
|
|
{
|
100
|
|
|
$handler = CJavaScript::encode($handler);
|
101
|
|
|
$cs->registerScript(__CLASS__.'#'.$id.'_'.$name, "jQuery('#{$id}').on('{$name}', {$handler});");
|
102
|
|
|
}
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
/**
|
106
|
|
|
* Renders the carousel items.
|
107
|
|
|
* @param array $items the item configuration.
|
108
|
|
|
*/
|
109
|
|
|
protected function renderItems($items)
|
110
|
|
|
{
|
111
|
|
|
foreach ($items as $i => $item)
|
112
|
|
|
{
|
113
|
|
|
if (!is_array($item))
|
114
|
|
|
continue;
|
115
|
|
|
|
116
|
|
|
if (isset($item['visible']) && $item['visible'] === false)
|
117
|
|
|
continue;
|
118
|
|
|
|
119
|
|
|
if (!isset($item['itemOptions']))
|
120
|
|
|
$item['itemOptions'] = array();
|
121
|
|
|
|
122
|
|
|
$classes = array('item');
|
123
|
|
|
|
124
|
|
|
if ($i === 0)
|
125
|
|
|
$classes[] = 'active';
|
126
|
|
|
|
127
|
|
View Code Duplication |
if (!empty($classes))
|
|
|
|
|
128
|
|
|
{
|
129
|
|
|
$classes = implode(' ', $classes);
|
130
|
|
|
if (isset($item['itemOptions']['class']))
|
131
|
|
|
$item['itemOptions']['class'] .= ' '.$classes;
|
132
|
|
|
else
|
133
|
|
|
$item['itemOptions']['class'] = $classes;
|
134
|
|
|
}
|
135
|
|
|
|
136
|
|
|
echo CHtml::openTag('div', $item['itemOptions']);
|
137
|
|
|
|
138
|
|
|
if (isset($item['image']))
|
139
|
|
|
{
|
140
|
|
|
if (!isset($item['alt']))
|
141
|
|
|
$item['alt'] = '';
|
142
|
|
|
|
143
|
|
|
if (!isset($item['imageOptions']))
|
144
|
|
|
$item['imageOptions'] = array();
|
145
|
|
|
|
146
|
|
|
echo CHtml::image($item['image'], $item['alt'], $item['imageOptions']);
|
147
|
|
|
}
|
148
|
|
|
|
149
|
|
|
if (!empty($item['caption']) && (isset($item['label']) || isset($item['caption'])))
|
150
|
|
|
{
|
151
|
|
|
if (!isset($item['captionOptions']))
|
152
|
|
|
$item['captionOptions'] = array();
|
153
|
|
|
|
154
|
|
|
if (isset($item['captionOptions']['class']))
|
155
|
|
|
$item['captionOptions']['class'] .= ' carousel-caption';
|
156
|
|
|
else
|
157
|
|
|
$item['captionOptions']['class'] = 'carousel-caption';
|
158
|
|
|
|
159
|
|
|
echo CHtml::openTag('div', $item['captionOptions']);
|
160
|
|
|
|
161
|
|
|
if (isset($item['label']))
|
162
|
|
|
echo '<h4>'.$item['label'].'</h4>';
|
163
|
|
|
|
164
|
|
|
if (isset($item['caption']))
|
165
|
|
|
echo '<p>'.$item['caption'].'</p>';
|
166
|
|
|
|
167
|
|
|
echo '</div>';
|
168
|
|
|
}
|
169
|
|
|
|
170
|
|
|
echo '</div>';
|
171
|
|
|
}
|
172
|
|
|
}
|
173
|
|
|
}
|
174
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.