X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FHTMLForm.php;h=2e7e3999bdce441fe8c76eeb415bdf855cea4d87;hb=8a6475bd704fd57c2207b20e1588134a742e3bb6;hp=3362fccf338e2492d02b8789f2e4272d8a98b165;hpb=7bbe971aec2d548de981a12ed08a7b56a536dcdb;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index 3362fccf33..2e7e3999bd 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -1,175 +1,1600 @@ $info, + * where $info is an Associative Array with any of the following: + * + * 'class' -- the subclass of HTMLFormField that will be used + * to create the object. *NOT* the CSS class! + * 'type' -- roughly translates into the \n"; + + $descriptor['fieldname'] = $fieldname; + + $obj = new $class( $descriptor ); + + return $obj; } /** - * @access private - * @param string $varname Name of the textbox. - * @param string $value Optional value (default empty) - * @param integer $size Optional size of the textbox (default 20) + * Prepare form for submission */ - function textbox( $varname, $value='', $size=20 ) { - if ( $this->mRequest->wasPosted() ) { - $value = $this->mRequest->getText( $varname, $value ); + function prepareForm() { + # Check if we have the info we need + if ( ! $this->mTitle ) { + throw new MWException( "You must call setTitle() on an HTMLForm" ); } - $value = htmlspecialchars( $value ); - return "
\n"; + + // FIXME shouldn't this be closer to displayForm() ? + self::addJS(); + + # Load data from the request. + $this->loadData(); } /** - * @access private - * @param string $varname Name of the radiobox. - * @param array $fields Various fields. + * Try submitting, with edit token check first + * @return Status|boolean */ - function radiobox( $varname, $fields ) { - foreach ( $fields as $value => $checked ) { - $s .= "
\n"; + function tryAuthorizedSubmit() { + global $wgUser, $wgRequest; + $editToken = $wgRequest->getVal( 'wpEditToken' ); + + $result = false; + if ( $this->getMethod() != 'post' || $wgUser->matchEditToken( $editToken ) ) { + $result = $this->trySubmit(); } - return $this->fieldset( $this->mName.'-'.$varname, $s ); + return $result; } - + /** - * @access private - * @param string $varname Name of the textareabox. - * @param string $value Optional value (default empty) - * @param integer $size Optional size of the textarea (default 20) + * The here's-one-I-made-earlier option: do the submission if + * posted, or display the form with or without funky valiation + * errors + * @return Bool or Status whether submission was successful. */ - function textareabox ( $varname, $value='', $size=20 ) { - if ( $this->mRequest->wasPosted() ) { - $value = $this->mRequest->getText( $varname, $value ); - } - $value = htmlspecialchars( $value ); - return '
\n"; + function show() { + $this->prepareForm(); + + $result = $this->tryAuthorizedSubmit(); + if ( $result === true || ( $result instanceof Status && $result->isGood() ) ){ + return $result; + } + + $this->displayForm( $result ); + return false; } /** - * @access private - * @param string $varname Name of the arraybox. - * @param integer $size Optional size of the textarea (default 20) + * Validate all the fields, and call the submision callback + * function if everything is kosher. + * @return Mixed Bool true == Successful submission, Bool false + * == No submission attempted, anything else == Error to + * display. */ - function arraybox( $varname , $size=20 ) { - $s = ''; - if ( $this->mRequest->wasPosted() ) { - $arr = $this->mRequest->getArray( $varname ); - if ( is_array( $arr ) ) { - foreach ( $_POST[$varname] as $index=>$element ) { - $s .= htmlspecialchars( $element )."\n"; - } + function trySubmit() { + # Check for validation + foreach ( $this->mFlatFields as $fieldname => $field ) { + if ( !empty( $field->mParams['nodata'] ) ) { + continue; + } + if ( $field->validate( + $this->mFieldData[$fieldname], + $this->mFieldData ) + !== true + ) { + return isset( $this->mValidationErrorMessage ) + ? $this->mValidationErrorMessage + : array( 'htmlform-invalid-input' ); } } - return "