From fdb8d77b680840866f44bc1515710db60af94fc5 Mon Sep 17 00:00:00 2001 From: Tom N Harris Date: Mon, 30 Jul 2007 23:50:07 +0200 Subject: New structured forms and action events Replaces a number of *FORM_INJECTION events with a more flexible way of modifying forms. Forms are created with a Doku_Form class (inc/form.php) that can be manipulated by plugins prior to output. Plugins register a HTML_{$name}FORM_OUTPUT event which can modify the form object prior to output. Available forms are: LOGIN DRAFT CONFLICT REGISTER UPDATEPROFILE EDIT RESENDPWD Documentation for the Doku_Form class is in inc/form.php. darcs-hash:20070730215007-6942e-a0cf08197f939e224a2b28c40aec5431b118ea94.gz --- _test/cases/inc/form_form.test.php | 97 +++++ inc/form.php | 831 +++++++++++++++++++++++++++++++++++++ inc/html.php | 395 +++++++----------- 3 files changed, 1078 insertions(+), 245 deletions(-) create mode 100644 _test/cases/inc/form_form.test.php create mode 100644 inc/form.php diff --git a/_test/cases/inc/form_form.test.php b/_test/cases/inc/form_form.test.php new file mode 100644 index 000000000..a409facda --- /dev/null +++ b/_test/cases/inc/form_form.test.php @@ -0,0 +1,97 @@ +startFieldset('Test'); + $form->addHidden('summary', 'changes &c'); + $form->addElement(form_makeTextField('t', 'v', 'Text', 'text__id', 'block')); + $form->addElement(form_makeCheckboxField('r', '1', 'Check', 'check__id', 'simple')); + $form->addElement(form_makeButton('submit', 'save', 'Save', array('accesskey'=>'s'))); + $form->addElement(form_makeButton('submit', 'cancel', 'Cancel')); + $form->endFieldset(); + return $form; + } + + function _realoutput() { + global $lang; + $realoutput = '
'; + $realoutput .= "\n"; + $realoutput .= '
'; + $realoutput .= "\n"; + $realoutput .= "
Test\n"; + $realoutput .= '
'; + $realoutput .= "\n"; + $realoutput .= ''; + $realoutput .= "\n"; + $realoutput .= ''; + $realoutput .= "\n"; + $realoutput .= ''; + $realoutput .= "\n"; + $realoutput .= "
\n
\n"; + return $realoutput; + } + + function test_form_print() { + $form = $this->_testform(); + ob_start(); + $form->printForm(); + $output = ob_get_contents(); + ob_end_clean(); + $this->assertEqual($output,$this->_realoutput()); + } + + function test_get_element_at() { + $form = $this->_testform(); + $e1 =& $form->getElementAt(1); + $this->assertEqual($e1, array('_elem'=>'textfield', + '_text'=>'Text', + '_class'=>'block', + 'id'=>'text__id', + 'name'=>'t', + 'value'=>'v')); + $e2 =& $form->getElementAt(99); + $this->assertEqual($e2, array('_elem'=>'closefieldset')); + } + + function test_find_element_by_type() { + $form = $this->_testform(); + $this->assertEqual($form->findElementByType('button'), 3); + $this->assertFalse($form->findElementByType('text')); + } + + function test_find_element_by_id() { + $form = $this->_testform(); + $this->assertEqual($form->findElementById('check__id'), 2); + $this->assertFalse($form->findElementById('dw__testform')); + } + + function test_find_element_by_attribute() { + $form = $this->_testform(); + $this->assertEqual($form->findElementByAttribute('value','Cancel'), 4); + $this->assertFalse($form->findElementByAttribute('name','cancel')); + } + + function test_close_fieldset() { + $form = new Doku_Form('dw__testform','/test'); + $form->startFieldset('Test'); + $form->addHidden('summary', 'changes &c'); + $form->addElement(form_makeTextField('t', 'v', 'Text', 'text__id', 'block')); + $form->addElement(form_makeCheckboxField('r', '1', 'Check', 'check__id', 'simple')); + $form->addElement(form_makeButton('submit', 'save', 'Save', array('accesskey'=>'s'))); + $form->addElement(form_makeButton('submit', 'cancel', 'Cancel')); + ob_start(); + $form->printForm(); + $output = ob_get_contents(); + ob_end_clean(); + $this->assertEqual($output,$this->_realoutput()); + } + +} diff --git a/inc/form.php b/inc/form.php new file mode 100644 index 000000000..fe5fc1152 --- /dev/null +++ b/inc/form.php @@ -0,0 +1,831 @@ + + */ + +if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); +if(!defined('NL')) define('NL',"\n"); +require_once(DOKU_INC.'inc/html.php'); + +/** + * Class for creating simple HTML forms. + * + * The forms is built from a list of pseudo-tags (arrays with expected keys). + * Every pseudo-tag must have the key '_elem' set to the name of the element. + * When printed, the form class calls functions named 'form_$type' for each + * element it contains. + * + * Standard practice is for non-attribute keys in a pseudo-element to start + * with '_'. Other keys are HTML attributes that will be included in the element + * tag. That way, the element output functions can pass the pseudo-element + * directly to buildAttributes. + * + * See the form_make* functions later in this file. + * + * @author Tom N Harris + */ +class Doku_Form { + + // Usually either DOKU_SCRIPT or wl($ID) + var $action = ''; + + // Most likely no need to change this + var $method = 'post'; + + // Form id attribute + var $id = ''; + + // Draw a border around form fields. + // Adds
around the elements + var $_infieldset = false; + + // Hidden form fields. + var $_hidden = array(); + + // Array of pseudo-tags + var $_content = array(); + + /** + * Constructor + * + * @param string $id ID attribute of the form. + * @param string $action (optional) submit URL, defaults to DOKU_SCRIPT + * @param string $method (optional) 'POST' or 'GET', default is post + * @author Tom N Harris + */ + function Doku_Form($id, $action=false, $method=false) { + $this->id = $id; + $this->action = ($action) ? $action : script(); + if ($method) $this->method = $method; + } + + /** + * startFieldset + * + * Add
tags around fields. + * Usually results in a border drawn around the form. + * + * @param string $legend Label that will be printed with the border. + * @author Tom N Harris + */ + function startFieldset($legend) { + if ($this->_infieldset) { + $this->addElement(array('_elem'=>'closefieldset')); + } + $this->addElement(array('_elem'=>'openfieldset', '_legend'=>$legend)); + $this->_infieldset = true; + } + + /** + * endFieldset + * + * @author Tom N Harris + */ + function endFieldset() { + if ($this->_infieldset) { + $this->addElement(array('_elem'=>'closefieldset')); + } + $this->_infieldset = false; + } + + /** + * addHidden + * + * Adds a name/value pair as a hidden field. + * The value of the field (but not the name) will be passed to + * formText() before printing. + * + * @param string $name Field name. + * @param string $value Field value. If null, remove a previously added field. + * @author Tom N Harris + */ + function addHidden($name, $value) { + if (is_null($value)) + unset($this->_hidden[$name]); + else + $this->_hidden[$name] = $value; + } + + /** + * addElement + * + * Appends a content element to the form. + * The element can be either a pseudo-tag or string. + * If string, it is printed without escaping special chars. * + * + * @param string $elem Pseudo-tag or string to add to the form. + * @author Tom N Harris + */ + function addElement($elem) { + $this->_content[] = $elem; + } + + /** + * insertElement + * + * Inserts a content element at a position. + * + * @param string $pos 0-based index where the element will be inserted. + * @param string $elem Pseudo-tag or string to add to the form. + * @author Tom N Harris + */ + function insertElement($pos, $elem) { + array_splice($this->_content, $pos, 0, array($elem)); + } + + /** + * replaceElement + * + * Replace with NULL to remove an element. + * + * @param int $pos 0-based index the element will be placed at. + * @param string $elem Pseudo-tag or string to add to the form. + * @author Tom N Harris + */ + function replaceElement($pos, $elem) { + $rep = array(); + if (!is_null($elem)) $rep[] = $elem; + array_splice($this->_content, $pos, 1, $rep); + } + + /** + * findElementByType + * + * Gets the position of the first of a type of element. + * + * @param string $type Element type to look for. + * @return array pseudo-element if found, false otherwise + * @author Tom N Harris + */ + function findElementByType($type) { + foreach ($this->_content as $pos=>$elem) { + if (is_array($elem) && $elem['_elem'] == $type) + return $pos; + } + return false; + } + + /** + * findElementById + * + * Gets the position of the element with an ID attribute. + * + * @param string $id ID of the element to find. + * @return array pseudo-element if found, false otherwise + * @author Tom N Harris + */ + function findElementById($id) { + foreach ($this->_content as $pos=>$elem) { + if (is_array($elem) && isset($elem['id']) && $elem['id'] == $id) + return $pos; + } + return false; + } + + /** + * findElementByAttribute + * + * Gets the position of the first element with a matching attribute value. + * + * @param string $name Attribute name. + * @param string $value Attribute value. + * @return array pseudo-element if found, false otherwise + * @author Tom N Harris + */ + function findElementByAttribute($name, $value) { + foreach ($this->_content as $pos=>$elem) { + if (is_array($elem) && isset($elem[$name]) && $elem[$name] == $value) + return $pos; + } + return false; + } + + /** + * getElementAt + * + * Returns a reference to the element at a position. + * A position out-of-bounds will return either the + * first (underflow) or last (overflow) element. + * + * @param int $pos 0-based index + * @return arrayreference pseudo-element + * @author Tom N Harris + */ + function &getElementAt($pos) { + if ($pos < 0) $pos = count($this->_content) + $pos; + if ($pos < 0) $pos = 0; + if ($pos >= count($this->_content)) $pos = count($this->_content) - 1; + return $this->_content[$pos]; + } + + /** + * printForm + * + * Output the form. + * Each element in the form will be passed to a function named + * 'form_$type'. The function should return the HTML to be printed. + * + * @author Tom N Harris + */ + function printForm() { + global $lang; + print '
id)) print ' id="'.$this->id.'"'; + print '>'.NL; + if (!empty($this->_hidden)) { + print '
'; + foreach ($this->_hidden as $name=>$value) + print form_hidden(array('name'=>$name, 'value'=>$value)); + print '
'.NL; + } + foreach ($this->_content as $element) { + if (is_array($element)) { + $elem_type = $element['_elem']; + if (function_exists('form_'.$elem_type)) { + print call_user_func('form_'.$elem_type, $element).NL; + } + } else { + print $element; + } + } + if ($this->_infieldset) print form_closefieldset().NL; + print '
'.NL; + } + +} + +/** + * form_makeTag + * + * Create a form element for a non-specific empty tag. + * + * @param string $tag Tag name. + * @param array $attrs Optional attributes. + * @return array pseudo-tag + * @author Tom N Harris + */ +function form_makeTag($tag, $attrs=array()) { + $elem = array('_elem'=>'tag', '_tag'=>$tag); + return array_merge($elem, $attrs); +} + +/** + * form_makeOpenTag + * + * Create a form element for a non-specific opening tag. + * Remember to put a matching close tag after this as well. + * + * @param string $tag Tag name. + * @param array $attrs Optional attributes. + * @return array pseudo-tag + * @author Tom N Harris + */ +function form_makeOpenTag($tag, $attrs=array()) { + $elem = array('_elem'=>'opentag', '_tag'=>$tag); + return array_merge($elem, $attrs); +} + +/** + * form_makeCloseTag + * + * Create a form element for a non-specific closing tag. + * Careless use of this will result in invalid XHTML. + * + * @param string $tag Tag name. + * @return array pseudo-tag + * @author Tom N Harris + */ +function form_makeCloseTag($tag) { + return array('_elem'=>'closetag', '_tag'=>$tag); +} + +/** + * form_makeWikiText + * + * Create a form element for a textarea containing wiki text. + * Only one wikitext element is allowed on a page. It will have + * a name of 'wikitext' and id 'wiki__text'. The text will + * be passed to formText() before printing. + * + * @param string $text Text to fill the field with. + * @param array $attrs Optional attributes. + * @return array pseudo-tag + * @author Tom N Harris + */ +function form_makeWikiText($text, $attrs=array()) { + $elem = array('_elem'=>'wikitext', '_text'=>$text); + return array_merge($elem, $attrs); +} + +/** + * form_makeButton + * + * Create a form element for an action button. + * A title will automatically be generated using the value and + * accesskey attributes, unless you provide one. + * + * @param string $type Type attribute. 'submit' or 'cancel' + * @param string $act Wiki action of the button, will be used as the do= parameter + * @param string $value (optional) Displayed label. Uses $act if not provided. + * @param array $attrs Optional attributes. + * @return array pseudo-tag + * @author Tom N Harris + */ +function form_makeButton($type, $act, $value='', $attrs=array()) { + if ($value == '') $value = $act; + //$name = (!empty($act)) ? 'do[$act]' : null; + $elem = array('_elem'=>'button', 'type'=>$type, '_action'=>$act, 'value'=>$value); + if (!empty($attrs['accesskey']) && empty($attrs['title'])) { + $attrs['title'] = $value . ' [ALT+'.strtoupper($attrs['accesskey']).']'; + } + return array_merge($elem, $attrs); +} + +/** + * form_makeField + * + * Create a form element for a labelled input element. + * The label text will be printed before the input. + * + * @param string $type Type attribute of input. + * @param string $name Name attribute of the input. + * @param string $value (optional) Default value. + * @param string $class Class attribute of the label. If this is 'block', + * then a line break will be added after the field. + * @param string $label Label that will be printed before the input. + * @param string $id ID attribute of the input. If set, the label will + * reference it with a 'for' attribute. + * @param array $attrs Optional attributes. + * @return array pseudo-tag + * @author Tom N Harris + */ +function form_makeField($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) { + if (is_null($label)) $label = $name; + $elem = array('_elem'=>'field', '_text'=>$label, '_class'=>$class, + 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value); + return array_merge($elem, $attrs); +} + +/** + * form_makeFieldRight + * + * Create a form element for a labelled input element. + * The label text will be printed after the input. + * + * @see form_makeField + * @author Tom N Harris + */ +function form_makeFieldRight($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) { + if (is_null($label)) $label = $name; + $elem = array('_elem'=>'fieldright', '_text'=>$label, '_class'=>$class, + 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value); + return array_merge($elem, $attrs); +} + +/** + * form_makeTextField + * + * Create a form element for a text input element with label. + * + * @see form_makeField + * @author Tom N Harris + */ +function form_makeTextField($name, $value='', $label=null, $id='', $class='', $attrs=array()) { + if (is_null($label)) $label = $name; + $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class, + 'id'=>$id, 'name'=>$name, 'value'=>$value); + return array_merge($elem, $attrs); +} + +/** + * form_makePasswordField + * + * Create a form element for a password input element with label. + * Password elements have no default value, for obvious reasons. + * + * @see form_makeField + * @author Tom N Harris + */ +function form_makePasswordField($name, $label=null, $id='', $class='', $attrs=array()) { + if (is_null($label)) $label = $name; + $elem = array('_elem'=>'passwordfield', '_text'=>$label, '_class'=>$class, + 'id'=>$id, 'name'=>$name); + return array_merge($elem, $attrs); +} + +/** + * form_makeCheckboxField + * + * Create a form element for a checkbox input element with label. + * + * @see form_makeFieldRight + * @author Tom N Harris + */ +function form_makeCheckboxField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) { + if (is_null($label)) $label = $name; + if (is_null($value) || $value=='') $value='0'; + $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class, + 'id'=>$id, 'name'=>$name, 'value'=>$value); + return array_merge($elem, $attrs); +} + +/** + * form_makeRadioField + * + * Create a form element for a radio button input element with label. + * + * @see form_makeFieldRight + * @author Tom N Harris + */ +function form_makeRadioField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) { + if (is_null($label)) $label = $name; + if (is_null($value) || $value=='') $value='0'; + $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class, + 'id'=>$id, 'name'=>$name, 'value'=>$value); + return array_merge($elem, $attrs); +} + +/** + * form_makeMenuField + * + * Create a form element for a drop-down menu with label. + * The list of values can be strings, arrays of (value,text), + * or an associative array with the values as keys and labels as values. + * An item is selected by supplying its value or integer index. + * If the list of values is an associative array, the selected item must be + * a string. + * + * @author Tom N Harris + */ +function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) { + if (is_null($label)) $label = $name; + $options = array(); + reset($values); + // FIXME: php doesn't know the difference between a string and an integer + if (is_string(key($values))) { + foreach ($values as $val=>$text) { + $options[] = array($val,$text, (!is_null($selected) && $val==$selected)); + } + } else { + if (is_integer($selected)) $selected = $values[$selected]; + foreach ($values as $val) { + if (is_array($val)) + @list($val,$text) = $val; + else + $text = null; + $options[] = array($val,$text,$val===$selected); + } + } + $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class, + 'id'=>$id, 'name'=>$name); + return array_merge($elem, $attrs); +} + +/** + * form_makeListboxField + * + * Create a form element for a list box with label. + * The list of values can be strings, arrays of (value,text), + * or an associative array with the values as keys and labels as values. + * Items are selected by supplying its value or an array of values. + * + * @author Tom N Harris + */ +function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) { + if (is_null($label)) $label = $name; + $options = array(); + reset($values); + if (is_null($selected) || $selected == '') + $selected = array(); + elseif (!is_array($selected)) + $selected = array($selected); + // FIXME: php doesn't know the difference between a string and an integer + if (is_string(key($values))) { + foreach ($values as $val=>$text) { + $options[] = array($val,$text,in_array($val,$selected)); + } + } else { + foreach ($values as $val) { + if (is_array($val)) + @list($val,$text) = $val; + else + $text = null; + $options[] = array($val,$text,in_array($val,$selected)); + } + } + $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class, + 'id'=>$id, 'name'=>$name); + return array_merge($elem, $attrs); +} + +/** + * form_tag + * + * Print the HTML for a generic empty tag. + * Requires '_tag' key with name of the tag. + * Attributes are passed to buildAttributes() + * + * @author Tom N Harris + */ +function form_tag($attrs) { + return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'/>'; +} + +/** + * form_opentag + * + * Print the HTML for a generic opening tag. + * Requires '_tag' key with name of the tag. + * Attributes are passed to buildAttributes() + * + * @author Tom N Harris + */ +function form_opentag($attrs) { + return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'>'; +} + +/** + * form_closetag + * + * Print the HTML for a generic closing tag. + * Requires '_tag' key with name of the tag. + * There are no attributes. + * + * @author Tom N Harris + */ +function form_closetag($attrs) { + return ''; +} + +/** + * form_openfieldset + * + * Print the HTML for an opening fieldset tag. + * Uses the '_legend' key. + * Attributes are passed to buildAttributes() + * + * @author Tom N Harris + */ +function form_openfieldset($attrs) { + $s = '
'; + if (!is_null($attrs['_legend'])) $s .= ''.$attrs['_legend'].''; + return $s; +} + +/** + * form_closefieldset + * + * Print the HTML for a closing fieldset tag. + * There are no attributes. + * + * @author Tom N Harris + */ +function form_closefieldset() { + return '
'; +} + +/** + * form_hidden + * + * Print the HTML for a hidden input element. + * Uses only 'name' and 'value' attributes. + * Value is passed to formText() + * + * @author Tom N Harris + */ +function form_hidden($attrs) { + return ''; +} + +/** + * form_wikitext + * + * Print the HTML for the wiki textarea. + * Requires '_text' with default text of the field. + * Text will be passed to formText(), attributes to buildAttributes() + * + * @author Tom N Harris + */ +function form_wikitext($attrs) { + return ''; +} + +/** + * form_button + * + * Print the HTML for a form button. + * If '_action' is set, the button name will be "do[_action]". + * Other attributes are passed to buildAttributes() + * + * @author Tom N Harris + */ +function form_button($attrs) { + $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : ''; + return ''; +} + +/** + * form_field + * + * Print the HTML for a form input field. + * _class : class attribute used on the label tag + * _text : Text to display before the input. Not escaped. + * Other attributes are passed to buildAttributes() for the input tag. + * + * @author Tom N Harris + */ +function form_field($attrs) { + $s = ''; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '
'; + return $s; +} + +/** + * form_fieldright + * + * Print the HTML for a form input field. (right-aligned) + * _class : class attribute used on the label tag + * _text : Text to display after the input. Not escaped. + * Other attributes are passed to buildAttributes() for the input tag. + * + * @author Tom N Harris + */ +function form_fieldright($attrs) { + $s = ''; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '
'; + return $s; +} + +/** + * form_textfield + * + * Print the HTML for a text input field. + * _class : class attribute used on the label tag + * _text : Text to display before the input. Not escaped. + * Other attributes are passed to buildAttributes() for the input tag. + * + * @author Tom N Harris + */ +function form_textfield($attrs) { + $s = ''; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '
'; + return $s; +} + +/** + * form_passwordfield + * + * Print the HTML for a password input field. + * _class : class attribute used on the label tag + * _text : Text to display before the input. Not escaped. + * Other attributes are passed to buildAttributes() for the input tag. + * + * @author Tom N Harris + */ +function form_passwordfield($attrs) { + $s = ''; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '
'; + return $s; +} + +/** + * form_checkboxfield + * + * Print the HTML for a checkbox input field. + * _class : class attribute used on the label tag + * _text : Text to display after the input. Not escaped. + * Other attributes are passed to buildAttributes() for the input tag. + * + * @author Tom N Harris + */ +function form_checkboxfield($attrs) { + $s = ''; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '
'; + return $s; +} + +/** + * form_radiofield + * + * Print the HTML for a radio button input field. + * _class : class attribute used on the label tag + * _text : Text to display after the input. Not escaped. + * Other attributes are passed to buildAttributes() for the input tag. + * + * @author Tom N Harris + */ +function form_radiofield($attrs) { + $s = ''; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '
'; + return $s; +} + +/** + * form_menufield + * + * Print the HTML for a drop-down menu. + * _options : Array of (value,text,selected) for the menu. + * Text can be omitted. Text and value are passed to formText() + * Only one item can be selected. + * _class : class attribute used on the label tag + * _text : Text to display before the menu. Not escaped. + * Other attributes are passed to buildAttributes() for the input tag. + * + * @author Tom N Harris + */ +function form_menufield($attrs) { + $attrs['size'] = '1'; + $s = ''; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '
'; + return $s; +} + +/** + * form_listboxfield + * + * Print the HTML for a list box. + * _options : Array of (value,text,selected) for the list. + * Text can be omitted. Text and value are passed to formText() + * _class : class attribute used on the label tag + * _text : Text to display before the menu. Not escaped. + * Other attributes are passed to buildAttributes() for the input tag. + * + * @author Tom N Harris + */ +function form_listboxfield($attrs) { + $s = ''; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '
'; + return $s; +} diff --git a/inc/html.php b/inc/html.php index 0c9660fa2..ff2194f2b 100644 --- a/inc/html.php +++ b/inc/html.php @@ -9,6 +9,7 @@ if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); if(!defined('NL')) define('NL',"\n"); require_once(DOKU_INC.'inc/parserutils.php'); +require_once(DOKU_INC.'inc/form.php'); /** * Convenience function to quickly build a wikilink @@ -42,7 +43,6 @@ function html_attbuild($attributes){ * The loginform * * @author Andreas Gohr - * @triggers HTML_LOGINFORM_INJECTION */ function html_login(){ global $lang; @@ -51,59 +51,32 @@ function html_login(){ global $auth; print p_locale_xhtml('login'); - ?> -
-
-
- - - -
-
- - - - - -
-
- canDo('addUser') && actionOK('register')){ - print '

'; - print $lang['reghere']; - print ': '.$lang['register'].''; - print '

'; - } + print '
'.NL; + $form = new Doku_Form('dw__login'); + $form->startFieldset($lang['btn_login']); + $form->addHidden('id', $ID); + $form->addHidden('do', 'login'); + $form->addElement(form_makeTextField('u', $_REQUEST['u'], $lang['user'], 'focus__this', 'block')); + $form->addElement(form_makePasswordField('p', $lang['pass'], '', 'block')); + $form->addElement(form_makeCheckboxField('r', '1', $lang['remember'], 'remember__me', 'simple')); + $form->addElement(form_makeButton('submit', '', $lang['btn_login'])); + $form->endFieldset(); + html_form('login', $form); + + if($auth && $auth->canDo('addUser') && actionOK('register')){ + print '

'; + print $lang['reghere']; + print ': '.$lang['register'].''; + print '

'; + } - if ($auth && $auth->canDo('modPass') && actionOK('resendpwd')) { - print '

'; - print $lang['pwdforget']; - print ': '.$lang['btn_resendpwd'].''; - print '

'; - } - ?> -
- canDo('modPass') && actionOK('resendpwd')) { + print '

'; + print $lang['pwdforget']; + print ': '.$lang['btn_resendpwd'].''; + print '

'; } -*/ + print '
'.NL; } /** @@ -264,21 +237,18 @@ function html_draft(){ $draft = unserialize(io_readFile($INFO['draft'],false)); $text = cleanText(con($draft['prefix'],$draft['text'],$draft['suffix'],true)); - echo p_locale_xhtml('draft'); - ?> -
- -
- - -
- - - - -
- addHidden('id', $ID); + $form->addHidden('date', $draft['date']); + $form->addElement(form_makeWikiText($text, array('readonly'=>'readonly'))); + $form->addElement(form_makeOpenTag('div', array('id'=>'draft__status'))); + $form->addElement($lang['draftdate'].' '. date($conf['dformat'],filemtime($INFO['draft']))); + $form->addElement(form_makeCloseTag('div')); + $form->addElement(form_makeButton('submit', 'recover', $lang['btn_recover'], array('tabindex'=>'1'))); + $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_draftdel'], array('tabindex'=>'2'))); + $form->addElement(form_makeButton('submit', 'show', $lang['btn_cancel'], array('tabindex'=>'3'))); + html_form('draft', $form); } /** @@ -886,19 +856,14 @@ function html_conflict($text,$summary){ global $lang; print p_locale_xhtml('conflict'); - ?> -
-
- - - - - - -
-
-



- addHidden('id', $ID); + $form->addHidden('wikitext', $text); + $form->addHidden('summary', $summary); + $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('accesskey'=>'s'))); + $form->addElement(form_makeButton('submit', 'cancel', $lang['btn_cancel'])); + html_form('conflict', $form); + print '



