1*12a4e4d1SAndreas Gohr<?php 2*12a4e4d1SAndreas Gohrnamespace dokuwiki\Form; 3*12a4e4d1SAndreas Gohr 4*12a4e4d1SAndreas Gohr/** 5*12a4e4d1SAndreas Gohr * Class Form 6*12a4e4d1SAndreas Gohr * 7*12a4e4d1SAndreas Gohr * Represents the whole Form. This is what you work on, and add Elements to 8*12a4e4d1SAndreas Gohr * 9*12a4e4d1SAndreas Gohr * @package dokuwiki\Form 10*12a4e4d1SAndreas Gohr */ 11*12a4e4d1SAndreas Gohrclass Form extends Element { 12*12a4e4d1SAndreas Gohr 13*12a4e4d1SAndreas Gohr /** 14*12a4e4d1SAndreas Gohr * @var array name value pairs for hidden values 15*12a4e4d1SAndreas Gohr */ 16*12a4e4d1SAndreas Gohr protected $hidden = array(); 17*12a4e4d1SAndreas Gohr 18*12a4e4d1SAndreas Gohr /** 19*12a4e4d1SAndreas Gohr * @var Element[] the elements of the form 20*12a4e4d1SAndreas Gohr */ 21*12a4e4d1SAndreas Gohr protected $elements = array(); 22*12a4e4d1SAndreas Gohr 23*12a4e4d1SAndreas Gohr /** 24*12a4e4d1SAndreas Gohr * Creates a new, empty form with some default attributes 25*12a4e4d1SAndreas Gohr * 26*12a4e4d1SAndreas Gohr * @param array $attributes 27*12a4e4d1SAndreas Gohr */ 28*12a4e4d1SAndreas Gohr public function __construct($attributes = array()) { 29*12a4e4d1SAndreas Gohr global $ID; 30*12a4e4d1SAndreas Gohr 31*12a4e4d1SAndreas Gohr parent::__construct('form', $attributes); 32*12a4e4d1SAndreas Gohr 33*12a4e4d1SAndreas Gohr // use the current URL as default action 34*12a4e4d1SAndreas Gohr if(!$this->attr('action')) { 35*12a4e4d1SAndreas Gohr $get = $_GET; 36*12a4e4d1SAndreas Gohr if(isset($get['id'])) unset($get['id']); 37*12a4e4d1SAndreas Gohr $self = wl($ID, $get); 38*12a4e4d1SAndreas Gohr $this->attr('action', $self); 39*12a4e4d1SAndreas Gohr } 40*12a4e4d1SAndreas Gohr 41*12a4e4d1SAndreas Gohr // post is default 42*12a4e4d1SAndreas Gohr if(!$this->attr('method')) { 43*12a4e4d1SAndreas Gohr $this->attr('method', 'post'); 44*12a4e4d1SAndreas Gohr } 45*12a4e4d1SAndreas Gohr 46*12a4e4d1SAndreas Gohr // we like UTF-8 47*12a4e4d1SAndreas Gohr if(!$this->attr('accept-charset')) { 48*12a4e4d1SAndreas Gohr $this->attr('accept-charset', 'utf-8'); 49*12a4e4d1SAndreas Gohr } 50*12a4e4d1SAndreas Gohr 51*12a4e4d1SAndreas Gohr // add the security token by default 52*12a4e4d1SAndreas Gohr $this->setHiddenField('sectok', getSecurityToken()); 53*12a4e4d1SAndreas Gohr 54*12a4e4d1SAndreas Gohr // identify this as a form2 based form in HTML 55*12a4e4d1SAndreas Gohr $this->addClass('doku_form2'); 56*12a4e4d1SAndreas Gohr } 57*12a4e4d1SAndreas Gohr 58*12a4e4d1SAndreas Gohr /** 59*12a4e4d1SAndreas Gohr * Sets a hidden field 60*12a4e4d1SAndreas Gohr * 61*12a4e4d1SAndreas Gohr * @param $name 62*12a4e4d1SAndreas Gohr * @param $value 63*12a4e4d1SAndreas Gohr * @return $this 64*12a4e4d1SAndreas Gohr */ 65*12a4e4d1SAndreas Gohr public function setHiddenField($name, $value) { 66*12a4e4d1SAndreas Gohr $this->hidden[$name] = $value; 67*12a4e4d1SAndreas Gohr return $this; 68*12a4e4d1SAndreas Gohr } 69*12a4e4d1SAndreas Gohr 70*12a4e4d1SAndreas Gohr /** 71*12a4e4d1SAndreas Gohr * Adds an element to the end of the form 72*12a4e4d1SAndreas Gohr * 73*12a4e4d1SAndreas Gohr * @param Element $element 74*12a4e4d1SAndreas Gohr * @param int $pos 0-based position in the form, -1 for at the end 75*12a4e4d1SAndreas Gohr * @return Element 76*12a4e4d1SAndreas Gohr */ 77*12a4e4d1SAndreas Gohr public function addElement(Element $element, $pos = -1) { 78*12a4e4d1SAndreas Gohr if(is_a($element, 'Doku_Form2')) throw new \InvalidArgumentException('You can\'t add a form to a form'); 79*12a4e4d1SAndreas Gohr if($pos < 0) { 80*12a4e4d1SAndreas Gohr $this->elements[] = $element; 81*12a4e4d1SAndreas Gohr } else { 82*12a4e4d1SAndreas Gohr array_splice($this->elements, $pos, 0, array($element)); 83*12a4e4d1SAndreas Gohr } 84*12a4e4d1SAndreas Gohr return $element; 85*12a4e4d1SAndreas Gohr } 86*12a4e4d1SAndreas Gohr 87*12a4e4d1SAndreas Gohr /** 88*12a4e4d1SAndreas Gohr * Adds a text input field 89*12a4e4d1SAndreas Gohr * 90*12a4e4d1SAndreas Gohr * @param $name 91*12a4e4d1SAndreas Gohr * @param $label 92*12a4e4d1SAndreas Gohr * @param int $pos 93*12a4e4d1SAndreas Gohr * @return InputElement 94*12a4e4d1SAndreas Gohr */ 95*12a4e4d1SAndreas Gohr public function addTextInput($name, $label, $pos = -1) { 96*12a4e4d1SAndreas Gohr return $this->addElement(new InputElement('text', $name, $label), $pos); 97*12a4e4d1SAndreas Gohr } 98*12a4e4d1SAndreas Gohr 99*12a4e4d1SAndreas Gohr /** 100*12a4e4d1SAndreas Gohr * Adds a password input field 101*12a4e4d1SAndreas Gohr * 102*12a4e4d1SAndreas Gohr * @param $name 103*12a4e4d1SAndreas Gohr * @param $label 104*12a4e4d1SAndreas Gohr * @param int $pos 105*12a4e4d1SAndreas Gohr * @return InputElement 106*12a4e4d1SAndreas Gohr */ 107*12a4e4d1SAndreas Gohr public function addPasswordInput($name, $label, $pos = -1) { 108*12a4e4d1SAndreas Gohr return $this->addElement(new InputElement('password', $name, $label), $pos); 109*12a4e4d1SAndreas Gohr } 110*12a4e4d1SAndreas Gohr 111*12a4e4d1SAndreas Gohr /** 112*12a4e4d1SAndreas Gohr * Adds a radio button field 113*12a4e4d1SAndreas Gohr * 114*12a4e4d1SAndreas Gohr * @param $name 115*12a4e4d1SAndreas Gohr * @param $label 116*12a4e4d1SAndreas Gohr * @param int $pos 117*12a4e4d1SAndreas Gohr * @return CheckableElement 118*12a4e4d1SAndreas Gohr */ 119*12a4e4d1SAndreas Gohr public function addRadioButton($name, $label, $pos = -1) { 120*12a4e4d1SAndreas Gohr return $this->addElement(new CheckableElement('radio', $name, $label), $pos); 121*12a4e4d1SAndreas Gohr } 122*12a4e4d1SAndreas Gohr 123*12a4e4d1SAndreas Gohr /** 124*12a4e4d1SAndreas Gohr * Adds a checkbox field 125*12a4e4d1SAndreas Gohr * 126*12a4e4d1SAndreas Gohr * @param $name 127*12a4e4d1SAndreas Gohr * @param $label 128*12a4e4d1SAndreas Gohr * @param int $pos 129*12a4e4d1SAndreas Gohr * @return CheckableElement 130*12a4e4d1SAndreas Gohr */ 131*12a4e4d1SAndreas Gohr public function addCheckbox($name, $label, $pos = -1) { 132*12a4e4d1SAndreas Gohr return $this->addElement(new CheckableElement('checkbox', $name, $label), $pos); 133*12a4e4d1SAndreas Gohr } 134*12a4e4d1SAndreas Gohr 135*12a4e4d1SAndreas Gohr /** 136*12a4e4d1SAndreas Gohr * Adds a textarea field 137*12a4e4d1SAndreas Gohr * 138*12a4e4d1SAndreas Gohr * @param $name 139*12a4e4d1SAndreas Gohr * @param $label 140*12a4e4d1SAndreas Gohr * @param int $pos 141*12a4e4d1SAndreas Gohr * @return TextareaElement 142*12a4e4d1SAndreas Gohr */ 143*12a4e4d1SAndreas Gohr public function addTextarea($name, $label, $pos = -1) { 144*12a4e4d1SAndreas Gohr return $this->addElement(new TextareaElement($name, $label), $pos); 145*12a4e4d1SAndreas Gohr } 146*12a4e4d1SAndreas Gohr 147*12a4e4d1SAndreas Gohr /** 148*12a4e4d1SAndreas Gohr * The HTML representation of the whole form 149*12a4e4d1SAndreas Gohr * 150*12a4e4d1SAndreas Gohr * @return string 151*12a4e4d1SAndreas Gohr */ 152*12a4e4d1SAndreas Gohr public function toHTML() { 153*12a4e4d1SAndreas Gohr $html = '<form ' . buildAttributes($this->attrs()) . '>' . DOKU_LF; 154*12a4e4d1SAndreas Gohr 155*12a4e4d1SAndreas Gohr foreach($this->hidden as $name => $value) { 156*12a4e4d1SAndreas Gohr $html .= '<input type="hidden" name="' . $name . '" value="' . formText($value) . '" />' . DOKU_LF; 157*12a4e4d1SAndreas Gohr } 158*12a4e4d1SAndreas Gohr 159*12a4e4d1SAndreas Gohr foreach($this->elements as $element) { 160*12a4e4d1SAndreas Gohr $html .= $element->toHTML() . DOKU_LF; 161*12a4e4d1SAndreas Gohr } 162*12a4e4d1SAndreas Gohr 163*12a4e4d1SAndreas Gohr $html .= '</form>' . DOKU_LF; 164*12a4e4d1SAndreas Gohr 165*12a4e4d1SAndreas Gohr return $html; 166*12a4e4d1SAndreas Gohr } 167*12a4e4d1SAndreas Gohr} 168