1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Brickrouge;
13
14 15 16
17 class extends Element
18 {
19 public function __construct(array $attributes=array())
20 {
21 parent::__construct('ul', $attributes);
22 }
23
24 protected function render_inner_html()
25 {
26 $html = '';
27 $options = $this[self::OPTIONS];
28 $value = $this['value'];
29
30 if ($value === null)
31 {
32 $value = $this[self::DEFAULT_VALUE];
33 }
34
35 foreach ($options as $key => $option)
36 {
37 if ($option === false)
38 {
39 $html .= '<li class="divider"></li>';
40
41 continue;
42 }
43 else if ($option === null)
44 {
45 continue;
46 }
47
48 $html .= '<li' . ((string) $key === (string) $value ? ' class="active"' : '') . '>';
49
50 if ($option instanceof Element)
51 {
52 $html .= $option;
53 }
54 else
55 {
56 $html .= '<a href="' . escape($key) . '" data-key="' . escape($key) . '">' . (is_string($option) ? escape($option) : $option) . '</a>';
57 }
58
59 $html .= '</li>';
60 }
61
62 return $html;
63 }
64
65 protected function render_class(array $class_names)
66 {
67 return parent::render_class($class_names + array('dropdown-menu' => true));
68 }
69 }
70
71 72 73
74 class SplitButton extends Element
75 {
76 public function __construct($label, array $attributes=array())
77 {
78 if (is_string($label))
79 {
80 $label = escape(t($label, array(), array('scope' => 'button')));
81 }
82
83 parent::__construct
84 (
85 'div', $attributes + array
86 (
87 self::INNER_HTML => $label
88 )
89 );
90 }
91
92 93 94 95 96 97 98 99
100 protected function render_inner_html()
101 {
102 $label = parent::render_inner_html();
103
104 $class_names = array_intersect_key
105 (
106 array
107 (
108 'btn-primary' => true,
109 'btn-danger' => true,
110 'btn-success' => true,
111 'btn-info' => true
112 ),
113
114 $this->class_names
115 );
116
117 $class = implode(' ', array_keys(array_filter($class_names)));
118
119 return $this->render_splitbutton_label($label, $class)
120 . $this->render_splitbutton_toggle($class)
121 . $this->resolve_options($this[self::OPTIONS]);
122 }
123
124 125 126 127 128 129 130 131 132
133 protected function render_splitbutton_label($label, $class)
134 {
135 return <<<EOT
136 <a href="javascript:void()" class="btn $class">$label</a>
137 EOT;
138 }
139
140 141 142 143 144 145 146
147 protected function render_splitbutton_toggle($class)
148 {
149 return <<<EOT
150 <a href="javascript:void()" class="btn dropdown-toggle $class" data-toggle="dropdown"><span class="caret"></span></a>
151 EOT;
152 }
153
154 155 156 157 158 159
160 protected function render_class(array $class_names)
161 {
162 return parent::render_class
163 (
164 array
165 (
166 'btn-primary' => false,
167 'btn-danger' => false,
168 'btn-success' => false,
169 'btn-info' => false
170 )
171
172 + $class_names + array('btn-group' => true)
173 );
174 }
175
176 177 178 179 180 181 182 183 184 185
186 protected function resolve_options($options)
187 {
188 if (is_array($options))
189 {
190 $options = new DropdownMenu(array(Element::OPTIONS => $options, 'value' => $this['value'] ?: $this[self::DEFAULT_VALUE]));
191 }
192
193 if (!($options instanceof DropdownMenu))
194 {
195 throw new \UnexpectedValueException(format('OPTIONS should be either an array or a Brickrouge\DropDownMenu instance, %type given.', array('type' => gettype($options))));
196 }
197
198 return $options;
199 }
200 }