/** * Returns the xhtml equivalent of options tags. This is basically a nice little wrapper * to make sure that id's are placed in names and also that it's xhtml compliant.\n * Parameters: * - 'items' - An associative array of key/values to represent the value and text of the items in the list. This can also be * passed a string in the form of 'key,value,key,value'. Defaults to array(). * - 'selected_value' - A string that will set the matching item (by value) as selected. Defaults = ''. * - 'selected_index' - An integer that will set the matching item (by index) as selected. Defaults to -1 (no selection). * - 'selected_values' - An array of strings that will set the matching item as selected. This is for multiple select items. * - 'extra' - Text to append to the <input>-statement, ex. for javascript-validation code. Defaults to ''. * - 'flip_items' - Boolean that tells whether or not the value and text of the given items should be swapped. Defaults to false. * * @param array An array of parameters to pass to the method. Unrecognized parameters will be added as attributes to the * tag and merged correctly with anything in the 'params' key if passed. * @param boolean Test whether keys are all valid or not. Not helpful if you're * passing extra key/values along, but good for debugging. * @return string * @author Ted Kulp */ public function create_input_options($params = array(), $check_keys = false) { $default_params = array('items' => coalesce_key($params, 'items', array()), 'selected_value' => coalesce_key($params, 'selected_value', '', FILTER_SANITIZE_STRING), 'selected_index' => coalesce_key($params, 'selected_index', -1, FILTER_SANITIZE_NUMBER_INT), 'selected_values' => coalesce_key($params, 'selected_value', array()), 'flip_items' => coalesce_key($params, 'flip_items', false, FILTER_VALIDATE_BOOLEAN), 'params' => coalesce_key($params, 'params', array())); if ($check_keys && !are_all_keys_valid($params, $default_params)) { throw new SilkInvalidKeyException(invalid_key($params, $default_params)); } //Combine EVERYTHING together into a big managerie $params = array_merge($default_params, forms()->strip_extra_params($params, $default_params, 'params')); unset($params['params']); $selected_index = $params['selected_index']; unset($params['selected_index']); $selected_value = $params['selected_value']; unset($params['selected_value']); $selected_values = $params['selected_values']; unset($params['selected_values']); $items = $params['items']; unset($params['items']); if (!is_array($items) && strlen($items) > 0) { $ary = array_chunk(explode(',', $items), 2); $items = array(); foreach ($ary as $one_item) { if (count($one_item) == 2) { $items[$one_item[0]] = $one_item[1]; } } } if ($params['flip_items']) { $items = array_flip($items); } unset($params['flip_items']); $text = ''; $count = 0; foreach ($items as $k => $v) { $hash = array('value' => $k); if ($count == $selected_index || $k == $selected_value || in_array($k, $selected_values)) { $hash['selected'] = 'selected'; } $text .= forms()->create_start_tag('option', $hash) . $v . forms()->create_end_tag('option'); $count++; } return $text; }
/** * Check to see if all keys in the given hash exist * in the check array. If there are any extra, it will * return false. * * @param array The hash to check * @param array The hash to test against * @return boolean Returns false if there are extra keys * @author Ted Kulp **/ function are_all_keys_valid($array, $valid_keys) { return invalid_key($array, $valid_keys) == null; }