From: Werdna Date: Wed, 6 Jun 2012 05:30:28 +0000 (+0000) Subject: Merge "Adds support to HTMLForm for rendering form fields wrapped in divs or just... X-Git-Tag: 1.31.0-rc.0~23389 X-Git-Url: http://git.cyclocoop.org//%22%22._DIR_PLUGIN_FULLCALENDAR.%22prive/themes/spip/images/event_edit.png/%22?a=commitdiff_plain;h=ebf2912801913acc724413a6e2967d78cae77223;hp=47de3d2aec5c054ece96bb82fb358336b432232f;p=lhc%2Fweb%2Fwiklou.git Merge "Adds support to HTMLForm for rendering form fields wrapped in divs or just plain old (mostly) raw form fields." --- diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index c14996541a..4f696382e4 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -142,16 +142,33 @@ class HTMLForm extends ContextSource { protected $mButtons = array(); protected $mWrapperLegend = false; - + /** * If true, sections that contain both fields and subsections will * render their subsections before their fields. - * + * * Subclasses may set this to false to render subsections after fields * instead. */ protected $mSubSectionBeforeFields = true; + /** + * Format in which to display form. For viable options, + * @see $availableDisplayFormats + * @var String + */ + protected $displayFormat = 'table'; + + /** + * Available formats in which to display the form + * @var Array + */ + protected $availableDisplayFormats = array( + 'table', + 'div', + 'raw', + ); + /** * Build a new HTMLForm from an array of field attributes * @param $descriptor Array of Field constructs, as described above @@ -160,13 +177,13 @@ class HTMLForm extends ContextSource { * @param $messagePrefix String a prefix to go in front of default messages */ public function __construct( $descriptor, /*IContextSource*/ $context = null, $messagePrefix = '' ) { - if( $context instanceof IContextSource ){ + if ( $context instanceof IContextSource ) { $this->setContext( $context ); $this->mTitle = false; // We don't need them to set a title $this->mMessagePrefix = $messagePrefix; } else { // B/C since 1.18 - if( is_string( $context ) && $messagePrefix === '' ){ + if ( is_string( $context ) && $messagePrefix === '' ) { // it's actually $messagePrefix $this->mMessagePrefix = $context; } @@ -210,6 +227,28 @@ class HTMLForm extends ContextSource { $this->mFieldTree = $loadedDescriptor; } + /** + * Set format in which to display the form + * @param $format String the name of the format to use, must be one of + * $this->availableDisplayFormats + * @since 1.20 + */ + public function setDisplayFormat( $format ) { + if ( !in_array( $format, $this->availableDisplayFormats ) ) { + throw new MWException ( 'Display format must be one of ' . print_r( $this->availableDisplayFormats, true ) ); + } + $this->displayFormat = $format; + } + + /** + * Getter for displayFormat + * @since 1.20 + * @return String + */ + public function getDisplayFormat() { + return $this->displayFormat; + } + /** * Add the HTMLForm-specific JavaScript, if it hasn't been * done already. @@ -275,7 +314,7 @@ class HTMLForm extends ContextSource { $editToken = $this->getRequest()->getVal( 'wpEditToken' ); if ( $this->getUser()->isLoggedIn() || $editToken != null ) { // Session tokens for logged-out users have no security value. - // However, if the user gave one, check it in order to give a nice + // However, if the user gave one, check it in order to give a nice // "session expired" error instead of "permission denied" or such. $submit = $this->getUser()->matchEditToken( $editToken ); } else { @@ -543,7 +582,7 @@ class HTMLForm extends ContextSource { global $wgArticlePath; $html = ''; - if( $this->getMethod() == 'post' ){ + if ( $this->getMethod() == 'post' ) { $html .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken(), array( 'id' => 'wpEditToken' ) ) . "\n"; $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n"; } @@ -786,20 +825,25 @@ class HTMLForm extends ContextSource { * @param $fieldsetIDPrefix string ID prefix for the
tag of each subsection, ignored if empty * @return String */ - function displaySection( $fields, $sectionName = '', $fieldsetIDPrefix = '' ) { - $tableHtml = ''; + public function displaySection( $fields, $sectionName = '', $fieldsetIDPrefix = '' ) { + $displayFormat = $this->getDisplayFormat(); + + $html = ''; $subsectionHtml = ''; - $hasLeftColumn = false; + $hasLabel = false; + + $getFieldHtmlMethod = ( $displayFormat == 'table' ) ? 'getTableRow' : 'get' . ucfirst( $displayFormat ); foreach ( $fields as $key => $value ) { - if ( is_object( $value ) ) { + if ( $value instanceof HTMLFormField ) { $v = empty( $value->mParams['nodata'] ) ? $this->mFieldData[$key] : $value->getDefault(); - $tableHtml .= $value->getTableRow( $v ); + $html .= $value->$getFieldHtmlMethod( $v ); - if ( $value->getLabel() != ' ' ) { - $hasLeftColumn = true; + $labelValue = trim( $value->getLabel() ); + if ( $labelValue != ' ' && $labelValue !== '' ) { + $hasLabel = true; } } elseif ( is_array( $value ) ) { $section = $this->displaySection( $value, $key ); @@ -818,27 +862,33 @@ class HTMLForm extends ContextSource { } } - $classes = array(); + if ( $displayFormat !== 'raw' ) { + $classes = array(); - if ( !$hasLeftColumn ) { // Avoid strange spacing when no labels exist - $classes[] = 'mw-htmlform-nolabel'; - } + if ( !$hasLabel ) { // Avoid strange spacing when no labels exist + $classes[] = 'mw-htmlform-nolabel'; + } - $attribs = array( - 'class' => implode( ' ', $classes ), - ); + $attribs = array( + 'class' => implode( ' ', $classes ), + ); - if ( $sectionName ) { - $attribs['id'] = Sanitizer::escapeId( "mw-htmlform-$sectionName" ); - } + if ( $sectionName ) { + $attribs['id'] = Sanitizer::escapeId( "mw-htmlform-$sectionName" ); + } - $tableHtml = Html::rawElement( 'table', $attribs, - Html::rawElement( 'tbody', array(), "\n$tableHtml\n" ) ) . "\n"; + if ( $displayFormat === 'table' ) { + $html = Html::rawElement( 'table', $attribs, + Html::rawElement( 'tbody', array(), "\n$html\n" ) ) . "\n"; + } elseif ( $displayFormat === 'div' ) { + $html = Html::rawElement( 'div', $attribs, "\n$html\n" ); + } + } if ( $this->mSubSectionBeforeFields ) { - return $subsectionHtml . "\n" . $tableHtml; + return $subsectionHtml . "\n" . $html; } else { - return $tableHtml . "\n" . $subsectionHtml; + return $html . "\n" . $subsectionHtml; } } @@ -1055,7 +1105,7 @@ abstract class HTMLFormField { $this->mFilterCallback = $params['filter-callback']; } - if ( isset( $params['flatlist'] ) ){ + if ( isset( $params['flatlist'] ) ) { $this->mClass .= ' mw-htmlform-flatlist'; } } @@ -1067,35 +1117,27 @@ abstract class HTMLFormField { * @return String complete HTML table row. */ function getTableRow( $value ) { - # Check for invalid data. - - $errors = $this->validate( $value, $this->mParent->mFieldData ); - + list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value ); + $inputHtml = $this->getInputHTML( $value ); + $fieldType = get_class( $this ); + $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() ); $cellAttributes = array(); - $verticalLabel = false; - if ( !empty($this->mParams['vertical-label']) ) { + if ( !empty( $this->mParams['vertical-label'] ) ) { $cellAttributes['colspan'] = 2; $verticalLabel = true; - } - - if ( $errors === true || ( !$this->mParent->getRequest()->wasPosted() && ( $this->mParent->getMethod() == 'post' ) ) ) { - $errors = ''; - $errorClass = ''; } else { - $errors = self::formatErrors( $errors ); - $errorClass = 'mw-htmlform-invalid-input'; + $verticalLabel = false; } $label = $this->getLabelHtml( $cellAttributes ); + $field = Html::rawElement( 'td', array( 'class' => 'mw-input' ) + $cellAttributes, - $this->getInputHTML( $value ) . "\n$errors" + $inputHtml . "\n$errors" ); - $fieldType = get_class( $this ); - if ( $verticalLabel ) { $html = Html::rawElement( 'tr', array( 'class' => 'mw-htmlform-vertical-label' ), $label ); @@ -1108,6 +1150,109 @@ abstract class HTMLFormField { $label . $field ); } + return $html . $helptext; + } + + /** + * Get the complete div for the input, including help text, + * labels, and whatever. + * @since 1.20 + * @param $value String the value to set the input to. + * @return String complete HTML table row. + */ + public function getDiv( $value ) { + list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value ); + $inputHtml = $this->getInputHTML( $value ); + $fieldType = get_class( $this ); + $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() ); + $cellAttributes = array(); + $label = $this->getLabelHtml( $cellAttributes ); + + $field = Html::rawElement( + 'div', + array( 'class' => 'mw-input' ) + $cellAttributes, + $inputHtml . "\n$errors" + ); + $html = Html::rawElement( 'div', + array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ), + $label . $field ); + $html .= $helptext; + return $html; + } + + /** + * Get the complete raw fields for the input, including help text, + * labels, and whatever. + * @since 1.20 + * @param $value String the value to set the input to. + * @return String complete HTML table row. + */ + public function getRaw( $value ) { + list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value ); + $inputHtml = $this->getInputHTML( $value ); + $fieldType = get_class( $this ); + $helptext = $this->getHelpTextHtmlRaw( $this->getHelpText() ); + $cellAttributes = array(); + $label = $this->getLabelHtml( $cellAttributes ); + + $html = "\n$errors"; + $html .= $label; + $html .= $inputHtml; + $html .= $helptext; + return $html; + } + + /** + * Generate help text HTML in table format + * @since 1.20 + * @param $helptext String|null + * @return String + */ + public function getHelpTextHtmlTable( $helptext ) { + if ( is_null( $helptext ) ) { + return ''; + } + + $row = Html::rawElement( + 'td', + array( 'colspan' => 2, 'class' => 'htmlform-tip' ), + $helptext + ); + $row = Html::rawElement( 'tr', array(), $row ); + return $row; + } + + /** + * Generate help text HTML in div format + * @since 1.20 + * @param $helptext String|null + * @return String + */ + public function getHelpTextHtmlDiv( $helptext ) { + if ( is_null( $helptext ) ) { + return ''; + } + + $div = Html::rawElement( 'div', array( 'class' => 'htmlform-tip' ), $helptext ); + return $div; + } + + /** + * Generate help text HTML formatted for raw output + * @since 1.20 + * @param $helptext String|null + * @return String + */ + public function getHelpTextHtmlRaw( $helptext ) { + return $this->getHelpTextHtmlDiv( $helptext ); + } + + /** + * Determine the help text to display + * @since 1.20 + * @return String + */ + public function getHelpText() { $helptext = null; if ( isset( $this->mParams['help-message'] ) ) { @@ -1115,12 +1260,12 @@ abstract class HTMLFormField { } if ( isset( $this->mParams['help-messages'] ) ) { - foreach( $this->mParams['help-messages'] as $name ) { + foreach ( $this->mParams['help-messages'] as $name ) { $helpMessage = (array)$name; $msg = wfMessage( array_shift( $helpMessage ), $helpMessage ); - if( $msg->exists() ) { - if( is_null( $helptext ) ) { + if ( $msg->exists() ) { + if ( is_null( $helptext ) ) { $helptext = ''; } else { $helptext .= wfMessage( 'word-separator' )->escaped(); // some space @@ -1132,23 +1277,32 @@ abstract class HTMLFormField { elseif ( isset( $this->mParams['help'] ) ) { $helptext = $this->mParams['help']; } + return $helptext; + } - if ( !is_null( $helptext ) ) { - $row = Html::rawElement( - 'td', - array( 'colspan' => 2, 'class' => 'htmlform-tip' ), - $helptext - ); - $row = Html::rawElement( 'tr', array(), $row ); - $html .= "$row\n"; - } + /** + * Determine form errors to display and their classes + * @since 1.20 + * @param $value String the value of the input + * @return Array + */ + public function getErrorsAndErrorClass( $value ) { + $errors = $this->validate( $value, $this->mParent->mFieldData ); - return $html; + if ( $errors === true || ( !$this->mParent->getRequest()->wasPosted() && ( $this->mParent->getMethod() == 'post' ) ) ) { + $errors = ''; + $errorClass = ''; + } else { + $errors = self::formatErrors( $errors ); + $errorClass = 'mw-htmlform-invalid-input'; + } + return array( $errors, $errorClass ); } function getLabel() { return $this->mLabel; } + function getLabelHtml( $cellAttributes = array() ) { # Don't output a for= attribute for labels with no associated input. # Kind of hacky here, possibly we don't want these to be