PHP Classes

File: form_template.inc

Recommend this page to a friend!
  Classes of Herman Veluwenkamp   form_template   form_template.inc   Download  
File: form_template.inc
Role: ???
Content type: text/plain
Description: Form_template class file.
Class: form_template
Form generation and validation class.
Author: By
Last change:
Date: 24 years ago
Size: 11,963 bytes
 

Contents

Class file image Download
<? class form_template { var $conf_file_name = ''; var $conf_file_text = ''; var $data = ''; var $parameter = array('validation' => ''); var $field = ''; var $field_delimiter = ':'; //delimiter used in config file to seperate field parameters. var $attribute_delimiter = ','; //delimiter used in field definition to seperate attributes. // default parameters for display var $cell_content = '&nbsp;'; var $cell_style = 'VALIGN="top"'; var $table_style = 'BORDER="0" CELLPADDING="2" CELLSPACING="1" VALIGN="top"'; var $class_field_input = 'formInput'; // class for input fields. var $class_field_name = 'formName'; // class for field names. var $class_seperator = 'formSeperator'; // class for table cell that seperates name and input fields. var $class_button = 'formButton'; // class for form button. var $class_error = 'formError'; // class for validation errors. // parameters for "pretty" table. var $background = 'CLASS="formBackground"'; var $border_width = ''; var $border_color = ''; // javascript array of fields to validate along with validation information. used for client-side validation var $javascript_validation_array = ''; function initialise() { //$this->conf_file = ''; $this->grab_conf_file(); $this->grab_parameters(); } // end of function initialise. function grab_conf_file() { if (file_exists($this->conf_file_name)) { $file_pointer = fopen($this->conf_file_name, "r"); while ($buffer = fgets($file_pointer, 255)) { $buffer = chop($buffer); //remove any whitespace from end of string. if (empty($buffer)) continue; //check for blank item. if (ereg('^#',$buffer,$temp)) continue; // check for comment. $this->conf_file_text[] = $buffer; } fclose($file_pointer); //} else handle_error(129,'file not found: '.$this->conf_file_name); } else die('config file not found'); } // end of function grab_conf_file. function grab_parameters() { $i = 0; $javascript_validation_array = ''; reset($this->conf_file_text); foreach ($this->conf_file_text as $line_num => $line_text) { if (preg_match('/^\$(\w+)\s*:\s*(.*)\s*$/U', $line_text, $match)) { if (empty($this->parameter[$match[1]])) $this->parameter[$match[1]] = chop($match[2]); else $this->parameter[$match[1]] .= chop($match[2]); } else { // we have field description $item = explode($this->field_delimiter, $line_text); $name = trim($item[0]); @set_var($this->field[$name]['type'], trim($item[1]), ''); @set_var($this->field[$name]['description'], trim($item[2]), $name); @set_var($this->field[$name]['value'], trim($item[3]), ''); @set_var($this->field[$name]['html_display_options'], trim($item[4]), ''); @set_var($this->field[$name]['valid_entry_regexp'], trim($item[5]), ''); @set_var($this->field[$name]['valid_entry_prompt'], trim($item[6]), ''); @set_var($this->field[$name]['selection_option_id'], trim($item[7]), ''); @set_var($this->field[$name]['selection_option_text'], trim($item[8]), $this->field[$name]['selection_option_id']); if ($this->parameter['validation']=='client') { $type = $this->field[$name]['type']; $description = $this->field[$name]['description']; $valid_entry_regexp = $this->field[$name]['valid_entry_regexp']; $valid_entry_prompt = $this->field[$name]['valid_entry_prompt']; $number_options = sizeof(split(',', $this->field[$name]['selection_option_id'])); $javascript_validation_array .= "field[$i] = Array('$name', '$type', '$description', "; $javascript_validation_array .= "$valid_entry_regexp, '$valid_entry_prompt', '$number_options');\n"; $i++; } } } if ($this->parameter['validation']=='client') { $this->javascript_validation_array = '<SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript">'."\n"; $this->javascript_validation_array .= $javascript_validation_array; $this->javascript_validation_array .= '</SCRIPT>'."\n"; } //unset($this->conf_file); } //end of function grab_parameters. function validate($data) { //$this->data = $data; unset($this->validation_error); foreach($this->field as $name => $field) { @set_var($value, $data[$name], ''); @set_var($valid, $this->field[$name]['valid_entry_regexp'], '/.*/'); switch ($this->field[$name]['type']) { case 'text' : $test_value = $value; break; case 'box' : $test_value = $value; break; case 'select' : $test_value = @sizeof($data[$name]); break; case 'multi' : $test_value = @sizeof($data[$name]); @set_var($value, implode(',', $value), ''); break; case 'radio' : $test_value = @sizeof($data[$name]); break; case 'check' : $test_value = @sizeof($data[$name]); @set_var($value, implode(',', $value), ''); break; } if (preg_match($valid, $test_value)) $this->field[$name]['value'] = $value; else { $this->field[$name]['value'] = $value; $this->field[$name]['validation_error'] = true; $this->field[$name]['validation_error_msg'] = $this->field[$name]['valid_entry_prompt']; $this->validation_error = true; } } } // end of function validate. function make_table() { global $PHP_SELF; $javascript_validation_array = ''; $table = new html_table_pretty; $table->background = $this->background; $table->border_width = $this->border_width; $table->border_color = $this->border_color; //$table = new html_table; $table->cell_content = $this->cell_content; $table->cell_style = $this->cell_style; $table->table_style = $this->table_style; if (isset($this->validation_error)) { $table->init(array("cols"=>4)); $table->col_style[4] = 'CLASS="'.$this->class_error.'" VALIGN="top"'; } else { $table->init(array("cols"=>3)); } $table->col_style[1] = 'CLASS="'.$this->class_field_name.'" VALIGN="top"'; $table->col_style[2] = 'CLASS="'.$this->class_seperator.'"'; $table->col_style[3] = 'CLASS="'.$this->class_field_input.'" VALIGN="top"'; $row = 0; reset($this->field); foreach ($this->field as $name => $field) { $row++; $table->add_rows(1); $table->cell[$row][1]['content'] = $field['description']; $table->cell[$row][3]['content'] = $this->html_form_element($name); if (isset($field['validation_error'])) { if (isset($this->parameter['validation_error_label'])) $table->cell[$row][4]['content'] = $this->parameter['validation_error_label']; $table->cell[$row][4]['content'] .= $field['validation_error_msg']; } } $row++; $table->add_rows(1); if (isset($this->parameter['update_buttons'])) { $update_buttons = $this->parameter['update_buttons']; } else { if ($this->parameter['validation']=='client') { $update_buttons = '<INPUT TYPE="hidden" NAME="form_action" VALUE="">'; $update_buttons .= '<INPUT CLASS="'.$this->class_button.'" TYPE="button" VALUE="ok" onClick="javascript:validate(\'ok\');">'; $update_buttons .= '&nbsp;<INPUT CLASS="'.$this->class_button.'" TYPE="button" VALUE="cancel" onClick="javascript:validate(\'cancel\');">'; $update_buttons .= '&nbsp;<INPUT CLASS="'.$this->class_button.'" TYPE="button" VALUE="apply" onClick="javascript:validate(\'apply\');">'; } else { $update_buttons = '<INPUT CLASS="'.$this->class_button.'" TYPE="submit" VALUE="ok" NAME="form_action">'; $update_buttons .= '&nbsp;<INPUT CLASS="'.$this->class_button.'" TYPE="submit" VALUE="cancel" NAME="form_action">'; $update_buttons .= '&nbsp;<INPUT CLASS="'.$this->class_button.'" TYPE="submit" VALUE="apply" NAME="form_action">'; } } $table->cell[$row][3]['content'] = $update_buttons; //$table->code(); $table->code_pretty(); //$this->table = '<FORM '.$this->parameter['form_html'].'>'.$table->html.'</FORM>'; $this->table = '<FORM '.$this->parameter['form_html'].'>'.$table->html_pretty.'</FORM>'; } //end of function draw. function html_form_element($name) { $html = ''; $type = $this->field[$name]['type']; $value = $this->field[$name]['value']; $html_display_options = $this->field[$name]['html_display_options']; $extra_html = "CLASS=\"$this->class_field_input\""; switch ($type) { case 'text' : $html = "<INPUT TYPE=\"text\" NAME=\"$name\" VALUE=\"$value\" $html_display_options $extra_html >"; break; case 'box' : $html = "<TEXTAREA NAME=\"$name\" $html_display_options $extra_html>$value</TEXTAREA>"; break; case 'select' : $html = "<SELECT NAME=\"$name\" $html_display_options $extra_html>"; $option_id = explode(',', $this->field[$name]['selection_option_id']); $option_text = explode(',', $this->field[$name]['selection_option_text']); foreach ($option_id as $key => $id) { if ($id==$value) $html .= "<OPTION VALUE=\"$id\" SELECTED>$option_text[$key]</OPTION>"; else $html .= "<OPTION VALUE=\"$id\">$option_text[$key]</OPTION>"; } $html .= '</SELECT>'; break; case 'multi' : $html = "<SELECT NAME=\"{$name}[]\" MULTIPLE $html_display_options $extra_html>"; $option_id = explode(',', $this->field[$name]['selection_option_id']); $option_text = explode(',', $this->field[$name]['selection_option_text']); $selected = explode(',', $value); foreach ($option_id as $key => $id) { $is_selected = false; foreach ($selected as $valid_selected) if ($id==$valid_selected) $is_selected = true; if ($is_selected) $html .= "<OPTION VALUE=\"$id\" SELECTED>$option_text[$key]</OPTION>"; else $html .= "<OPTION VALUE=\"$id\">$option_text[$key]</OPTION>"; } $html .= '</SELECT>'; break; case 'radio' : $option_id = explode(',', $this->field[$name]['selection_option_id']); $option_text = explode(',', $this->field[$name]['selection_option_text']); while (list($key, $id) = each($option_id)) { if ($id==$value) $html .= "<INPUT TYPE=\"radio\" NAME=\"$name\" VALUE=\"$id\" CHECKED>$option_text[$key]</INPUT>$html_display_options"; else $html .= "<INPUT TYPE=\"radio\" NAME=\"$name\" VALUE=\"$id\">$option_text[$key]</INPUT>$html_display_options"; } break; case 'check' : $option_id = explode(',', $this->field[$name]['selection_option_id']); $option_text = explode(',', $this->field[$name]['selection_option_text']); $selected = explode(',', $value); $index = 0; // need to keep track of individual checkboxes. foreach ($option_id as $key => $id) { $is_selected = false; foreach ($selected as $valid_selected) if ($id==$valid_selected) $is_selected = true; if ($is_selected) $html .= "<INPUT TYPE=\"checkbox\" NAME=\"{$name}[{$index}]\" VALUE=\"$id\" CHECKED>$option_text[$key]</INPUT>$html_display_options"; else $html .= "<INPUT TYPE=\"checkbox\" NAME=\"{$name}[{$index}]\" VALUE=\"$id\">$option_text[$key]</INPUT>$html_display_options"; $index++; } break; } // end switch. return $html; } // end of function html_form_element. } // end class form_template. ?>