diff --git a/ContactPage.php b/ContactPage.php index 917e505..c57f6fb 100644 --- a/ContactPage.php +++ b/ContactPage.php @@ -22,7 +22,7 @@ $wgExtensionCredits['specialpage'][] = array( 'author' => array( 'Daniel Kinzler', 'Sam Reed' ), 'url' => 'https://www.mediawiki.org/wiki/Extension:ContactPage', 'descriptionmsg' => 'contactpage-desc', - 'version' => 2 + 'version' => 2.1 ); // Set up the new special page @@ -48,6 +48,7 @@ $wgSpecialPages['Contact'] = 'SpecialContact'; * 'SenderName' => 'User Email', * 'RequireDetails' => true, * 'IncludeIP' => true, + * 'AdditionalFields' => array(), * ); * @endcode */ @@ -70,4 +71,20 @@ $wgContactConfig['default'] = array( // If true, the form will include a checkbox offering to put the IP // address of the submitter in the subject line 'IncludeIP' => false, -); \ No newline at end of file + + // Any additional fields to display on the contact form. + // Uses https://www.mediawiki.org/wiki/HTMLForm notation + // Using any of your own "AdditionalFields" will replce the large text box + // Copy the code below into your own config if still wanted + // + // 'type' => 'selectandother' currently isn't supported. + 'AdditionalFields' => array( + 'Text' => array( + 'label-message' => 'emailmessage', + 'type' => 'textarea', + 'rows' => 20, + 'cols' => 80, + 'required' => true, + ), + ), +); diff --git a/ContactPage_body.php b/ContactPage_body.php index 8ba937e..161a1b3 100644 --- a/ContactPage_body.php +++ b/ContactPage_body.php @@ -95,7 +95,6 @@ class SpecialContact extends UnlistedSpecialPage { } $this->getOutput()->setPageTitle( $pageTitle ); - $text = ''; $subject = ''; # Check for type in [[Special:Contact/type]]: change pagetext and prefill form fields @@ -111,17 +110,11 @@ class SpecialContact extends UnlistedSpecialPage { if ( !$message->isDisabled() ) { $subject = $message->inContentLanguage()->plain(); } - - $message = wfMessage( 'contactpage-text-' . $this->formType ); - if ( !$message->isDisabled() ) { - $text = $message->inContentLanguage()->plain(); - } } else { $formText = wfMessage( 'contactpage-pagetext' )->parseAsBlock(); } $subject = trim( $subject ); - $text = trim( $text ); if ( $subject === '' ) { $subject = wfMessage( 'contactpage-defsubject' )->inContentLanguage()->text(); @@ -141,6 +134,8 @@ class SpecialContact extends UnlistedSpecialPage { $fromAddress = $user->getEmail(); } + $additional = $config['AdditionalFields']; + $formItems = array( 'FromName' => array( 'label-message' => 'contactpage-fromname', @@ -167,14 +162,7 @@ class SpecialContact extends UnlistedSpecialPage { 'type' => 'text', 'default' => $subject, ), - 'Text' => array( - 'label-message' => 'emailmessage', - 'type' => 'textarea', - 'rows' => 20, - 'cols' => 80, - 'default' => $text, - 'required' => true, - ), + ) + $additional + array( 'CCme' => array( 'label-message' => 'emailccme', 'type' => 'check', @@ -294,11 +282,61 @@ class SpecialContact extends UnlistedSpecialPage { )->inContentLanguage()->text(); } - wfDebug( __METHOD__ . ': sending mail from ' . $submitterAddress->toString() . - ' to ' . $targetAddress->toString(). - ' replyto ' . ( $replyto == null ? '-/-' : $replyto->toString() ) . "\n" ); + $text = ''; + foreach( $config['AdditionalFields'] as $name => $field ) { + $class = HTMLForm::getClassFromDescriptor( $name, $field ); - $text = $formData['Text']; + $value = ''; + // TODO: Support selectandother/HTMLSelectAndOtherField + // options, options-messages and options-message + if ( isset( $field['options-messages'] ) ) { // Multiple values! + if ( is_string( $formData[$name] ) ) { + $optionValues = array_flip( $field['options-messages'] ); + if ( isset( $optionValues[$formData[$name]] ) ) { + $value = wfMessage( $optionValues[$formData[$name]] )->inContentLanguage()->text(); + } else { + $value = $formData[$name]; + } + } elseif ( count( $formData[$name] ) ) { + $formValues = array_flip( $formData[$name] ); + $value .= "\n"; + foreach( $field['options-messages'] as $msg => $optionValue ) { + $msg = wfMessage( $msg )->inContentLanguage()->text(); + $optionValue = self::getYesOrNoMsg( isset( $formValues[$optionValue] ) ); + $value .= "\t$msg: $optionValue\n"; + } + } + } elseif ( isset( $field['options'] ) ) { + if ( is_string( $formData[$name] ) ) { + $value = $formData[$name]; + } elseif ( count( $formData[$name] ) ) { + $formValues = array_flip( $formData[$name] ); + $value .= "\n"; + foreach( $field['options'] as $msg => $optionValue ) { + $optionValue = self::getYesOrNoMsg( isset( $formValues[$optionValue] ) ); + $value .= "\t$msg: $optionValue\n"; + } + } + } elseif ( $class === 'HTMLCheckField' ) { + $value = self::getYesOrNoMsg( $formData[$name] xor ( isset( $field['invert'] ) && $field['invert'] ) ); + } elseif ( isset( $formData[$name] ) ) { + // HTMLTextField, HTMLTextAreaField + // HTMLFloatField, HTMLIntField + + // Just dump the value if its wordy + $value = $formData[$name]; + } else { + continue; + } + + if ( isset( $field['label-message'] ) ) { + $name = wfMessage( $field['label-message'] )->inContentLanguage()->text(); + } else { + $name = $field['label']; + } + + $text .= "{$name}: $value\n"; + } // Stolen from Special:EmailUser $error = ''; @@ -310,6 +348,10 @@ class SpecialContact extends UnlistedSpecialPage { return false; // TODO: Need to do some proper error handling here } + wfDebug( __METHOD__ . ': sending mail from ' . $submitterAddress->toString() . + ' to ' . $targetAddress->toString(). + ' replyto ' . ( $replyto == null ? '-/-' : $replyto->toString() ) . "\n" + ); $mailResult = UserMailer::send( $targetAddress, $submitterAddress, $subject, $text, $replyto ); if( !$mailResult->isOK() ) { @@ -323,7 +365,8 @@ class SpecialContact extends UnlistedSpecialPage { $cc_subject = wfMessage( 'emailccsubject', $contactUser->getName(), $subject )->text(); if( wfRunHooks( 'ContactForm', array( &$submitterAddress, &$contactSender, &$cc_subject, &$text, $this->formType ) ) ) { wfDebug( __METHOD__ . ': sending cc mail from ' . $contactSender->toString() . - ' to ' . $submitterAddress->toString() . "\n" ); + ' to ' . $submitterAddress->toString() . "\n" + ); $ccResult = UserMailer::send( $submitterAddress, $contactSender, $cc_subject, $text ); if( !$ccResult->isOK() ) { // At this stage, the user's CC mail has failed, but their @@ -340,4 +383,12 @@ class SpecialContact extends UnlistedSpecialPage { return true; } + + /** + * @param bool $value + * @return string + */ + private static function getYesOrNoMsg( $value ) { + return wfMessage( $value ? 'htmlform-yes' : 'htmlform-no' )->inContentLanguage()->text(); + } } diff --git a/README b/README index 6ade0bd..b3815b2 100644 --- a/README +++ b/README @@ -38,6 +38,7 @@ $wgContactConfig. 'SenderName' => 'User Email', 'RequireDetails' => true, 'IncludeIP' => true, + 'AdditionalFields' => array(), ); All contact form keys (in this case 'formname') should be in @@ -59,6 +60,23 @@ email address on Special:Contact. IncludeIP Whether the form will include a checkbox offering to put the IP address of the submitter in the subject line. +AdditionalFields is used to add any additional fields to the contact form. +These are done using https://www.mediawiki.org/wiki/HTMLForm notation. +The default message text box is not included by default, and if required, +should be added manually to the AdditionalFields array like below. + +It should be noted that type 'selectandother' is not currently supported. + + 'AdditionalFields' => array( + 'Text' => array( + 'label-message' => 'emailmessage', + 'type' => 'textarea', + 'rows' => 20, + 'cols' => 80, + 'required' => true, + ), + ), + == Customization == [[Special:Contact]] calls the 'default' form.