diff options
author | Tom N Harris <tnharris@whoopdedo.org> | 2007-07-30 23:50:07 +0200 |
---|---|---|
committer | Tom N Harris <tnharris@whoopdedo.org> | 2007-07-30 23:50:07 +0200 |
commit | fdb8d77b680840866f44bc1515710db60af94fc5 (patch) | |
tree | 5a32f32e3ceb23e8951f177a2fc2bff18105a290 /inc | |
parent | 955cd091f176d552da7e760d5c61ba1e8d692a03 (diff) | |
download | rpg-fdb8d77b680840866f44bc1515710db60af94fc5.tar.gz rpg-fdb8d77b680840866f44bc1515710db60af94fc5.tar.bz2 |
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
Diffstat (limited to 'inc')
-rw-r--r-- | inc/form.php | 831 | ||||
-rw-r--r-- | inc/html.php | 395 |
2 files changed, 981 insertions, 245 deletions
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 @@ +<?php +/** + * DokuWiki XHTML Form + * + * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) + * @author Tom N Harris <tnharris@whoopdedo.org> + */ + +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 <tnharris@whoopdedo.org> + */ +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 <fieldset></fieldset> 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 <tnharris@whoopdedo.org> + */ + function Doku_Form($id, $action=false, $method=false) { + $this->id = $id; + $this->action = ($action) ? $action : script(); + if ($method) $this->method = $method; + } + + /** + * startFieldset + * + * Add <fieldset></fieldset> 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 <tnharris@whoopdedo.org> + */ + 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 <tnharris@whoopdedo.org> + */ + 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 <tnharris@whoopdedo.org> + */ + 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 <tnharris@whoopdedo.org> + */ + 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 <tnharris@whoopdedo.org> + */ + 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 <tnharris@whoopdedo.org> + */ + 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 <tnharris@whoopdedo.org> + */ + 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 <tnharris@whoopdedo.org> + */ + 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 <tnharris@whoopdedo.org> + */ + 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 <tnharris@whoopdedo.org> + */ + 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 <tnharris@whoopdedo.org> + */ + function printForm() { + global $lang; + print '<form action="'.$this->action.'" method="'.$this->method.'" accept-charset="'.$lang['encoding'].'"'; + if (!empty($this->id)) print ' id="'.$this->id.'"'; + print '>'.NL; + if (!empty($this->_hidden)) { + print '<div class="no">'; + foreach ($this->_hidden as $name=>$value) + print form_hidden(array('name'=>$name, 'value'=>$value)); + print '</div>'.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 '</form>'.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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +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 <tnharris@whoopdedo.org> + */ +function form_closetag($attrs) { + return '</'.$attrs['_tag'].'>'; +} + +/** + * form_openfieldset + * + * Print the HTML for an opening fieldset tag. + * Uses the '_legend' key. + * Attributes are passed to buildAttributes() + * + * @author Tom N Harris <tnharris@whoopdedo.org> + */ +function form_openfieldset($attrs) { + $s = '<fieldset '.buildAttributes($attrs,true).'>'; + if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>'; + return $s; +} + +/** + * form_closefieldset + * + * Print the HTML for a closing fieldset tag. + * There are no attributes. + * + * @author Tom N Harris <tnharris@whoopdedo.org> + */ +function form_closefieldset() { + return '</fieldset>'; +} + +/** + * 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 <tnharris@whoopdedo.org> + */ +function form_hidden($attrs) { + return '<input type="hidden" name="'.$attrs['name'].'" value="'.formText($attrs['value']).'" />'; +} + +/** + * 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 <tnharris@whoopdedo.org> + */ +function form_wikitext($attrs) { + return '<textarea name="wikitext" id="wiki__text" cols="80" rows="10" class="edit" ' + .buildAttributes($attrs,true).'>'.NL + .formText($attrs['_text']) + .'</textarea>'; +} + +/** + * 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 <tnharris@whoopdedo.org> + */ +function form_button($attrs) { + $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : ''; + return '<input class="button" '.$p.buildAttributes($attrs,true).'/>'; +} + +/** + * 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 <tnharris@whoopdedo.org> + */ +function form_field($attrs) { + $s = '<label class="'.$attrs['_class'].'"'; + if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; + $s .= '><span>'.$attrs['_text'].'</span>'; + $s .= '<input '.buildAttributes($attrs,true).'/></label>'; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '<br />'; + 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 <tnharris@whoopdedo.org> + */ +function form_fieldright($attrs) { + $s = '<label class="'.$attrs['_class'].'"'; + if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; + $s .= '><input '.buildAttributes($attrs,true).'/>'; + $s .= '<span>'.$attrs['_text'].'</span></label>'; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '<br />'; + 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 <tnharris@whoopdedo.org> + */ +function form_textfield($attrs) { + $s = '<label class="'.$attrs['_class'].'"'; + if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; + $s .= '><span>'.$attrs['_text'].'</span>'; + $s .= '<input type="text" class="edit" '.buildAttributes($attrs,true).'/></label>'; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '<br />'; + 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 <tnharris@whoopdedo.org> + */ +function form_passwordfield($attrs) { + $s = '<label class="'.$attrs['_class'].'"'; + if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; + $s .= '><span>'.$attrs['_text'].'</span>'; + $s .= '<input type="password" class="edit" '.buildAttributes($attrs,true).'/></label>'; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '<br />'; + 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 <tnharris@whoopdedo.org> + */ +function form_checkboxfield($attrs) { + $s = '<label class="'.$attrs['_class'].'"'; + if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; + $s .= '><input type="checkbox" '.buildAttributes($attrs,true).'/>'; + $s .= '<span>'.$attrs['_text'].'</span></label>'; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '<br />'; + 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 <tnharris@whoopdedo.org> + */ +function form_radiofield($attrs) { + $s = '<label class="'.$attrs['_class'].'"'; + if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; + $s .= '><input type="radio" '.buildAttributes($attrs,true).'/>'; + $s .= '<span>'.$attrs['_text'].'</span></label>'; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '<br />'; + 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 <tnharris@whoopdedo.org> + */ +function form_menufield($attrs) { + $attrs['size'] = '1'; + $s = '<label class="'.$attrs['_class'].'"'; + if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; + $s .= '><span>'.$attrs['_text'].'</span>'; + $s .= '<select '.buildAttributes($attrs,true).'>'.NL; + if (!empty($attrs['_options'])) { + $selected = false; + for($n=0;$n<count($attrs['_options']);$n++){ + @list($value,$text,$select) = $attrs['_options'][$n]; + $p = ''; + if (!is_null($text)) + $p .= ' value="'.formText($value).'"'; + else + $text = $value; + if (!empty($select) && !$selected) { + $p .= ' selected="selected"'; + $selected = true; + } + $s .= '<option'.$p.'>'.formText($text).'</option>'; + } + } else { + $s .= '<option></option>'; + } + $s .= NL.'</select></label>'; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '<br />'; + 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 <tnharris@whoopdedo.org> + */ +function form_listboxfield($attrs) { + $s = '<label class="'.$attrs['_class'].'"'; + if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; + $s .= '><span>'.$attrs['_text'].'</span>'; + $s = '<select '.buildAttributes($attrs,true).'>'.NL; + if (!empty($attrs['_options'])) { + foreach ($attrs['_options'] as $opt) { + @list($value,$text,$select) = $opt; + $p = ''; + if (!is_null($text)) + $p .= ' value="'.formText($value).'"'; + else + $text = $value; + if (!empty($select)) $p .= ' selected="selected"'; + $s .= '<option'.$p.'>'.formText($text).'</option>'; + } + } else { + $s .= '<option></option>'; + } + $s .= NL.'</select></label>'; + if (preg_match('/(^| )block($| )/', $attrs['_class'])) + $s .= '<br />'; + 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 <andi@splitbrain.org> - * @triggers HTML_LOGINFORM_INJECTION */ function html_login(){ global $lang; @@ -51,59 +51,32 @@ function html_login(){ global $auth; print p_locale_xhtml('login'); - ?> - <div class="centeralign"> - <form action="<?php echo script()?>" accept-charset="<?php echo $lang['encoding']?>" - method="post" id="dw__login"> - <fieldset> - <legend><?php echo $lang['btn_login']?></legend> - <input type="hidden" name="id" value="<?php echo $ID?>" /> - <input type="hidden" name="do" value="login" /> - <label class="block"> - <span><?php echo $lang['user']?></span> - <input type="text" name="u" value="<?php echo formText($_REQUEST['u'])?>" - class="edit" id="focus__this" /> - </label><br /> - <label class="block"> - <span><?php echo $lang['pass']?></span> - <input type="password" name="p" class="edit" /> - </label><br /> - - <?php //bad and dirty event insert hook - $evdata = array(); - trigger_event('HTML_LOGINFORM_INJECTION', $evdata); - ?> - - <label for="remember__me" class="simple"> - <input type="checkbox" name="r" id="remember__me" value="1" /> - <span><?php echo $lang['remember']?></span> - </label> - <input type="submit" value="<?php echo $lang['btn_login']?>" class="button" /> - </fieldset> - </form> - <?php - if($auth && $auth->canDo('addUser') && actionOK('register')){ - print '<p>'; - print $lang['reghere']; - print ': <a href="'.wl($ID,'do=register').'" rel="nofollow" class="wikilink1">'.$lang['register'].'</a>'; - print '</p>'; - } + print '<div class="centeralign">'.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 '<p>'; + print $lang['reghere']; + print ': <a href="'.wl($ID,'do=register').'" rel="nofollow" class="wikilink1">'.$lang['register'].'</a>'; + print '</p>'; + } - if ($auth && $auth->canDo('modPass') && actionOK('resendpwd')) { - print '<p>'; - print $lang['pwdforget']; - print ': <a href="'.wl($ID,'do=resendpwd').'" rel="nofollow" class="wikilink1">'.$lang['btn_resendpwd'].'</a>'; - print '</p>'; - } - ?> - </div> - <?php -/* - FIXME provide new hook - if(@file_exists('includes/login.txt')){ - print io_cacheParse('includes/login.txt'); + if ($auth && $auth->canDo('modPass') && actionOK('resendpwd')) { + print '<p>'; + print $lang['pwdforget']; + print ': <a href="'.wl($ID,'do=resendpwd').'" rel="nofollow" class="wikilink1">'.$lang['btn_resendpwd'].'</a>'; + print '</p>'; } -*/ + print '</div>'.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'); - ?> - <form id="dw__editform" method="post" action="<?php echo script()?>" - accept-charset="<?php echo $lang['encoding']?>"><div class="no"> - <input type="hidden" name="id" value="<?php echo $ID?>" /> - <input type="hidden" name="date" value="<?php echo $draft['date']?>" /></div> - <textarea name="wikitext" id="wiki__text" readonly="readonly" cols="80" rows="10" class="edit"><?php echo "\n".formText($text)?></textarea> - - <div id="draft__status"><?php echo $lang['draftdate'].' '.date($conf['dformat'],filemtime($INFO['draft']))?></div> - - <input class="button" type="submit" name="do[recover]" value="<?php echo $lang['btn_recover']?>" tabindex="1" /> - <input class="button" type="submit" name="do[draftdel]" value="<?php echo $lang['btn_draftdel']?>" tabindex="2" /> - <input class="button" type="submit" name="do[show]" value="<?php echo $lang['btn_cancel']?>" tabindex="3" /> - </form> - <?php + print p_locale_xhtml('draft'); + $form = new Doku_Form('dw__editform'); + $form->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'); - ?> - <form id="dw__editform" method="post" action="<?php echo script()?>" accept-charset="<?php echo $lang['encoding']?>"> - <div class="centeralign"> - <input type="hidden" name="id" value="<?php echo $ID?>" /> - <input type="hidden" name="wikitext" value="<?php echo formText($text)?>" /> - <input type="hidden" name="summary" value="<?php echo formText($summary)?>" /> - - <input class="button" type="submit" name="do[save]" value="<?php echo $lang['btn_save']?>" accesskey="s" title="<?php echo $lang['btn_save']?> [ALT+S]" /> - <input class="button" type="submit" name="do[cancel]" value="<?php echo $lang['btn_cancel']?>" /> - </div> - </form> - <br /><br /><br /><br /> - <?php + $form = new Doku_Form('dw__editform'); + $form->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 '<br /><br /><br /><br />'.NL; } /** @@ -922,7 +887,6 @@ function html_msgarea(){ * Prints the registration form * * @author Andreas Gohr <andi@splitbrain.org> - * @triggers HTML_REGISTERFORM_INJECTION */ function html_register(){ global $lang; @@ -930,51 +894,23 @@ function html_register(){ global $ID; print p_locale_xhtml('register'); -?> - <div class="centeralign"> - <form id="dw__register" method="post" action="<?php echo wl($ID)?>" accept-charset="<?php echo $lang['encoding']?>"> - <fieldset> - <input type="hidden" name="do" value="register" /> - <input type="hidden" name="save" value="1" /> - - <legend><?php echo $lang['register']?></legend> - <label class="block"> - <?php echo $lang['user']?> - <input type="text" name="login" class="edit" size="50" value="<?php echo formText($_POST['login'])?>" /> - </label><br /> - - <?php - if (!$conf['autopasswd']) { - ?> - <label class="block"> - <?php echo $lang['pass']?> - <input type="password" name="pass" class="edit" size="50" /> - </label><br /> - <label class="block"> - <?php echo $lang['passchk']?> - <input type="password" name="passchk" class="edit" size="50" /> - </label><br /> - <?php - } - ?> - - <label class="block"> - <?php echo $lang['fullname']?> - <input type="text" name="fullname" class="edit" size="50" value="<?php echo formText($_POST['fullname'])?>" /> - </label><br /> - <label class="block"> - <?php echo $lang['email']?> - <input type="text" name="email" class="edit" size="50" value="<?php echo formText($_POST['email'])?>" /> - </label><br /> - <?php //bad and dirty event insert hook - $evdata = array(); - trigger_event('HTML_REGISTERFORM_INJECTION', $evdata); - ?> - <input type="submit" class="button" value="<?php echo $lang['register']?>" /> - </fieldset> - </form> - </div> -<?php + print '<div class="centeralign">'.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 '</div>'.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']; -?> - <div class="centeralign"> - <form id="dw__register" method="post" action="<?php echo wl($ID)?>" accept-charset="<?php echo $lang['encoding']?>"> - <fieldset style="width: 80%;"> - <input type="hidden" name="do" value="profile" /> - <input type="hidden" name="save" value="1" /> - - <legend><?php echo $lang['profile']?></legend> - <label class="block"> - <?php echo $lang['user']?> - <input type="text" name="fullname" disabled="disabled" class="edit" size="50" value="<?php echo formText($_SERVER['REMOTE_USER'])?>" /> - </label><br /> - <label class="block"> - <?php echo $lang['fullname']?> - <input type="text" name="fullname" <?php if(!$auth->canDo('modName')) echo 'disabled="disabled"'?> class="edit" size="50" value="<?php echo formText($_POST['fullname'])?>" /> - </label><br /> - <label class="block"> - <?php echo $lang['email']?> - <input type="text" name="email" <?php if(!$auth->canDo('modName')) echo 'disabled="disabled"'?> class="edit" size="50" value="<?php echo formText($_POST['email'])?>" /> - </label><br /><br /> - - <?php if($auth->canDo('modPass')) { ?> - <label class="block"> - <?php echo $lang['newpass']?> - <input type="password" name="newpass" class="edit" size="50" /> - </label><br /> - <label class="block"> - <?php echo $lang['passchk']?> - <input type="password" name="passchk" class="edit" size="50" /> - </label><br /> - <?php } ?> - - <?php if ($conf['profileconfirm']) { ?> - <br /> - <label class="block"> - <?php echo $lang['oldpass']?> - <input type="password" name="oldpass" class="edit" size="50" /> - </label><br /> - <?php } ?> - <?php //bad and dirty event insert hook - $evdata = array(); - trigger_event('HTML_PROFILEFORM_INJECTION', $evdata); - ?> - <input type="submit" class="button" value="<?php echo $lang['btn_save']?>" /> - <input type="reset" class="button" value="<?php echo $lang['btn_reset']?>" /> - </fieldset> - </form> - </div> -<?php + print '<div class="centeralign">'.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 '</div>'.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? <div class="toolbar"> <div id="draft__status"><?php if(!empty($INFO['draft'])) echo $lang['draftdate'].' '.date($conf['dformat']);?></div> - <div id="tool__bar"><?php if(!$ro){?><a href="<?php echo DOKU_BASE?>lib/exe/mediamanager.php?ns=<?php echo $INFO['namespace']?>" + <div id="tool__bar"><?php if($wr){?><a href="<?php echo DOKU_BASE?>lib/exe/mediamanager.php?ns=<?php echo $INFO['namespace']?>" target="_blank"><?php echo $lang['mediaselect'] ?></a><?php }?></div> <?php if($wr){?> @@ -1139,44 +1048,35 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed? <?php } ?> </div> <div id="spell__result"></div> - - - <form id="dw__editform" method="post" action="<?php echo script()?>" accept-charset="<?php echo $lang['encoding']?>"><div class="no"> - <input type="hidden" name="id" value="<?php echo $ID?>" /> - <input type="hidden" name="rev" value="<?php echo $REV?>" /> - <input type="hidden" name="date" value="<?php echo $DATE?>" /> - <input type="hidden" name="prefix" value="<?php echo formText($PRE)?>" /> - <input type="hidden" name="suffix" value="<?php echo formText($SUF)?>" /> - <input type="hidden" name="changecheck" value="<?php echo $check?>" /> - </div> - - <textarea name="wikitext" id="wiki__text" <?php echo $ro?> cols="80" rows="10" class="edit" tabindex="1"><?php echo "\n".formText($text)?></textarea> - - <?php //bad and dirty event insert hook - $evdata = array('writable' => $wr); - trigger_event('HTML_EDITFORM_INJECTION', $evdata); - ?> - - <div id="wiki__editbar"> - <div id="size__ctl"></div> - <?php if($wr){?> - <div class="editButtons"> - <input class="button" id="edbtn__save" type="submit" name="do[save]" value="<?php echo $lang['btn_save']?>" accesskey="s" title="<?php echo $lang['btn_save']?> [ALT+S]" tabindex="4" /> - <input class="button" id="edbtn__preview" type="submit" name="do[preview]" value="<?php echo $lang['btn_preview']?>" accesskey="p" title="<?php echo $lang['btn_preview']?> [ALT+P]" tabindex="5" /> - <input class="button" type="submit" name="do[draftdel]" value="<?php echo $lang['btn_cancel']?>" tabindex="6" /> - </div> - <?php } ?> - <?php if($wr){ ?> - <div class="summary"> - <label for="edit__summary" class="nowrap"><?php echo $lang['summary']?>:</label> - <input type="text" class="edit" name="summary" id="edit__summary" size="50" value="<?php echo formText($SUM)?>" tabindex="2" /> - <?php html_minoredit()?> - </div> - <?php }?> - </div> - </form> - </div> <?php + $form = new Doku_Form('dw__editform'); + $form->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 '</div>'.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 '<span class="nowrap">'; - print "<input $att />"; - print '<label for="minoredit">'; - print $lang['minoredit']; - print '</label>'; - print '</span>'; + return form_makeCheckboxField('minor', '1', $lang['minoredit'], 'minoredit', 'nowrap', $p); } /** @@ -1341,23 +1230,39 @@ function html_resendpwd() { global $ID; print p_locale_xhtml('resendpwd'); -?> - <div class="centeralign"> - <form id="dw__resendpwd" action="<?php echo wl($ID)?>" accept-charset="<?php echo $lang['encoding']?>" method="post"> - <fieldset> - <br /> - <legend><?php echo $lang['resendpwd']?></legend> - <input type="hidden" name="do" value="resendpwd" /> - <input type="hidden" name="save" value="1" /> - <label class="block"> - <span><?php echo $lang['user']?></span> - <input type="text" name="login" value="<?php echo formText($_POST['login'])?>" class="edit" /><br /><br /> - </label><br /> - <input type="submit" value="<?php echo $lang['btn_resendpwd']?>" class="button" /> - </fieldset> - </form> - </div> -<?php + print '<div class="centeralign">'.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 '</div>'.NL; +} + +/** + * Output a Doku_Form object. + * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT + * + * @author Tom N Harris <tnharris@whoopdedo.org> + */ +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 : |