HTMLMultiSelectField.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Multi-select field
  4. */
  5. class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable {
  6. /**
  7. * @param array $params
  8. * In adition to the usual HTMLFormField parameters, this can take the following fields:
  9. * - dropdown: If given, the options will be displayed inside a dropdown with a text field that
  10. * can be used to filter them. This is desirable mostly for very long lists of options.
  11. * This only works for users with JavaScript support and falls back to the list of checkboxes.
  12. * - flatlist: If given, the options will be displayed on a single line (wrapping to following
  13. * lines if necessary), rather than each one on a line of its own. This is desirable mostly
  14. * for very short lists of concisely labelled options.
  15. */
  16. public function __construct( $params ) {
  17. parent::__construct( $params );
  18. // If the disabled-options parameter is not provided, use an empty array
  19. if ( isset( $this->mParams['disabled-options'] ) === false ) {
  20. $this->mParams['disabled-options'] = [];
  21. }
  22. if ( isset( $params['dropdown'] ) ) {
  23. $this->mClass .= ' mw-htmlform-dropdown';
  24. }
  25. if ( isset( $params['flatlist'] ) ) {
  26. $this->mClass .= ' mw-htmlform-flatlist';
  27. }
  28. }
  29. public function validate( $value, $alldata ) {
  30. $p = parent::validate( $value, $alldata );
  31. if ( $p !== true ) {
  32. return $p;
  33. }
  34. if ( !is_array( $value ) ) {
  35. return false;
  36. }
  37. # If all options are valid, array_intersect of the valid options
  38. # and the provided options will return the provided options.
  39. $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
  40. $validValues = array_intersect( $value, $validOptions );
  41. if ( count( $validValues ) == count( $value ) ) {
  42. return true;
  43. } else {
  44. return $this->msg( 'htmlform-select-badoption' );
  45. }
  46. }
  47. public function getInputHTML( $value ) {
  48. if ( isset( $this->mParams['dropdown'] ) ) {
  49. $this->mParent->getOutput()->addModules( 'jquery.chosen' );
  50. }
  51. $value = HTMLFormField::forceToStringRecursive( $value );
  52. $html = $this->formatOptions( $this->getOptions(), $value );
  53. return $html;
  54. }
  55. public function formatOptions( $options, $value ) {
  56. $html = '';
  57. $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
  58. foreach ( $options as $label => $info ) {
  59. if ( is_array( $info ) ) {
  60. $html .= Html::rawElement( 'h1', [], $label ) . "\n";
  61. $html .= $this->formatOptions( $info, $value );
  62. } else {
  63. $thisAttribs = [
  64. 'id' => "{$this->mID}-$info",
  65. 'value' => $info,
  66. ];
  67. if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
  68. $thisAttribs['disabled'] = 'disabled';
  69. }
  70. $checked = in_array( $info, $value, true );
  71. $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
  72. $html .= ' ' . Html::rawElement(
  73. 'div',
  74. [ 'class' => 'mw-htmlform-flatlist-item' ],
  75. $checkbox
  76. );
  77. }
  78. }
  79. return $html;
  80. }
  81. protected function getOneCheckbox( $checked, $attribs, $label ) {
  82. if ( $this->mParent instanceof OOUIHTMLForm ) {
  83. throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
  84. } else {
  85. $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
  86. $checkbox =
  87. Xml::check( "{$this->mName}[]", $checked, $attribs ) .
  88. "\u{00A0}" .
  89. call_user_func( $elementFunc,
  90. 'label',
  91. [ 'for' => $attribs['id'] ],
  92. $label
  93. );
  94. if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
  95. $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
  96. $checkbox .
  97. Html::closeElement( 'div' );
  98. }
  99. return $checkbox;
  100. }
  101. }
  102. /**
  103. * Get options and make them into arrays suitable for OOUI.
  104. * @throws MWException
  105. */
  106. public function getOptionsOOUI() {
  107. // Sections make this difficult. See getInputOOUI().
  108. throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
  109. }
  110. /**
  111. * Get the OOUI version of this field.
  112. *
  113. * Returns OOUI\CheckboxMultiselectInputWidget for fields that only have one section,
  114. * string otherwise.
  115. *
  116. * @since 1.28
  117. * @param string[] $value
  118. * @return string|OOUI\CheckboxMultiselectInputWidget
  119. * @suppress PhanParamSignatureMismatch
  120. */
  121. public function getInputOOUI( $value ) {
  122. $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
  123. $hasSections = false;
  124. $optionsOouiSections = [];
  125. $options = $this->getOptions();
  126. // If the options are supposed to be split into sections, each section becomes a separate
  127. // CheckboxMultiselectInputWidget.
  128. foreach ( $options as $label => $section ) {
  129. if ( is_array( $section ) ) {
  130. $optionsOouiSections[ $label ] = Xml::listDropDownOptionsOoui( $section );
  131. unset( $options[$label] );
  132. $hasSections = true;
  133. }
  134. }
  135. // If anything remains in the array, they are sectionless options. Put them in a separate widget
  136. // at the beginning.
  137. if ( $options ) {
  138. $optionsOouiSections = array_merge(
  139. [ '' => Xml::listDropDownOptionsOoui( $options ) ],
  140. $optionsOouiSections
  141. );
  142. }
  143. $out = [];
  144. foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
  145. $attr = [];
  146. $attr['name'] = "{$this->mName}[]";
  147. $attr['value'] = $value;
  148. $attr['options'] = $optionsOoui;
  149. foreach ( $attr['options'] as &$option ) {
  150. $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
  151. }
  152. if ( $this->mOptionsLabelsNotFromMessage ) {
  153. foreach ( $attr['options'] as &$option ) {
  154. $option['label'] = new OOUI\HtmlSnippet( $option['label'] );
  155. }
  156. }
  157. $attr += OOUI\Element::configFromHtmlAttributes(
  158. $this->getAttributes( [ 'disabled', 'tabindex' ] )
  159. );
  160. if ( $this->mClass !== '' ) {
  161. $attr['classes'] = [ $this->mClass ];
  162. }
  163. $widget = new OOUI\CheckboxMultiselectInputWidget( $attr );
  164. if ( $sectionLabel ) {
  165. $out[] = new OOUI\FieldsetLayout( [
  166. 'items' => [ $widget ],
  167. 'label' => new OOUI\HtmlSnippet( $sectionLabel ),
  168. ] );
  169. } else {
  170. $out[] = $widget;
  171. }
  172. }
  173. if ( !$hasSections ) {
  174. // Directly return the only OOUI\CheckboxMultiselectInputWidget.
  175. // This allows it to be made infusable and later tweaked by JS code.
  176. return $out[ 0 ];
  177. }
  178. return implode( '', $out );
  179. }
  180. /**
  181. * @param WebRequest $request
  182. *
  183. * @return string|array
  184. */
  185. public function loadDataFromRequest( $request ) {
  186. $fromRequest = $request->getArray( $this->mName, [] );
  187. // Fetch the value in either one of the two following case:
  188. // - we have a valid submit attempt (form was just submitted)
  189. // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
  190. if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
  191. // Checkboxes are just not added to the request arrays if they're not checked,
  192. // so it's perfectly possible for there not to be an entry at all
  193. return $fromRequest;
  194. } else {
  195. // That's ok, the user has not yet submitted the form, so show the defaults
  196. return $this->getDefault();
  197. }
  198. }
  199. public function getDefault() {
  200. return $this->mDefault ?? [];
  201. }
  202. public function filterDataForSubmit( $data ) {
  203. $data = HTMLFormField::forceToStringRecursive( $data );
  204. $options = HTMLFormField::flattenOptions( $this->getOptions() );
  205. $res = [];
  206. foreach ( $options as $opt ) {
  207. $res["$opt"] = in_array( $opt, $data, true );
  208. }
  209. return $res;
  210. }
  211. protected function needsLabel() {
  212. return false;
  213. }
  214. }