Merge "HTMLCheckMatrix support for forcing options on/off"
[lhc/web/wiklou.git] / includes / HTMLForm.php
index 64498fd..e2fa5fd 100644 (file)
@@ -83,9 +83,8 @@
  * $form = new HTMLForm( $someFields );
  * $form->setMethod( 'get' )
  *      ->setWrapperLegendMsg( 'message-key' )
- *      ->suppressReset()
  *      ->prepareForm()
- *      ->displayForm();
+ *      ->displayForm( '' );
  * @endcode
  * Note that you will have prepareForm and displayForm at the end. Other
  * methods call done after that would simply not be part of the form :(
@@ -1094,7 +1093,6 @@ class HTMLForm extends ContextSource {
                $this->mAction = $action;
                return $this;
        }
-
 }
 
 /**
@@ -1112,6 +1110,12 @@ abstract class HTMLFormField {
        protected $mClass = '';
        protected $mDefault;
 
+       /**
+        * @var bool If true will generate an empty div element with no label
+        * @since 1.22
+        */
+       protected $mShowEmptyLabels = true;
+
        /**
         * @var HTMLForm
         */
@@ -1204,6 +1208,8 @@ abstract class HTMLFormField {
        /**
         * Initialise the object
         * @param array $params Associative Array. See HTMLForm doc for syntax.
+        *
+        * @since 1.22 The 'label' attribute no longer accepts raw HTML, use 'label-raw' instead
         * @throws MWException
         */
        function __construct( $params ) {
@@ -1222,7 +1228,14 @@ abstract class HTMLFormField {
 
                        $this->mLabel = wfMessage( $msg, $msgInfo )->parse();
                } elseif ( isset( $params['label'] ) ) {
-                       $this->mLabel = $params['label'];
+                       if ( $params['label'] === ' ' ) {
+                               // Apparently some things set &nbsp directly and in an odd format
+                               $this->mLabel = ' ';
+                       } else {
+                               $this->mLabel = htmlspecialchars( $params['label'] );
+                       }
+               } elseif ( isset( $params['label-raw'] ) ) {
+                       $this->mLabel = $params['label-raw'];
                }
 
                $this->mName = "wp{$params['fieldname']}";
@@ -1267,6 +1280,10 @@ abstract class HTMLFormField {
                if ( isset( $params['flatlist'] ) ) {
                        $this->mClass .= ' mw-htmlform-flatlist';
                }
+
+               if ( isset( $params['hidelabel'] ) ) {
+                       $this->mShowEmptyLabels = false;
+               }
        }
 
        /**
@@ -1327,9 +1344,14 @@ abstract class HTMLFormField {
                $cellAttributes = array();
                $label = $this->getLabelHtml( $cellAttributes );
 
+               $outerDivClass = array(
+                       'mw-input',
+                       'mw-htmlform-nolabel' => ( $label === '' )
+               );
+
                $field = Html::rawElement(
                        'div',
-                       array( 'class' => 'mw-input' ) + $cellAttributes,
+                       array( 'class' => $outerDivClass ) + $cellAttributes,
                        $inputHtml . "\n$errors"
                );
                $html = Html::rawElement( 'div',
@@ -1458,7 +1480,7 @@ abstract class HTMLFormField {
        }
 
        function getLabel() {
-               return $this->mLabel;
+               return is_null( $this->mLabel ) ? '' : $this->mLabel;
        }
 
        function getLabelHtml( $cellAttributes = array() ) {
@@ -1470,20 +1492,32 @@ abstract class HTMLFormField {
                        $for['for'] = $this->mID;
                }
 
+               $labelValue = trim( $this->getLabel() );
+               $hasLabel = false;
+               if ( $labelValue !== ' ' && $labelValue !== '' ) {
+                       $hasLabel = true;
+               }
+
                $displayFormat = $this->mParent->getDisplayFormat();
-               $labelElement = Html::rawElement( 'label', $for, $this->getLabel() );
+               $html = '';
 
-               if ( $displayFormat == 'table' ) {
-                       return Html::rawElement( 'td', array( 'class' => 'mw-label' ) + $cellAttributes,
-                               Html::rawElement( 'label', $for, $this->getLabel() )
+               if ( $displayFormat === 'table' ) {
+                       $html = Html::rawElement( 'td', array( 'class' => 'mw-label' ) + $cellAttributes,
+                               Html::rawElement( 'label', $for, $labelValue )
                        );
-               } elseif ( $displayFormat == 'div' ) {
-                       return Html::rawElement( 'div', array( 'class' => 'mw-label' ) + $cellAttributes,
-                               Html::rawElement( 'label', $for, $this->getLabel() )
-                       );
-               } else {
-                       return $labelElement;
+               } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
+                       if ( $displayFormat === 'div' ) {
+                               $html = Html::rawElement(
+                                       'div',
+                                       array( 'class' => 'mw-label' ) + $cellAttributes,
+                                       Html::rawElement( 'label', $for, $labelValue )
+                               );
+                       } else {
+                               $html = Html::rawElement( 'label', $for, $labelValue );
+                       }
                }
+
+               return $html;
        }
 
        function getDefault() {
@@ -1623,16 +1657,19 @@ class HTMLTextField extends HTMLFormField {
        }
 }
 class HTMLTextAreaField extends HTMLFormField {
+       const DEFAULT_COLS = 80;
+       const DEFAULT_ROWS = 25;
+
        function getCols() {
                return isset( $this->mParams['cols'] )
                        ? $this->mParams['cols']
-                       : 80;
+                       : static::DEFAULT_COLS;
        }
 
        function getRows() {
                return isset( $this->mParams['rows'] )
                        ? $this->mParams['rows']
-                       : 25;
+                       : static::DEFAULT_ROWS;
        }
 
        function getInputHTML( $value ) {
@@ -2610,7 +2647,19 @@ class HTMLHiddenField extends HTMLFormField {
  * Add a submit button inline in the form (as opposed to
  * HTMLForm::addButton(), which will add it at the end).
  */
-class HTMLSubmitField extends HTMLFormField {
+class HTMLSubmitField extends HTMLButtonField {
+       protected $buttonType = 'submit';
+}
+
+/**
+ * Adds a generic button inline to the form. Does not do anything, you must add
+ * click handling code in JavaScript. Use a HTMLSubmitField if you merely
+ * wish to add a submit button to a form.
+ *
+ * @since 1.22
+ */
+class HTMLButtonField extends HTMLFormField {
+       protected $buttonType = 'button';
 
        public function __construct( $info ) {
                $info['nodata'] = true;
@@ -2620,7 +2669,6 @@ class HTMLSubmitField extends HTMLFormField {
        public function getInputHTML( $value ) {
                $attr = array(
                        'class' => 'mw-htmlform-submit ' . $this->mClass,
-                       'name' => $this->mName,
                        'id' => $this->mID,
                );
 
@@ -2628,7 +2676,12 @@ class HTMLSubmitField extends HTMLFormField {
                        $attr['disabled'] = 'disabled';
                }
 
-               return Xml::submitButton( $value, $attr );
+               return Html::input(
+                       $this->mName,
+                       $value,
+                       $this->buttonType,
+                       $attr
+               );
        }
 
        protected function needsLabel() {