1 <?php
2 3 4 5 6 7 8 9
10
11 Yii::import('bootstrap.widgets.TbCollapse');
12
13 14 15
16 class TbNavbar extends CWidget
17 {
18
19 const TYPE_INVERSE = 'inverse';
20
21
22 const FIXED_TOP = 'top';
23 const FIXED_BOTTOM = 'bottom';
24
25 26 27 28
29 public $type;
30 31 32
33 public $brand;
34 35 36
37 public $brandUrl;
38 39 40
41 public $brandOptions = array();
42 43 44 45 46 47
48 public $fixed = self::FIXED_TOP;
49 50 51 52
53 public $fluid = false;
54 55 56
57 public $collapse = false;
58 59 60 61
62 public $items = array();
63 64 65
66 public $htmlOptions = array();
67
68 69 70
71 public function init()
72 {
73 if ($this->brand !== false)
74 {
75 if (!isset($this->brand))
76 $this->brand = CHtml::encode(Yii::app()->name);
77
78 if (!isset($this->brandUrl))
79 $this->brandUrl = Yii::app()->homeUrl;
80
81 $this->brandOptions['href'] = CHtml::normalizeUrl($this->brandUrl);
82
83 if (isset($this->brandOptions['class']))
84 $this->brandOptions['class'] .= ' brand';
85 else
86 $this->brandOptions['class'] = 'brand';
87 }
88
89 $classes = array('navbar');
90
91 if (isset($this->type) && in_array($this->type, array(self::TYPE_INVERSE)))
92 $classes[] = 'navbar-'.$this->type;
93
94 if ($this->fixed !== false && in_array($this->fixed, array(self::FIXED_TOP, self::FIXED_BOTTOM)))
95 $classes[] = 'navbar-fixed-'.$this->fixed;
96
97 if (!empty($classes))
98 {
99 $classes = implode(' ', $classes);
100 if (isset($this->htmlOptions['class']))
101 $this->htmlOptions['class'] .= ' '.$classes;
102 else
103 $this->htmlOptions['class'] = $classes;
104 }
105 }
106
107 108 109
110 public function run()
111 {
112 echo CHtml::openTag('div', $this->htmlOptions);
113 echo '<div class="navbar-inner"><div class="'.$this->getContainerCssClass().'">';
114
115 $collapseId = TbCollapse::getNextContainerId();
116
117 if ($this->collapse !== false)
118 {
119 echo '<a class="btn btn-navbar" data-toggle="collapse" data-target="#'.$collapseId.'">';
120 echo '<span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>';
121 echo '</a>';
122 }
123
124 if ($this->brand !== false)
125 echo CHtml::openTag('a', $this->brandOptions).$this->brand.'</a>';
126
127 if ($this->collapse !== false)
128 {
129 $this->controller->beginWidget('bootstrap.widgets.TbCollapse', array(
130 'id'=>$collapseId,
131 'toggle'=>false,
132 'htmlOptions'=>array('class'=>'nav-collapse'),
133 ));
134 }
135
136 foreach ($this->items as $item)
137 {
138 if (is_string($item))
139 echo $item;
140 else
141 {
142 if (isset($item['class']))
143 {
144 $className = $item['class'];
145 unset($item['class']);
146
147 $this->controller->widget($className, $item);
148 }
149 }
150 }
151
152 if ($this->collapse !== false)
153 $this->controller->endWidget();
154
155 echo '</div></div></div>';
156 }
157
158 159 160 161
162 protected function getContainerCssClass()
163 {
164 return $this->fluid ? 'container-fluid' : 'container';
165 }
166 }
167