2013-11-19 12:55:50 +00:00
|
|
|
<?php
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
/**
|
|
|
|
|
* Multi-select field
|
|
|
|
|
*/
|
|
|
|
|
class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable {
|
|
|
|
|
function validate( $value, $alldata ) {
|
|
|
|
|
$p = parent::validate( $value, $alldata );
|
|
|
|
|
|
|
|
|
|
if ( $p !== true ) {
|
|
|
|
|
return $p;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( !is_array( $value ) ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# If all options are valid, array_intersect of the valid options
|
|
|
|
|
# and the provided options will return the provided options.
|
2014-02-18 20:28:06 +00:00
|
|
|
$validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
$validValues = array_intersect( $value, $validOptions );
|
|
|
|
|
if ( count( $validValues ) == count( $value ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return $this->msg( 'htmlform-select-badoption' )->parse();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getInputHTML( $value ) {
|
2014-03-05 20:15:41 +00:00
|
|
|
$value = HTMLFormField::forceToStringRecursive( $value );
|
2014-02-18 20:28:06 +00:00
|
|
|
$html = $this->formatOptions( $this->getOptions(), $value );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatOptions( $options, $value ) {
|
|
|
|
|
$html = '';
|
|
|
|
|
|
2013-05-02 18:27:44 +00:00
|
|
|
$attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
foreach ( $options as $label => $info ) {
|
|
|
|
|
if ( is_array( $info ) ) {
|
|
|
|
|
$html .= Html::rawElement( 'h1', array(), $label ) . "\n";
|
|
|
|
|
$html .= $this->formatOptions( $info, $value );
|
|
|
|
|
} else {
|
2015-06-14 18:14:31 +00:00
|
|
|
$thisAttribs = array(
|
|
|
|
|
'id' => "{$this->mID}-$info",
|
|
|
|
|
'value' => $info,
|
2013-11-19 13:08:16 +00:00
|
|
|
);
|
2015-06-14 18:14:31 +00:00
|
|
|
$checked = in_array( $info, $value, true );
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2015-06-14 18:14:31 +00:00
|
|
|
$checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
|
2014-09-23 21:53:12 +00:00
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
$html .= ' ' . Html::rawElement(
|
|
|
|
|
'div',
|
|
|
|
|
array( 'class' => 'mw-htmlform-flatlist-item' ),
|
|
|
|
|
$checkbox
|
|
|
|
|
);
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-14 18:14:31 +00:00
|
|
|
protected function getOneCheckbox( $checked, $attribs, $label ) {
|
|
|
|
|
if ( $this->mParent instanceof OOUIHTMLForm ) {
|
|
|
|
|
if ( $this->mOptionsLabelsNotFromMessage ) {
|
|
|
|
|
$label = new OOUI\HtmlSnippet( $label );
|
|
|
|
|
}
|
|
|
|
|
return new OOUI\FieldLayout(
|
|
|
|
|
new OOUI\CheckboxInputWidget( array(
|
|
|
|
|
'name' => "{$this->mName}[]",
|
|
|
|
|
'selected' => $checked,
|
|
|
|
|
'value' => $attribs['value'],
|
|
|
|
|
) + $attribs ),
|
|
|
|
|
array(
|
|
|
|
|
'label' => $label,
|
|
|
|
|
'align' => 'inline',
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' );
|
|
|
|
|
$checkbox =
|
|
|
|
|
Xml::check( "{$this->mName}[]", $checked, $attribs ) .
|
|
|
|
|
' ' .
|
|
|
|
|
call_user_func( $elementFunc,
|
|
|
|
|
'label',
|
|
|
|
|
array( 'for' => $attribs['id'] ),
|
|
|
|
|
$label
|
|
|
|
|
);
|
|
|
|
|
if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
|
|
|
|
|
$checkbox = Html::openElement( 'div', array( 'class' => 'mw-ui-checkbox' ) ) .
|
|
|
|
|
$checkbox .
|
|
|
|
|
Html::closeElement( 'div' );
|
|
|
|
|
}
|
|
|
|
|
return $checkbox;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param WebRequest $request
|
2013-11-19 12:55:50 +00:00
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return string
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
function loadDataFromRequest( $request ) {
|
|
|
|
|
if ( $this->mParent->getMethod() == 'post' ) {
|
|
|
|
|
if ( $request->wasPosted() ) {
|
|
|
|
|
# Checkboxes are just not added to the request arrays if they're not checked,
|
|
|
|
|
# so it's perfectly possible for there not to be an entry at all
|
|
|
|
|
return $request->getArray( $this->mName, array() );
|
|
|
|
|
} else {
|
|
|
|
|
# That's ok, the user has not yet submitted the form, so show the defaults
|
|
|
|
|
return $this->getDefault();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
# This is the impossible case: if we look at $_GET and see no data for our
|
|
|
|
|
# field, is it because the user has not yet submitted the form, or that they
|
|
|
|
|
# have submitted it with all the options unchecked? We will have to assume the
|
|
|
|
|
# latter, which basically means that you can't specify 'positive' defaults
|
|
|
|
|
# for GET forms.
|
|
|
|
|
# @todo FIXME...
|
|
|
|
|
return $request->getArray( $this->mName, array() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getDefault() {
|
|
|
|
|
if ( isset( $this->mDefault ) ) {
|
|
|
|
|
return $this->mDefault;
|
|
|
|
|
} else {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function filterDataForSubmit( $data ) {
|
2014-03-05 20:15:41 +00:00
|
|
|
$data = HTMLFormField::forceToStringRecursive( $data );
|
2014-02-18 20:28:06 +00:00
|
|
|
$options = HTMLFormField::flattenOptions( $this->getOptions() );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
$res = array();
|
|
|
|
|
foreach ( $options as $opt ) {
|
2014-03-05 20:15:41 +00:00
|
|
|
$res["$opt"] = in_array( $opt, $data, true );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function needsLabel() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
}
|