Merge "Add comment to unused EDIT_DEFER_UPDATES in Defines.php"
[lhc/web/wiklou.git] / includes / htmlform / OOUIHTMLForm.php
index fed32d0..d328ecc 100644 (file)
@@ -25,6 +25,8 @@
  * Compact stacked vertical format for forms, implemented using OOUI widgets.
  */
 class OOUIHTMLForm extends HTMLForm {
+       private $oouiErrors;
+
        public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
                parent::__construct( $descriptor, $context, $messagePrefix );
                $this->getOutput()->enableOOUI();
@@ -37,7 +39,9 @@ class OOUIHTMLForm extends HTMLForm {
         */
        protected $displayFormat = 'ooui';
 
-       public static function loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent = null ) {
+       public static function loadInputFromParameters( $fieldname, $descriptor,
+               HTMLForm $parent = null
+       ) {
                $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
                $field->setShowEmptyLabel( false );
                return $field;
@@ -125,19 +129,81 @@ class OOUIHTMLForm extends HTMLForm {
        }
 
        /**
-        * @param string|array|Status $errors
+        * @param string|array|Status $err
         * @return string
         */
-       function getErrors( $errors ) {
-               // TODO Write me!
+       function getErrors( $err ) {
+               if ( !$err ) {
+                       $errors = array();
+               } elseif ( $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();
+                       }
+                       // if the error is already a message object, don't use it as a message key
+                       if ( !$msg instanceof Message ) {
+                               $error = $this->msg( $msg, $error )->parse();
+                       } else {
+                               $error = $msg->parse();
+                       }
+                       $error = new OOUI\HtmlSnippet( $error );
+               }
+
+               // Used in getBody()
+               $this->oouiErrors = $errors;
                return '';
        }
 
+       function getHeaderText( $section = null ) {
+               if ( is_null( $section ) ) {
+                       // We handle $this->mHeader elsewhere, in getBody()
+                       return '';
+               } else {
+                       return parent::getHeaderText( $section );
+               }
+       }
+
        function getBody() {
                $fieldset = parent::getBody();
                // FIXME This only works for forms with no subsections
                if ( $fieldset instanceof OOUI\FieldsetLayout ) {
-                       $fieldset->group->prependContent( new OOUI\HtmlSnippet( $this->mHeader ) );
+                       $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;
        }