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