1 <?php
2 3 4 5 6 7 8 9
10
11 12 13 14
15 class TbCarousel extends CWidget
16 {
17 18 19
20 public $prevLabel = '‹';
21 22 23
24 public $nextLabel = '›';
25 26 27
28 public $slide = true;
29 30 31
32 public $displayPrevAndNext = true;
33 34 35
36 public $items = array();
37 38 39
40 public $options = array();
41 42 43
44 public $events = array();
45 46 47
48 public $htmlOptions = array();
49
50 51 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 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
84 if ($this->displayPrevAndNext)
85 {
86 echo '</div>';
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 echo '</div>';
90 }
91
92
93 $cs = Yii::app()->getClientScript();
94 $options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
95 $cs->registerScript(__CLASS__.'#'.$id, "jQuery('#{$id}').carousel({$options});");
96
97 foreach ($this->events as $name => $handler)
98 {
99 $handler = CJavaScript::encode($handler);
100 $cs->registerScript(__CLASS__.'#'.$id.'_'.$name, "jQuery('#{$id}').on('{$name}', {$handler});");
101 }
102 }
103
104 105 106 107
108 protected function renderItems($items)
109 {
110 foreach ($items as $i => $item)
111 {
112 if (!is_array($item))
113 continue;
114
115 if (isset($item['visible']) && $item['visible'] === false)
116 continue;
117
118 if (!isset($item['itemOptions']))
119 $item['itemOptions'] = array();
120
121 $classes = array('item');
122
123 if ($i === 0)
124 $classes[] = 'active';
125
126 if (!empty($classes))
127 {
128 $classes = implode(' ', $classes);
129 if (isset($item['itemOptions']['class']))
130 $item['itemOptions']['class'] .= ' '.$classes;
131 else
132 $item['itemOptions']['class'] = $classes;
133 }
134
135 echo CHtml::openTag('div', $item['itemOptions']);
136
137 if (isset($item['image']))
138 {
139 if (!isset($item['alt']))
140 $item['alt'] = '';
141
142 if (!isset($item['imageOptions']))
143 $item['imageOptions'] = array();
144
145 echo CHtml::image($item['image'], $item['alt'], $item['imageOptions']);
146 }
147
148 if (!empty($item['caption']) && (isset($item['label']) || isset($item['caption'])))
149 {
150 if (!isset($item['captionOptions']))
151 $item['captionOptions'] = array();
152
153 if (isset($item['captionOptions']['class']))
154 $item['captionOptions']['class'] .= ' carousel-caption';
155 else
156 $item['captionOptions']['class'] = 'carousel-caption';
157
158 echo CHtml::openTag('div', $item['captionOptions']);
159
160 if (isset($item['label']))
161 echo '<h4>'.$item['label'].'</h4>';
162
163 if (isset($item['caption']))
164 echo '<p>'.$item['caption'].'</p>';
165
166 echo '</div>';
167 }
168
169 echo '</div>';
170 }
171 }
172 }
173