X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FHTMLForm.php;h=d8d83e8f2196dba54ccd8216caaeb2bd83e760cb;hb=81e9b02ff51bfe86a8da159c7ad3c0f16977e0c8;hp=53cf698b16c3c0f4aed0bfe22ee8c446e896475e;hpb=7b66b147387fa4e1fe58a88815765aaa622575c7;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index 53cf698b16..d8d83e8f21 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -1,19 +1,64 @@ $info, + * where $info is an Associative Array with any of the following: + * + * 'class' -- the subclass of HTMLFormField that will be used + * to create the object. *NOT* the CSS class! + * 'type' -- roughly translates into the : "", or "" if nothing has been selected in the + * select dropdown. + * FIXME: If made 'required', only the text field should be compulsory. + */ +class HTMLSelectAndOtherField extends HTMLSelectField { + + function __construct( $params ) { + if ( array_key_exists( 'other', $params ) ) { + } elseif( array_key_exists( 'other-message', $params ) ){ + $params['other'] = wfMsg( $params['other-message'] ); + } else { + $params['other'] = wfMsg( 'htmlform-selectorother-other' ); + } + + if ( array_key_exists( 'options', $params ) ) { + # Options array already specified + } elseif( array_key_exists( 'options-message', $params ) ){ + # Generate options array from a system message + $params['options'] = self::parseMessage( wfMsg( $params['options-message'], $params['other'] ) ); + } else { + # Sulk + throw new MWException( 'HTMLSelectAndOtherField called without any options' ); + } + $this->mFlatOptions = self::flattenOptions( $params['options'] ); + + parent::__construct( $params ); + } + + /** + * Build a drop-down box from a textual list. + * @param $string String message text + * @param $otherName String name of "other reason" option + * @return Array + * TODO: this is copied from Xml::listDropDown(), deprecate/avoid duplication? + */ + public static function parseMessage( $string, $otherName=null ) { + if( $otherName === null ){ + $otherName = wfMsg( 'htmlform-selectorother-other' ); + } + + $optgroup = false; + $options = array( $otherName => 'other' ); + + foreach ( explode( "\n", $string ) as $option ) { + $value = trim( $option ); + if ( $value == '' ) { + continue; + } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) { + # A new group is starting... + $value = trim( substr( $value, 1 ) ); + $optgroup = $value; + } elseif ( substr( $value, 0, 2) == '**' ) { + # groupmember + $opt = trim( substr( $value, 2 ) ); + $parts = array_map( 'trim', explode( '|', $opt, 2 ) ); + if( count( $parts ) === 1 ){ + $parts[1] = $parts[0]; + } + if( $optgroup === false ){ + $options[$parts[1]] = $parts[0]; + } else { + $options[$optgroup][$parts[1]] = $parts[0]; + } + } else { + # groupless reason list + $optgroup = false; + $parts = array_map( 'trim', explode( '|', $opt, 2 ) ); + if( count( $parts ) === 1 ){ + $parts[1] = $parts[0]; + } + $options[$parts[1]] = $parts[0]; + } + } + + return $options; + } + + function getInputHTML( $value ) { + $select = parent::getInputHTML( $value[1] ); + + $textAttribs = array( + 'id' => $this->mID . '-other', + 'size' => $this->getSize(), + ); + + foreach ( array( 'required', 'autofocus', 'multiple', 'disabled' ) as $param ) { + if ( isset( $this->mParams[$param] ) ) { + $textAttribs[$param] = ''; + } + } + + $textbox = Html::input( + $this->mName . '-other', + $value[2], + 'text', + $textAttribs + ); + + return "$select
\n$textbox"; + } + + /** + * @param $request WebRequest + * @return Array( ,