Don't require commandLine.inc when not using the command line; instead, move wfWaitFo...
[lhc/web/wiklou.git] / includes / Xml.php
index d7261a1..6689a4a 100644 (file)
@@ -99,11 +99,19 @@ class Xml {
         * @param bool $hidden Include hidden namespaces? [WTF? --RC]
         * @return string
         */
-       public static function namespaceSelector( $selected = '', $all = null, $hidden = false ) {
+       public static function namespaceSelector( $selected = '', $all = null, $hidden = false, $element_name = 'namespace' ) {
                global $wgContLang;
                $namespaces = $wgContLang->getFormattedNamespaces();
                $options = array();
                
+               // Godawful hack... we'll be frequently passed selected namespaces
+               // as strings since PHP is such a shithole.
+               // But we also don't want blanks and nulls and "all"s matching 0,
+               // so let's convert *just* string ints to clean ints.
+               if( preg_match( '/^\d+$/', $selected ) ) {
+                       $selected = intval( $selected );
+               }
+               
                if( !is_null( $all ) )
                        $namespaces = array( $all => wfMsg( 'namespacesall' ) ) + $namespaces;
                foreach( $namespaces as $index => $name ) {
@@ -114,7 +122,7 @@ class Xml {
                        $options[] = self::option( $name, $index, $index === $selected );
                }
                
-               return Xml::openElement( 'select', array( 'id' => 'namespace', 'name' => 'namespace',
+               return Xml::openElement( 'select', array( 'id' => 'namespace', 'name' => $element_name,
                        'class' => 'namespaceselector' ) )
                        . "\n"
                        . implode( "\n", $options )
@@ -301,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
@@ -321,6 +329,63 @@ class Xml {
                return self::element( 'option', $attribs, $text );
        }
 
+       /**
+        * Build a drop-down box from a textual list.
+        * 
+        * @param mixed $name Name and id for the drop-down
+        * @param mixed $class CSS classes for the drop-down
+        * @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 listDropDown( $name= '', $list = '', $other = '', $selected = '', $class = '', $tabindex = Null ) {
+               $options = '';
+               $optgroup = false;
+               
+               $options = self::option( $other, 'other', $selected === 'other' );
+               
+               foreach ( explode( "\n", $list ) 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 ) );
+                                       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');
+               
+               $attribs = array();
+               if( $name ) {
+                       $attribs['id'] = $name;
+                       $attribs['name'] = $name;
+               }
+               if( $class ) {
+                       $attribs['class'] = $class;
+               }
+               if( $tabindex ) {
+                       $attribs['tabindex'] = $tabindex;
+               }
+               return Xml::openElement( 'select', $attribs )
+                       . "\n"
+                       . $options
+                       . "\n"
+                       . Xml::closeElement( 'select' );
+       }
+
        /**
         * Returns an escaped string suitable for inclusion in a string literal
         * for JavaScript source code.