'.NL; } /** @@ -922,7 +887,6 @@ function html_msgarea(){ * Prints the registration form * * @author Andreas Gohr - * @triggers HTML_REGISTERFORM_INJECTION */ function html_register(){ global $lang; @@ -930,51 +894,23 @@ function html_register(){ global $ID; print p_locale_xhtml('register'); -?> -
-
-
- - - - -
- - -
-
- - -
-
- - -
-
-
-'.NL; + $form = new Doku_Form('dw__register', wl($ID)); + $form->startFieldset($lang['register']); + $form->addHidden('do', 'register'); + $form->addHidden('save', '1'); + $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], null, 'block', array('size'=>'50'))); + if (!$conf['autopasswd']) { + $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', array('size'=>'50'))); + $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50'))); + } + $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', array('size'=>'50'))); + $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', array('size'=>'50'))); + $form->addElement(form_makeButton('submit', '', $lang['register'])); + $form->endFieldset(); + html_form('register', $form); + + print ''.NL; } /** @@ -994,55 +930,30 @@ function html_updateprofile(){ if (empty($_POST['fullname'])) $_POST['fullname'] = $INFO['userinfo']['name']; if (empty($_POST['email'])) $_POST['email'] = $INFO['userinfo']['mail']; -?> -
-
-
- - - - -
-
-

