jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/374915 )

Change subject: TY mailer: skip extra queries to look up contact and custom 
fields
......................................................................


TY mailer: skip extra queries to look up contact and custom fields

Contribution API is janky till v4 comes out, let's just grab all
the info in one query.

TODO: tests should cover setting thankyou_date, no_thank_you logic

Change-Id: Idf77f9cdf7022c8e2faaf5d1eb318026f32e6205
---
M sites/all/modules/thank_you/thank_you.module
1 file changed, 55 insertions(+), 68 deletions(-)

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



diff --git a/sites/all/modules/thank_you/thank_you.module 
b/sites/all/modules/thank_you/thank_you.module
index 0f32722..796cea9 100644
--- a/sites/all/modules/thank_you/thank_you.module
+++ b/sites/all/modules/thank_you/thank_you.module
@@ -234,99 +234,86 @@
  * TODO: rewrite the civi api stuff to work like other code
  */
 function thank_you_for_contribution( $contribution_id ) {
-       civicrm_initialize( true );
-       $contribution = civicrm_api("Contribution","get",
-               array (
-                         'version' =>'3',
-                         'contribution_id' => $contribution_id
-               ));
+       civicrm_initialize();
+       $mailingData = CRM_Core_DAO::executeQuery("
+               SELECT
+                       cntr.id AS contribution_id,
+                       cntr.currency,
+                       cntr.receive_date,
+                       cntr.thankyou_date,
+                       cntr.total_amount,
+                       cntr.trxn_id,
+                       cntc.id AS contact_id,
+                       cntc.display_name,
+                       cntc.first_name,
+                       cntc.last_name,
+                       cntc.preferred_language,
+                       e.email,
+                       x.no_thank_you,
+                       x.original_amount,
+                       x.original_currency
+               FROM civicrm_contribution cntr
+               INNER JOIN civicrm_contact cntc ON cntr.contact_id = cntc.id
+               LEFT JOIN civicrm_email e ON e.contact_id = cntc.id AND 
e.is_primary = 1
+               INNER JOIN wmf_contribution_extra x ON cntr.id = x.entity_id
+               WHERE cntr.id = %1
+       ", array(
+               1 => array(
+                       $contribution_id,
+                       'Int'
+               )
+       ));
+       $found = $mailingData->fetch();
 
        // check that the API result is a valid contribution result
