Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
PFCheckboxInput
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 6
306
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
 getDefaultPropTypes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDefaultCargoTypes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHTML
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 1
110
 getParameters
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getHtmlText
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @file
4 * @ingroup PF
5 */
6
7/**
8 * @ingroup PFFormInput
9 */
10class PFCheckboxInput extends PFFormInput {
11
12    public static function getName(): string {
13        return 'checkbox';
14    }
15
16    public static function getDefaultPropTypes() {
17        return [ '_boo' => [] ];
18    }
19
20    public static function getDefaultCargoTypes() {
21        return [ 'Boolean' => [] ];
22    }
23
24    public static function getHTML( $cur_value, $input_name, $is_mandatory, $is_disabled, array $other_args ) {
25        global $wgPageFormsTabIndex, $wgPageFormsFieldNum;
26
27        $className = '';
28        if ( array_key_exists( 'class', $other_args ) ) {
29            $className .= ' ' . $other_args['class'];
30        }
31        $inputID = "input_$wgPageFormsFieldNum";
32        if ( array_key_exists( 'show on select', $other_args ) ) {
33            $className .= ' pfShowIfCheckedCheckbox';
34            PFFormUtils::setShowOnSelect( $other_args['show on select'], $inputID, true );
35        }
36
37        // Can show up here either as an array or a string, depending on
38        // whether it came from user input or a wiki page
39        if ( !isset( $cur_value ) ) {
40            $isChecked = false;
41        } elseif ( is_array( $cur_value ) ) {
42            $isChecked = array_key_exists( 'value', $cur_value ) && $cur_value['value'] == 'on';
43        } else {
44            // Default to false - no need to check if it matches
45            // a 'false' word.
46            $lowercaseCurValue = strtolower( trim( $cur_value ) );
47
48            $possibleYesMessages = [
49                strtolower( wfMessage( 'htmlform-yes' )->inContentLanguage()->text() ),
50                // Add in '1', and some hardcoded English.
51                '1', 'yes', 'true'
52            ];
53
54            // Add values from Semantic MediaWiki, if it's installed.
55            if ( wfMessage( 'smw_true_words' )->exists() ) {
56                $smwTrueWords = explode( ',', wfMessage( 'smw_true_words' )->inContentLanguage()->text() );
57                foreach ( $smwTrueWords as $smwTrueWord ) {
58                    $possibleYesMessages[] = strtolower( trim( $smwTrueWord ) );
59                }
60            }
61            $isChecked = in_array( $lowercaseCurValue, $possibleYesMessages );
62        }
63        $text = "\t" . Html::hidden( $input_name . '[is_checkbox]', 'true' ) . "\n";
64        $checkboxAttrs = [
65            'selected' => $isChecked,
66            'id' => $inputID,
67            'classes' => [ $className ],
68            'tabIndex' => $wgPageFormsTabIndex,
69            'name' => "{$input_name}[value]"
70        ];
71        if ( $is_disabled ) {
72            $checkboxAttrs['disabled'] = true;
73        }
74        if ( isset( $other_args['label'] ) ) {
75            $labelText = new OOUI\HtmlSnippet( $other_args['label'] );
76            $labelAttrs = [
77                'label' => $labelText,
78                'align' => 'inline',
79                'for' => $inputID
80            ];
81        } else {
82            $labelAttrs = [];
83        }
84        $text .= new OOUI\FieldLayout(
85            new OOUI\CheckboxInputWidget( $checkboxAttrs ),
86            $labelAttrs
87        );
88        $text = Html::rawElement(
89            'div',
90            [ 'class' => 'pf-checkbox-input-container' ],
91            $text
92        );
93        return $text;
94    }
95
96    public static function getParameters() {
97        // Remove the 'mandatory' option - it doesn't make sense for
98        // checkboxes.
99        $params = [];
100        foreach ( parent::getParameters() as $param ) {
101            if ( $param['name'] != 'mandatory' ) {
102                $params[] = $param;
103            }
104        }
105        return $params;
106    }
107
108    /**
109     * Returns the HTML code to be included in the output page for this input.
110     * @return string
111     */
112    public function getHtmlText(): string {
113        return self::getHTML(
114            $this->mCurrentValue,
115            $this->mInputName,
116            $this->mIsMandatory,
117            $this->mIsDisabled,
118            $this->mOtherArgs
119        );
120    }
121}