From fbc9f38fae43e787d458eac988974f7bdc55bdea Mon Sep 17 00:00:00 2001 From: John Du Hart Date: Sat, 22 Oct 2011 20:03:40 +0000 Subject: [PATCH] Adding functions to HTMLForm for setting the submitText and WrapperLegend to messages instead of having to use wfMsg every single time --- includes/HTMLForm.php | 65 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index a9cbc79bad..15a73f9f50 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -580,6 +580,14 @@ class HTMLForm { $this->mSubmitText = $t; } + /** + * Set the text for the submit button to a message + * @param $msg String message key + */ + public function setSubmitTextMsg( $msg ) { + return $this->setSubmitText( wfMsg( $msg ) ); + } + /** * Get the text for the submit button, either customised or a default. * @return unknown_type @@ -618,6 +626,15 @@ class HTMLForm { */ public function setWrapperLegend( $legend ) { $this->mWrapperLegend = $legend; } + /** + * Prompt the whole form to be wrapped in a
, with + * this message as its element. + * @param $msg String message key + */ + public function setWrapperLegendMsg( $msg ) { + return $this->setWrapperLegend( wfMsg( $msg ) ); + } + /** * Set the prefix for various default messages * TODO: currently only used for the
legend on forms @@ -1357,6 +1374,8 @@ class HTMLSelectField extends HTMLFormField { return $p; } + print_r( $value ); + $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] ); if ( in_array( $value, $validOptions ) ) @@ -1382,10 +1401,54 @@ class HTMLSelectField extends HTMLFormField { $select->setAttribute( 'disabled', 'disabled' ); } + if ( !empty( $this->mParams['multiple'] ) ) { + $select->setAttribute( 'name', $this->mName . '[]' ); + $select->setAttribute( 'multiple', 'multiple' ); + + if ( !empty( $this->mParams['size'] ) ) { + $select->setAttribute( 'size', $this->mParams['size'] ); + } + } + $select->addOptions( $this->mParams['options'] ); return $select->getHTML(); } + + /** + * @param $request WebRequest + * @return String + */ + function loadDataFromRequest( $request ) { + if ( $this->mParent->getMethod() == 'post' ) { + if( $request->wasPosted() ){ + # Checkboxes are just not added to the request arrays if they're not checked, + # so it's perfectly possible for there not to be an entry at all + return $request->getArray( $this->mName, array() ); + } else { + # That's ok, the user has not yet submitted the form, so show the defaults + return $this->getDefault(); + } + } else { + # This is the impossible case: if we look at $_GET and see no data for our + # field, is it because the user has not yet submitted the form, or that they + # have submitted it with all the options unchecked? We will have to assume the + # latter, which basically means that you can't specify 'positive' defaults + # for GET forms. + # @todo FIXME... + return $request->getArray( $this->mName, array() ); + } + } + + public static function keysAreValues( $array ) { + $resultArray = array(); + + foreach ( $array as $name => $value ) { + $resultArray[$value] = $value; + } + + return $resultArray; + } } /** @@ -1861,7 +1924,7 @@ class HTMLHiddenField extends HTMLFormField { $this->mParent->addHiddenField( $this->mName, - $this->mDefault, + $value, $params ); -- 2.20.1