-       if( !WMFCiviAPICheck::check_api_contribution( $contribution, 
$contribution_id ) ){
+       if( !$found ){
                // the API result is bad
-               $msg = 'Could not retrieve contribution record for: ' . 
$contribution_id . '<pre>' . print_r( $contribution, true ) . '</pre>';
+               $msg = 'Could not retrieve contribution record for: ' . 
$contribution_id . '<pre>' . print_r( $mailingData, true ) . '</pre>';
                throw new WmfException( 'GET_CONTRIBUTION', $msg );
        }
-       // go ahead and remove the extra layer of indirection to make it easier 
to use
-       $simplified = WMFCiviAPICheck::check_api_simplify( $contribution, 
$contribution_id );
-       if( !$simplified ){
-               // simplification failed
-               $msg = 'Could not simplify contribution record for: ' . 
$contribution_id . '<pre>' . print_r( $contribution, true )  . '</pre>';
-               throw new WmfException( 'GET_CONTRIBUTION', $msg );
-       }
-       $contribution = $simplified;
+       $mailingData = (array) $mailingData;
 
     // don't send a Thank You email if one has already been sent
-    if ( !empty($contribution['thankyou_date']) ) {
+    if ( !empty($mailingData['thankyou_date']) ) {
         watchdog('thank_you', 'Thank you email already sent for this 
transaction.', array(), WATCHDOG_INFO);
         return false;
     }
     // only send a Thank You email if we are within the specified window
-    if (strtotime($contribution['receive_date']) < time() - 86400 * 
variable_get('thank_you_days', 14)) {
+    if (strtotime($mailingData['receive_date']) < time() - 86400 * 
variable_get('thank_you_days', 14)) {
         watchdog('thank_you', 'Contribution is older than limit, ignoring.', 
array(), WATCHDOG_INFO);
         return false;
     }
 
-       // get the information for the associated contact
-       $contact = civicrm_api("Contact","get",
-               array (
-                         'version' =>'3',
-                         'id' => $contribution[ 'contact_id' ],
-                         'return' => 
"display_name,first_name,last_name,email,preferred_language"
-               )
-    );
-
-       // check that the API result is a valid contact result
-       if( !WMFCiviAPICheck::check_api_contact( $contact, $contribution[ 
'contact_id' ] ) ){
-               // the API result is bad
-               $msg = 'Could not retrieve contact record for: ' . 
$contribution['contact_id'] . '<pre>' . print_r( $contact, true )  . '</pre>';
-               throw new WmfException( 'GET_CONTACT', $msg );
-       }
-       // go ahead and remove the extra layer of indirection to make it easier 
to use
-       $simplified = WMFCiviAPICheck::check_api_simplify( $contact, 
$contribution[ 'contact_id' ] );
-       if( !$simplified ){
-               // simplification failed
-               $msg = 'Could not simplify contact record for: ' . 
$contribution['contact_id']. '<pre>' . print_r( $contact, true ). '</pre>';
-               throw new WmfException( 'GET_CONTACT', $msg );
-       }
-       $contact = $simplified;
-
     // check for contacts without an email address
-    if ( empty( $contact['email'] ) or $contact['email'] === 
'[email protected]' ) {
+    if ( empty( $mailingData['email'] ) or $mailingData['email'] === 
'[email protected]' ) {
         watchdog('thank_you', 'No email address found. Processing as 
anonymous.', array(), WATCHDOG_INFO);
-        wmf_civicrm_set_no_thank_you( $contribution['contribution_id'], 
'anonymous' );
+        wmf_civicrm_set_no_thank_you( $contribution_id, 'anonymous' );
         return false;
     }
 
-    $custom_values = wmf_civicrm_get_custom_values( $contribution_id, array(
-        'no_thank_you',
-        'original_amount',
-        'original_currency',
-    ) );
-
-    if ( $custom_values['no_thank_you'] ) {
-        watchdog('thank_you', "Contribution has been marked 
no_thank_you={$custom_values['no_thank_you']}, skipping.", array(), 
WATCHDOG_INFO);
+    if ( $mailingData['no_thank_you'] ) {
+        watchdog('thank_you', "Contribution has been marked 
no_thank_you={$mailingData['no_thank_you']}, skipping.", array(), 
WATCHDOG_INFO);
         return false;
     }
 
-    $amount = $custom_values['original_amount'];
-    $currency = $custom_values['original_currency'];
+    $amount = $mailingData['original_amount'];
+    $currency = $mailingData['original_currency'];
 
     // Use settlement currency if the original currency is virtual, for tax 
reasons.
-    if ( $custom_values['original_currency'] === 'BTC' ) {
-        $amount = $contribution['total_amount'];
-        $currency = $contribution['currency'];
+    if ( $mailingData['original_currency'] === 'BTC' ) {
+        $amount = $mailingData['total_amount'];
+        $currency = $mailingData['currency'];
     }
 
     $is_recurring = false;
     try {
-        $transaction = WmfTransaction::from_unique_id( 
$contribution['trxn_id'] );
+        $transaction = WmfTransaction::from_unique_id( $mailingData['trxn_id'] 
);
         $is_recurring = $transaction->is_recurring;
     } catch ( WmfException $ex ) {
         watchdog( 'thank_you', $ex->getMessage(), NULL, WATCHDOG_NOTICE );
     }
 
-    $locale = $contact['preferred_language'];
+    $locale = $mailingData['preferred_language'];
     if ( !$locale ) {
         watchdog( 'thank_you', "Donor language unknown.  Defaulting to 
English...", NULL, WATCHDOG_INFO );
         $locale = 'en';
@@ -335,19 +322,19 @@
 
     $params = array(
         'amount' => $amount,
-        'contact_id' => $contact['id'],
+        'contact_id' => $mailingData['contact_id'],
         'currency' => $currency,
-        'first_name' => $contact['first_name'],
+        'first_name' => $mailingData['first_name'],
                'from_name' => variable_get( 'thank_you_from_name', 'Wikimedia 
Foundation' ),
                'from_address' => variable_get( 'thank_you_from_address', 
'[email protected]' ),
-        'last_name' => $contact['last_name'],
+        'last_name' => $mailingData['last_name'],
         'locale' => $locale,
-        'name' => $contact['display_name'],
-        'receive_date' => $contribution['receive_date'],
-        'recipient_address' => $contact['email'],
+        'name' => $mailingData['display_name'],
+        'receive_date' => $mailingData['receive_date'],
+        'recipient_address' => $mailingData['email'],
         'recurring' => $is_recurring,
-        'transaction_id' => "CNTCT-{$contact['id']}",
-        'unsubscribe_link' => build_unsub_link( $contribution['id'], 
$contact['email'], $locale ),
+        'transaction_id' => "CNTCT-{$mailingData['contact_id']}",
+        'unsubscribe_link' => build_unsub_link( $contribution_id, 
$mailingData['email'], $locale ),
         'contribution_tags' => wmf_thank_you_get_tag_names($contribution_id),
     );
 
@@ -355,7 +342,7 @@
 
     if ( $success ) {
                watchdog('thank_you', "Thank you mail sent successfully for 
contribution id: $contribution_id to " . $params['recipient_address'], array(), 
WATCHDOG_INFO);
-        thank_you_update_ty_date( $contribution );
+        thank_you_update_ty_date( $mailingData );
                return true;
     } else {
                $msg = "Thank you mail failed for contribution id: 
$contribution_id to " . $params['recipient_address'];

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Idf77f9cdf7022c8e2faaf5d1eb318026f32e6205
Gerrit-PatchSet: 8
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: AndyRussG <[email protected]>
Gerrit-Reviewer: Cdentinger <[email protected]>
Gerrit-Reviewer: Eileen <[email protected]>
Gerrit-Reviewer: Katie Horn <[email protected]>
Gerrit-Reviewer: Mepps <[email protected]>
Gerrit-Reviewer: XenoRyet <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to