/** @param $name @param $lookup_results This can be a MySQL $results reference or an PHP associative array. Even if you index your array numerically, the 'value' and index still get set inside this function. @param $display_mask This mask can be anything. Any %??????% will be interpreted as a database column name and will be selected from there as appropriate. @param $lookup_results Database results mysql reference @param $size Size of SELECT object. Specify a size of 'NA' to stop the <select ...> and </select> tags being generated. This will allow things like empty options to be included, while still taking advantage of the generation of database/array options. @param $value Checked Ind value. If the value param is found in the current 'value' column, that record will be SELECTED. @param $value_column Specifies the value column, or 'value' as a default. @param $checked_ind Specifies the checked_ind column to select a default record, or '' as default, which will mean the first record will be selected by the browser when building the generated select. @param $include_ind_func If defined, will call the function with the current lookup array as argument. If the function returns TRUE, the record will be included, otherwise it will be skipped. However the $lookup_results DO have to include a 'value' column for this process to work, as this will be used for the value of each select option. You can however set the $value_column to a value indicating a column name to be used instead of 'value' */ function custom_select($name, $lookup_results, $display_mask, $size = 1, $value = NULL, $value_column = 'value', $include_ind_func = NULL, $checked_ind = '', $onchange_event = '', $disabled = FALSE, $id = NULL) { // allows function to be called with an array of args, instead of individual arguments. if (is_array($name)) { extract($name); } if ($size !== 'NA') { if (is_numeric($size) && $size > 1) { $var = "\n<select " . ($id != NULL ? "id=\"{$id}\"" : "") . " name=\"" . $name . "[]\" size=\"{$size}\" onchange=\"{$onchange_event}\"" . ($disabled ? ' DISABLED' : '') . " MULTIPLE>"; } else { $var = "\n<select " . ($id != NULL ? "id=\"{$id}\"" : "") . " name=\"{$name}\" onchange=\"{$onchange_event}\"" . ($disabled ? ' DISABLED' : '') . ">"; } } else { $var = ''; } $lookup_results = fetch_results_array($lookup_results); reset($lookup_results); $empty_display_mask = expand_display_mask($display_mask, NULL, '%'); $value_found = FALSE; while ($lookup_r = each($lookup_results)) { // Check if this record should be included in list of values. if (!function_exists($include_ind_func) || $include_ind_func($lookup_r)) { $lookup_value = get_array_variable_value($lookup_r, $value_column); $display = expand_display_mask($display_mask, $lookup_r, '%'); // if all variables were replaced with nothing, then assume empty option if (strlen(strval($lookup_value)) == 0 && $display == $empty_display_mask) { $display = ''; } if (is_array($value)) { if (in_array($lookup_value, $value) !== FALSE) { $var .= "\n<option value=\"" . $lookup_value . "\" SELECTED>{$display}"; } else { $var .= "\n<option value=\"" . $lookup_value . "\">{$display}"; } } else { if (!$value_found && $value == NULL && $lookup_r[$checked_ind] == 'Y') { $var .= "\n<option value=\"" . $lookup_value . "\" SELECTED>{$display}"; } else { if (strcasecmp(trim($value), strval($lookup_value)) === 0) { $value_found = TRUE; $var .= "\n<option value=\"" . $lookup_value . "\" SELECTED>{$display}"; } else { $var .= "\n<option value=\"" . $lookup_value . "\">{$display}"; } } } } } if ($size !== 'NA') { $var .= "\n</select>"; } return $var; }
/** This is a simple mask processor (Used by widgets.php::custom_select(...)). It is not as advanced as the parse_title_mask functionality, because it does not support mask functions (if, ifdef, elsedef), or the special mask options '.img', etc. @param $display_mask The display mask with variables delimited by $variable_char. The variable_name must exist as a keyname in $values_r. @param $values_r @param $variable_char */ function expand_display_mask($display_mask, $values_r, $variable_char = "%") { $i = 0; $inside_variable = FALSE; $variable = ""; $value = $display_mask; for ($i = 0; $i < strlen($display_mask); $i++) { if ($inside_variable) { // If closing bracket if ($display_mask[$i] == $variable_char && ($i == 0 || $display_mask[$i - 1] != '\\')) { // Indicate close of reference. $inside_variable = FALSE; if (strlen($variable) > 0) { $replace = get_array_variable_value($values_r, $variable); $value = str_replace($variable_char . $variable . $variable_char, $replace, $value); $variable = ''; } } else { $variable .= $display_mask[$i]; } } else { if ($display_mask[$i] == $variable_char && ($i == 0 || $display_mask[$i - 1] != '\\')) { $inside_variable = TRUE; } } } if ($value != NULL) { return trim($value); } else { return NULL; } }