Adamw has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/106472


Change subject: (FR #1280) Store queue message source metadata fields in Civi
......................................................................

(FR #1280) Store queue message source metadata fields in Civi

Change-Id: I1a6fd85ba876376a452dd8f012283e4aa3b269d5
---
M sites/all/modules/queue2civicrm/queue2civicrm.module
M sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
2 files changed, 49 insertions(+), 30 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/72/106472/1

diff --git a/sites/all/modules/queue2civicrm/queue2civicrm.module 
b/sites/all/modules/queue2civicrm/queue2civicrm.module
index 3aead0c..454abe0 100644
--- a/sites/all/modules/queue2civicrm/queue2civicrm.module
+++ b/sites/all/modules/queue2civicrm/queue2civicrm.module
@@ -214,14 +214,24 @@
 
 /**
  * Process one contribution from the queue to CiviCRM.
+ *
+ * @param Stomp_Frame $msg
  */
-function queue2civicrm_import( $msg ) {
+function queue2civicrm_import( Stomp_Frame $msg ) {
     // save the original message for logging
     $msg_orig = $msg;
 
-    if (!is_array($msg)) {
-        $msg = json_decode($msg->body, true);
-    }
+    // Take valuable message headers and flatten them into the message body
+    $pull_headers = array(
+        'source_name',
+        'source_type',
+        'source_host',
+        'source_run_id',
+        'source_version',
+        'source_enqueued_time',
+    );
+    $msg = json_decode( $msg->body, true )
+        + array_intersect_key( $msg->headers, array_flip( $pull_headers ) );
 
     /**
     * prepare data for logging
@@ -229,7 +239,7 @@
     $log = array(
         'gateway' => $msg[ 'gateway' ],
         'gateway_txn_id' => $msg[ 'gateway_txn_id' ],
-        'data' => ( is_array( $msg_orig ) ? json_encode( $msg_orig ) : 
$msg_orig->body ),
+        'data' => $msg_orig->body,
         'timestamp' => time(),
         'verified' => 0,
     );
diff --git a/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
index e2b2c3d..91c1e3e 100644
--- a/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_common/wmf_civicrm/wmf_civicrm.module
@@ -290,39 +290,48 @@
     }
   
     /**
-    *  Apply custom field defaults.
-    *
     *  Hopefully the Civi API will eventually handle custom fields in the
     *  above "Create" call.
     *
-    * At the moment, all custom fields get default values for online 
contribution EXCEPT for 'Donor Comment'.
-    *
     * NOTE: the custom field for "Gift_Information" indicates that check 
number is stored here.  It is not.
     */
-
-    $comment = ( isset( $msg[ 'comment' ] ) ) ? $msg[ 'comment'] : '';
-    $lettercode = ( $msg[ 'letter_code' ] ) ? $msg[ 'letter_code'] : NULL;
-    $gateway_account = array_key_exists( 'gateway_account', $msg ) ? $msg[ 
'gateway_account' ] : '';
-
-    // n.b. Data field names are legacy, and do not necessarily match the 
labels.
-    $custom_vars = array(
-        'Contribution_Comments' => $comment,
-        'Gift_Data' => $lettercode,
-        'gateway_account' => $gateway_account,
+    $custom_field_defaults = array(
+        'Contribution_Comments' => '',
+        'Gift_Data' => NULL,
+        'gateway_account' => '',
     );
-    if ( array_key_exists( 'gift_source', $msg ) ) {
-        $custom_vars['Campaign'] = $msg['gift_source'];
-    }
-    if ( array_key_exists( 'direct_mail_appeal', $msg ) ) {
-        $custom_vars['Appeal'] = $msg['direct_mail_appeal'];
-    }
-    if ( array_key_exists( 'restrictions', $msg ) ) {
-        $custom_vars['Fund'] = $msg['restrictions'];
-    }
-    if ( array_key_exists( 'import_batch_number', $msg ) ) {
-        $custom_vars['import_batch_number'] = $msg['import_batch_number'];
+
+    // Legacy field names which do not match the message field names
+    $custom_name_mangle = array(
+        'comment' => 'Contribution_Comments',
+        'direct_mail_appeal' => 'Appeal',
+        'gift_source' => 'Campaign',
+        'letter_code' => 'Gift_Data',
+        'restrictions' => 'Fund',
+    );
+
+    // Custom fields with names that match the message fields
+    $custom_name_passthrough = array(
+        'gateway_account',
+        'import_batch_number',
+        'source_name',
+        'source_type',
+        'source_host',
+        'source_run_id',
+        'source_version',
+        'source_enqueued_time',
+    );
+
+    $custom_fields = $custom_name_mangle
+        + array_combine( $custom_name_passthrough, $custom_name_passthrough );
+
+    foreach ( $custom_fields as $msg_key => $custom_key ) {
+        if ( array_key_exists( $msg_key, $msg ) ) {
+            $custom_vars[$custom_key] = $msg[$msg_key];
+        }
     }
 
+    $custom_vars += $custom_field_defaults;
     wmf_civicrm_set_custom_field_values( $contribution_result[ 'id' ], 
$custom_vars );
 
     return $contribution_result[ 'values' ][ $contribution_result[ 'id' ] ];

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1a6fd85ba876376a452dd8f012283e4aa3b269d5
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Adamw <[email protected]>

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

Reply via email to