112a4e4d1SAndreas Gohr<?php 212a4e4d1SAndreas Gohrnamespace dokuwiki\Form; 312a4e4d1SAndreas Gohr 412a4e4d1SAndreas Gohr/** 512a4e4d1SAndreas Gohr * Class Form 612a4e4d1SAndreas Gohr * 712a4e4d1SAndreas Gohr * Represents the whole Form. This is what you work on, and add Elements to 812a4e4d1SAndreas Gohr * 912a4e4d1SAndreas Gohr * @package dokuwiki\Form 1012a4e4d1SAndreas Gohr */ 1112a4e4d1SAndreas Gohrclass Form extends Element { 1212a4e4d1SAndreas Gohr 1312a4e4d1SAndreas Gohr /** 1412a4e4d1SAndreas Gohr * @var array name value pairs for hidden values 1512a4e4d1SAndreas Gohr */ 1612a4e4d1SAndreas Gohr protected $hidden = array(); 1712a4e4d1SAndreas Gohr 1812a4e4d1SAndreas Gohr /** 1912a4e4d1SAndreas Gohr * @var Element[] the elements of the form 2012a4e4d1SAndreas Gohr */ 2112a4e4d1SAndreas Gohr protected $elements = array(); 2212a4e4d1SAndreas Gohr 2312a4e4d1SAndreas Gohr /** 2412a4e4d1SAndreas Gohr * Creates a new, empty form with some default attributes 2512a4e4d1SAndreas Gohr * 2612a4e4d1SAndreas Gohr * @param array $attributes 2712a4e4d1SAndreas Gohr */ 2812a4e4d1SAndreas Gohr public function __construct($attributes = array()) { 2912a4e4d1SAndreas Gohr global $ID; 3012a4e4d1SAndreas Gohr 3112a4e4d1SAndreas Gohr parent::__construct('form', $attributes); 3212a4e4d1SAndreas Gohr 3312a4e4d1SAndreas Gohr // use the current URL as default action 3412a4e4d1SAndreas Gohr if(!$this->attr('action')) { 3512a4e4d1SAndreas Gohr $get = $_GET; 3612a4e4d1SAndreas Gohr if(isset($get['id'])) unset($get['id']); 376d0ceaf9SAndreas Gohr $self = wl($ID, $get, false, '&'); //attributes are escaped later 3812a4e4d1SAndreas Gohr $this->attr('action', $self); 3912a4e4d1SAndreas Gohr } 4012a4e4d1SAndreas Gohr 4112a4e4d1SAndreas Gohr // post is default 4212a4e4d1SAndreas Gohr if(!$this->attr('method')) { 4312a4e4d1SAndreas Gohr $this->attr('method', 'post'); 4412a4e4d1SAndreas Gohr } 4512a4e4d1SAndreas Gohr 4612a4e4d1SAndreas Gohr // we like UTF-8 4712a4e4d1SAndreas Gohr if(!$this->attr('accept-charset')) { 4812a4e4d1SAndreas Gohr $this->attr('accept-charset', 'utf-8'); 4912a4e4d1SAndreas Gohr } 5012a4e4d1SAndreas Gohr 5112a4e4d1SAndreas Gohr // add the security token by default 5212a4e4d1SAndreas Gohr $this->setHiddenField('sectok', getSecurityToken()); 5312a4e4d1SAndreas Gohr 546d0ceaf9SAndreas Gohr // identify this as a new form based form in HTML 556d0ceaf9SAndreas Gohr $this->addClass('doku_form'); 5612a4e4d1SAndreas Gohr } 5712a4e4d1SAndreas Gohr 5812a4e4d1SAndreas Gohr /** 5912a4e4d1SAndreas Gohr * Sets a hidden field 6012a4e4d1SAndreas Gohr * 6112a4e4d1SAndreas Gohr * @param $name 6212a4e4d1SAndreas Gohr * @param $value 6312a4e4d1SAndreas Gohr * @return $this 6412a4e4d1SAndreas Gohr */ 6512a4e4d1SAndreas Gohr public function setHiddenField($name, $value) { 6612a4e4d1SAndreas Gohr $this->hidden[$name] = $value; 6712a4e4d1SAndreas Gohr return $this; 6812a4e4d1SAndreas Gohr } 6912a4e4d1SAndreas Gohr 70ef0c211bSAndreas Gohr #region element query function 7164744a10SAndreas Gohr 7212a4e4d1SAndreas Gohr /** 73ef0c211bSAndreas Gohr * Returns the numbers of elements in the form 74ef0c211bSAndreas Gohr * 75ef0c211bSAndreas Gohr * @return int 76ef0c211bSAndreas Gohr */ 77ef0c211bSAndreas Gohr public function elementCount() { 78ef0c211bSAndreas Gohr return count($this->elements); 79ef0c211bSAndreas Gohr } 80ef0c211bSAndreas Gohr 81ef0c211bSAndreas Gohr /** 82ef0c211bSAndreas Gohr * Returns a reference to the element at a position. 83ef0c211bSAndreas Gohr * A position out-of-bounds will return either the 84ef0c211bSAndreas Gohr * first (underflow) or last (overflow) element. 85ef0c211bSAndreas Gohr * 86ef0c211bSAndreas Gohr * @param $pos 87ef0c211bSAndreas Gohr * @return Element 88ef0c211bSAndreas Gohr */ 89ef0c211bSAndreas Gohr public function getElementAt($pos) { 90ef0c211bSAndreas Gohr if($pos < 0) $pos = count($this->elements) + $pos; 91ef0c211bSAndreas Gohr if($pos < 0) $pos = 0; 92ef0c211bSAndreas Gohr if($pos >= count($this->elements)) $pos = count($this->elements) - 1; 93ef0c211bSAndreas Gohr return $this->elements[$pos]; 94ef0c211bSAndreas Gohr } 95ef0c211bSAndreas Gohr 96ef0c211bSAndreas Gohr /** 97ef0c211bSAndreas Gohr * Gets the position of the first of a type of element 98ef0c211bSAndreas Gohr * 99ef0c211bSAndreas Gohr * @param string $type Element type to look for. 100ef0c211bSAndreas Gohr * @param int $offset search from this position onward 101ef0c211bSAndreas Gohr * @return false|int position of element if found, otherwise false 102ef0c211bSAndreas Gohr */ 103ef0c211bSAndreas Gohr public function findPositionByType($type, $offset = 0) { 104ef0c211bSAndreas Gohr $len = $this->elementCount(); 105ef0c211bSAndreas Gohr for($pos = $offset; $pos < $len; $pos++) { 106ef0c211bSAndreas Gohr if($this->elements[$pos]->getType() == $type) { 107ef0c211bSAndreas Gohr return $pos; 108ef0c211bSAndreas Gohr } 109ef0c211bSAndreas Gohr } 110ef0c211bSAndreas Gohr return false; 111ef0c211bSAndreas Gohr } 112ef0c211bSAndreas Gohr 113ef0c211bSAndreas Gohr /** 114ef0c211bSAndreas Gohr * Gets the position of the first element matching the attribute 115ef0c211bSAndreas Gohr * 116ef0c211bSAndreas Gohr * @param string $name Name of the attribute 117ef0c211bSAndreas Gohr * @param string $value Value the attribute should have 118ef0c211bSAndreas Gohr * @param int $offset search from this position onward 119ef0c211bSAndreas Gohr * @return false|int position of element if found, otherwise false 120ef0c211bSAndreas Gohr */ 121ef0c211bSAndreas Gohr public function findPositionByAttribute($name, $value, $offset = 0) { 122ef0c211bSAndreas Gohr $len = $this->elementCount(); 123ef0c211bSAndreas Gohr for($pos = $offset; $pos < $len; $pos++) { 124ef0c211bSAndreas Gohr if($this->elements[$pos]->attr($name) == $value) { 125ef0c211bSAndreas Gohr return $pos; 126ef0c211bSAndreas Gohr } 127ef0c211bSAndreas Gohr } 128ef0c211bSAndreas Gohr return false; 129ef0c211bSAndreas Gohr } 130ef0c211bSAndreas Gohr 131ef0c211bSAndreas Gohr #endregion 132ef0c211bSAndreas Gohr 133ef0c211bSAndreas Gohr #region Element positioning functions 134ef0c211bSAndreas Gohr 135ef0c211bSAndreas Gohr /** 136ef0c211bSAndreas Gohr * Adds or inserts an element to the form 13712a4e4d1SAndreas Gohr * 13812a4e4d1SAndreas Gohr * @param Element $element 13912a4e4d1SAndreas Gohr * @param int $pos 0-based position in the form, -1 for at the end 14012a4e4d1SAndreas Gohr * @return Element 14112a4e4d1SAndreas Gohr */ 14212a4e4d1SAndreas Gohr public function addElement(Element $element, $pos = -1) { 143ef0c211bSAndreas Gohr if(is_a($element, '\dokuwiki\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form'); 14412a4e4d1SAndreas Gohr if($pos < 0) { 14512a4e4d1SAndreas Gohr $this->elements[] = $element; 14612a4e4d1SAndreas Gohr } else { 14712a4e4d1SAndreas Gohr array_splice($this->elements, $pos, 0, array($element)); 14812a4e4d1SAndreas Gohr } 14912a4e4d1SAndreas Gohr return $element; 15012a4e4d1SAndreas Gohr } 15112a4e4d1SAndreas Gohr 15212a4e4d1SAndreas Gohr /** 153ef0c211bSAndreas Gohr * Replaces an existing element with a new one 154ef0c211bSAndreas Gohr * 155ef0c211bSAndreas Gohr * @param Element $element the new element 156ef0c211bSAndreas Gohr * @param $pos 0-based position of the element to replace 157ef0c211bSAndreas Gohr */ 158ef0c211bSAndreas Gohr public function replaceElement(Element $element, $pos) { 159ef0c211bSAndreas Gohr if(is_a($element, '\dokuwiki\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form'); 160ef0c211bSAndreas Gohr array_splice($this->elements, $pos, 1, array($element)); 161ef0c211bSAndreas Gohr } 162ef0c211bSAndreas Gohr 163ef0c211bSAndreas Gohr /** 164ef0c211bSAndreas Gohr * Remove an element from the form completely 165ef0c211bSAndreas Gohr * 166ef0c211bSAndreas Gohr * @param $pos 0-based position of the element to remove 167ef0c211bSAndreas Gohr */ 168ef0c211bSAndreas Gohr public function removeElement($pos) { 169ef0c211bSAndreas Gohr array_splice($this->elements, $pos, 1); 170ef0c211bSAndreas Gohr } 171ef0c211bSAndreas Gohr 172ef0c211bSAndreas Gohr #endregion 173ef0c211bSAndreas Gohr 174ef0c211bSAndreas Gohr #region Element adding functions 175ef0c211bSAndreas Gohr 176ef0c211bSAndreas Gohr /** 17712a4e4d1SAndreas Gohr * Adds a text input field 17812a4e4d1SAndreas Gohr * 17912a4e4d1SAndreas Gohr * @param $name 18012a4e4d1SAndreas Gohr * @param $label 18112a4e4d1SAndreas Gohr * @param int $pos 18212a4e4d1SAndreas Gohr * @return InputElement 18312a4e4d1SAndreas Gohr */ 184de19515fSAndreas Gohr public function addTextInput($name, $label = '', $pos = -1) { 18512a4e4d1SAndreas Gohr return $this->addElement(new InputElement('text', $name, $label), $pos); 18612a4e4d1SAndreas Gohr } 18712a4e4d1SAndreas Gohr 18812a4e4d1SAndreas Gohr /** 18912a4e4d1SAndreas Gohr * Adds a password input field 19012a4e4d1SAndreas Gohr * 19112a4e4d1SAndreas Gohr * @param $name 19212a4e4d1SAndreas Gohr * @param $label 19312a4e4d1SAndreas Gohr * @param int $pos 19412a4e4d1SAndreas Gohr * @return InputElement 19512a4e4d1SAndreas Gohr */ 196de19515fSAndreas Gohr public function addPasswordInput($name, $label = '', $pos = -1) { 19712a4e4d1SAndreas Gohr return $this->addElement(new InputElement('password', $name, $label), $pos); 19812a4e4d1SAndreas Gohr } 19912a4e4d1SAndreas Gohr 20012a4e4d1SAndreas Gohr /** 20112a4e4d1SAndreas Gohr * Adds a radio button field 20212a4e4d1SAndreas Gohr * 20312a4e4d1SAndreas Gohr * @param $name 20412a4e4d1SAndreas Gohr * @param $label 20512a4e4d1SAndreas Gohr * @param int $pos 20612a4e4d1SAndreas Gohr * @return CheckableElement 20712a4e4d1SAndreas Gohr */ 208de19515fSAndreas Gohr public function addRadioButton($name, $label = '', $pos = -1) { 20912a4e4d1SAndreas Gohr return $this->addElement(new CheckableElement('radio', $name, $label), $pos); 21012a4e4d1SAndreas Gohr } 21112a4e4d1SAndreas Gohr 21212a4e4d1SAndreas Gohr /** 21312a4e4d1SAndreas Gohr * Adds a checkbox field 21412a4e4d1SAndreas Gohr * 21512a4e4d1SAndreas Gohr * @param $name 21612a4e4d1SAndreas Gohr * @param $label 21712a4e4d1SAndreas Gohr * @param int $pos 21812a4e4d1SAndreas Gohr * @return CheckableElement 21912a4e4d1SAndreas Gohr */ 220de19515fSAndreas Gohr public function addCheckbox($name, $label = '', $pos = -1) { 22112a4e4d1SAndreas Gohr return $this->addElement(new CheckableElement('checkbox', $name, $label), $pos); 22212a4e4d1SAndreas Gohr } 22312a4e4d1SAndreas Gohr 22412a4e4d1SAndreas Gohr /** 22512a4e4d1SAndreas Gohr * Adds a textarea field 22612a4e4d1SAndreas Gohr * 22712a4e4d1SAndreas Gohr * @param $name 22812a4e4d1SAndreas Gohr * @param $label 22912a4e4d1SAndreas Gohr * @param int $pos 23012a4e4d1SAndreas Gohr * @return TextareaElement 23112a4e4d1SAndreas Gohr */ 232de19515fSAndreas Gohr public function addTextarea($name, $label = '', $pos = -1) { 23312a4e4d1SAndreas Gohr return $this->addElement(new TextareaElement($name, $label), $pos); 23412a4e4d1SAndreas Gohr } 23512a4e4d1SAndreas Gohr 23612a4e4d1SAndreas Gohr /** 237*8f0df229SAndreas Gohr * Adds a simple button, escapes the content for you 238*8f0df229SAndreas Gohr * 239*8f0df229SAndreas Gohr * @param string $name 240*8f0df229SAndreas Gohr * @param string $content 241*8f0df229SAndreas Gohr * @param int $pos 242*8f0df229SAndreas Gohr * @return Element 243*8f0df229SAndreas Gohr */ 244*8f0df229SAndreas Gohr public function addButton($name, $content, $pos = -1) { 245*8f0df229SAndreas Gohr return $this->addElement(new ButtonElement($name, hsc($content)), $pos); 246*8f0df229SAndreas Gohr } 247*8f0df229SAndreas Gohr 248*8f0df229SAndreas Gohr /** 249*8f0df229SAndreas Gohr * Adds a simple button, allows HTML for content 250*8f0df229SAndreas Gohr * 251*8f0df229SAndreas Gohr * @param string $name 252*8f0df229SAndreas Gohr * @param string $html 253*8f0df229SAndreas Gohr * @param int $pos 254*8f0df229SAndreas Gohr * @return Element 255*8f0df229SAndreas Gohr */ 256*8f0df229SAndreas Gohr public function addButtonHTML($name, $html, $pos = -1) { 257*8f0df229SAndreas Gohr return $this->addElement(new ButtonElement($name, $html), $pos); 258*8f0df229SAndreas Gohr } 259*8f0df229SAndreas Gohr 260*8f0df229SAndreas Gohr /** 261de19515fSAndreas Gohr * Add fixed HTML to the form 262de19515fSAndreas Gohr * 263de19515fSAndreas Gohr * @param $html 264de19515fSAndreas Gohr * @param int $pos 26564744a10SAndreas Gohr * @return HTMLElement 266de19515fSAndreas Gohr */ 267de19515fSAndreas Gohr public function addHTML($html, $pos = -1) { 268de19515fSAndreas Gohr return $this->addElement(new HTMLElement($html), $pos); 269de19515fSAndreas Gohr } 270de19515fSAndreas Gohr 27164744a10SAndreas Gohr /** 27264744a10SAndreas Gohr * Add a closed HTML tag to the form 27364744a10SAndreas Gohr * 27464744a10SAndreas Gohr * @param $tag 27564744a10SAndreas Gohr * @param int $pos 27664744a10SAndreas Gohr * @return TagElement 27764744a10SAndreas Gohr */ 27864744a10SAndreas Gohr public function addTag($tag, $pos = -1) { 27964744a10SAndreas Gohr return $this->addElement(new TagElement($tag), $pos); 28064744a10SAndreas Gohr } 28164744a10SAndreas Gohr 28264744a10SAndreas Gohr /** 28364744a10SAndreas Gohr * Add an open HTML tag to the form 28464744a10SAndreas Gohr * 28564744a10SAndreas Gohr * Be sure to close it again! 28664744a10SAndreas Gohr * 28764744a10SAndreas Gohr * @param $tag 28864744a10SAndreas Gohr * @param int $pos 28964744a10SAndreas Gohr * @return TagOpenElement 29064744a10SAndreas Gohr */ 29164744a10SAndreas Gohr public function addTagOpen($tag, $pos = -1) { 29264744a10SAndreas Gohr return $this->addElement(new TagOpenElement($tag), $pos); 29364744a10SAndreas Gohr } 29464744a10SAndreas Gohr 29564744a10SAndreas Gohr /** 29664744a10SAndreas Gohr * Add a closing HTML tag to the form 29764744a10SAndreas Gohr * 29864744a10SAndreas Gohr * Be sure it had been opened before 29964744a10SAndreas Gohr * 30064744a10SAndreas Gohr * @param $tag 30164744a10SAndreas Gohr * @param int $pos 30264744a10SAndreas Gohr * @return TagCloseElement 30364744a10SAndreas Gohr */ 30464744a10SAndreas Gohr public function addTagClose($tag, $pos = -1) { 30564744a10SAndreas Gohr return $this->addElement(new TagCloseElement($tag), $pos); 30664744a10SAndreas Gohr } 30764744a10SAndreas Gohr 30864744a10SAndreas Gohr /** 30964744a10SAndreas Gohr * Open a Fieldset 31064744a10SAndreas Gohr * 31164744a10SAndreas Gohr * @param $legend 31264744a10SAndreas Gohr * @param int $pos 31364744a10SAndreas Gohr * @return FieldsetOpenElement 31464744a10SAndreas Gohr */ 31564744a10SAndreas Gohr public function addFieldsetOpen($legend = '', $pos = -1) { 31664744a10SAndreas Gohr return $this->addElement(new FieldsetOpenElement($legend), $pos); 31764744a10SAndreas Gohr } 31864744a10SAndreas Gohr 31964744a10SAndreas Gohr /** 32064744a10SAndreas Gohr * Close a fieldset 32164744a10SAndreas Gohr * 32264744a10SAndreas Gohr * @param int $pos 32364744a10SAndreas Gohr * @return TagCloseElement 32464744a10SAndreas Gohr */ 32564744a10SAndreas Gohr public function addFieldsetClose($pos = -1) { 32664744a10SAndreas Gohr return $this->addElement(new FieldsetCloseElement(), $pos); 32764744a10SAndreas Gohr } 32864744a10SAndreas Gohr 32964744a10SAndreas Gohr #endregion 33064744a10SAndreas Gohr 3311f5d8b65SAndreas Gohr /** 3321f5d8b65SAndreas Gohr * Adjust the elements so that fieldset open and closes are matching 3331f5d8b65SAndreas Gohr */ 334de19515fSAndreas Gohr protected function balanceFieldsets() { 3351f5d8b65SAndreas Gohr $lastclose = 0; 3361f5d8b65SAndreas Gohr $isopen = false; 3371f5d8b65SAndreas Gohr $len = count($this->elements); 3381f5d8b65SAndreas Gohr 3391f5d8b65SAndreas Gohr for($pos = 0; $pos < $len; $pos++) { 3401f5d8b65SAndreas Gohr $type = $this->elements[$pos]->getType(); 3411f5d8b65SAndreas Gohr if($type == 'fieldsetopen') { 3421f5d8b65SAndreas Gohr if($isopen) { 34380b13baaSGerrit Uitslag //close previous fieldset 3441f5d8b65SAndreas Gohr $this->addFieldsetClose($pos); 3451f5d8b65SAndreas Gohr $lastclose = $pos + 1; 3461f5d8b65SAndreas Gohr $pos++; 3471f5d8b65SAndreas Gohr $len++; 3481f5d8b65SAndreas Gohr } 3491f5d8b65SAndreas Gohr $isopen = true; 3501f5d8b65SAndreas Gohr } else if($type == 'fieldsetclose') { 3511f5d8b65SAndreas Gohr if(!$isopen) { 3521f5d8b65SAndreas Gohr // make sure there was a fieldsetopen 3531f5d8b65SAndreas Gohr // either right after the last close or at the begining 3541f5d8b65SAndreas Gohr $this->addFieldsetOpen('', $lastclose); 3551f5d8b65SAndreas Gohr $len++; 3561f5d8b65SAndreas Gohr $pos++; 3571f5d8b65SAndreas Gohr } 3581f5d8b65SAndreas Gohr $lastclose = $pos; 3591f5d8b65SAndreas Gohr $isopen = false; 3601f5d8b65SAndreas Gohr } 3611f5d8b65SAndreas Gohr } 3621f5d8b65SAndreas Gohr 3631f5d8b65SAndreas Gohr // close open fieldset at the end 3641f5d8b65SAndreas Gohr if($isopen) { 3651f5d8b65SAndreas Gohr $this->addFieldsetClose(); 3661f5d8b65SAndreas Gohr } 367de19515fSAndreas Gohr } 368de19515fSAndreas Gohr 369de19515fSAndreas Gohr /** 37012a4e4d1SAndreas Gohr * The HTML representation of the whole form 37112a4e4d1SAndreas Gohr * 37212a4e4d1SAndreas Gohr * @return string 37312a4e4d1SAndreas Gohr */ 37412a4e4d1SAndreas Gohr public function toHTML() { 375de19515fSAndreas Gohr $this->balanceFieldsets(); 376de19515fSAndreas Gohr 37712a4e4d1SAndreas Gohr $html = '<form ' . buildAttributes($this->attrs()) . '>' . DOKU_LF; 37812a4e4d1SAndreas Gohr 37912a4e4d1SAndreas Gohr foreach($this->hidden as $name => $value) { 38012a4e4d1SAndreas Gohr $html .= '<input type="hidden" name="' . $name . '" value="' . formText($value) . '" />' . DOKU_LF; 38112a4e4d1SAndreas Gohr } 38212a4e4d1SAndreas Gohr 38312a4e4d1SAndreas Gohr foreach($this->elements as $element) { 38412a4e4d1SAndreas Gohr $html .= $element->toHTML() . DOKU_LF; 38512a4e4d1SAndreas Gohr } 38612a4e4d1SAndreas Gohr 38712a4e4d1SAndreas Gohr $html .= '</form>' . DOKU_LF; 38812a4e4d1SAndreas Gohr 38912a4e4d1SAndreas Gohr return $html; 39012a4e4d1SAndreas Gohr } 39112a4e4d1SAndreas Gohr} 392