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