summaryrefslogtreecommitdiff
path: root/inc/Form/Form.php
blob: 30e16615cccf9d8e00a0662c947d4d647362e3b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
namespace dokuwiki\Form;

/**
 * Class Form
 *
 * Represents the whole Form. This is what you work on, and add Elements to
 *
 * @package dokuwiki\Form
 */
class Form extends Element {

    /**
     * @var array name value pairs for hidden values
     */
    protected $hidden = array();

    /**
     * @var Element[] the elements of the form
     */
    protected $elements = array();

    /**
     * Creates a new, empty form with some default attributes
     *
     * @param array $attributes
     */
    public function __construct($attributes = array()) {
        global $ID;

        parent::__construct('form', $attributes);

        // use the current URL as default action
        if(!$this->attr('action')) {
            $get = $_GET;
            if(isset($get['id'])) unset($get['id']);
            $self = wl($ID, $get, false, '&'); //attributes are escaped later
            $this->attr('action', $self);
        }

        // post is default
        if(!$this->attr('method')) {
            $this->attr('method', 'post');
        }

        // we like UTF-8
        if(!$this->attr('accept-charset')) {
            $this->attr('accept-charset', 'utf-8');
        }

        // add the security token by default
        $this->setHiddenField('sectok', getSecurityToken());

        // identify this as a new form based form in HTML
        $this->addClass('doku_form');
    }

    /**
     * Sets a hidden field
     *
     * @param $name
     * @param $value
     * @return $this
     */
    public function setHiddenField($name, $value) {
        $this->hidden[$name] = $value;
        return $this;
    }

    #region Element adding functions

    /**
     * Adds an element to the end of the form
     *
     * @param Element $element
     * @param int $pos 0-based position in the form, -1 for at the end
     * @return Element
     */
    public function addElement(Element $element, $pos = -1) {
        if(is_a($element, 'Doku_Form2')) throw new \InvalidArgumentException('You can\'t add a form to a form');
        if($pos < 0) {
            $this->elements[] = $element;
        } else {
            array_splice($this->elements, $pos, 0, array($element));
        }
        return $element;
    }

    /**
     * Adds a text input field
     *
     * @param $name
     * @param $label
     * @param int $pos
     * @return InputElement
     */
    public function addTextInput($name, $label = '', $pos = -1) {
        return $this->addElement(new InputElement('text', $name, $label), $pos);
    }

    /**
     * Adds a password input field
     *
     * @param $name
     * @param $label
     * @param int $pos
     * @return InputElement
     */
    public function addPasswordInput($name, $label = '', $pos = -1) {
        return $this->addElement(new InputElement('password', $name, $label), $pos);
    }

    /**
     * Adds a radio button field
     *
     * @param $name
     * @param $label
     * @param int $pos
     * @return CheckableElement
     */
    public function addRadioButton($name, $label = '', $pos = -1) {
        return $this->addElement(new CheckableElement('radio', $name, $label), $pos);
    }

    /**
     * Adds a checkbox field
     *
     * @param $name
     * @param $label
     * @param int $pos
     * @return CheckableElement
     */
    public function addCheckbox($name, $label = '', $pos = -1) {
        return $this->addElement(new CheckableElement('checkbox', $name, $label), $pos);
    }

    /**
     * Adds a textarea field
     *
     * @param $name
     * @param $label
     * @param int $pos
     * @return TextareaElement
     */
    public function addTextarea($name, $label = '', $pos = -1) {
        return $this->addElement(new TextareaElement($name, $label), $pos);
    }

    /**
     * Add fixed HTML to the form
     *
     * @param $html
     * @param int $pos
     * @return HTMLElement
     */
    public function addHTML($html, $pos = -1) {
        return $this->addElement(new HTMLElement($html), $pos);
    }

    /**
     * Add a closed HTML tag to the form
     *
     * @param $tag
     * @param int $pos
     * @return TagElement
     */
    public function addTag($tag, $pos = -1) {
        return $this->addElement(new TagElement($tag), $pos);
    }

    /**
     * Add an open HTML tag to the form
     *
     * Be sure to close it again!
     *
     * @param $tag
     * @param int $pos
     * @return TagOpenElement
     */
    public function addTagOpen($tag, $pos = -1) {
        return $this->addElement(new TagOpenElement($tag), $pos);
    }

    /**
     * Add a closing HTML tag to the form
     *
     * Be sure it had been opened before
     *
     * @param $tag
     * @param int $pos
     * @return TagCloseElement
     */
    public function addTagClose($tag, $pos = -1) {
        return $this->addElement(new TagCloseElement($tag), $pos);
    }

    /**
     * Open a Fieldset
     *
     * @param $legend
     * @param int $pos
     * @return FieldsetOpenElement
     */
    public function addFieldsetOpen($legend = '', $pos = -1) {
        return $this->addElement(new FieldsetOpenElement($legend), $pos);
    }

    /**
     * Close a fieldset
     *
     * @param int $pos
     * @return TagCloseElement
     */
    public function addFieldsetClose($pos = -1) {
        return $this->addElement(new FieldsetCloseElement(), $pos);
    }

    #endregion

    /**
     * Adjust the elements so that fieldset open and closes are matching
     */
    protected function balanceFieldsets() {
        $lastclose = 0;
        $isopen = false;
        $len = count($this->elements);

        for($pos = 0; $pos < $len; $pos++) {
            $type = $this->elements[$pos]->getType();
            if($type == 'fieldsetopen') {
                if($isopen) {
                    //close previous feldset
                    $this->addFieldsetClose($pos);
                    $lastclose = $pos + 1;
                    $pos++;
                    $len++;
                }
                $isopen = true;
            } else if($type == 'fieldsetclose') {
                if(!$isopen) {
                    // make sure there was a fieldsetopen
                    // either right after the last close or at the begining
                    $this->addFieldsetOpen('', $lastclose);
                    $len++;
                    $pos++;
                }
                $lastclose = $pos;
                $isopen = false;
            }
        }

        // close open fieldset at the end
        if($isopen) {
            $this->addFieldsetClose();
        }
    }

    /**
     * The HTML representation of the whole form
     *
     * @return string
     */
    public function toHTML() {
        $this->balanceFieldsets();

        $html = '<form ' . buildAttributes($this->attrs()) . '>' . DOKU_LF;

        foreach($this->hidden as $name => $value) {
            $html .= '<input type="hidden" name="' . $name . '" value="' . formText($value) . '" />' . DOKU_LF;
        }

        foreach($this->elements as $element) {
            $html .= $element->toHTML() . DOKU_LF;
        }

        $html .= '</form>' . DOKU_LF;

        return $html;
    }
}