1 |
<?php |
2 |
/** |
3 |
* This file contains the Text FormElement class. |
4 |
* |
5 |
* $Id: FERadioGroup.inc,v 1.19.2.1 2005/05/12 01:24:03 hemna Exp $ |
6 |
* |
7 |
* @author Walter A. Boring IV <waboring@newsblob.com> |
8 |
* @author Suren Markosyan <suren@bcsweb.com> |
9 |
* @package phpHtmlLib |
10 |
* @subpackage FormProcessing |
11 |
* |
12 |
* @copyright LGPL - See LICENCE |
13 |
* |
14 |
*/ |
15 |
|
16 |
/** |
17 |
* This is the Radio Button Group FormElement which builds a |
18 |
* List of Radio buttons that can be used in any |
19 |
* style of layout. |
20 |
* |
21 |
* It has no validation method. |
22 |
* |
23 |
* |
24 |
* @author Walter A. Boring IV <waboring@newsblob.com> |
25 |
* @author Suren Markossian <suren@bcsweb.com> |
26 |
* @package phpHtmlLib |
27 |
* @subpackage FormProcessing |
28 |
* |
29 |
* @copyright LGPL - See LICENCE |
30 |
*/ |
31 |
class FERadioGroup extends FormElement { |
32 |
|
33 |
/** |
34 |
* Holds the list of available |
35 |
* data elements |
36 |
* |
37 |
*/ |
38 |
var $_data_list = array(); |
39 |
|
40 |
/** |
41 |
* the br flag |
42 |
*/ |
43 |
var $_br_flag=FALSE; |
44 |
|
45 |
/** |
46 |
* The constructor |
47 |
* |
48 |
* @param label string - text label for the element |
49 |
* @param array - the name => value pairs of the radio |
50 |
* buttons text => value |
51 |
* @param boolean - required flag |
52 |
*/ |
53 |
function FERadioGroup($label, $data_list=array(), $required=true) { |
54 |
$this->FormElement($label, $required); |
55 |
foreach ($data_list as $name=>$value) { |
56 |
$this->_data_list[] = array($name=>$value); |
57 |
} |
58 |
} |
59 |
|
60 |
/** |
61 |
* This provides a method |
62 |
* for the FormContent |
63 |
* to get access to the |
64 |
* text associated with a |
65 |
* field. This is only available |
66 |
* on FormElements that have text |
67 |
* associated with a field. |
68 |
* It is used during Confirmation |
69 |
* |
70 |
* @param mixed - the value to look up |
71 |
* @return string - the text associated |
72 |
*/ |
73 |
function get_value_text() { |
74 |
$value = $this->get_value(); |
75 |
foreach( $this->_data_list as $arr) { |
76 |
$flip = array_flip($arr); |
77 |
if (isset($flip[$value])) { |
78 |
return $flip[$value]; |
79 |
} |
80 |
} |
81 |
return NULL; |
82 |
} |
83 |
|
84 |
|
85 |
/** |
86 |
* This function is used to set the br flag |
87 |
* that will automatically add a BR tag after |
88 |
* each radio button |
89 |
* |
90 |
* @param boolean |
91 |
*/ |
92 |
function set_br_flag($flag=TRUE) { |
93 |
$this->_br_flag = $flag; |
94 |
} |
95 |
|
96 |
|
97 |
/** |
98 |
* This function builds and returns the |
99 |
* form element object |
100 |
* |
101 |
* @param int the index to get |
102 |
* @param boolean the br flag. |
103 |
* NOTE: the class's br flag takes precedence. |
104 |
* @return object |
105 |
*/ |
106 |
function get_element($index=NULL, $br_flag=FALSE) { |
107 |
$container = container(); |
108 |
|
109 |
if ($br_flag != $this->_br_flag) { |
110 |
$br_flag = $this->_br_flag; |
111 |
} |
112 |
|
113 |
if ($index === NULL) { |
114 |
$count = count( $this->_data_list ); |
115 |
for ($x=0;$x<=$count-1;$x++) { |
116 |
if ($br_flag) { |
117 |
$container->add( $this->_get_index_element($x), |
118 |
html_br()); |
119 |
} else { |
120 |
$container->add( $this->_get_index_element($x)); |
121 |
} |
122 |
|
123 |
} |
124 |
} else { |
125 |
$container->add( $this->_get_index_element($index)); |
126 |
} |
127 |
|
128 |
return $container; |
129 |
} |
130 |
|
131 |
|
132 |
/** |
133 |
* This method builds an individual Radio button |
134 |
* with its associated text |
135 |
* |
136 |
* @param int - the index |
137 |
* @return INPUTtag of type radio |
138 |
*/ |
139 |
function _get_index_element($index) { |
140 |
$attributes = $this->_build_element_attributes(); |
141 |
$attributes["type"] = "radio"; |
142 |
|
143 |
list($name, $value) = each($this->_data_list[$index]); |
144 |
$attributes["value"] = $value; |
145 |
|
146 |
$id_name = $this->get_element_name().'_'.$index; |
147 |
$attributes['id'] = $id_name; |
148 |
|
149 |
$attr_onclick = isset($attributes["onclick"])?$attributes["onclick"]:""; |
150 |
|
151 |
if (($value == $this->get_value())) |
152 |
$attributes[] = "checked"; |
153 |
|
154 |
$tag = new INPUTtag($attributes); |
155 |
|
156 |
//now build the href so we can click on it. |
157 |
$attr["class"] ="form_link"; |
158 |
$attr["href"] = "javascript:void(0)"; |
159 |
|
160 |
$js = "e=document.getElementById('".$id_name."'); e.click();"; |
161 |
$attr["onclick"] = $js.$attr_onclick; |
162 |
|
163 |
$href = new Atag($attr, $name); |
164 |
|
165 |
$c = container($tag, $href); |
166 |
$c->set_collapse(); |
167 |
return $c; |
168 |
} |
169 |
} |
170 |
|
171 |
|
172 |
|
173 |
/** |
174 |
* This is the Yes/No Radio Button Group FormElement. |
175 |
* It builds a FERadioGroup with 2 radio buttons labeled |
176 |
* "Yes" and "No". |
177 |
* |
178 |
* It has no validation method. |
179 |
* |
180 |
* |
181 |
* @author Walter A. Boring IV <waboring@newsblob.com> |
182 |
* @author Suren Markossian <suren@bcsweb.com> |
183 |
* @package phpHtmlLib |
184 |
* @subpackage FormProcessing |
185 |
* |
186 |
* @copyright LGPL - See LICENCE |
187 |
*/ |
188 |
class FEYesNoRadioGroup extends FERadioGroup { |
189 |
|
190 |
/** |
191 |
* The constructor |
192 |
* |
193 |
* @param label string - text label for the element |
194 |
* @param bool required - is this a required element |
195 |
* @param string - the value to use for the 'yes' radio |
196 |
* NOTE: default is 'yes' |
197 |
* @param string - the value to use for the 'no' radio |
198 |
* NOTE: default is 'no' |
199 |
*/ |
200 |
function FEYesNoRadioGroup($label, $required = TRUE, |
201 |
$yes_value="yes", $no_value="no") { |
202 |
$this->FormElement($label, $required); |
203 |
$this->_data_list[] = array("Yes" => $yes_value); |
204 |
$this->_data_list[] = array("No" => $no_value); |
205 |
} |
206 |
} |
207 |
|
208 |
|
209 |
/** |
210 |
* This is a special FERadioButtonGroup form element |
211 |
* that allows each of the radio buttons to show/hide |
212 |
* a div on the fly. |
213 |
* |
214 |
* @author Walter A. Boring IV <waboring@newsblob.com> |
215 |
* @author Suren Markossian <suren@bcsweb.com> |
216 |
* @package phpHtmlLib |
217 |
* @subpackage FormProcessing |
218 |
* |
219 |
* @copyright LGPL - See LICENCE |
220 |
*/ |
221 |
class FEActiveDIVRadioButtonGroup extends FERadioGroup { |
222 |
|
223 |
|
224 |
function javascript() { |
225 |
$name = $this->get_element_name(); |
226 |
|
227 |
$js = 'function '.$this->_form_name.'_'.$name."_activate(id) {\n"; |
228 |
$js .= " var e,o;\n"; |
229 |
$js .= " e = document.getElementById(id);\n"; |
230 |
|
231 |
foreach ($this->_data_list as $button) { |
232 |
list($name, $value) = each( $button ); |
233 |
$js .= " o = document.getElementById('".$this->_id_name($name)."');\n"; |
234 |
$js .= " o.style.display = 'none';\n"; |
235 |
} |
236 |
|
237 |
$js .= " e.style.display = 'block';\n}\n\n"; |
238 |
|
239 |
return $js; |
240 |
} |
241 |
|
242 |
|
243 |
/** |
244 |
* This method builds an individual Radio button |
245 |
* with its associated text |
246 |
* |
247 |
* @param int - the index |
248 |
* @return INPUTtag of type radio |
249 |
*/ |
250 |
function _get_index_element($index) { |
251 |
$element_name = $this->get_element_name(); |
252 |
|
253 |
$attributes = $this->_build_element_attributes(); |
254 |
$attributes["type"] = "radio"; |
255 |
|
256 |
list($name, $value) = each($this->_data_list[$index]); |
257 |
$attributes["value"] = $value; |
258 |
|
259 |
$id_name = $this->get_element_name().'_'.$index; |
260 |
$attributes['id'] = $id_name; |
261 |
|
262 |
$attributes['onclick'] = 'javascript:'.$this->_form_name.'_'.$element_name. |
263 |
"_activate('".$this->_id_name($name)."');"; |
264 |
|
265 |
if (($value == $this->get_value())) |
266 |
$attributes['checked'] = "checked"; |
267 |
|
268 |
$tag = new INPUTtag($attributes); |
269 |
|
270 |
//now build the href so we can click on it. |
271 |
$attr["class"] ="form_link"; |
272 |
$attr["href"] = "javascript:void(0)"; |
273 |
$js = "javascript: function check(item){item.click();} ". |
274 |
"check(".$element_name.".item($index));"; |
275 |
|
276 |
$js = "e=document.getElementById('".$id_name."'); e.click();"; |
277 |
$attr["onclick"] = $js; |
278 |
|
279 |
$href = new Atag($attr, $name); |
280 |
|
281 |
$c = container($tag, $href); |
282 |
$c->set_collapse(); |
283 |
return $c; |
284 |
} |
285 |
|
286 |
|
287 |
/** |
288 |
* This builds the magic div |
289 |
* |
290 |
* @param int the index |
291 |
* @return DIVtag object |
292 |
*/ |
293 |
function build_div($index) { |
294 |
//which one is selected? |
295 |
|
296 |
reset($this->_data_list[$index]); |
297 |
list($name, $value) = each($this->_data_list[$index]); |
298 |
|
299 |
$style = 'position:absolute;left:0px;top:0px;'; |
300 |
|
301 |
if ($value == $this->get_value()) { |
302 |
//ok this item is selected! |
303 |
//it needs to be visible first. |
304 |
$style .= 'visibility:visible;'; |
305 |
} else { |
306 |
$style .= 'visibility:hidden;'; |
307 |
} |
308 |
|
309 |
return new DIVtag(array('id' => $this->_id_name($name), |
310 |
'style' => $style)); |
311 |
} |
312 |
|
313 |
function _id_name($name) { |
314 |
return $this->_form_name.'_'.$this->get_element_name(). |
315 |
'_'.$this->_sanitize_string($name); |
316 |
} |
317 |
|
318 |
} |
319 |
?> |