Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.46% covered (success)
98.46%
64 / 65
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PFRadioButtonInput
98.46% covered (success)
98.46%
64 / 65
66.67% covered (warning)
66.67%
2 / 3
21
0.00% covered (danger)
0.00%
0 / 1
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHTML
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
1 / 1
19
 getHtmlText
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @file
4 * @ingroup PF
5 */
6
7/**
8 * @ingroup PFFormInput
9 */
10class PFRadioButtonInput extends PFEnumInput {
11
12    public static function getName(): string {
13        return 'radiobutton';
14    }
15
16    public static function getHTML( $cur_value, $input_name, $is_mandatory, $is_disabled, array $other_args ) {
17        global $wgPageFormsTabIndex, $wgPageFormsFieldNum;
18
19        if ( array_key_exists( 'possible_values', $other_args ) ) {
20            $possible_values = $other_args['possible_values'];
21        } elseif (
22            array_key_exists( 'property_type', $other_args ) &&
23            $other_args['property_type'] == '_boo'
24        ) {
25            // If it's a Boolean property, display 'Yes' and 'No'
26            // as the values.
27            $possible_values = [
28                PFUtils::getWordForYesOrNo( true ),
29                PFUtils::getWordForYesOrNo( false ),
30            ];
31        } else {
32            $possible_values = [];
33        }
34
35        // Add a "None" value at the beginning, unless this is a
36        // mandatory field and there's a current value in place (either
37        // through a default value or because we're editing an existing
38        // page).
39        if ( !$is_mandatory || $cur_value === '' ) {
40            array_unshift( $possible_values, '' );
41        }
42
43        // If $cur_value is an invalid value (not null, and not one
44        // of the allowed options), set it to blank, so it can show
45        // up as "None" (if "None" is one of the options).
46        if ( $cur_value !== null && !in_array( $cur_value, $possible_values ) ) {
47            $cur_value = '';
48        }
49
50        $text = "\n";
51        $itemClass = 'radioButtonItem';
52        if ( array_key_exists( 'class', $other_args ) ) {
53            $itemClass .= ' ' . $other_args['class'];
54        }
55
56        foreach ( $possible_values as $possible_value ) {
57            $wgPageFormsTabIndex++;
58            $wgPageFormsFieldNum++;
59            $input_id = "input_$wgPageFormsFieldNum";
60
61            $radiobutton_attrs = [
62                'value' => $possible_value,
63                'id' => $input_id,
64                'tabindex' => $wgPageFormsTabIndex,
65            ];
66            if ( array_key_exists( 'origName', $other_args ) ) {
67                $radiobutton_attrs['origname'] = $other_args['origName'];
68            }
69            $isChecked = ( $cur_value == $possible_value );
70            if ( $is_disabled ) {
71                $radiobutton_attrs['disabled'] = true;
72            }
73            if ( $possible_value === '' ) {
74                // blank/"None" value
75                $label = wfMessage( 'pf_formedit_none' )->text();
76            } elseif (
77                array_key_exists( 'value_labels', $other_args ) &&
78                is_array( $other_args['value_labels'] ) &&
79                array_key_exists( $possible_value, $other_args['value_labels'] )
80            ) {
81                $label = htmlspecialchars( $other_args['value_labels'][$possible_value] );
82            } else {
83                $label = $possible_value;
84            }
85
86            $itemAttrs = [ 'class' => $itemClass ];
87            $text .= "\t" . Html::rawElement( 'label', $itemAttrs,
88                Html::radio( $input_name, $isChecked, $radiobutton_attrs ) .
89                '&nbsp;' . $label ) . "\n";
90        }
91
92        $spanClass = 'radioButtonSpan';
93        if ( array_key_exists( 'class', $other_args ) ) {
94            $spanClass .= ' ' . $other_args['class'];
95        }
96        if ( $is_mandatory ) {
97            $spanClass .= ' mandatoryFieldSpan';
98        }
99
100        $spanID = "span_$wgPageFormsFieldNum";
101
102        // Do the 'show on select' handling.
103        if ( array_key_exists( 'show on select', $other_args ) ) {
104            $spanClass .= ' pfShowIfChecked';
105            PFFormUtils::setShowOnSelect( $other_args['show on select'], $spanID );
106        }
107        $spanAttrs = [
108            'id' => $spanID,
109            'class' => $spanClass
110        ];
111        $text = Html::rawElement( 'span', $spanAttrs, $text );
112
113        return $text;
114    }
115
116    /**
117     * Returns the HTML code to be included in the output page for this input.
118     * @return string
119     */
120    public function getHtmlText(): string {
121        return self::getHTML(
122            $this->mCurrentValue,
123            $this->mInputName,
124            $this->mIsMandatory,
125            $this->mIsDisabled,
126            $this->mOtherArgs
127        );
128    }
129}