1 <?php
2 3 4 5 6 7
8 namespace foundation\helpers;
9
10 use foundation\enum\Enum;
11 use foundation\exception\InvalidConfigException;
12
13 14 15 16 17 18
19 class Nav
20 {
21
22 23 24 25 26 27 28 29 30 31 32
33 public static function side($items, $htmlOptions = array())
34 {
35 ArrayHelper::addValue('class', Enum::NAV_SIDE, $htmlOptions);
36 ob_start();
37 echo \CHtml::openTag('ul', $htmlOptions);
38 foreach ($items as $item) {
39
40 if (is_string($item))
41 echo \CHtml::tag('li', array('class' => Enum::NAV_DIVIDER));
42 else {
43 $itemOptions = ArrayHelper::getValue($item, 'itemOptions', array());
44 $linkOptions = ArrayHelper::getValue($item, 'linkOptions', array());
45 $label = ArrayHelper::getValue($item, 'label', 'Item');
46 $url = ArrayHelper::getValue($item, 'url', '#');
47
48 echo \CHtml::openTag('li', $itemOptions);
49 echo \CHtml::link($label, $url, $linkOptions);
50 echo \CHtml::closeTag('li');
51 }
52 }
53 echo \CHtml::closeTag('ul');
54
55 return ob_get_clean();
56 }
57
58 59 60 61 62 63 64 65 66 67 68
69 public static function sub($items, $htmlOptions = array())
70 {
71 ArrayHelper::addValue('class', Enum::NAV_SUB, $htmlOptions);
72 ob_start();
73 echo \CHtml::openTag('dl', $htmlOptions);
74 foreach ($items as $item) {
75 $itemOptions = ArrayHelper::getValue($item, 'itemOptions', array());
76 $linkOptions = ArrayHelper::getValue($item, 'linkOptions', array());
77 $label = ArrayHelper::getValue($item, 'label', 'Item');
78 $url = ArrayHelper::getValue($item, 'url', '#');
79 echo \CHtml::openTag('dt', $itemOptions);
80 echo \CHtml::link($label, $url, $linkOptions);
81 echo \CHtml::closeTag('dt');
82 }
83 echo \CHtml::closeTag('dl');
84
85 return ob_get_clean();
86 }
87
88 89 90 91 92 93 94
95 public static function dropdown($items, $encodeLabels = true)
96 {
97 $li = array();
98 foreach ($items as $item) {
99 if (is_string($item)) {
100 $li[] = $item;
101 continue;
102 }
103 if (!isset($item['label'])) {
104 throw new InvalidConfigException("The 'label' option is required.");
105 }
106 $label = $encodeLabels ? \CHtml::encode($item['label']) : $item['label'];
107 $options = ArrayHelper::getValue($item, 'options', array());
108 $items = ArrayHelper::getValue($item, 'items');
109 $url = \CHtml::normalizeUrl(ArrayHelper::getValue($item, 'url', '#'));
110 $linkOptions = ArrayHelper::getValue($item, 'linkOptions', array());
111
112 if (ArrayHelper::getValue($item, 'active')) {
113 ArrayHelper::addValue('class', 'active', $options);
114 }
115
116 if ($items !== null) {
117 ArrayHelper::addValue('class', 'has-dropdown', $options);
118 if (is_array($items)) {
119 $items = static::dropdown($items, $encodeLabels);
120 }
121 }
122 $li[] = \CHtml::tag('li', $options, \CHtml::link($label, $url, $linkOptions) . $items);
123 }
124
125 return \CHtml::tag('ul', array('class' => 'dropdown'), implode("\n", $li));
126 }
127 }