xref: /dokuwiki/inc/Form/LegacyForm.php (revision 9d01c1d91a93cf50f37d1486481a6493e98be821)
1de19515fSAndreas Gohr<?php
2*9d01c1d9SSatoshi Sahara
3de19515fSAndreas Gohrnamespace dokuwiki\Form;
4de19515fSAndreas Gohr
5de19515fSAndreas Gohr/**
6de19515fSAndreas Gohr * Class LegacyForm
7de19515fSAndreas Gohr *
8de19515fSAndreas Gohr * Provides a compatibility layer to the old Doku_Form API
9de19515fSAndreas Gohr *
10de19515fSAndreas Gohr * This can be used to work with the modern API on forms provided by old events for
11de19515fSAndreas Gohr * example. When you start new forms, just use Form\Form
12de19515fSAndreas Gohr *
13de19515fSAndreas Gohr * @package dokuwiki\Form
14de19515fSAndreas Gohr */
15*9d01c1d9SSatoshi Saharaclass LegacyForm extends Form
16*9d01c1d9SSatoshi Sahara{
17de19515fSAndreas Gohr    /**
18de19515fSAndreas Gohr     * Creates a new modern form from an old legacy Doku_Form
19de19515fSAndreas Gohr     *
20de19515fSAndreas Gohr     * @param \Doku_Form $oldform
21de19515fSAndreas Gohr     */
22*9d01c1d9SSatoshi Sahara    public function __construct(\Doku_Form $oldform)
23*9d01c1d9SSatoshi Sahara    {
24de19515fSAndreas Gohr        parent::__construct($oldform->params);
25de19515fSAndreas Gohr
26de19515fSAndreas Gohr        $this->hidden = $oldform->_hidden;
27de19515fSAndreas Gohr
28de19515fSAndreas Gohr        foreach ($oldform->_content as $element) {
29de19515fSAndreas Gohr            list($ctl, $attr) = $this->parseLegacyAttr($element);
30de19515fSAndreas Gohr
31de19515fSAndreas Gohr            if (is_array($element)) {
32de19515fSAndreas Gohr                switch ($ctl['elem']) {
33de19515fSAndreas Gohr                    case 'wikitext':
34de19515fSAndreas Gohr                        $this->addTextarea('wikitext')
35de19515fSAndreas Gohr                             ->attrs($attr)
36de19515fSAndreas Gohr                             ->id('wiki__text')
37de19515fSAndreas Gohr                             ->val($ctl['text'])
38de19515fSAndreas Gohr                             ->addClass($ctl['class']);
39de19515fSAndreas Gohr                        break;
40de19515fSAndreas Gohr                    case 'textfield':
41de19515fSAndreas Gohr                        $this->addTextInput($ctl['name'], $ctl['text'])
42de19515fSAndreas Gohr                             ->attrs($attr)
43de19515fSAndreas Gohr                             ->id($ctl['id'])
44de19515fSAndreas Gohr                             ->addClass($ctl['class']);
45de19515fSAndreas Gohr                        break;
46de19515fSAndreas Gohr                    case 'passwordfield':
47de19515fSAndreas Gohr                        $this->addPasswordInput($ctl['name'], $ctl['text'])
48de19515fSAndreas Gohr                             ->attrs($attr)
49de19515fSAndreas Gohr                             ->id($ctl['id'])
50de19515fSAndreas Gohr                             ->addClass($ctl['class']);
51de19515fSAndreas Gohr                        break;
52de19515fSAndreas Gohr                    case 'checkboxfield':
53de19515fSAndreas Gohr                        $this->addCheckbox($ctl['name'], $ctl['text'])
54de19515fSAndreas Gohr                             ->attrs($attr)
55de19515fSAndreas Gohr                             ->id($ctl['id'])
56de19515fSAndreas Gohr                             ->addClass($ctl['class']);
57de19515fSAndreas Gohr                        break;
58de19515fSAndreas Gohr                    case 'radiofield':
59de19515fSAndreas Gohr                        $this->addRadioButton($ctl['name'], $ctl['text'])
60de19515fSAndreas Gohr                             ->attrs($attr)
61de19515fSAndreas Gohr                             ->id($ctl['id'])
62de19515fSAndreas Gohr                             ->addClass($ctl['class']);
63de19515fSAndreas Gohr                        break;
64de19515fSAndreas Gohr                    case 'tag':
6564744a10SAndreas Gohr                        $this->addTag($ctl['tag'])
6664744a10SAndreas Gohr                             ->attrs($attr)
6764744a10SAndreas Gohr                             ->attr('name', $ctl['name'])
6864744a10SAndreas Gohr                             ->id($ctl['id'])
6964744a10SAndreas Gohr                             ->addClass($ctl['class']);
7064744a10SAndreas Gohr                        break;
71de19515fSAndreas Gohr                    case 'opentag':
7264744a10SAndreas Gohr                        $this->addTagOpen($ctl['tag'])
7364744a10SAndreas Gohr                             ->attrs($attr)
7464744a10SAndreas Gohr                             ->attr('name', $ctl['name'])
7564744a10SAndreas Gohr                             ->id($ctl['id'])
7664744a10SAndreas Gohr                             ->addClass($ctl['class']);
7764744a10SAndreas Gohr                        break;
78de19515fSAndreas Gohr                    case 'closetag':
7964744a10SAndreas Gohr                        $this->addTagClose($ctl['tag']);
8064744a10SAndreas Gohr                        break;
81de19515fSAndreas Gohr                    case 'openfieldset':
8264744a10SAndreas Gohr                        $this->addFieldsetOpen($ctl['legend'])
8364744a10SAndreas Gohr                            ->attrs($attr)
8464744a10SAndreas Gohr                            ->attr('name', $ctl['name'])
8564744a10SAndreas Gohr                            ->id($ctl['id'])
8664744a10SAndreas Gohr                            ->addClass($ctl['class']);
8764744a10SAndreas Gohr                        break;
88de19515fSAndreas Gohr                    case 'closefieldset':
8964744a10SAndreas Gohr                        $this->addFieldsetClose();
9064744a10SAndreas Gohr                        break;
91de19515fSAndreas Gohr                    case 'button':
92de19515fSAndreas Gohr                    case 'field':
93de19515fSAndreas Gohr                    case 'fieldright':
94de19515fSAndreas Gohr                    case 'filefield':
95de19515fSAndreas Gohr                    case 'menufield':
96de19515fSAndreas Gohr                    case 'listboxfield':
97de19515fSAndreas Gohr                        throw new \UnexpectedValueException('Unsupported legacy field ' . $ctl['elem']);
98de19515fSAndreas Gohr                        break;
99de19515fSAndreas Gohr                    default:
100de19515fSAndreas Gohr                        throw new \UnexpectedValueException('Unknown legacy field ' . $ctl['elem']);
101de19515fSAndreas Gohr
102de19515fSAndreas Gohr                }
103de19515fSAndreas Gohr            } else {
104de19515fSAndreas Gohr                $this->addHTML($element);
105de19515fSAndreas Gohr            }
106de19515fSAndreas Gohr        }
107de19515fSAndreas Gohr
108de19515fSAndreas Gohr    }
109de19515fSAndreas Gohr
110de19515fSAndreas Gohr    /**
111de19515fSAndreas Gohr     * Parses out what is the elements attributes and what is control info
112de19515fSAndreas Gohr     *
113de19515fSAndreas Gohr     * @param array $legacy
114de19515fSAndreas Gohr     * @return array
115de19515fSAndreas Gohr     */
116*9d01c1d9SSatoshi Sahara    protected function parseLegacyAttr($legacy)
117*9d01c1d9SSatoshi Sahara    {
118de19515fSAndreas Gohr        $attributes = array();
119de19515fSAndreas Gohr        $control = array();
120de19515fSAndreas Gohr
121de19515fSAndreas Gohr        foreach ($legacy as $key => $val) {
1222401f18dSSyntaxseed            if ($key[0] == '_') {
123de19515fSAndreas Gohr                $control[substr($key, 1)] = $val;
124de19515fSAndreas Gohr            } elseif($key == 'name') {
125de19515fSAndreas Gohr                $control[$key] = $val;
126de19515fSAndreas Gohr            } elseif($key == 'id') {
127de19515fSAndreas Gohr                $control[$key] = $val;
128de19515fSAndreas Gohr            } else {
129de19515fSAndreas Gohr                $attributes[$key] = $val;
130de19515fSAndreas Gohr            }
131de19515fSAndreas Gohr        }
132de19515fSAndreas Gohr
133de19515fSAndreas Gohr        return array($control, $attributes);
134de19515fSAndreas Gohr    }
135de19515fSAndreas Gohr
136de19515fSAndreas Gohr    /**
137de19515fSAndreas Gohr     * Translates our types to the legacy types
138de19515fSAndreas Gohr     *
139de19515fSAndreas Gohr     * @param string $type
140de19515fSAndreas Gohr     * @return string
141de19515fSAndreas Gohr     */
142*9d01c1d9SSatoshi Sahara    protected function legacyType($type)
143*9d01c1d9SSatoshi Sahara    {
144de19515fSAndreas Gohr        static $types = array(
145de19515fSAndreas Gohr            'text' => 'textfield',
146de19515fSAndreas Gohr            'password' => 'passwordfield',
147de19515fSAndreas Gohr            'checkbox' => 'checkboxfield',
148de19515fSAndreas Gohr            'radio' => 'radiofield',
14964744a10SAndreas Gohr            'tagopen' => 'opentag',
15064744a10SAndreas Gohr            'tagclose' => 'closetag',
15164744a10SAndreas Gohr            'fieldsetopen' => 'openfieldset',
15264744a10SAndreas Gohr            'fieldsetclose' => 'closefieldset',
153de19515fSAndreas Gohr        );
154de19515fSAndreas Gohr        if (isset($types[$type])) return $types[$type];
155de19515fSAndreas Gohr        return $type;
156de19515fSAndreas Gohr    }
157de19515fSAndreas Gohr
158de19515fSAndreas Gohr    /**
159de19515fSAndreas Gohr     * Creates an old legacy form from this modern form's data
160de19515fSAndreas Gohr     *
161de19515fSAndreas Gohr     * @return \Doku_Form
162de19515fSAndreas Gohr     */
163*9d01c1d9SSatoshi Sahara    public function toLegacy()
164*9d01c1d9SSatoshi Sahara    {
165de19515fSAndreas Gohr        $this->balanceFieldsets();
166de19515fSAndreas Gohr
167de19515fSAndreas Gohr        $legacy = new \Doku_Form($this->attrs());
168de19515fSAndreas Gohr        $legacy->_hidden = $this->hidden;
169de19515fSAndreas Gohr        foreach ($this->elements as $element) {
170de19515fSAndreas Gohr            if (is_a($element, 'dokuwiki\Form\HTMLElement')) {
171de19515fSAndreas Gohr                $legacy->_content[] = $element->toHTML();
172de19515fSAndreas Gohr            } elseif (is_a($element, 'dokuwiki\Form\InputElement')) {
173de19515fSAndreas Gohr                /** @var InputElement $element */
174de19515fSAndreas Gohr                $data = $element->attrs();
175de19515fSAndreas Gohr                $data['_elem'] = $this->legacyType($element->getType());
176de19515fSAndreas Gohr                $label = $element->getLabel();
177de19515fSAndreas Gohr                if ($label) {
178de19515fSAndreas Gohr                    $data['_class'] = $label->attr('class');
179de19515fSAndreas Gohr                }
180de19515fSAndreas Gohr                $legacy->_content[] = $data;
181de19515fSAndreas Gohr            }
182de19515fSAndreas Gohr        }
183de19515fSAndreas Gohr
184de19515fSAndreas Gohr        return $legacy;
185de19515fSAndreas Gohr    }
186de19515fSAndreas Gohr}
187