HTMLCheckField.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * A checkbox field
  4. */
  5. class HTMLCheckField extends HTMLFormField {
  6. public function getInputHTML( $value ) {
  7. global $wgUseMediaWikiUIEverywhere;
  8. if ( !empty( $this->mParams['invert'] ) ) {
  9. $value = !$value;
  10. }
  11. $attr = $this->getTooltipAndAccessKey();
  12. $attr['id'] = $this->mID;
  13. $attr += $this->getAttributes( [ 'disabled', 'tabindex' ] );
  14. if ( $this->mClass !== '' ) {
  15. $attr['class'] = $this->mClass;
  16. }
  17. $attrLabel = [ 'for' => $this->mID ];
  18. if ( isset( $attr['title'] ) ) {
  19. // propagate tooltip to label
  20. $attrLabel['title'] = $attr['title'];
  21. }
  22. $chkLabel = Xml::check( $this->mName, $value, $attr ) .
  23. "\u{00A0}" .
  24. Html::rawElement( 'label', $attrLabel, $this->mLabel );
  25. if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
  26. $chkLabel = Html::rawElement(
  27. 'div',
  28. [ 'class' => 'mw-ui-checkbox' ],
  29. $chkLabel
  30. );
  31. }
  32. return $chkLabel;
  33. }
  34. /**
  35. * Get the OOUI version of this field.
  36. * @since 1.26
  37. * @param string $value
  38. * @return OOUI\CheckboxInputWidget The checkbox widget.
  39. */
  40. public function getInputOOUI( $value ) {
  41. if ( !empty( $this->mParams['invert'] ) ) {
  42. $value = !$value;
  43. }
  44. $attr = $this->getTooltipAndAccessKeyOOUI();
  45. $attr['id'] = $this->mID;
  46. $attr['name'] = $this->mName;
  47. $attr += OOUI\Element::configFromHtmlAttributes(
  48. $this->getAttributes( [ 'disabled', 'tabindex' ] )
  49. );
  50. if ( $this->mClass !== '' ) {
  51. $attr['classes'] = [ $this->mClass ];
  52. }
  53. $attr['selected'] = $value;
  54. $attr['value'] = '1'; // Nasty hack, but needed to make this work
  55. return new OOUI\CheckboxInputWidget( $attr );
  56. }
  57. /**
  58. * For a checkbox, the label goes on the right hand side, and is
  59. * added in getInputHTML(), rather than HTMLFormField::getRow()
  60. *
  61. * ...unless OOUI is being used, in which case we actually return
  62. * the label here.
  63. *
  64. * @return string
  65. */
  66. public function getLabel() {
  67. if ( $this->mParent instanceof OOUIHTMLForm ) {
  68. return $this->mLabel;
  69. } elseif (
  70. $this->mParent instanceof HTMLForm &&
  71. $this->mParent->getDisplayFormat() === 'div'
  72. ) {
  73. return '';
  74. } else {
  75. return "\u{00A0}";
  76. }
  77. }
  78. /**
  79. * Get label alignment when generating field for OOUI.
  80. * @return string 'left', 'right', 'top' or 'inline'
  81. */
  82. protected function getLabelAlignOOUI() {
  83. return 'inline';
  84. }
  85. /**
  86. * checkboxes don't need a label.
  87. * @return bool
  88. */
  89. protected function needsLabel() {
  90. return false;
  91. }
  92. /**
  93. * @param WebRequest $request
  94. *
  95. * @return bool
  96. */
  97. public function loadDataFromRequest( $request ) {
  98. $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
  99. // Fetch the value in either one of the two following case:
  100. // - we have a valid submit attempt (form was just submitted)
  101. // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
  102. if ( $this->isSubmitAttempt( $request ) || $request->getCheck( $this->mName ) ) {
  103. return $invert
  104. ? !$request->getBool( $this->mName )
  105. : $request->getBool( $this->mName );
  106. } else {
  107. return (bool)$this->getDefault();
  108. }
  109. }
  110. }