- - canDo('modPass')) { ?> -
-
- - - -
-
- - - - -
-
-
-'.NL; + $form = new Doku_Form('dw__register', wl($ID)); + $form->startFieldset($lang['profile']); + $form->addHidden('do', 'profile'); + $form->addHidden('save', '1'); + $form->addElement(form_makeTextField('fullname', $_SERVER['REMOTE_USER'], $lang['user'], '', 'block', array('size'=>'50', 'disabled'=>'disabled'))); + $attr = array('size'=>'50'); + if (!$auth->canDo('modName')) $attr['disabled'] = 'disabled'; + $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', $attr)); + $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', $attr)); + $form->addElement(form_makeTag('br')); + if ($auth->canDo('modPass')) { + $form->addElement(form_makePasswordField('newpass', $lang['newpass'], '', 'block', array('size'=>'50'))); + $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50'))); + } + if ($conf['profileconfirm']) { + $form->addElement(form_makeTag('br')); + $form->addElement(form_makePasswordField('oldpass', $lang['oldpass'], '', 'block', array('size'=>'50'))); + } + $form->addElement(form_makeButton('submit', '', $lang['btn_save'])); + $form->addElement(form_makeButton('reset', '', $lang['btn_reset'])); + $form->endFieldset(); + html_form('updateprofile', $form); + print ''.NL; } /** @@ -1108,7 +1019,6 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed? if($wr){ if ($REV) print p_locale_xhtml('editrev'); print p_locale_xhtml($include); - $ro=false; }else{ // check pseudo action 'source' if(!actionOK('source')){ @@ -1116,7 +1026,6 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed? return; } print p_locale_xhtml('read'); - $ro='readonly="readonly"'; } if(!$DATE) $DATE = $INFO['lastmod']; @@ -1126,7 +1035,7 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed?
-
@@ -1139,44 +1048,35 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed?
- - -
- - - - - - -
- - - - $wr); - trigger_event('HTML_EDITFORM_INJECTION', $evdata); - ?> - -
-
- -
- - - -
- - -
- - - -
- -
-
- addHidden('id', $ID); + $form->addHidden('rev', $REV); + $form->addHidden('date', $DATE); + $form->addHidden('prefix', $PRE); + $form->addHidden('suffix', $SUF); + $form->addHidden('changecheck', $check); + $attr = array('tabindex'=>'1'); + if (!$wr) $attr['readonly'] = 'readonly'; + $form->addElement(form_makeWikiText($text, $attr)); + $form->addElement(form_makeOpenTag('div', array('id'=>'wiki__editbar'))); + $form->addElement(form_makeOpenTag('div', array('id'=>'size__ctl'))); + $form->addElement(form_makeCloseTag('div')); + if ($wr) { + $form->addElement(form_makeOpenTag('div', array('class'=>'editButtons'))); + $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('id'=>'edbtn__save', 'accesskey'=>'s', 'tabindex'=>'4'))); + $form->addElement(form_makeButton('submit', 'preview', $lang['btn_preview'], array('id'=>'edbtn__preview', 'accesskey'=>'p', 'tabindex'=>'5'))); + $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_cancel'], array('tabindex'=>'6'))); + $form->addElement(form_makeCloseTag('div')); + $form->addElement(form_makeOpenTag('div', array('class'=>'summary'))); + $form->addElement(form_makeTextField('summary', $SUM, $lang['summary'], 'edit__summary', 'nowrap', array('size'=>'50', 'tabindex'=>'2'))); + $elem = html_minoredit(); + if ($elem) $form->addElement($elem); + $form->addElement(form_makeCloseTag('div')); + } + $form->addElement(form_makeCloseTag('div')); + html_form('edit', $form); + print ''.NL; } /** @@ -1189,24 +1089,13 @@ function html_minoredit(){ global $lang; // minor edits are for logged in users only if(!$conf['useacl'] || !$_SERVER['REMOTE_USER']){ - return; + return false; } $p = array(); - $p['name'] = 'minor'; - $p['type'] = 'checkbox'; - $p['id'] = 'minoredit'; $p['tabindex'] = 3; - $p['value'] = '1'; if(!empty($_REQUEST['minor'])) $p['checked']='checked'; - $att = buildAttributes($p); - - print ''; - print ""; - print ''; - print ''; + return form_makeCheckboxField('minor', '1', $lang['minoredit'], 'minoredit', 'nowrap', $p); } /** @@ -1341,23 +1230,39 @@ function html_resendpwd() { global $ID; print p_locale_xhtml('resendpwd'); -?> -
-
-
-
- - - -
- -
-
-
-'.NL; + $form = new Doku_Form('dw__resendpwd', wl($ID)); + $form->startFieldset($lang['resendpwd']); + $form->addHidden('do', 'resendpwd'); + $form->addHidden('save', '1'); + $form->addElement(form_makeTag('br')); + $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], '', 'block')); + $form->addElement(form_makeTag('br')); + $form->addElement(form_makeTag('br')); + $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd'])); + $form->endFieldset(); + html_form('resendpwd', $form); + print ''.NL; +} + +/** + * Output a Doku_Form object. + * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT + * + * @author Tom N Harris + */ +function html_form($name, &$form) { + // Safety check in case the caller forgets. + $form->endFieldset(); + trigger_event('HTML_'.strtoupper($name).'FORM_OUTPUT', $form, 'html_form_output', false); +} + +/** + * Form print function. + * Just calls printForm() on the data object. + */ +function html_form_output($data) { + $data->printForm(); } //Setup VIM: ex: et ts=2 enc=utf-8 : -- cgit v1.2.3