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 /** 237de19515fSAndreas Gohr * Add fixed HTML to the form 238de19515fSAndreas Gohr * 239de19515fSAndreas Gohr * @param $html 240de19515fSAndreas Gohr * @param int $pos 24164744a10SAndreas Gohr * @return HTMLElement 242de19515fSAndreas Gohr */ 243de19515fSAndreas Gohr public function addHTML($html, $pos = -1) { 244de19515fSAndreas Gohr return $this->addElement(new HTMLElement($html), $pos); 245de19515fSAndreas Gohr } 246de19515fSAndreas Gohr 24764744a10SAndreas Gohr /** 24864744a10SAndreas Gohr * Add a closed HTML tag to the form 24964744a10SAndreas Gohr * 25064744a10SAndreas Gohr * @param $tag 25164744a10SAndreas Gohr * @param int $pos 25264744a10SAndreas Gohr * @return TagElement 25364744a10SAndreas Gohr */ 25464744a10SAndreas Gohr public function addTag($tag, $pos = -1) { 25564744a10SAndreas Gohr return $this->addElement(new TagElement($tag), $pos); 25664744a10SAndreas Gohr } 25764744a10SAndreas Gohr 25864744a10SAndreas Gohr /** 25964744a10SAndreas Gohr * Add an open HTML tag to the form 26064744a10SAndreas Gohr * 26164744a10SAndreas Gohr * Be sure to close it again! 26264744a10SAndreas Gohr * 26364744a10SAndreas Gohr * @param $tag 26464744a10SAndreas Gohr * @param int $pos 26564744a10SAndreas Gohr * @return TagOpenElement 26664744a10SAndreas Gohr */ 26764744a10SAndreas Gohr public function addTagOpen($tag, $pos = -1) { 26864744a10SAndreas Gohr return $this->addElement(new TagOpenElement($tag), $pos); 26964744a10SAndreas Gohr } 27064744a10SAndreas Gohr 27164744a10SAndreas Gohr /** 27264744a10SAndreas Gohr * Add a closing HTML tag to the form 27364744a10SAndreas Gohr * 27464744a10SAndreas Gohr * Be sure it had been opened before 27564744a10SAndreas Gohr * 27664744a10SAndreas Gohr * @param $tag 27764744a10SAndreas Gohr * @param int $pos 27864744a10SAndreas Gohr * @return TagCloseElement 27964744a10SAndreas Gohr */ 28064744a10SAndreas Gohr public function addTagClose($tag, $pos = -1) { 28164744a10SAndreas Gohr return $this->addElement(new TagCloseElement($tag), $pos); 28264744a10SAndreas Gohr } 28364744a10SAndreas Gohr 28464744a10SAndreas Gohr /** 28564744a10SAndreas Gohr * Open a Fieldset 28664744a10SAndreas Gohr * 28764744a10SAndreas Gohr * @param $legend 28864744a10SAndreas Gohr * @param int $pos 28964744a10SAndreas Gohr * @return FieldsetOpenElement 29064744a10SAndreas Gohr */ 29164744a10SAndreas Gohr public function addFieldsetOpen($legend = '', $pos = -1) { 29264744a10SAndreas Gohr return $this->addElement(new FieldsetOpenElement($legend), $pos); 29364744a10SAndreas Gohr } 29464744a10SAndreas Gohr 29564744a10SAndreas Gohr /** 29664744a10SAndreas Gohr * Close a fieldset 29764744a10SAndreas Gohr * 29864744a10SAndreas Gohr * @param int $pos 29964744a10SAndreas Gohr * @return TagCloseElement 30064744a10SAndreas Gohr */ 30164744a10SAndreas Gohr public function addFieldsetClose($pos = -1) { 30264744a10SAndreas Gohr return $this->addElement(new FieldsetCloseElement(), $pos); 30364744a10SAndreas Gohr } 30464744a10SAndreas Gohr 30564744a10SAndreas Gohr #endregion 30664744a10SAndreas Gohr 3071f5d8b65SAndreas Gohr /** 3081f5d8b65SAndreas Gohr * Adjust the elements so that fieldset open and closes are matching 3091f5d8b65SAndreas Gohr */ 310de19515fSAndreas Gohr protected function balanceFieldsets() { 3111f5d8b65SAndreas Gohr $lastclose = 0; 3121f5d8b65SAndreas Gohr $isopen = false; 3131f5d8b65SAndreas Gohr $len = count($this->elements); 3141f5d8b65SAndreas Gohr 3151f5d8b65SAndreas Gohr for($pos = 0; $pos < $len; $pos++) { 3161f5d8b65SAndreas Gohr $type = $this->elements[$pos]->getType(); 3171f5d8b65SAndreas Gohr if($type == 'fieldsetopen') { 3181f5d8b65SAndreas Gohr if($isopen) { 319*80b13baaSGerrit Uitslag //close previous fieldset 3201f5d8b65SAndreas Gohr $this->addFieldsetClose($pos); 3211f5d8b65SAndreas Gohr $lastclose = $pos + 1; 3221f5d8b65SAndreas Gohr $pos++; 3231f5d8b65SAndreas Gohr $len++; 3241f5d8b65SAndreas Gohr } 3251f5d8b65SAndreas Gohr $isopen = true; 3261f5d8b65SAndreas Gohr } else if($type == 'fieldsetclose') { 3271f5d8b65SAndreas Gohr if(!$isopen) { 3281f5d8b65SAndreas Gohr // make sure there was a fieldsetopen 3291f5d8b65SAndreas Gohr // either right after the last close or at the begining 3301f5d8b65SAndreas Gohr $this->addFieldsetOpen('', $lastclose); 3311f5d8b65SAndreas Gohr $len++; 3321f5d8b65SAndreas Gohr $pos++; 3331f5d8b65SAndreas Gohr } 3341f5d8b65SAndreas Gohr $lastclose = $pos; 3351f5d8b65SAndreas Gohr $isopen = false; 3361f5d8b65SAndreas Gohr } 3371f5d8b65SAndreas Gohr } 3381f5d8b65SAndreas Gohr 3391f5d8b65SAndreas Gohr // close open fieldset at the end 3401f5d8b65SAndreas Gohr if($isopen) { 3411f5d8b65SAndreas Gohr $this->addFieldsetClose(); 3421f5d8b65SAndreas Gohr } 343de19515fSAndreas Gohr } 344de19515fSAndreas Gohr 345de19515fSAndreas Gohr /** 34612a4e4d1SAndreas Gohr * The HTML representation of the whole form 34712a4e4d1SAndreas Gohr * 34812a4e4d1SAndreas Gohr * @return string 34912a4e4d1SAndreas Gohr */ 35012a4e4d1SAndreas Gohr public function toHTML() { 351de19515fSAndreas Gohr $this->balanceFieldsets(); 352de19515fSAndreas Gohr 35312a4e4d1SAndreas Gohr $html = '<form ' . buildAttributes($this->attrs()) . '>' . DOKU_LF; 35412a4e4d1SAndreas Gohr 35512a4e4d1SAndreas Gohr foreach($this->hidden as $name => $value) { 35612a4e4d1SAndreas Gohr $html .= '<input type="hidden" name="' . $name . '" value="' . formText($value) . '" />' . DOKU_LF; 35712a4e4d1SAndreas Gohr } 35812a4e4d1SAndreas Gohr 35912a4e4d1SAndreas Gohr foreach($this->elements as $element) { 36012a4e4d1SAndreas Gohr $html .= $element->toHTML() . DOKU_LF; 36112a4e4d1SAndreas Gohr } 36212a4e4d1SAndreas Gohr 36312a4e4d1SAndreas Gohr $html .= '</form>' . DOKU_LF; 36412a4e4d1SAndreas Gohr 36512a4e4d1SAndreas Gohr return $html; 36612a4e4d1SAndreas Gohr } 36712a4e4d1SAndreas Gohr} 368