6bdc5b28 by mpenner

Implement a simple form linked dropdown method that allows adding extra attribut…

…es to the input field. refs #1555
1 parent 310d25ff
......@@ -136,7 +136,7 @@ function validate_creditcard($cc_num, $type) {
if ( ! function_exists('form_dropdown'))
{
function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '', $blankFirstOption = true)
{
if ( ! is_array($selected))
{
......@@ -157,7 +157,10 @@ if ( ! function_exists('form_dropdown'))
$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
$form = '<select name="'.$name.'"'.$extra.$multiple.">\n<option value=''>Select...</option>";
$form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
if ($blankFirstOption) {
$form .= '<option value="">Select...</option>';
}
foreach ($options as $key => $val)
{
......@@ -279,6 +282,103 @@ function form_linked_dropdown($name = '', $parent = '', $options = array(), $par
return $form;
}
if (!function_exists('formLinkedDropdownWithExtra')) {
function formLinkedDropdownWithExtra($name = '', $parent = '', $options = array(), $parent_selected = '', $selected = '', $extra = '', $provide_js = true, $blankFirstOption = true, $inputExtra = '')
{
if ($extra != '') {
$extra = ' '.$extra;
}
if ($inputExtra != '') {
$inputExtra = ' '.str_ireplace('"', '\"', $inputExtra);
}
$form = '<select name="'.$name.'" id="'.$name.'"'.$extra.">\n";
if (isset($options[$parent_selected])) {
foreach ($options[$parent_selected] as $key => $val) {
$sel = ($selected != $key) ? '' : ' selected="selected"';
$form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
}
}
if ($provide_js) {
$form .= '</select>
<script>
function update_'.$name.'(value) {
if (value == "") {
return;
}
jQuery("#'.$name.'_custom").attr("disabled", "disabled").hide();
jQuery("#'.$name.'").show();
var options = new Array();
var select = document.getElementById("'.$name.'");
select.disabled = false;
while (select.firstChild)
select.removeChild(select.firstChild);
switch (value.toString()) {';
foreach ($options as $k => $v) {
$form .= 'case "'.$k.'":
options = new Array(';
foreach ($v as $value => $content) {
$form .= '"'.$value.'","'.$content.'",';
}
$form = substr($form, 0, -1);
$form .= ');
break;';
}
$form .= 'default:
select.disabled = true;
jQuery("#'.$name.'_custom").removeAttr("disabled").show();
jQuery("#'.$name.'").hide();
return;
}';
if ($blankFirstOption) {
$form .= '
var option = document.createElement("option");
var val = "";
option.val = val;
option.innerHTML = "Select...";
select.appendChild(option);';
}
$form .= 'for(var i = 0; i < options.length;) {
var option = document.createElement("option");
var val = options[i++];
option.value = val;
option.innerHTML = options[i++];
if (val == "'.$selected.'") {
option.selected = true;
}
select.appendChild(option);
}
}
var elem = $("<input type=\'text\' name=\''.$name.'\' id=\''.$name.'_custom\' class=\'input-field-css input-large\' value=\''.$selected.'\' '.$inputExtra.'/>").hide();
elem.insertAfter(jQuery("#'.$name.'"));
update_'.$name.'(document.getElementById("'.$parent.'").value);
document.getElementById("'.$parent.'").onchange = function() { update_'.$name.'(this.value); }
</script>
';
} else {
$form = '';
if (array_key_exists($parent_selected, $options)) {
foreach ($options[$parent_selected] as $name => $option) {
$sel = ($selected == $name) ? ' selected="selected"' : '';
$form .= '<option value="'.$name.'"'.$sel.'>'.$option."</option>\n";
}
}
}
return $form;
}
}
if ( ! function_exists('url_title'))
{
function url_title($str, $separator = 'dash', $lowercase = FALSE)
......