Introducing Xml::reasonDropDown
authorHuji <huji@users.mediawiki.org>
Fri, 25 Jan 2008 11:28:45 +0000 (11:28 +0000)
committerHuji <huji@users.mediawiki.org>
Fri, 25 Jan 2008 11:28:45 +0000 (11:28 +0000)
* 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

index 650ad04..3c209d0 100644 (file)
@@ -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.