HTMLRadioField.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Radio checkbox fields.
  4. */
  5. class HTMLRadioField extends HTMLFormField {
  6. /**
  7. * @param array $params
  8. * In adition to the usual HTMLFormField parameters, this can take the following fields:
  9. * - flatlist: If given, the options will be displayed on a single line (wrapping to following
  10. * lines if necessary), rather than each one on a line of its own. This is desirable mostly
  11. * for very short lists of concisely labelled options.
  12. */
  13. public function __construct( $params ) {
  14. parent::__construct( $params );
  15. if ( isset( $params['flatlist'] ) ) {
  16. $this->mClass .= ' mw-htmlform-flatlist';
  17. }
  18. }
  19. public function validate( $value, $alldata ) {
  20. $p = parent::validate( $value, $alldata );
  21. if ( $p !== true ) {
  22. return $p;
  23. }
  24. if ( !is_string( $value ) && !is_int( $value ) ) {
  25. return $this->msg( 'htmlform-required' );
  26. }
  27. $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
  28. if ( in_array( strval( $value ), $validOptions, true ) ) {
  29. return true;
  30. } else {
  31. return $this->msg( 'htmlform-select-badoption' );
  32. }
  33. }
  34. /**
  35. * This returns a block of all the radio options, in one cell.
  36. * @see includes/HTMLFormField#getInputHTML()
  37. *
  38. * @param string $value
  39. *
  40. * @return string
  41. */
  42. public function getInputHTML( $value ) {
  43. $html = $this->formatOptions( $this->getOptions(), strval( $value ) );
  44. return $html;
  45. }
  46. public function getInputOOUI( $value ) {
  47. $options = [];
  48. foreach ( $this->getOptions() as $label => $data ) {
  49. $options[] = [
  50. 'data' => $data,
  51. 'label' => $this->mOptionsLabelsNotFromMessage ? new OOUI\HtmlSnippet( $label ) : $label,
  52. ];
  53. }
  54. return new OOUI\RadioSelectInputWidget( [
  55. 'name' => $this->mName,
  56. 'id' => $this->mID,
  57. 'value' => $value,
  58. 'options' => $options,
  59. ] + OOUI\Element::configFromHtmlAttributes(
  60. $this->getAttributes( [ 'disabled', 'tabindex' ] )
  61. ) );
  62. }
  63. public function formatOptions( $options, $value ) {
  64. global $wgUseMediaWikiUIEverywhere;
  65. $html = '';
  66. $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
  67. $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
  68. # @todo Should this produce an unordered list perhaps?
  69. foreach ( $options as $label => $info ) {
  70. if ( is_array( $info ) ) {
  71. $html .= Html::rawElement( 'h1', [], $label ) . "\n";
  72. $html .= $this->formatOptions( $info, $value );
  73. } else {
  74. $id = Sanitizer::escapeIdForAttribute( $this->mID . "-$info" );
  75. $classes = [ 'mw-htmlform-flatlist-item' ];
  76. if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
  77. $classes[] = 'mw-ui-radio';
  78. }
  79. $radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + [ 'id' => $id ] );
  80. $radio .= "\u{00A0}" . call_user_func( $elementFunc, 'label', [ 'for' => $id ], $label );
  81. $html .= ' ' . Html::rawElement(
  82. 'div',
  83. [ 'class' => $classes ],
  84. $radio
  85. );
  86. }
  87. }
  88. return $html;
  89. }
  90. protected function needsLabel() {
  91. return false;
  92. }
  93. }