xref: /dokuwiki/inc/Form/Form.php (revision de19515f04567db78bd41d5bff68a88bfb8c2a22)
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
7012a4e4d1SAndreas Gohr    /**
7112a4e4d1SAndreas Gohr     * Adds an element to the end of the form
7212a4e4d1SAndreas Gohr     *
7312a4e4d1SAndreas Gohr     * @param Element $element
7412a4e4d1SAndreas Gohr     * @param int $pos 0-based position in the form, -1 for at the end
7512a4e4d1SAndreas Gohr     * @return Element
7612a4e4d1SAndreas Gohr     */
7712a4e4d1SAndreas Gohr    public function addElement(Element $element, $pos = -1) {
7812a4e4d1SAndreas Gohr        if(is_a($element, 'Doku_Form2')) throw new \InvalidArgumentException('You can\'t add a form to a form');
7912a4e4d1SAndreas Gohr        if($pos < 0) {
8012a4e4d1SAndreas Gohr            $this->elements[] = $element;
8112a4e4d1SAndreas Gohr        } else {
8212a4e4d1SAndreas Gohr            array_splice($this->elements, $pos, 0, array($element));
8312a4e4d1SAndreas Gohr        }
8412a4e4d1SAndreas Gohr        return $element;
8512a4e4d1SAndreas Gohr    }
8612a4e4d1SAndreas Gohr
8712a4e4d1SAndreas Gohr    /**
8812a4e4d1SAndreas Gohr     * Adds a text input field
8912a4e4d1SAndreas Gohr     *
9012a4e4d1SAndreas Gohr     * @param $name
9112a4e4d1SAndreas Gohr     * @param $label
9212a4e4d1SAndreas Gohr     * @param int $pos
9312a4e4d1SAndreas Gohr     * @return InputElement
9412a4e4d1SAndreas Gohr     */
95*de19515fSAndreas Gohr    public function addTextInput($name, $label = '', $pos = -1) {
9612a4e4d1SAndreas Gohr        return $this->addElement(new InputElement('text', $name, $label), $pos);
9712a4e4d1SAndreas Gohr    }
9812a4e4d1SAndreas Gohr
9912a4e4d1SAndreas Gohr    /**
10012a4e4d1SAndreas Gohr     * Adds a password input field
10112a4e4d1SAndreas Gohr     *
10212a4e4d1SAndreas Gohr     * @param $name
10312a4e4d1SAndreas Gohr     * @param $label
10412a4e4d1SAndreas Gohr     * @param int $pos
10512a4e4d1SAndreas Gohr     * @return InputElement
10612a4e4d1SAndreas Gohr     */
107*de19515fSAndreas Gohr    public function addPasswordInput($name, $label = '', $pos = -1) {
10812a4e4d1SAndreas Gohr        return $this->addElement(new InputElement('password', $name, $label), $pos);
10912a4e4d1SAndreas Gohr    }
11012a4e4d1SAndreas Gohr
11112a4e4d1SAndreas Gohr    /**
11212a4e4d1SAndreas Gohr     * Adds a radio button field
11312a4e4d1SAndreas Gohr     *
11412a4e4d1SAndreas Gohr     * @param $name
11512a4e4d1SAndreas Gohr     * @param $label
11612a4e4d1SAndreas Gohr     * @param int $pos
11712a4e4d1SAndreas Gohr     * @return CheckableElement
11812a4e4d1SAndreas Gohr     */
119*de19515fSAndreas Gohr    public function addRadioButton($name, $label = '', $pos = -1) {
12012a4e4d1SAndreas Gohr        return $this->addElement(new CheckableElement('radio', $name, $label), $pos);
12112a4e4d1SAndreas Gohr    }
12212a4e4d1SAndreas Gohr
12312a4e4d1SAndreas Gohr    /**
12412a4e4d1SAndreas Gohr     * Adds a checkbox field
12512a4e4d1SAndreas Gohr     *
12612a4e4d1SAndreas Gohr     * @param $name
12712a4e4d1SAndreas Gohr     * @param $label
12812a4e4d1SAndreas Gohr     * @param int $pos
12912a4e4d1SAndreas Gohr     * @return CheckableElement
13012a4e4d1SAndreas Gohr     */
131*de19515fSAndreas Gohr    public function addCheckbox($name, $label = '', $pos = -1) {
13212a4e4d1SAndreas Gohr        return $this->addElement(new CheckableElement('checkbox', $name, $label), $pos);
13312a4e4d1SAndreas Gohr    }
13412a4e4d1SAndreas Gohr
13512a4e4d1SAndreas Gohr    /**
13612a4e4d1SAndreas Gohr     * Adds a textarea field
13712a4e4d1SAndreas Gohr     *
13812a4e4d1SAndreas Gohr     * @param $name
13912a4e4d1SAndreas Gohr     * @param $label
14012a4e4d1SAndreas Gohr     * @param int $pos
14112a4e4d1SAndreas Gohr     * @return TextareaElement
14212a4e4d1SAndreas Gohr     */
143*de19515fSAndreas Gohr    public function addTextarea($name, $label = '', $pos = -1) {
14412a4e4d1SAndreas Gohr        return $this->addElement(new TextareaElement($name, $label), $pos);
14512a4e4d1SAndreas Gohr    }
14612a4e4d1SAndreas Gohr
14712a4e4d1SAndreas Gohr    /**
148*de19515fSAndreas Gohr     * Add fixed HTML to the form
149*de19515fSAndreas Gohr     *
150*de19515fSAndreas Gohr     * @param $html
151*de19515fSAndreas Gohr     * @param int $pos
152*de19515fSAndreas Gohr     * @return Element
153*de19515fSAndreas Gohr     */
154*de19515fSAndreas Gohr    public function addHTML($html, $pos = -1) {
155*de19515fSAndreas Gohr        return $this->addElement(new HTMLElement($html), $pos);
156*de19515fSAndreas Gohr    }
157*de19515fSAndreas Gohr
158*de19515fSAndreas Gohr    protected function balanceFieldsets() {
159*de19515fSAndreas Gohr        //todo implement!
160*de19515fSAndreas Gohr    }
161*de19515fSAndreas Gohr
162*de19515fSAndreas Gohr    /**
16312a4e4d1SAndreas Gohr     * The HTML representation of the whole form
16412a4e4d1SAndreas Gohr     *
16512a4e4d1SAndreas Gohr     * @return string
16612a4e4d1SAndreas Gohr     */
16712a4e4d1SAndreas Gohr    public function toHTML() {
168*de19515fSAndreas Gohr        $this->balanceFieldsets();
169*de19515fSAndreas Gohr
17012a4e4d1SAndreas Gohr        $html = '<form ' . buildAttributes($this->attrs()) . '>' . DOKU_LF;
17112a4e4d1SAndreas Gohr
17212a4e4d1SAndreas Gohr        foreach($this->hidden as $name => $value) {
17312a4e4d1SAndreas Gohr            $html .= '<input type="hidden" name="' . $name . '" value="' . formText($value) . '" />' . DOKU_LF;
17412a4e4d1SAndreas Gohr        }
17512a4e4d1SAndreas Gohr
17612a4e4d1SAndreas Gohr        foreach($this->elements as $element) {
17712a4e4d1SAndreas Gohr            $html .= $element->toHTML() . DOKU_LF;
17812a4e4d1SAndreas Gohr        }
17912a4e4d1SAndreas Gohr
18012a4e4d1SAndreas Gohr        $html .= '</form>' . DOKU_LF;
18112a4e4d1SAndreas Gohr
18212a4e4d1SAndreas Gohr        return $html;
18312a4e4d1SAndreas Gohr    }
18412a4e4d1SAndreas Gohr}
185