From 433bb02da8efb168992175ca90a04bead58a4a1f Mon Sep 17 00:00:00 2001 From: Huji Date: Fri, 25 Jan 2008 11:28:45 +0000 Subject: [PATCH] Introducing Xml::reasonDropDown * Generates a drop-down box given the required information * Supports marking the approrpiate option as pre-selected * Shall be used on deletion and protection pages, where a dropdown is used to select the appropriate reason for the action --- includes/Xml.php | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/includes/Xml.php b/includes/Xml.php index 650ad041fc..3c209d0a5f 100644 --- a/includes/Xml.php +++ b/includes/Xml.php @@ -309,7 +309,7 @@ class Xml { 'type' => 'hidden', 'value' => $value ) + $attribs ); } - + /** * Convenience function to build an HTML drop-down list item. * @param $text String: text for this item @@ -329,6 +329,51 @@ class Xml { return self::element( 'option', $attribs, $text ); } + /** + * Build a drop-down box for selecting a reason for an action + * + * @param mixed $other Text for the "Other reasons" option + * @param mixed $list Correctly formatted text to be used to generate the options + * @param mixed $selected Option which should be pre-selected + * @return string + */ + public static function reasonDropDown( $other = '', $list = '', $selected = '' ) { + $options = ''; + $optgroup = false; + + $options = self::option( $other, 'other', $selected === 'other' ); + + foreach ( explode( "\n", $list ) as $option) { + $value = trim( htmlspecialchars($option) ); + if ( $value == '' ) { + continue; + } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) { + // A new group is starting ... + $value = trim( substr( $value, 1 ) ); + if( $optgroup ) $options .= self::closeElement('optgroup'); + $options .= self::openElement( 'optgroup', array( 'label' => $value ) ); + $optgroup = true; + } elseif ( substr( $value, 0, 2) == '**' ) { + // groupmember + $value = trim( substr( $value, 2 ) ); + $options .= self::option( $value, $value, $selected === $value ); + } else { + // groupless reason list + if( $optgroup ) $options .= self::closeElement('optgroup'); + $options .= self::option( $value, $value, $selected === $value ); + $optgroup = false; + } + } + if( $optgroup ) $options .= self::closeElement('optgroup'); + + return Xml::openElement( 'select', array( 'id' => 'wpReasonDropDown', 'name' => 'wpReasonDropDown', + 'class' => 'wpReasonDropDown' ) ) + . "\n" + . $options + . "\n" + . Xml::closeElement( 'select' ); + } + /** * Returns an escaped string suitable for inclusion in a string literal * for JavaScript source code. -- 2.20.1