(mixed) custom id attribute value * if numeric value is passed it'll be just appended to the name e.g. {filed-name}-{value} * if false is passed id will be not created * if empty string is passed (or no 'id' option is found) * in all other cases the value will be used as field id * default: empty string * * - class => (string) field class(es) * Example: 'tbox select class1 class2 class3' * NOTE: this will override core classes, so you have to explicit include them! * default: empty string * * - size => (int) size attribute value (used when needed) * default: 40 * * - title (string) title attribute * default: empty string (omitted) * * - readonly => (bool) readonly attribute * default: false * * - selected => (bool) selected attribute (used when needed) * default: false * * checked => (bool) checked attribute (used when needed) * default: false * - disabled => (bool) disabled attribute * default: false * * - tabindex => (int) tabindex attribute value * default: inner tabindex counter * * - other => (string) additional data * Example: 'attribute1="value1" attribute2="value2"' * default: empty string */ class e_form { var $_tabindex_counter = 0; var $_tabindex_enabled = true; var $_cached_attributes = array(); function e_form($enable_tabindex = true) { $this->_tabindex_enabled = $enable_tabindex; } function text($name, $value, $maxlength = 200, $options = array()) { $options = $this->format_options('text', $name, $options); return "get_attributes($options, $name, $value)." />"; } function file($name, $options = array()) { $options = $this->format_options('text', $name, $options); return "get_attributes($options, $name, $value)." />"; } function password($name, $maxlength = 50, $options = array()) { $options = $this->format_options('text', $name, $options); return "get_attributes($options, $name, $value)." />"; } function textarea($name, $value, $rows = 15, $cols = 40, $options = array()) { $options = $this->format_options('textarea', $name, $options); return ""; } function checkbox($name, $value, $checked = false, $options = array()) { $options['checked'] = $checked; //comes as separate argument just for convenience $options = $this->format_options('checkbox', $name, $options); return "get_attributes($options, $name, $value)." />"; } function radio($name, $value, $checked = false, $options = array()) { $options['checked'] = $checked; //comes as separate argument just for convenience $options = $this->format_options('radio', $name, $options); return "get_attributes($options, $name, $value)." />"; } function label($text, $name = '', $value = '') { $for_id = $this->_format_id('', $name, $value, 'for'); return ""; } function select_open($name, $options = array()) { $options = $this->format_options('select', $name, $options); return ""; } function hidden($name, $value, $options = array()) { $options = $this->format_options('hidden', $name, $options); return "get_attributes($options, $name, $value)." />"; } function submit($name, $value, $options = array()) { $options = $this->format_options('submit', $name, $options); return "get_attributes($options, $name, $value)." />"; } function submit_image($name, $value, $image, $options = array()) { $options = $this->format_options('submit', $name, $options); return "get_attributes($options, $name, $value)." />"; } function admin_button($name, $value, $action = 'submit', $label = '', $options = array()) { $options['class'] = $action; //additional classes in options not allowed $btype = 'submit'; if($action == 'action') $btype = 'button'; $options = $this->format_options('admin_button', $name, $options); if(empty($label)) $label = $value; return " "; } function get_attributes($options, $name = '', $value = '') { $ret = ''; // foreach ($options as $option => $optval) { switch ($option) { case 'id': $ret .= $this->_format_id($optval, $name, $value); break; case 'class': if(!empty($optval)) $ret .= " class='{$optval}'"; break; case 'size': if($optval) $ret .= " size='{$optval}'"; break; case 'title': if($optval) $ret .= " title='{$optval}'"; break; case 'tabindex': if($optval === false || !$this->_tabindex_enabled) break; $this->_tabindex_counter++; $ret .= " tabindex='".($optval ? $optval : $this->_tabindex_counter)."'"; break; case 'readonly': if($optval) $ret .= " readonly='readonly'"; break; case 'selected': if($optval) $ret .= " selected='selected'"; break; case 'checked': if($optval) $ret .= " checked='checked'"; break; case 'disabled': if($optval) $ret .= " disabled='disabled'"; break; case 'other': if($optval) $ret .= " $optval"; break; } } return $ret; } /** * Auto-build field attribute id * * @param string $id_value value for attribute id passed with the option array * @param string $name the name attribute passed to that field * @param unknown_type $value the value attribute passed to that field * @return unknown */ function _format_id($id_value, $name, $value = '', $return_attribute = 'id') { if($id_value === false) return ''; //format the name first $name = str_replace(array('[]', '[', ']', '_'), array('', '-', '', '-'), $name); if(empty($id_value) ) return " {$return_attribute}='{$name}".($value ? "-{$value}" : '')."'";// also useful when name is e.g. name='my_name[some_id]' elseif(is_numeric($id_value) && $name) return " {$return_attribute}='{$name}-{$id_value}'";// also useful when name is e.g. name='my_name[]' else return " {$return_attribute}='{$id_value}'"; } /** * Format options based on the field type, * merge with default * * @param string $type * @param string $name form name attribute value * @param array|string $user_options * @return array merged options */ function format_options($type, $name, $user_options) { if(is_string($user_options)) parse_str($user_options, $user_options); $def_options = $this->_default_options($type); foreach (array_keys($user_options) as $key) { if(!isset($def_options[$key])) unset($user_options[$key]); } $user_options['name'] = $name; //required for some of the automated tasks return array_merge($def_options, $user_options); } /** * Get default options array based on the filed type * * @param string $type * @return array default options */ function _default_options($type) { if(isset($this->_cached_attributes[$type])) return $this->_cached_attributes[$type]; $def_options = array( 'id' => '', 'class' => '', 'title' => '', 'size' => '', 'readonly' => false, 'selected' => false, 'checked' => false, 'disabled' => false, 'tabindex' => 0, 'other' => '' ); switch ($type) { case 'hidden': $def_options = array('disabled' => false, 'other' => ''); break; case 'text': $def_options['class'] = 'tbox input-text'; unset($def_options['selected'], $def_options['checked']); break; case 'textarea': $def_options['class'] = 'tbox textarea'; unset($def_options['selected'], $def_options['checked'], $def_options['size']); break; case 'select': $def_options['class'] = 'tbox select'; unset($def_options['checked'], $def_options['checked']); break; case 'option': $def_options = array('class' => '', 'selected' => false, 'other' => ''); break; case 'radio': $def_options['class'] = 'radio'; unset($def_options['size'], $def_options['selected']); break; case 'checkbox': $def_options['class'] = 'checkbox'; unset($def_options['size'], $def_options['selected']); break; case 'submit': $def_options['class'] = 'button'; unset($def_options['checked'], $def_options['selected'], $def_options['readonly']); break; case 'admin_button': unset($def_options['checked'], $def_options['selected'], $def_options['readonly']); break; } $this->_cached_attributes[$type] = $def_options; return $def_options; } } class form { function form_open($form_method, $form_action, $form_name = "", $form_target = "", $form_enctype = "", $form_js = "") { $method = ($form_method ? "method='".$form_method."'" : ""); $target = ($form_target ? " target='".$form_target."'" : ""); $name = ($form_name ? " id='".$form_name."' " : " id='myform'"); return "\n
"; } } /* Usage echo $rs->form_open("post", e_SELF, "_blank"); echo $rs->form_text("testname", 100, "this is the value", 100, 0, "tooltip"); echo $rs->form_button("submit", "testsubmit", "SUBMIT!", "", "Click to submit"); echo $rs->form_button("reset", "testreset", "RESET!", "", "Click to reset"); echo $rs->form_textarea("textareaname", 10, 10, "Value", "overflow:hidden"); echo $rs->form_checkbox("testcheckbox", 1, 1); echo $rs->form_checkbox("testcheckbox2", 2); echo $rs->form_hidden("hiddenname", "hiddenvalue"); echo $rs->form_radio("testcheckbox", 1, 1); echo $rs->form_radio("testcheckbox", 1); echo $rs->form_file("testfile", "20"); echo $rs->form_select_open("testselect"); echo $rs->form_option("Option 1"); echo $rs->form_option("Option 2"); echo $rs->form_option("Option 3", 1, "defaultvalue"); echo $rs->form_option("Option 4"); echo $rs->form_select_close(); echo $rs->form_close(); */ ?>