|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Radio checkbox fields. |
|
5
|
|
|
*/ |
|
6
|
|
|
class HTMLRadioField extends HTMLFormField { |
|
7
|
|
|
/** |
|
8
|
|
|
* @param array $params |
|
9
|
|
|
* In adition to the usual HTMLFormField parameters, this can take the following fields: |
|
10
|
|
|
* - flatlist: If given, the options will be displayed on a single line (wrapping to following |
|
11
|
|
|
* lines if necessary), rather than each one on a line of its own. This is desirable mostly |
|
12
|
|
|
* for very short lists of concisely labelled options. |
|
13
|
|
|
*/ |
|
14
|
|
|
public function __construct( $params ) { |
|
15
|
|
|
parent::__construct( $params ); |
|
16
|
|
|
|
|
17
|
|
|
if ( isset( $params['flatlist'] ) ) { |
|
18
|
|
|
$this->mClass .= ' mw-htmlform-flatlist'; |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
function validate( $value, $alldata ) { |
|
23
|
|
|
$p = parent::validate( $value, $alldata ); |
|
24
|
|
|
|
|
25
|
|
|
if ( $p !== true ) { |
|
26
|
|
|
return $p; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if ( !is_string( $value ) && !is_int( $value ) ) { |
|
30
|
|
|
return false; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$validOptions = HTMLFormField::flattenOptions( $this->getOptions() ); |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
if ( in_array( strval( $value ), $validOptions, true ) ) { |
|
36
|
|
|
return true; |
|
37
|
|
|
} else { |
|
38
|
|
|
return $this->msg( 'htmlform-select-badoption' )->parse(); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* This returns a block of all the radio options, in one cell. |
|
44
|
|
|
* @see includes/HTMLFormField#getInputHTML() |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $value |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
function getInputHTML( $value ) { |
|
51
|
|
|
$html = $this->formatOptions( $this->getOptions(), strval( $value ) ); |
|
52
|
|
|
|
|
53
|
|
|
return $html; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
function getInputOOUI( $value ) { |
|
57
|
|
|
$options = []; |
|
58
|
|
|
foreach ( $this->getOptions() as $label => $data ) { |
|
|
|
|
|
|
59
|
|
|
$options[] = [ |
|
60
|
|
|
'data' => $data, |
|
61
|
|
|
'label' => $this->mOptionsLabelsNotFromMessage ? new OOUI\HtmlSnippet( $label ) : $label, |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return new OOUI\RadioSelectInputWidget( [ |
|
66
|
|
|
'name' => $this->mName, |
|
67
|
|
|
'id' => $this->mID, |
|
68
|
|
|
'value' => $value, |
|
69
|
|
|
'options' => $options, |
|
70
|
|
|
] + OOUI\Element::configFromHtmlAttributes( |
|
71
|
|
|
$this->getAttributes( [ 'disabled', 'tabindex' ] ) |
|
72
|
|
|
) ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function shouldInfuseOOUI() { |
|
76
|
|
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
function formatOptions( $options, $value ) { |
|
80
|
|
|
global $wgUseMediaWikiUIEverywhere; |
|
81
|
|
|
|
|
82
|
|
|
$html = ''; |
|
83
|
|
|
|
|
84
|
|
|
$attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] ); |
|
85
|
|
|
$elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ]; |
|
86
|
|
|
|
|
87
|
|
|
# @todo Should this produce an unordered list perhaps? |
|
88
|
|
|
foreach ( $options as $label => $info ) { |
|
89
|
|
|
if ( is_array( $info ) ) { |
|
90
|
|
|
$html .= Html::rawElement( 'h1', [], $label ) . "\n"; |
|
91
|
|
|
$html .= $this->formatOptions( $info, $value ); |
|
92
|
|
|
} else { |
|
93
|
|
|
$id = Sanitizer::escapeId( $this->mID . "-$info" ); |
|
94
|
|
|
$classes = [ 'mw-htmlform-flatlist-item' ]; |
|
95
|
|
|
if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) { |
|
96
|
|
|
$classes[] = 'mw-ui-radio'; |
|
97
|
|
|
} |
|
98
|
|
|
$radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + [ 'id' => $id ] ); |
|
99
|
|
|
$radio .= ' ' . call_user_func( $elementFunc, 'label', [ 'for' => $id ], $label ); |
|
100
|
|
|
|
|
101
|
|
|
$html .= ' ' . Html::rawElement( |
|
102
|
|
|
'div', |
|
103
|
|
|
[ 'class' => $classes ], |
|
104
|
|
|
$radio |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $html; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function needsLabel() { |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.