xref: /dokuwiki/inc/Form/Form.php (revision ef0c211b2ac87b8e3c6e85b600021389be6209bf)
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
70*ef0c211bSAndreas Gohr    #region element query function
7164744a10SAndreas Gohr
7212a4e4d1SAndreas Gohr    /**
73*ef0c211bSAndreas Gohr     * Returns the numbers of elements in the form
74*ef0c211bSAndreas Gohr     *
75*ef0c211bSAndreas Gohr     * @return int
76*ef0c211bSAndreas Gohr     */
77*ef0c211bSAndreas Gohr    public function elementCount() {
78*ef0c211bSAndreas Gohr        return count($this->elements);
79*ef0c211bSAndreas Gohr    }
80*ef0c211bSAndreas Gohr
81*ef0c211bSAndreas Gohr    /**
82*ef0c211bSAndreas Gohr     * Returns a reference to the element at a position.
83*ef0c211bSAndreas Gohr     * A position out-of-bounds will return either the
84*ef0c211bSAndreas Gohr     * first (underflow) or last (overflow) element.
85*ef0c211bSAndreas Gohr     *
86*ef0c211bSAndreas Gohr     * @param $pos
87*ef0c211bSAndreas Gohr     * @return Element
88*ef0c211bSAndreas Gohr     */
89*ef0c211bSAndreas Gohr    public function getElementAt($pos) {
90*ef0c211bSAndreas Gohr        if($pos < 0) $pos = count($this->elements) + $pos;
91*ef0c211bSAndreas Gohr        if($pos < 0) $pos = 0;
92*ef0c211bSAndreas Gohr        if($pos >= count($this->elements)) $pos = count($this->elements) - 1;
93*ef0c211bSAndreas Gohr        return $this->elements[$pos];
94*ef0c211bSAndreas Gohr    }
95*ef0c211bSAndreas Gohr
96*ef0c211bSAndreas Gohr    /**
97*ef0c211bSAndreas Gohr     * Gets the position of the first of a type of element
98*ef0c211bSAndreas Gohr     *
99*ef0c211bSAndreas Gohr     * @param string $type Element type to look for.
100*ef0c211bSAndreas Gohr     * @param int $offset search from this position onward
101*ef0c211bSAndreas Gohr     * @return false|int position of element if found, otherwise false
102*ef0c211bSAndreas Gohr     */
103*ef0c211bSAndreas Gohr    public function findPositionByType($type, $offset = 0) {
104*ef0c211bSAndreas Gohr        $len = $this->elementCount();
105*ef0c211bSAndreas Gohr        for($pos = $offset; $pos < $len; $pos++) {
106*ef0c211bSAndreas Gohr            if($this->elements[$pos]->getType() == $type) {
107*ef0c211bSAndreas Gohr                return $pos;
108*ef0c211bSAndreas Gohr            }
109*ef0c211bSAndreas Gohr        }
110*ef0c211bSAndreas Gohr        return false;
111*ef0c211bSAndreas Gohr    }
112*ef0c211bSAndreas Gohr
113*ef0c211bSAndreas Gohr    /**
114*ef0c211bSAndreas Gohr     * Gets the position of the first element matching the attribute
115*ef0c211bSAndreas Gohr     *
116*ef0c211bSAndreas Gohr     * @param string $name Name of the attribute
117*ef0c211bSAndreas Gohr     * @param string $value Value the attribute should have
118*ef0c211bSAndreas Gohr     * @param int $offset search from this position onward
119*ef0c211bSAndreas Gohr     * @return false|int position of element if found, otherwise false
120*ef0c211bSAndreas Gohr     */
121*ef0c211bSAndreas Gohr    public function findPositionByAttribute($name, $value, $offset = 0) {
122*ef0c211bSAndreas Gohr        $len = $this->elementCount();
123*ef0c211bSAndreas Gohr        for($pos = $offset; $pos < $len; $pos++) {
124*ef0c211bSAndreas Gohr            if($this->elements[$pos]->attr($name) == $value) {
125*ef0c211bSAndreas Gohr                return $pos;
126*ef0c211bSAndreas Gohr            }
127*ef0c211bSAndreas Gohr        }
128*ef0c211bSAndreas Gohr        return false;
129*ef0c211bSAndreas Gohr    }
130*ef0c211bSAndreas Gohr
131*ef0c211bSAndreas Gohr    #endregion
132*ef0c211bSAndreas Gohr
133*ef0c211bSAndreas Gohr    #region Element positioning functions
134*ef0c211bSAndreas Gohr
135*ef0c211bSAndreas Gohr    /**
136*ef0c211bSAndreas 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) {
143*ef0c211bSAndreas 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    /**
153*ef0c211bSAndreas Gohr     * Replaces an existing element with a new one
154*ef0c211bSAndreas Gohr     *
155*ef0c211bSAndreas Gohr     * @param Element $element the new element
156*ef0c211bSAndreas Gohr     * @param $pos 0-based position of the element to replace
157*ef0c211bSAndreas Gohr     */
158*ef0c211bSAndreas Gohr    public function replaceElement(Element $element, $pos) {
159*ef0c211bSAndreas Gohr        if(is_a($element, '\dokuwiki\Form')) throw new \InvalidArgumentException('You can\'t add a form to a form');
160*ef0c211bSAndreas Gohr        array_splice($this->elements, $pos, 1, array($element));
161*ef0c211bSAndreas Gohr    }
162*ef0c211bSAndreas Gohr
163*ef0c211bSAndreas Gohr    /**
164*ef0c211bSAndreas Gohr     * Remove an element from the form completely
165*ef0c211bSAndreas Gohr     *
166*ef0c211bSAndreas Gohr     * @param $pos 0-based position of the element to remove
167*ef0c211bSAndreas Gohr     */
168*ef0c211bSAndreas Gohr    public function removeElement($pos) {
169*ef0c211bSAndreas Gohr        array_splice($this->elements, $pos, 1);
170*ef0c211bSAndreas Gohr    }
171*ef0c211bSAndreas Gohr
172*ef0c211bSAndreas Gohr    #endregion
173*ef0c211bSAndreas Gohr
174*ef0c211bSAndreas Gohr    #region Element adding functions
175*ef0c211bSAndreas Gohr
176*ef0c211bSAndreas 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) {
3191f5d8b65SAndreas Gohr                    //close previous feldset
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