* @param $attribs Array of attributes for an XML element
*/
private static function expandAttributes( $attribs ) {
+ $out = '';
if( is_null( $attribs ) ) {
return null;
- } else {
- $out = '';
- foreach( $attribs as $name => $val ) {
- $out .= ' ' . $name . '="' . Sanitizer::encodeAttribute( $val ) . '"';
- }
+ } elseif( is_array( $attribs ) ) {
+ foreach( $attribs as $name => $val )
+ $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
return $out;
+ } else {
+ throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
}
}
}
/**
- * Create a namespace selector
+ * Build a drop-down box for selecting a namespace
*
- * @param $selected Mixed: the namespace which should be selected, default ''
- * @param $allnamespaces String: value of a special item denoting all namespaces. Null to not include (default)
- * @param $includehidden Bool: include hidden namespaces?
- * @return String: Html string containing the namespace selector
+ * @param mixed $selected Namespace which should be pre-selected
+ * @param mixed $all Value of an item denoting all namespaces, or null to omit
+ * @param bool $hidden Include hidden namespaces? [WTF? --RC]
+ * @return string
*/
- public static function namespaceSelector($selected = '', $allnamespaces = null, $includehidden=false) {
+ public static function namespaceSelector( $selected = '', $all = null, $hidden = false, $element_name = 'namespace' ) {
global $wgContLang;
- if( is_null( $selected ) )
- $selected = '';
- $s = "\n<select id='namespace' name='namespace' class='namespaceselector'>\n";
- $arr = $wgContLang->getFormattedNamespaces();
- if( !is_null($allnamespaces) ) {
- $arr = array($allnamespaces => wfMsg('namespacesall')) + $arr;
+ $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 );
}
- foreach ($arr as $index => $name) {
- if ($index < NS_MAIN) continue;
-
- $name = $index !== 0 ? $name : wfMsg('blanknamespace');
-
- if ($index === $selected) {
- $s .= "\t" . self::element("option",
- array("value" => $index, "selected" => "selected"),
- $name) . "\n";
- } else {
- $s .= "\t" . self::element("option", array("value" => $index), $name) . "\n";
- }
+
+ if( !is_null( $all ) )
+ $namespaces = array( $all => wfMsg( 'namespacesall' ) ) + $namespaces;
+ foreach( $namespaces as $index => $name ) {
+ if( $index < NS_MAIN )
+ continue;
+ if( $index === 0 )
+ $name = wfMsg( 'blanknamespace' );
+ $options[] = self::option( $name, $index, $index === $selected );
}
- $s .= "</select>\n";
- return $s;
+
+ return Xml::openElement( 'select', array( 'id' => 'namespace', 'name' => $element_name,
+ 'class' => 'namespaceselector' ) )
+ . "\n"
+ . implode( "\n", $options )
+ . "\n"
+ . Xml::closeElement( 'select' );
}
-
+
/**
* Create a date selector
*
'type' => 'hidden',
'value' => $value ) + $attribs );
}
-
+
/**
* Convenience function to build an HTML drop-down list item.
* @param $text String: text for this item
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.