Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / htmlform / OOUIHTMLForm.php
index 80e91f7..84d40a1 100644 (file)
  * Compact stacked vertical format for forms, implemented using OOUI widgets.
  */
 class OOUIHTMLForm extends HTMLForm {
-       /**
-        * Wrapper and its legend are never generated in OOUI mode.
-        * @var boolean
-        */
-       protected $mWrapperLegend = false;
+       private $oouiErrors;
 
        public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
                parent::__construct( $descriptor, $context, $messagePrefix );
@@ -71,7 +67,7 @@ class OOUIHTMLForm extends HTMLForm {
                        $attribs['type'] = 'submit';
                        $attribs['label'] = $this->getSubmitText();
                        $attribs['value'] = $this->getSubmitText();
-                       $attribs['flags'] = array( $this->mSubmitFlag );
+                       $attribs['flags'] = $this->mSubmitFlags;
 
                        $buttons .= new OOUI\ButtonInputWidget( $attribs );
                }
@@ -110,23 +106,116 @@ class OOUIHTMLForm extends HTMLForm {
                return $html;
        }
 
-       function getFormAttributes() {
-               $attribs = parent::getFormAttributes();
-               if ( !isset( $attribs['class'] ) ) {
-                       $attribs['class'] = '';
+       /**
+        * Put a form section together from the individual fields' HTML, merging it and wrapping.
+        * @param OOUI\\FieldLayout[] $fieldsHtml
+        * @param string $sectionName
+        * @param bool $anyFieldHasLabel Unused
+        * @return string HTML
+        */
+       protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
+               $config = array(
+                       'items' => $fieldsHtml,
+               );
+               if ( $sectionName ) {
+                       $config['id'] = Sanitizer::escapeId( $sectionName );
+               }
+               if ( is_string( $this->mWrapperLegend ) ) {
+                       $config['label'] = $this->mWrapperLegend;
+               }
+               return new OOUI\FieldsetLayout( $config );
+       }
+
+       /**
+        * @param string|array|Status $err
+        * @return string
+        */
+       function getErrors( $err ) {
+               if ( !$err ) {
+                       $errors = array();
+               } else if ( $err instanceof Status ) {
+                       if ( $err->isOK() ) {
+                               $errors = array();
+                       } else {
+                               $errors = $err->getErrorsByType( 'error' );
+                               foreach ( $errors as &$error ) {
+                                       // Input:  array( 'message' => 'foo', 'errors' => array( 'a', 'b', 'c' ) )
+                                       // Output: array( 'foo', 'a', 'b', 'c' )
+                                       $error = array_merge( array( $error['message'] ), $error['params'] );
+                               }
+                       }
+               } else {
+                       $errors = $err;
+                       if ( !is_array( $errors ) ) {
+                               $errors = array( $errors );
+                       }
+               }
+
+               foreach ( $errors as &$error ) {
+                       if ( is_array( $error ) ) {
+                               $msg = array_shift( $error );
+                       } else {
+                               $msg = $error;
+                               $error = array();
+                       }
+                       $error = $this->msg( $msg, $error )->parse();
+                       $error = new OOUI\HtmlSnippet( $error );
                }
 
-               if ( is_string( $attribs['class'] ) ) {
-                       $attribs['class'] = trim( $attribs['class'] . ' mw-htmlform-ooui' );
+               // Used in getBody()
+               $this->oouiErrors = $errors;
+               return '';
+       }
+
+       function getHeaderText( $section = null ) {
+               if ( is_null( $section ) ) {
+                       // We handle $this->mHeader elsewhere, in getBody()
+                       return '';
                } else {
-                       $attribs['class'][] = 'mw-htmlform-ooui';
+                       return parent::getHeaderText( $section );
                }
+       }
 
-               return $attribs;
+       function getBody() {
+               $fieldset = parent::getBody();
+               // FIXME This only works for forms with no subsections
+               if ( $fieldset instanceof OOUI\FieldsetLayout ) {
+                       $classes = array( 'mw-htmlform-ooui-header' );
+                       if ( !$this->mHeader ) {
+                               $classes[] = 'mw-htmlform-ooui-header-empty';
+                       }
+                       if ( $this->oouiErrors ) {
+                               $classes[] = 'mw-htmlform-ooui-header-errors';
+                       }
+                       $fieldset->addItems( array(
+                               new OOUI\FieldLayout(
+                                       new OOUI\LabelWidget( array( 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ) ),
+                                       array(
+                                               'align' => 'top',
+                                               'errors' => $this->oouiErrors,
+                                               'classes' => $classes,
+                                       )
+                               )
+                       ), 0 );
+               }
+               return $fieldset;
        }
 
        function wrapForm( $html ) {
-               // Always discard $this->mWrapperLegend
-               return Html::rawElement( 'form', $this->getFormAttributes(), $html );
+               $form = new OOUI\FormLayout( $this->getFormAttributes() + array(
+                       'classes' => array( 'mw-htmlform-ooui' ),
+                       'content' => new OOUI\HtmlSnippet( $html ),
+               ) );
+
+               // Include a wrapper for style, if requested.
+               $form = new OOUI\PanelLayout( array(
+                       'classes' => array( 'mw-htmlform-ooui-wrapper' ),
+                       'expanded' => false,
+                       'padded' => $this->mWrapperLegend !== false,
+                       'framed' => $this->mWrapperLegend !== false,
+                       'content' => $form,
+               ) );
+
+               return $form;
        }
 }