cvs: pear /HTML_OOH_Form form.php form_page.php form_page_form.php form_wizard.php form_xmlfactory.php

From: Date: Sun, 14 Oct 2001 18:16:07 +0000
Subject: cvs: pear /HTML_OOH_Form form.php form_page.php form_page_form.php form_wizard.php form_xmlfactory.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-1015@lists.php.net to get a copy of this message
uw Sun Oct 14 14:16:07 2001 EDT Added files: /pear/HTML_OOH_Form form_page.php form_page_form.php form_wizard.php Modified files: /pear/HTML_OOH_Form form.php form_xmlfactory.php Log: Holiday: sitting on a balcony in Bologna looking over the historic city, bright sky and 25 degrees, no customers at all, hacking for the php conference, perfect pizza served by an italian girls... Ok, this is the first part of the results of all this. - simplified and generally cleaned up wizard - improved javascript code - implemented several layout manager: flow, grid, IT[X] template - added some more examples (more commits to come)

Index: pear/HTML_OOH_Form/form.php diff -u pear/HTML_OOH_Form/form.php:1.2 pear/HTML_OOH_Form/form.php:1.3 --- pear/HTML_OOH_Form/form.php:1.2 Tue Aug 14 05:25:20 2001 +++ pear/HTML_OOH_Form/form.php Sun Oct 14 14:16:02 2001 @@ -1,5 +1,5 @@ <?php -require_once(FORM_INCLUDE_DIR . 'form_commonobject.inc'); +include_once(FORM_INCLUDE_DIR . 'form_commonobject.php'); /** * Object oriented HTML form generation and validation. @@ -28,7 +28,7 @@ * - tree (select box with options show as a tree) * * @author Ulf Wendel <ulf.wendel@phpdoc.de> -* @version $Id: form.php,v 1.2 2001/08/14 09:25:20 sebastian Exp $ +* @version $Id: form.php,v 1.3 2001/10/14 18:16:02 uw Exp $ * @access public * @package Form */ @@ -66,14 +66,14 @@ * * @var $elements array */ - var $elements = array(); + var $elements; /** * List of all element names. * * @var array */ - var $element_names = array(); + var $element_names; /** * Global element name prefix. @@ -92,7 +92,7 @@ * @var array Format - $validation_flags[ $name ] = $message * @see validate(), setValidationError(), getValidationResult(), $validation_msg */ - var $validation_flags = array(); + var $validation_flags; /** * String with all error messages. @@ -112,14 +112,14 @@ * * @var array */ - var $hidden_elements = array(); + var $hidden_elements; /** * Array of radio elements. * * @var array $radio_elements */ - var $radio_elements = array(); + var $radio_elements; /** * HTML name attribute of the opening form Tag. @@ -198,7 +198,6 @@ */ var $additional_html; - /** * Sets all paramters required for the opening [form} Tag * @@ -230,6 +229,7 @@ } // end func constructor + /** * Sets the JavaScript validation mode. * @@ -252,6 +252,17 @@ $this->method = ('GET' == strtoupper($method)) ? 'GET' : 'POST'; } // end func setMethod + + /** + * Returns the method (GET/POST) used in the form. + * + * @return string + * @access public + */ + function getMethod() { + return $this->method; + } // end func getMethod + /** * Loads the form with HTTP_[POST|GET]_VARS. * @@ -269,7 +280,7 @@ $ellist = array($ellist); if (!is_array($ellist) || 0 == count($ellist)) { - $this->exceptions[] = new form_error('Can't load the form with submitted values, no elements in form or no elements given.', __FILE__, __LINE__); + $this->exceptions[] = new form_error('Can\'t load the form with submitted values, no elements in form or no elements given.', __FILE__, __LINE__); return false; } @@ -277,7 +288,7 @@ $data = &$GLOBALS['HTTP_GET_VARS']; else $data = &$GLOBALS['HTTP_POST_VARS']; - + if (!is_array($data) || 0 == count($data)) { $this->exceptions[] = new form_error("Can't find any HTTP_$this->method_VARS.", __FILE__, __LINE__); return true; @@ -285,7 +296,7 @@ foreach ($ellist as $k => $name) { - $http_x_name = $this->element_prefix . $this->elements[$name]->name; + $http_x_name = $this->elements[$name]->name; if (']' == substr($http_x_name, -1) && $start = strpos($http_x_name, '[')) { @@ -507,7 +518,6 @@ * @access public */ function getFinish($additional_html = '', $sess = '') { - $html = ('' != $additional_html) ? $additional_html : $this->additional_html; if (0 != count($this->hidden_elements)) { @@ -515,7 +525,7 @@ $html .= $this->getElement($elname); } - return $html . '</form>' . $this->CR_HTML .$this->getJS();; + return $html . '</form>' . $this->CR_HTML . $this->getJS();; } // end func getFinish /** @@ -573,6 +583,9 @@ return false; } + if (!isset($element_data['elname'])) + $element_data['elname'] = $element_data['name']; + if (isset($element_data['name']) && '' != $element_data['name']) $element_data['name'] = $this->element_prefix . $element_data['name']; @@ -580,38 +593,63 @@ $this->addDefaultAttributes($element_data); $objectname = 'form_element_' . $element_data['type']; - include_once(FORM_INCLUDE_DIR . 'elements/' . $objectname . '.inc'); + include_once(FORM_INCLUDE_DIR . 'elements/' . $objectname . '.php'); $el = new $objectname($element_data, $this->method, $this->js_name, $this->js_mode); if ($el->isHidden()) $this->hidden_elements[] = $element_data['name']; - if (isset($element_data['elname'])) { - - $this->elements[$element_data['elname']] = $el; - $this->element_names[$element_data['elname']] = $element_data['elname']; + $this->elements[$element_data['elname']] = $el; + $this->element_names[$element_data['elname']] = $element_data['elname']; - if ('radio' == $element_data['type'] ) - $this->radio_elements[$el->name][$element_data['elname']] = $el->value; + if ('radio' == $element_data['type'] ) + $this->radio_elements[$el->name][$element_data['elname']] = $el->value; - } else { - - $this->elements[$el->name] = $el; - $this->element_names[$el->name] = $el->name; - - } - return true; } // end func addElement + + /** + * Returns a reference on a form element object + * + * You should not use this function to access the internal element objects + * of a form unless you have a good reason for doing so as a pages does. + * + * A pages needs full access to an element to be able to dump it into + * a layout manager. + * + * @param string element name + * @param string element subname + * @return object form_element + * @access public + * @throws form_error + */ + function &getElementObject($elname, $subname = '') { + if (!$this->ElementExists($elname)) { + $this->exceptions[] = new form_error("'$elname' is unknown.", __FILE__, __LINE__); + $null = ''; + return $null; + } + + if (!isset($this->radio_elements[$elname])) + return $this->elements[$elname]; + + $ret = array(); + foreach ($this->radio_elements[$elname] as $name => $v) + $ret[$name] = &$this->elements[$name]; + + return $ret; + } // end func getElementObject + + /** * Generate and return the html representation of an element. * * @param string Element name * @param string Element subname - * @return string HTML + * @return string HTML * @throws form_error - * @access public + * @access public */ function getElement($elname, $subname = '') { if (!$this->ElementExists($elname)) { @@ -628,6 +666,37 @@ return $html; } // end func getElement + + + /** + * Returns the label of an element + * + * @param string element name + * @param string element subname + * @return string + * @throws form_error + * @access public + */ + function getLabel($elname, $subname = '') { + if (!$this->ElementExists($elname)) { + $this->exceptions[] = new form_error("'$elname' is unknown.", __FILE__, __LINE__); + return ''; + } + + if (!isset($this->radio_elements[$elname])) + return $this->elements[$elname]->getLabel($subname); + + + $ret = ''; + foreach ($this->radio_elements[$elname] as $name => $v) + if ('' != $subname && $name == $subname) + return $this->elements[$name]->getLabel(); + else + $ret .= $this->elements[$name]->getLabel() . ' '; + + return $ret; + } // end func getLabel + /** * Return the type of an element. @@ -650,7 +719,7 @@ } // end func getType() /** - * Return the current value of an element. + * Return the current value of an element * * Use the function within your custom validation functions. * @@ -667,7 +736,7 @@ if (!isset($this->radio_elements[$name])) return $this->elements[$name]->getValue(); - + // Watch out: the meaning of $name changes! // $name does now referr to the HTML/Javascript name of a group of // elements, you can't adress one radio button by it's elname. @@ -733,7 +802,7 @@ return $this->getValidationResult(); } // end func validate - + /** * Sets the validation error message for of an element. * @@ -885,7 +954,7 @@ foreach ($this->getCustomValidators() as $k => $validator) if ('' != $validator['jscode']) $validators[] = $validator['name']; - + if (0 != count($validators)) { $complex = true; $file = 'form_js_complex.js'; @@ -928,6 +997,10 @@ // custom validator and get_value function list($eldata, $getvalue_func, $getvalue_fname, $js_validator) = $el->getJS($complex); + + // replace newlines, otherwise we'll get unterminated string literals. + $eldata = str_replace(array("\n", "\m"), array('', ''), $eldata); + if ($js_validator) $custom .= $js_validator . "\n"; @@ -937,7 +1010,7 @@ if ($getvalue_fname) { - // don't send radio entries twice + // don't radio entries twice if (!isset($send_gv_el[$elname])) { $get_value .= sprintf('"%s", "%s", ', $elname, $getvalue_fname); $send_gv_el[$elname] = true; @@ -945,40 +1018,36 @@ // complex function used to get the value of this form element if (!isset($send_func[$getvalue_fname])) { - $base_code = $base_code . "\n" . $getvalue_func . ';'; + $base_code = $base_code . " " . $getvalue_func . ';'; $send_func[$getvalue_fname] = true; } } } + // form('forma', ['elname', [eldata], 'elname', [eldata, flags]], ['elname', 'gv'], ['val']); // name, els, val, gv - // putting it all together if ($complex) { - // list of javascript custom validators $vals = ''; foreach($validators as $k => $validator) $vals .= sprintf('"%s", ', $validator); $vals = substr($vals, 0, -2); - - $js = sprintf('[%s],%s[%s],%s[%s]', - substr($js, 0, -2), - "\n", + $js = sprintf('[%s], [%s], [%s]', + substr($js, 0, strrpos($js, ',')), substr($get_value, 0, -2), - "\n", $vals ); } else { - $js = sprintf('[%s]', substr($js, 0, -2)); + $js = sprintf('[%s]', substr($js, 0, strrpos($js, ','))); } // ;-) compress the JS code.... -# $base_code = $this->compressJS($base_code); + // $base_code = $this->compressJS($base_code); // customize the error message $base_code = str_replace('{ERROR_MSG_PREFIX}', $this->js_error_prefix, $base_code); @@ -1031,7 +1100,7 @@ */ function compressJS($code) { - $search = array('/function[ \t]+/i', '/new[ \t]+/i', '/var[ \t]+/i', '/return[ \t]+/i', '/[ \t]*else[ \t]*/i', '/[ \t]+in[ \t]+/i'); + $search = array('/function\s+/i', '/new\s+/i', '/var\s+/i', '/return\s+/i', '/\s*else\s*/i', '/\s+in\s+/i'); $meta = array('{FUNCTION_}', '{NEW_}', '{VAR_}', '{RETURN_}', '{_ELSE_}', '{_IN_}'); $final = array('function ', 'new ', 'var ', 'return ', ' else ', ' in '); @@ -1039,13 +1108,16 @@ $code = preg_replace($search, $meta, $code); $code = preg_replace('/[ \t]/', '', $code); $code = preg_replace('/[\n][\r]*/', "\n", $code); + + // some class method declarations + $code = preg_replace('/function\s+\(/i', 'function(', $code); // remove some newlines $tmp = array(); $code = explode("\n", $code); $j = 0; foreach ($code as $k => $line) { - if (strlen($tmp[$j]) < 120) + if (strlen($tmp[$j]) < 512) $tmp[$j] .= trim($line); else $tmp[++$j] = trim($line); @@ -1284,6 +1356,7 @@ return $names; } // end func getNameTranslationList + /** * FIXME */ @@ -1296,42 +1369,27 @@ return $elements; } // end func getElementnames + /** * Reformate generated code * * @param string $code - * @return string $code + * @return string $code */ function clearCode($code) { - $code = ereg_replace("[\t]+", "\t", $code); - $code = ereg_replace("[ ]{2,}", '', $code); + $code = preg_replace("@[\t]+@", "\t", $code); + $code = preg_replace("@[ ]{2,}@", '', $code); return $code; } // end func clearCode - /** - * FIXME - * - * Returns an array with elname => value pairs, that can be used - * to be stored in a session. new_assistant needs this - * - * @return array $session_hash - * @access public - */ - function getSessionHash() { - - $data = array(); - foreach ($this->elements as $name => $value) - $data[$name] = $this->getValue($name); - - return $data; - } // end func getSessionHash /** - * Sets a global Prefix for element names. + * Sets a global prefix for element names. * * @param string - * @return boolean - * @see $element_prefix + * @return boolean + * @see $element_prefix, getelementnamePrefix() + * @access public */ function setElementnamePrefix($prefix) { if ('' == $prefix) { @@ -1342,8 +1400,21 @@ $this->element_prefix = $prefix; return true; } // end func setElementnamePrefix + - /** + + /** + * Returns the global prefix for element names. + * + * @return string + * @access public + */ + function getElementnamePrefix() { + return $this->element_prefix; + } // end func getElementnamePrefix + + + /** * Is this checkboxobject checked? * * @param string Element name Index: pear/HTML_OOH_Form/form_xmlfactory.php diff -u pear/HTML_OOH_Form/form_xmlfactory.php:1.2 pear/HTML_OOH_Form/form_xmlfactory.php:1.3 --- pear/HTML_OOH_Form/form_xmlfactory.php:1.2 Tue Aug 14 05:25:20 2001 +++ pear/HTML_OOH_Form/form_xmlfactory.php Sun Oct 14 14:16:06 2001 @@ -1,6 +1,6 @@ <?php -require_once(FORM_INCLUDE_DIR . 'form_commonobject.inc'); -require_once(FORM_INCLUDE_DIR . 'form.inc'); +include_once(FORM_INCLUDE_DIR . 'form_commonobject.php'); +include_once(FORM_INCLUDE_DIR . 'form.php'); /** * Creates a form object from a xml file. @@ -150,7 +150,6 @@ return false; } - $this->defaults = array(); $this->attributes = array(); $this->intros = array(); @@ -174,7 +173,7 @@ function parse($xml) { $parser = xml_parser_create(); - xml_set_object($parser, &$this); + xml_set_object($parser, $this); xml_set_element_handler($parser, 'startElement', 'endElement'); xml_set_character_data_handler($parser, 'cdata'); xml_parse($parser, $xml); @@ -183,7 +182,7 @@ } // end func parse - /** + /* * XML Parser opening tag handler * * @param resource xmlparser @@ -390,7 +389,7 @@ if (isset($attrs['callback'])) $this->attributes['validator_callback'] = ('false' == $attrs['validator_callback']) ? false : true; break; - + default: break; } @@ -453,9 +452,9 @@ case 'intros': if (!$this->flag_defaults) - $this->attributes['intro_e'] = (string)$this->intros['errormsg']; + $this->attributes['intro_e'] = trim((string)$this->intros['errormsg']); else - $this->defaults['selectintro_e'] = (string)$this->intros['errormsg']; + $this->defaults['selectintro_e'] = trim((string)$this->intros['errormsg']); break; @@ -468,11 +467,11 @@ break; case 'length': - $this->attributes['length_e'] = (string)$this->cdata; + $this->attributes['length_e'] = trim((string)$this->cdata); break; case 'regexp': - $this->attributes['valid_e'] = (string)$this->cdata; + $this->attributes['valid_e'] = trim((string)$this->cdata); break; case 'calendar': Index: pear/HTML_OOH_Form/form_page.php +++ pear/HTML_OOH_Form/form_page.php <?php include_once(FORM_INCLUDE_DIR . 'layoutmanager/form_gridlayout.php'); /** * Dumps a form into html and offers the interface all wizards need. * * @access public * @version $Id: form_page.php,v 1.1 2001/10/14 18:16:05 uw Exp $ * @author Ulf Wendel <ulf.wendel@phpdoc.de> * @package Form */ class form_page { /** * Page type * * @var string * @access public */ var $type = 'page'; /** * Returns the html repesentation of this page. * * @return string * @access public * @abstract */ function get() { return NULL; } // end func get /** * Prints and returns the form * * @brother get() */ function show() { $ret = $this->get(); print $ret; return $ret; } // end func show /** * Returns false if * * @return boolean * @access public */ function isValid() { return true; } // end func isValid /** * Returns all relevant values connected to this page. * * @return a rray * @access pulic */ function getValues() { return array(); } // end func NULL /** * Registers buttons which perform certain events. * * @param string form element / button name * @param string event caused if pressed, e.g.: reset, next page, exit ... * @access public function registerNavigationButton($name, $event) { $this->buttons[$name] = $event; } // end func registerNavigationButton /** * Returns a list of button which cause certain events. * * @return array [form element name => event] function getNavigationButtonlist() { return $this->buttons; } // end func getNavigationButtonlist /** * Returns the name of the button which was pressed when submitting the form and the event it caused. * * @return array [form element name / button name => event], * [NULL, NULL] if the form was not submitted function getSelectedNavigationButton() { $ellist = $this->form->getElementnames(); $http_x_vars = &$GLOBALS['HTTP_' . $this->form->getMethod() . '_VARS']; reset($ellist); while (list($k, $name) = each($ellist)) if ($this->form->elements[$name]->isButton() && isset($this->buttons[$name]) && (isset($http_x_vars[$name]) || isset($http_x_vars[$name . '_x'])) ) return array($name, $this->buttons[$name]); return array(NULL, NULL); } // end func getSelectedNavigationButton */ } // end class form_page ?> Index: pear/HTML_OOH_Form/form_page_form.php +++ pear/HTML_OOH_Form/form_page_form.php <?php include_once(FORM_INCLUDE_DIR . 'layoutmanager/form_gridlayout.php'); include_once(FORM_INCLUDE_DIR . 'layoutmanager/form_flowlayout.php'); include_once(FORM_INCLUDE_DIR . 'layoutmanager/form_grouplayout.php'); /** * Dumps a form into html and offers the interface all wizards need. * * @access public * @version $Id: form_page_form.php,v 1.1 2001/10/14 18:16:06 uw Exp $ * @author Ulf Wendel <ulf.wendel@phpdoc.de> * @package Form */ class form_page_form { var $type = 'form'; /** * Form object * * @var object form * @see form_page() */ var $form = NULL; /** * Stores the form object to work on. * * @var object form */ function form_page_form(&$form, $name = '') { $this->form = &$form; $this->name = $name; } // end constructor /** * Returns a html fragment containing the form. * * The form is layouted in a very basic way: one element per row * * @return string * @access public */ function get($startfinish = false) { $this->form->autoloadValues(); if ($startfinish) $html = $this->form->getStart(); $html.= $this->dumpFormElements(); if ($startfinish) $html .= $this->form->getFinish(); $group = new grouplayout($this->name); $group->addContent($html); return $group->get(); } // end func get function show($startfinish = false) { $ret = $this->get($startfinish); print $ret; return $ret; } // end func show function &getFormObject() { return $this->form; } // end func getFormObject /** * Returns true if the contained form object is valid, otherwise false */ function isValid() { $this->form->autoloadValues(); $valres = $this->form->validate(); return ('' == $valres[0]) ? true : false; } // end func isValid function getValues() { $ellist = $this->form->getElements(); $values = array(); reset($ellist); while (list($k, $name) = each($ellist)) $values[$name] = $this->form->getValue($name); return $values; } // end func getValues /** * Dumps all form elements using the simple rule: one element per row */ function dumpFormElements() { $ellist = $this->form->getElements(); $valresults = $this->form->validate(); $valresults = $valresults[1]; if (!is_array($valresults)) $valresults = array(); $grid = new gridlayout(count($ellist) * 2, 2); reset($ellist); while (list($k, $name) = each($ellist)) { $el = &$this->form->getElementObject($name); if (is_array($el)) { // radio elements $flow = new flowlayout(); reset($el); while (list($_name, ) = each($el)) { $flow->addContent($el[$_name]->get($_name)); } $grid->addContent($el[$name]->getLabel() . '&nbsp;'); if (isset($valresults[$el[$name]->getName()])) { // error $error = new gridlayout(2, 1); $error->addContent($flow->get()); $error->addContent('<div style="color: maroon;">' . $valresults[$el[$name]->getName()] . '</div>'); $grid->addContent($error->get()); } else { $grid->addContent($flow->get()); } $grid->addContent('<font size="-3">&nbsp;</font>'); $grid->addContent('<font size="-3">&nbsp;</font>'); } else if ('' != $el) { $grid->addContent($el->getLabel() . '&nbsp;'); if (isset($valresults[$el->getName()])) { // error $error = new gridlayout(2, 1); $error->addContent($el->get()); $error->addContent('<div style="color: maroon;">' . $valresults[$el->getName()] . '</div>'); $grid->addContent($error->get()); } else { $grid->addContent($el->get()); } $grid->addContent('<font size="-3">&nbsp;</font>'); $grid->addContent('<font size="-3">&nbsp;</font>'); } } return $grid->get(); } // end func dumpFormElements } // end class form_page ?> Index: pear/HTML_OOH_Form/form_wizard.php +++ pear/HTML_OOH_Form/form_wizard.php <?php include_once(FORM_INCLUDE_DIR . 'form.php'); include_once(FORM_INCLUDE_DIR . 'layoutmanager/form_flowlayout.php'); /** * Puts several pages together to a wizard. * * This is not a general purpose wizard, it's a dedicated wizard of * the form stuff. It's intention is to guide a user through several * forms in a comfortable way. * * Using derived page classes and addPage()/removePage() you can write * dynamically changing wizards. * * @version $Id: form_wizard.php,v 1.1 2001/10/14 18:16:06 uw Exp $ * @author Ulf Wendel <ulf.wendel@phpdoc.de> * @package Form * @access public */ class form_wizard { /** * List of all events. * * @var array */ var $events = array('back', 'next', 'finish', 'help', 'cancel'); /** * List of form page object * * @var array */ var $pages; /** * Cached validation results of all pages * * @var array */ var $validation_flags; /** * Validation / switching mode. * * @var string */ var $mode = ''; /** * Name of the page which was shown last * * @var string */ var $last_page = ''; /** * List of navigation buttons per page * * @var array * @see enableButton() */ var $buttons; /** * List of form object contained in form pages * * @var array */ var $forms; /** * Sets the switching mode. * * This wizard can handle two events automatically for your: back, next. * You don't have to care on page switching if you don't want to. There're two * switching modes: strong, weak. 'weak' allows you always to switch to * the previous or next page. 'strong' allows switching only if the page * currently show is 'valid'. The meaning of valid depends on the page. * In case of a "form_page" valid means that the form is valid. * * @param string switching mode: strong, weak */ function form_wizard($mode = 'weak') { $this->mode = $mode; } // end constructor /** * Returns the values of all pages. * * As with 'valid' it depends on a page what it's values are. In case of a * 'form_page' the values are the form element values. * * @param mixed string all, string page_name, array(page_name, page_name...) * @return array * @access public */ function getPageValues($pages = 'all') { $values = array(); if ('all' == $pages) { reset($this->pages); while (list($page, ) = each($this->pages)) $values[$page] = $this->pages[$page]->getValues(); } else if (is_array($pages)) { reset($pages); while (list($k, $page) = each($pages)) if (isset($this->pages[$page])) $values[$page] = $this->pages[$page]->getValues(); } else if (isset($this->pages[$pages])) { $values[$pages] = $this->pages[$pages]->getValues(); } return $values; } // end func getPageValues /** * Returns true is all contained pages (forms) are valid, otherwise false * * @return boolean * @access public */ function isValid() { reset($this->validation_flags); while (list($page, $valid) = each($this->validation_flags)) if (false == $valid) return false; return true; } // end func isValid /** * Enables certain buttons for a page * * Buttons [ = events]: back, next, finish, help, cancel * * @param string page * @param mixed List of buttons or 'all' * @see disableButton() */ function enableButton($page, $events = 'all') { if ('all' == $events) $events = $this->events; reset($events); while (list($k, $event) = each($events)) $this->buttons[$page][$event]['active'] = true; } // enableButton /** * Disables certain buttons for a page * * Buttons [ = events]: back, next, finish, help, cancel * * @param string page * @param mixed List of buttons or 'all' * @see enableButton() */ function disableButton($page, $events = 'all') { if ('all' == $events) $events = $this->events; reset($events); while (list($k, $event) = each($events)) $this->buttons[$page][$event]['active'] = false; } // disableButton /** * Adds a page to the wizard. * * @param string page name * @param object form_page * @access public * @return boolean */ function addPage($name, &$page) { if (isset($this->pages[$name])) return false; $this->pages[$name] = &$page; $this->validation_flags[$name] = $page->isValid(); $this->createButtons($name); $this->enableButton($name); return true; } // end func addPage /** * Removes a page * * @param string page name * @access public * @return boolean */ function removePage($name) { if (!isset($this->pages[$name]) || 1 == count($this->pages)) return false; unset($this->pages[$name]); unset($this->validation_flags[$name]); unset($this->buttons[$name]); // don't unset the form - it's a reference... return true; } // end func removePage /** * Returns the html of the wizard with the currently selected page * * @return string * @access public */ function get() { list($page, $event) = $this->findPage(); print "find: $page, $event<br>"; if ('form' == $this->pages[$page]->type) { $html = $this->dumpPage($page, $this->pages[$page]->get(false)); } else { $html = $this->dumpPage($page, $this->pages[$page]->get()); } return $html; } // end func get /** * Prints and returns the html represenation of the wizard * * @brother get() */ function show() { $ret = $this->get(); print $ret; return $ret; } // end func show /** * Adds the navigation buttons (next, back, finish...) to the page. * * You should override this method to get a better layout. * * @param string page * @param boolean add form->Start() and form->Finish() ? * @param string page content / page html */ function dumpPage($page, $html = '') { $flow = new flowlayout(); // opening form tag $flow->addContent($this->forms[$page]->getStart()); // upper row $found = false; $events = array('help', 'finish', 'cancel'); reset($events); while (list($k, $event) = each($events)) if ($this->buttons[$page][$event]['active']) { $flow->addContent($this->forms[$page]->getElement($this->buttons[$page][$event]['name'])); $found = true; } if ($found) $flow->insertBreak(); // page content in the middle $flow->addContent($html); $flow->insertBreak(); // lower row $events = array('back', 'next', 'cancel'); reset($events); while (list($k, $event) = each($events)) if ($this->buttons[$page][$event]['active']) $flow->addContent($this->forms[$page]->getElement($this->buttons[$page][$event]['name'])); // closing form tag $flow->addContent($this->forms[$page]->getFinish()); return $flow->get(); } // end func dumpPage /** * Calls the event handler and returns the name of the page to show * * @return array [page_name, event] */ function findPage() { list($last, $valid) = $this->checkLast(); $this->validation_flags[$last] = $valid; $event = $this->getEvent($last); switch ($event) { case 'back': $page = $this->handleBack($last); break; case 'next': $page = $this->handleNext($last); break; case 'help': $page = $this->handleHelp($last); break; case 'finish': $page = $this->handleFinish($last); break; case 'cancel': $page = $this->handleCancel($last); break; default: $page = $last; break; } $this->last_page = $page; return array($page, $event); } // end func findPage /** * Handles the event 'cancel' * * @param string name of the last page * @return string name of the page to show next * @see handleFinish(), handleHelp(), handleNext(), handleBack() */ function handleCancel($last) { return $last; } // end func handleCancel /** * Handles the 'finish' event. * * @brother handleCancel() */ function handleFinish($last) { return $last; } // end func handleFinish /** * Handles the 'help' event. * * @brother handleCancel() */ function handleHelp($last) { return $last; } // end func handleHelp /** * Handles the 'back' event. * * @brother handleCancel() */ function handleBack($last) { return $this->handleBackNext($last, 'back'); } // end func handleBack /** * Handles the 'next' event. * * @brother handleCancel() */ function handleNext($last) { return $this->handleBackNext($last, 'next'); } // end func handleNext /** * Default handling for 'back' and 'next' events * * @param string name of the last page * @param string event * @return string name of the next page */ function handleBackNext($last, $event) { list($back, $next) = $this->getBackNext($last); switch ($this->mode) { case 'strong': if (!$this->validation_flags[$last]) { $page = $last; break; } case 'weak': if ('back' == $event) { if (NULL == $back) $page = $last; else $page = $back; } else { $page = $next; } break; } // end switch $this->mode return $page; } // end func handleBackNext /** * Returns the last event if the wizard was submitted or NULL * * @param string name of the last page * @return mixed event or NULL */ function getEvent($page) { $buttons = array(); reset($this->buttons[$page]); while (list($event, $data) = each($this->buttons[$page])) if ($data['active']) $buttons[$data['name']] = $event; $ellist = $this->forms[$page]->getElementnames(); $http_x_vars = &$GLOBALS['HTTP_' . $this->forms[$page]->getMethod() . '_VARS']; reset($ellist); while (list($k, $name) = each($ellist)) if ($this->forms[$page]->elements[$name]->isButton() && isset($buttons[$name]) && (isset($http_x_vars[$name]) || isset($http_x_vars[$name . '_x'])) ) return $buttons[$name]; return NULL; } // end func getEvent /** * Returns the previous- and next page relativ to the given page * * @param string page name * @return array [previous page, next page] */ function getBackNext($current) { $back = NULL; $next = NULL; $i = 0; reset($this->pages); while ((list($name, ) = each($this->pages)) && (NULL == $next)) { if ($current == $name) { if (list($nname, ) = each($this->pages)) $next = $nname; break; } $back = $name; } if (NULL == $next) { end($this->pages); list($next, ) = each($this->pages); } return array($back, $next); } // end func getBackNext /** * Returns the name of the last page and it's validation status * * @return array [page name, validation status] */ function checkLast() { if ('' == $this->last_page) { reset($this->pages); list($this->last_page, ) = each($this->pages); } if (!isset($this->pages[$this->last_page])) { // removed page - you might not like this rule - subclass if neccessary reset($this->pages); list($this->last_page, ) = each($this->pages); } return array($this->last_page, $this->pages[$this->last_page]->isValid()); } // end func checkLast /** * Creates all buttons for a page no matter if all of them are visible. * * You should subclass this method to change to layout of your wizards. * * @param string page */ function createButtons($page) { if ('form' == $this->pages[$page]->type) // page contains a form we've to modify $this->forms[$page] = &$this->pages[$page]->getFormObject(); else // create a new form $this->forms[$page] = new form; // List of buttons (event => label) $events = array( 'back' => ' << Back ', 'next' => ' Next >> ', 'finish' => ' Finish ', 'help' => ' Help ', 'cancel' => ' Cancel ' ); reset($events); while (list($event, $label) = each($events)) $this->createButton(&$this->forms[$page], $page, $event, $label); } // end func createButtons /** * Adds a button to the given form object. * * @param object form * @param string used with the button name * @param string used with the button name * @param string label of the button */ function createButton(&$form, $page, $event, $label) { $form->addElement(array( 'name' => '_' . $page . '_' . $event, 'type' => 'submit', 'value' => $label ) ); // don't forget to remember the button so that we can find out which // event a request caused. Note getElementnamePrefix(). $this->buttons[$page][$event]['name'] = $form->getElementnamePrefix() . '_' . $page . '_' . $event; } // end func createButton } // end class form_wizard ?>
« previous php.pear.cvs (#1015) next »