+
+ /**
+ * Generate a form (without the opening form element).
+ * Output optionally includes a submit button.
+ * @param $fields Array Associative array, key is message corresponding to a description for the field (colon is in the message), value is appropriate input.
+ * @param $submitLabel String A message containing a label for the submit button.
+ * @return string HTML form.
+ */
+ public static function buildForm( $fields, $submitLabel = null ) {
+ $form = '';
+ $form .= "<table><tbody>";
+
+ foreach( $fields as $labelmsg => $input ) {
+ $id = "mw-$labelmsg";
+ $form .= Xml::openElement( 'tr', array( 'id' => $id ) );
+ $form .= Xml::tags( 'td', array('class' => 'mw-label'), wfMsgExt( $labelmsg, array('parseinline') ) );
+ $form .= Xml::openElement( 'td', array( 'class' => 'mw-input' ) ) . $input . Xml::closeElement( 'td' );
+ $form .= Xml::closeElement( 'tr' );
+ }
+
+ if( $submitLabel ) {
+ $form .= Xml::openElement( 'tr' );
+ $form .= Xml::tags( 'td', array(), '' );
+ $form .= Xml::openElement( 'td', array( 'class' => 'mw-submit' ) ) . Xml::submitButton( wfMsg( $submitLabel ) ) . Xml::closeElement( 'td' );
+ $form .= Xml::closeElement( 'tr' );
+ }
+
+ $form .= "</tbody></table>";
+
+
+ return $form;
+ }
+
+ /**
+ * Build a table of data
+ * @param $rows An array of arrays of strings, each to be a row in a table
+ * @param $attribs An array of attributes to apply to the table tag [optional]
+ * @param $headers An array of strings to use as table headers [optional]
+ * @return string
+ */
+ public static function buildTable( $rows, $attribs = array(), $headers = null ) {
+ $s = Xml::openElement( 'table', $attribs );
+ if ( is_array( $headers ) ) {
+ foreach( $headers as $id => $header ) {
+ $attribs = array();
+ if ( is_string( $id ) ) $attribs['id'] = $id;
+ $s .= Xml::element( 'th', $attribs, $header );
+ }
+ }
+ foreach( $rows as $id => $row ) {
+ $attribs = array();
+ if ( is_string( $id ) ) $attribs['id'] = $id;
+ $s .= Xml::buildTableRow( $attribs, $row );
+ }
+ $s .= Xml::closeElement( 'table' );
+ return $s;
+ }
+
+ /**
+ * Build a row for a table
+ * @param $attribs An array of attributes to apply to the tr tag
+ * @param $cells An array of strings to put in <td>
+ * @return string
+ */
+ public static function buildTableRow( $attribs, $cells ) {
+ $s = Xml::openElement( 'tr', $attribs );
+ foreach( $cells as $id => $cell ) {
+ $attribs = array();
+ if ( is_string( $id ) ) $attribs['id'] = $id;
+ $s .= Xml::element( 'td', $attribs, $cell );
+ }
+ $s .= Xml::closeElement( 'tr' );
+ return $s;
+ }
+}
+
+class XmlSelect {
+ protected $options = array();
+ protected $default = false;
+ protected $attributes = array();
+
+ public function __construct( $name = false, $id = false, $default = false ) {
+ if ( $name ) {
+ $this->setAttribute( 'name', $name );
+ }
+ if ( $id ) {
+ $this->setAttribute( 'id', $id );
+ }
+ if ( $default !== false ) {
+ $this->default = $default;
+ }
+ }
+
+ public function setDefault( $default ) {
+ $this->default = $default;
+ }
+
+ public function setAttribute( $name, $value ) {
+ $this->attributes[$name] = $value;
+ }
+
+ public function getAttribute( $name ) {
+ if ( isset($this->attributes[$name]) ) {
+ return $this->attributes[$name];
+ } else {
+ return null;
+ }
+ }
+
+ public function addOption( $name, $value = false ) {
+ // Stab stab stab
+ $value = ($value !== false) ? $value : $name;
+ $this->options[] = Xml::option( $name, $value, $value === $this->default );
+ }
+
+ // This accepts an array of form
+ // label => value
+ // label => ( label => value, label => value )
+ public function addOptions( $options ) {
+ $this->options[] = trim(self::formatOptions( $options, $this->default ));
+ }
+
+ // This accepts an array of form
+ // label => value
+ // label => ( label => value, label => value )
+ static function formatOptions( $options, $default = false ) {
+ $data = '';
+ foreach( $options as $label => $value ) {
+ if ( is_array( $value ) ) {
+ $contents = self::formatOptions( $value, $default );
+ $data .= Xml::tags( 'optgroup', array( 'label' => $label ), $contents ) . "\n";
+ } else {
+ $data .= Xml::option( $label, $value, $value === $default ) . "\n";
+ }
+ }
+
+ return $data;
+ }
+
+ public function getHTML() {
+ return Xml::tags( 'select', $this->attributes, implode( "\n", $this->options ) );
+ }
+