--- nfo/php/libs/com.newsblob.phphtmllib/form/FormProcessor.inc 2003/04/03 23:47:38 1.2 +++ nfo/php/libs/com.newsblob.phphtmllib/form/FormProcessor.inc 2003/09/20 00:18:43 1.3 @@ -2,7 +2,7 @@ /** * This file contains the FormProcessor class. * - * $Id: FormProcessor.inc,v 1.2 2003/04/03 23:47:38 jonen Exp $ + * $Id: FormProcessor.inc,v 1.3 2003/09/20 00:18:43 jonen Exp $ * * @author Walter A. Boring IV * @author Suren Markossian @@ -13,9 +13,9 @@ * */ -define(FORM_ACTION, "_form_action"); -define(FORM_VISITED, "_form_visited"); -define(FORM_CONFIRM, "_form_confirm"); +define("FORM_ACTION", "_form_action"); +define("FORM_VISITED", "_form_visited"); +define("FORM_CONFIRM", "_form_confirm"); /** * This is the main engine for the processing @@ -30,24 +30,18 @@ */ class FormProcessor extends Container { - /** - * This holds the name of the form - * for js that needs it - */ - var $_form_name="forms[0]"; - - - /** - * The action for the form tag - */ - var $_form_action = ""; /** - * The form's enctype attribute. - * ie
+ * This array holds the FORMtag + * attributes for this form * */ - var $_form_enctype = NULL; + var $_form_attributes = array("method" => "post", + "action" => "", + "name" => "myform", + "target" => "", + "onsubmit" => "", + "style" => "margin: 0px 0px 0px 0px;"); /** * This holds the FormContent Object @@ -77,6 +71,33 @@ */ var $_form_success_render = TRUE; + /** + * This is the FormValidation object + * used to validate the form elements + */ + var $_FormValidation = NULL; + + /** + * The action that was taken + * for the form + */ + var $_form_submit_action = NULL; + + /** + * The form was processed and passed + * the confirmation if any, and + * it was successfull ? + */ + var $_confirmed_successfull = FALSE; + + /** + * This tells us to show or not to + * show the form errors autmatically. + * The user of the FormProcessor + * may want to deal with errors manually + */ + var $_auto_show_errors = TRUE; + /** * The constructor for the FormProcessor @@ -84,22 +105,43 @@ * @param FormContent object * @param string the form name */ - function FormProcessor(&$form_content, $form_name="forms[0]", + function FormProcessor(&$form_content, $form_name="myform", $form_action=NULL) { - $this->_form_name = $form_name; $this->_form_content = &$form_content; + $this->set_form_name( $form_name ); $this->_form_content->set_form_name($form_name); if ($form_action != NULL) { - $this->_form_action = $form_action; + $this->set_form_action( $form_action ); } else { - $this->_form_action = $_SERVER["PHP_SELF"]; + $this->set_form_action( $_SERVER["PHP_SELF"] ); } + //Set up the Validation object + //and the FormErrors object to + //be used by this form. + $this->setup_validation(); + + //now process the form + $this->_process_form(); } /** + * This function is used to setup + * the validation object and the + * form errors object that is to be + * used by this form. + * + * You can override this method to + * use a different FormErrors object + * for localization. + */ + function setup_validation() { + $this->_FormValidation = new FormValidation( new FormErrors ); + } + + /** * This method does the logic of * doing the form processing */ @@ -109,7 +151,7 @@ $this->_form_content->form_init_elements(); //first we need to - if (!$_POST[FORM_VISITED]) { + if (!@$_REQUEST[FORM_VISITED]) { $this->_form_content->form_init_data(); } @@ -117,11 +159,11 @@ //we only need to process the form //if it has been visited. Otherwise //it just gets rendered. - if ($_POST[FORM_VISITED] == 1) { - $this->_form_content->set_action($_POST[FORM_ACTION]); + if (!empty($_REQUEST[FORM_VISITED]) && $_REQUEST[FORM_VISITED] == 1) { + $this->_set_action(); //let see if this was a confirmation page. - if ( $_POST[FORM_CONFIRM] == 1 ) { + if ( !empty($_REQUEST[FORM_CONFIRM]) && $_REQUEST[FORM_CONFIRM] == 1 ) { //looks like this was a submit on a //confirmation page. we don't need //to do form field validation. @@ -147,6 +189,9 @@ $this->_has_errors = !$this->_form_content->form_backend_validation(); if (!$this->_has_errors) { $this->_has_errors = !$this->_process_action(); + if (!$this->_has_errors) { + $this->_set_confirmed_success(TRUE); + } } } } @@ -155,6 +200,9 @@ $this->_has_errors = !$this->_form_content->form_backend_validation(); if (!$this->_has_errors) { $this->_has_errors = !$this->_process_action(); + if (!$this->_has_errors) { + $this->_set_confirmed_success(TRUE); + } } } } @@ -191,7 +239,7 @@ function do_validation() { $keys = array_keys( $this->_form_content->_elements ); foreach( $keys as $key ) { - $valid = $this->_form_content->_elements[$key]->_do_validation(); + $valid = $this->_form_content->_elements[$key]->_do_validation($this->_FormValidation); if (!$valid) { $this->_has_errors = TRUE; } @@ -204,22 +252,19 @@ * */ function render($indent_level=0, $output_debug=0) { - //now process the form - $this->_process_form(); - if ($this->_has_errors) { //we need to render the form errors //and then the form return $this->render_error($indent_level, $output_debug); } else { //there are no errors! - if ($_POST[FORM_VISITED] == 1) { + if (@$_REQUEST[FORM_VISITED] == 1) { //looks like the form has been processed? if ($this->_form_content->has_confirm() && !$this->_confirmed) { return $this->render_confirm($indent_level, $output_debug); } else { //Looks like the action worked - $success = $this->_form_content->form_success($this->_message); + $success = $this->_form_content->form_success(); if ($this->_form_success_render) { return $this->render_form($indent_level, $output_debug, @@ -257,6 +302,16 @@ //build the $this->_form object. $this->_build_form_tag(); + //check to see if the form_content + //has any js, or any of the form elements + //have js. + $form_js = $this->_build_javascript(); + if (strlen($form_js) > 0) { + $script = html_script(); + $script->add( $form_js ); + //$this->_form->add( $script ); + } + if ($obj) { $this->_form->add_reference( $obj ); } @@ -270,7 +325,13 @@ //Ok lets add our hidden vars $this->__hidden_fields(); - return $this->_form->render($indent_level, $output_debug); + if (isset($script)) { + $c = container( $script, $this->_form ); + return $c->render($indent_level, $output_debug); + } else { + return $this->_form->render($indent_level, $output_debug); + } + } /** @@ -291,7 +352,7 @@ $this->_build_form_tag(); //add the confirm object/html - $confirm = &$this->_form_content->form_confirm( $this->data ); + $confirm = &$this->_form_content->form_confirm( ); $this->_form->add_reference( $confirm ); //ok add all of the submitted data as hidden form fields @@ -316,13 +377,18 @@ * @return raw html */ function render_error( $indent_level, $output_debug) { - $wrapper_div = new DIVtag; - - //Ok first lets build the error table - $errors = &$this->_form_content->form_errors(); - if ($errors != NULL) $wrapper_div->add( $errors, html_br() ); + + if ($this->_auto_show_errors) { + //Ok first lets build the error table + $wrapper = new DIVtag; + $errors = &$this->_form_content->form_errors(); + if ($errors != NULL) $wrapper->add( $errors, html_br() ); + } else { + $wrapper = NULL; + } + - return $this->render_form( $indent_level, $output_debug, $wrapper_div); + return $this->render_form( $indent_level, $output_debug, $wrapper); } @@ -351,8 +417,222 @@ * is off * */ - function set_render_form_after_success() { - $this->_form_success_render = TRUE; + function set_render_form_after_success($flag=TRUE) { + $this->_form_success_render = $flag; + } + + /** + * This is used to test to see if the form action + * was processed succesfully. + * This is usefull for external entities to determine + * if the form was processed, and it was successfull. + * + * @return boolean + */ + function is_action_successfull() { + return $this->_confirmed_successfull; + } + + /** + * This flag sets the flag that tells + * if we successfully confirmed the form, + * and processed the action + * + * @param boolean + */ + function _set_confirmed_success($flag=TRUE) { + $this->_confirmed_successfull = $flag; + } + + /** + * This sets the flag that tells this class + * to automatically call the form contents + * form errors and display it or not + * + * @param boolean - show errors? + */ + function set_auto_error_display($flag=TRUE) { + $this->_auto_show_errors = $flag; + } + + /** + * This gets the current value of the flag + * that tells us to show form errors automatically + * or not. + * + * @return boolean + */ + function get_auto_error_display() { + return $this->_auto_show_errors; + } + + + /** + * This method allows us to get access to the + * errors display object that is generated by + * the form content. This is the display + * object that is meant to be rendered directly. + * If there are no errors. we will return NULL + * + * @return object + */ + function &get_error_display_object() { + if ($this->_has_errors) { + return $this->_form_content->form_errors(); + } else { + return NULL; + } + } + + + /** + * This method returns an array of errors that + * happened in the form. + * + * @return array + */ + function get_error_array() { + return $this->_form_content->get_error_array(); + } + + /** + * This returns the flag that tells us that + * the form has errors during processing + * + * @return boolean + */ + function has_errors() { + return $this->_has_errors; + } + + + + + //************************************************// + //* FORMtag Attributes for this class *// + //************************************************// + + /** + * This function is used to set the + * form name + * + * @param string + */ + function set_form_name($name) { + $this->_form_attributes["name"] = $name; + } + + /** + * This function is used to get + * the form name + * + * @return string + */ + function get_form_name() { + return $this->_form_attributes["name"]; + } + + /** + * This function is used to set the + * form target + * + * @param string + */ + function set_form_target($target) { + $this->_form_attributes["target"] = $target; + } + + /** + * This function is used to get + * the form target + * + * @return string + */ + function get_form_target() { + return $this->_form_attributes["target"]; + } + + /** + * This function is used to set the + * form method + * + * @param string (POST or GET) + */ + function set_form_method($method) { + if ( strcasecmp($method,"GET") !=0 && strcasecmp($method,"POST") !=0 ) { + user_error("FormProcessor::set_form_method() - INVALID Form method ".$method); + } else { + $this->_form_attributes["method"] = $method; + } + } + + /** + * This function is used to get + * the form method + * + * @return string (POST or GET) + */ + function get_form_method() { + return $this->_form_attributes["method"]; + } + + /** + * Sets the form action + * + * @param string + */ + function set_form_action($action) { + $this->_form_attributes["action"] = $action; + } + + /** + * This function is used to get + * the form action + * + * @return string (POST or GET) + */ + function get_form_action() { + return $this->_form_attributes["action"]; + } + + /** + * Sets the form enctype + * + * @param string + */ + function set_form_enctype($enctype) { + $this->_form_attributes["enctype"] = $enctype; + } + + /** + * This function is used to get + * the form enctype value + * + * @return string + */ + function get_form_enctype() { + return $this->_form_attributes["enctype"]; + } + + + /** + * This is used to set the action + * submitted by the user + * + */ + function _set_action() { + $this->_form_submit_action = $_REQUEST[FORM_ACTION]; + $this->_form_content->set_action($this->_form_submit_action); + } + + /** + * This is used to get the action that was + * processed by the form + * + * @return string + */ + function get_action() { + return $this->_form_submit_action; } @@ -363,17 +643,12 @@ * @return FORMtag object. */ function _build_form_tag() { - $form_attrs = array("method" => "POST", - "action" => $this->_form_action, - "name" => $this->_form_name, - "style" => "margin: 0px 0px 0px 0px;"); - if ($this->_enctype != NULL) - $form_attrs["enctype"] = $this->_enctype; - if ($this->_target != NULL) - $form_attrs["target"] = $this->_target; - if ($this->_onsubmit != NULL) - $form_attrs["onSubmit"] = $this->_onsubmit; - + $form_attrs = array(); + foreach( $this->_form_attributes as $name => $value) { + if ($value) { + $form_attrs[$name] = $value; + } + } $this->_form = new FORMtag( $form_attrs ); } @@ -428,7 +703,7 @@ form_hidden(FORM_VISITED,1) ); if ($this->_form_content->has_confirm() && !$this->_confirmed) { - if (!$_POST[FORM_VISITED] || $this->_has_errors) { + if (@!$_REQUEST[FORM_VISITED] || $this->_has_errors) { $this->_form->add( form_hidden(FORM_CONFIRM, 0 ) ); } else { $this->_form->add( form_hidden(FORM_CONFIRM, 1 ) ); @@ -438,5 +713,23 @@ $this->_form->add( form_hidden(FORM_CONFIRM, 0 ) ); } } + + /** + * This method is used to build any Javascript + * that is used by the form and/or the form elements + * used in the form. + * + * @return string + */ + function _build_javascript() { + $form_js = $this->_form_content->javascript(); + + //now walk each form element and try to get any js + foreach( $this->_form_content->_elements as $element ) { + $form_js .= $element->javascript(); + } + + return $form_js; + } } ?>