diff options
author | Bartosz Dziewoński <matma.rex@gmail.com> | 2016-07-30 23:59:24 +0200 |
---|---|---|
committer | Bartosz Dziewoński <matma.rex@gmail.com> | 2016-08-01 07:58:56 +0000 |
commit | 15692fa6d4f2d7d15e3c0df8937612d9bdda93bb (patch) | |
tree | b14250670b1385a4a827cee904affc4b024ca471 /includes/htmlform/fields/HTMLRadioField.php | |
parent | 3823c900f15d75a218286d227506ae8877e944ca (diff) | |
download | mediawikicore-15692fa6d4f2d7d15e3c0df8937612d9bdda93bb.tar.gz mediawikicore-15692fa6d4f2d7d15e3c0df8937612d9bdda93bb.zip |
Move HTMLFormField subclasses to a separate directory
It's getting more difficult to navigate the files in includes/htmlform/
with every new field and every new helper class that is being added.
Change-Id: I92ce2356baf6151f17b2440970d5abdf86503820
Diffstat (limited to 'includes/htmlform/fields/HTMLRadioField.php')
-rw-r--r-- | includes/htmlform/fields/HTMLRadioField.php | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/includes/htmlform/fields/HTMLRadioField.php b/includes/htmlform/fields/HTMLRadioField.php new file mode 100644 index 000000000000..e5b5e68f203e --- /dev/null +++ b/includes/htmlform/fields/HTMLRadioField.php @@ -0,0 +1,96 @@ +<?php + +/** + * Radio checkbox fields. + */ +class HTMLRadioField extends HTMLFormField { + function validate( $value, $alldata ) { + $p = parent::validate( $value, $alldata ); + + if ( $p !== true ) { + return $p; + } + + if ( !is_string( $value ) && !is_int( $value ) ) { + return false; + } + + $validOptions = HTMLFormField::flattenOptions( $this->getOptions() ); + + if ( in_array( strval( $value ), $validOptions, true ) ) { + return true; + } else { + return $this->msg( 'htmlform-select-badoption' )->parse(); + } + } + + /** + * This returns a block of all the radio options, in one cell. + * @see includes/HTMLFormField#getInputHTML() + * + * @param string $value + * + * @return string + */ + function getInputHTML( $value ) { + $html = $this->formatOptions( $this->getOptions(), strval( $value ) ); + + return $html; + } + + function getInputOOUI( $value ) { + $options = []; + foreach ( $this->getOptions() as $label => $data ) { + $options[] = [ + 'data' => $data, + 'label' => $this->mOptionsLabelsNotFromMessage ? new OOUI\HtmlSnippet( $label ) : $label, + ]; + } + + return new OOUI\RadioSelectInputWidget( [ + 'name' => $this->mName, + 'id' => $this->mID, + 'value' => $value, + 'options' => $options, + ] + OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( [ 'disabled', 'tabindex' ] ) + ) ); + } + + function formatOptions( $options, $value ) { + global $wgUseMediaWikiUIEverywhere; + + $html = ''; + + $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] ); + $elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ]; + + # @todo Should this produce an unordered list perhaps? + foreach ( $options as $label => $info ) { + if ( is_array( $info ) ) { + $html .= Html::rawElement( 'h1', [], $label ) . "\n"; + $html .= $this->formatOptions( $info, $value ); + } else { + $id = Sanitizer::escapeId( $this->mID . "-$info" ); + $classes = [ 'mw-htmlform-flatlist-item' ]; + if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) { + $classes[] = 'mw-ui-radio'; + } + $radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + [ 'id' => $id ] ); + $radio .= ' ' . call_user_func( $elementFunc, 'label', [ 'for' => $id ], $label ); + + $html .= ' ' . Html::rawElement( + 'div', + [ 'class' => $classes ], + $radio + ); + } + } + + return $html; + } + + protected function needsLabel() { + return false; + } +} |