jenkins-bot has submitted this change and it was merged.

Change subject: Allow inclusion of additional fields
......................................................................


Allow inclusion of additional fields

Change-Id: I10ac5a9b91ae35af5c8277d06823af869b3226fc
---
M ContactPage.php
M ContactPage_body.php
M README
3 files changed, 108 insertions(+), 22 deletions(-)

Approvals:
  Reedy: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/ContactPage.php b/ContactPage.php
index 917e505..c57f6fb 100644
--- a/ContactPage.php
+++ b/ContactPage.php
@@ -22,7 +22,7 @@
        '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 @@
  *      'SenderName' => 'User Email',
  *      'RequireDetails' => true,
  *      'IncludeIP' => true,
+ *      'AdditionalFields' => array(),
  * );
  * @endcode
  */
@@ -70,4 +71,20 @@
        // 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 @@
                }
                $this->getOutput()->setPageTitle( $pageTitle );
 
-               $text = '';
                $subject = '';
 
                # Check for type in [[Special:Contact/type]]: change pagetext 
and prefill form fields
@@ -111,17 +110,11 @@
                        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();
@@ -140,6 +133,8 @@
                        }
                        $fromAddress = $user->getEmail();
                }
+
+               $additional = $config['AdditionalFields'];
 
                $formItems = array(
                        'FromName' => array(
@@ -167,14 +162,7 @@
                                '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 @@
                        )->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 @@
                        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 @@
                        $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 @@
 
                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 @@
        'SenderName' => 'User Email',
        'RequireDetails' => true,
        'IncludeIP' => true,
+       'AdditionalFields' => array(),
   );
 
 All contact form keys (in this case 'formname') should be in
@@ -59,6 +60,23 @@
 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.

-- 
To view, visit https://gerrit.wikimedia.org/r/113883
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I10ac5a9b91ae35af5c8277d06823af869b3226fc
Gerrit-PatchSet: 17
Gerrit-Project: mediawiki/extensions/ContactPage
Gerrit-Branch: master
Gerrit-Owner: Reedy <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Reedy <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to