Awight has uploaded a new change for review.

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

Change subject: WIP hook_civicrm_post; step towards Mailer->Templating 
integration; large_donation overhaul
......................................................................

WIP hook_civicrm_post; step towards Mailer->Templating integration; 
large_donation overhaul

Change-Id: Iad4da4aab1cbca206b3a66b11023e928f0b4312e
---
M sites/all/modules/large_donation/large_donation.module
A sites/all/modules/large_donation/templates/notification.en.html
M sites/all/modules/thank_you/thank_you.module
M sites/all/modules/wmf_campaigns/wmf_campaigns.module
M sites/all/modules/wmf_communication/Mailer.php
M sites/all/modules/wmf_communication/Templating.php
6 files changed, 143 insertions(+), 158 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/76/153376/1

diff --git a/sites/all/modules/large_donation/large_donation.module 
b/sites/all/modules/large_donation/large_donation.module
index 4ddd578..1645152 100644
--- a/sites/all/modules/large_donation/large_donation.module
+++ b/sites/all/modules/large_donation/large_donation.module
@@ -1,5 +1,8 @@
 <?php
 
+use wmf_communication\Mailer;
+use wmf_communication\Templating;
+
 /**
  * Implementation of hook_menu().
  */
@@ -63,40 +66,29 @@
 }
 
 /**
- * Implements hook_wmf_civicrm_contribution_message_import
- * queue2civicrm_import().
+ * Implements hook_civicrm_post
  */
-function large_donation_wmf_civicrm_contribution_message_import( $msg, 
$contribution ) {
+function large_donation_civicrm_post( $action, $type, $id, &$contribution ) {
 
-  $contribution_id = $contribution['id'];
+    switch ( $action ) {
+    case 'create':
+    case 'edit':
+        if ( $type === 'Contribution' ) {
+            $large_donation_amount = floatval( variable_get( 
'large_donation_amount', 0.00 ) );
 
-  $large_donation_amount = (float) variable_get('large_donation_amount', 0.00);
+            if ( $contribution['total_amount'] >= $large_donation_amount ) {
 
-  $gross = isset( $msg['gross'] ) ?  (float) $msg['gross'] : 0.00;
+              watchdog('large_donation', 'Calling large donation function for 
contribution: ' . $contribution['id'] . '<pre>' . 
check_plain(print_r($contribution, TRUE)) . '</pre>');
 
-  $currency = isset( $msg['currency'] ) ? $msg['currency'] : '';
+              large_donation_notification_send( $contribution );
 
-  $date = isset( $msg['date'] ) ? $msg['date'] : NULL;
-
-  if ( empty( $contribution_id ) ) {
-
-    watchdog('large_donation', 'Contribution id is empty!');
-
-  } elseif ( empty( $large_donation_amount ) ) {
-
-    watchdog('large_donation', 'Large donation threshold amount is not set 
up!');
-
-  } elseif ( exchange_rate_convert( $currency, $gross, $date ) >= 
$large_donation_amount ) {
-
-    watchdog('large_donation', 'Calling large donation function for 
contribution: ' . $contribution['id'] . '<pre>' . 
check_plain(print_r($contribution, TRUE)) . '</pre>');
-
-    large_donation_notification_send( $msg, $contribution );
-
-  } else {
-
-    watchdog('large_donation', 'Donation amount [ ' . $gross . ' ' . $currency 
. ' ] was not above the threshold [ ' . $large_donation_amount . ' ]');
-
-  }
+            } else {
+              watchdog('large_donation', "Donation amount 
{$contribution['total_amount']} was not above the threshold 
[{$large_donation_amount}]");
+            }
+        }
+        break;
+    default:
+    }
 }
 
 /**
@@ -109,86 +101,53 @@
  *
  * @param array $contribution_info
  */
-function large_donation_notification_send( $msg, $contribution ) {
+function large_donation_notification_send( $contribution ) {
 
-  $site_url = CIVICRM_UF_BASEURL . base_path();
+  $contribution_link = CRM_Utils_System::url(
+    'civicrm/contact/view',
+    array(
+      'selectedChild' => 'contribute',
+      'cid' => $contribution['contact_id'],
+      'reset' => 1,
+    ),
+    true,
+    'Contributions'
+  );
 
   $to = variable_get('large_donation_notifymail', '');
 
-  $large_donation_amount = (float) variable_get('large_donation_amount', 0.00);
+  $params = array(
+    'large_donation_amount' => floatval( variable_get( 
'large_donation_amount', 0.00 ) ),
+    'contact_id' => $contribution['contact_id'],
+    'contribution_id' => $contribution['id'],
+    'total_amount' => $contribution['currency'],
+    'payment_method' => $contribution['payment_method'],
+  );
 
-  $contact_id = $contribution['contact_id'];
+  if ( !$to ) {
+    watchdog( 'large_donation', 'Notification recipient address is not set 
up!', NULL, WATCHDOG_ERROR );
+  }
 
-  $link = $site_url . 
'?q=civicrm/contact/view&reset=1&selectedChild=contribute&cid=' . $contact_id . 
'#Contributions';
+    $mailer = Mailer::getDefault();
 
-  $contribution_id = $contribution['id'];
-
-  $gross = $msg['gross'];
-
-  $currency = $msg['currency'];
-
-  $payment_method = $msg['payment_method'];
-
-  if ($to != ''){
-
-    $message = '';
-
-    $message .= 'To whom it may concern:';
-    $message .= PHP_EOL;
-    $message .= PHP_EOL;
-    $message .= 'A large donation was made >= ' . $large_donation_amount . ' ' 
. $currency;
-    $message .= PHP_EOL;
-    $message .= PHP_EOL;
-    $message .= 'contact_id: ' . $contact_id;
-    $message .= PHP_EOL;
-    $message .= 'contribution_id: ' . $contribution_id;
-    $message .= PHP_EOL;
-    $message .= 'currency: ' . $currency;
-    $message .= PHP_EOL;
-    $message .= 'gross: ' . $gross;
-    $message .= PHP_EOL;
-    $message .= 'payment_method: ' . $payment_method;
-
-    $message .= PHP_EOL;
-    $message .= PHP_EOL;
-    $message .= 'Contact contribution in CiviCRM:';
-    $message .= PHP_EOL;
-    $message .= PHP_EOL;
-    $message .= $link;
-    $message .= PHP_EOL;
-    $message .= PHP_EOL;
-
-    $message .= 'You may need to examine this donation.';
-
-       $path = variable_get('wmf_common_phpmailer_location', '');
-    require_once(implode(DIRECTORY_SEPARATOR, array($path, 
'class.phpmailer.php')));
-
-    $mail = new PHPMailer( true );
+    $message = Templating::render( __DIR__ . '/templates', 'en', 
'notification', $params );
 
     try {
-        $mail->set('Charset','utf-8');
+        $email = array(
+            'to' => preg_split( '/\\s*[,\\n]\\s*/', $to, -1, 
PREG_SPLIT_NO_EMPTY ),
+            'from_address' => '[email protected]',
+            'from_name' => 'Large Donation Bot',
+            'subject' => "WMF - large donation: 
\${$contribution['total_amount']}",
+        );
 
-        $to = preg_split('/\\s*[,\\n]\\s*/', $to, -1, PREG_SPLIT_NO_EMPTY);
-               foreach ( $to as $addx ){
-               $mail->AddAddress( trim( $addx ) );
-               }
-
-        $mail->SetFrom( '[email protected]', 'WMF CiviCRM-Bot' );
-
-        $mail->Subject = 'WMF - large donation: ' . $gross . ' ' . $currency;
-
-        $mail->AltBody = $message;
         $mail->MsgHTML( nl2br( $message ) );
+        $mail->AltBody = $message;
         $mail->Send();
 
         $email_success = true;
-    } catch (phpmailerException $e) {
-        watchdog('large_donation', 'Sending large donation message failed for 
contribution (2): ' . $contribution_id . '<pre>' . 
check_plain(print_r($contribution, TRUE)) . "\n\n" . $e->errorMessage() . 
'</pre>', array(), WATCHDOG_ERROR);
-    } catch (Exception $e) {
-        watchdog('large_donation', 'Sending large donation message failed for 
contribution (3): ' . $contribution_id . '<pre>' . 
check_plain(print_r($contribution, TRUE)) . "\n\n" . $e->getMessage() . 
'</pre>', array(), WATCHDOG_ERROR);
+    } catch ( Exception $e ) {
+        watchdog('large_donation', 'Sending large donation message failed for 
contribution: ' . $contribution_id . '<pre>' . 
check_plain(print_r($contribution, TRUE)) . "\n\n" . $e->getMessage() . 
'</pre>', array(), WATCHDOG_ERROR);
     }
-  } else {
-    watchdog('large_donation', 'Notification recipient address is not set 
up!');
   }
 
   if ( $email_success ) {
diff --git a/sites/all/modules/large_donation/templates/notification.en.html 
b/sites/all/modules/large_donation/templates/notification.en.html
new file mode 100644
index 0000000..e30e8e2
--- /dev/null
+++ b/sites/all/modules/large_donation/templates/notification.en.html
@@ -0,0 +1,19 @@
+To whom it may concern:
+
+A large donation was made, over {{ large_donation_amount }} {{ currency }}
+
+contact_id: {{ contact_id }}
+
+contribution_id: {{ contribution_id }}
+
+currency: {{ currency }}
+
+gross: {{ gross }}
+
+payment_method: {{ payment_method }}
+
+Contact contribution in CiviCRM:
+
+{{ contribution_link }}
+
+You may need to examine this donation.
diff --git a/sites/all/modules/thank_you/thank_you.module 
b/sites/all/modules/thank_you/thank_you.module
index af95994..3297491 100644
--- a/sites/all/modules/thank_you/thank_you.module
+++ b/sites/all/modules/thank_you/thank_you.module
@@ -432,15 +432,6 @@
                return false;
        }
 
-       $converter = new Html2Text( $email['html'], false, array( 'do_links' => 
'table' ) );
-       $email['plaintext'] = $converter->get_text();
-
-       if( $email['plaintext'] == false ){
-               watchdog( 'thank_you', "Text rendering of template failed in 
{$params['locale']}.", array(), WATCHDOG_ERROR );
-               wmf_common_failmail( 'thank_you', "FAILED TO RENDER PLAINTEXT 
EMAIL in " . __FUNCTION__, $params );
-               return false;
-       }
-
        $subj_msg = "donate_interface-email-subject";
        $email['subject'] = Translation::get_translated_message( $subj_msg, 
$params['locale'] );
 
diff --git a/sites/all/modules/wmf_campaigns/wmf_campaigns.module 
b/sites/all/modules/wmf_campaigns/wmf_campaigns.module
index 0b801a1..a0f4d87 100644
--- a/sites/all/modules/wmf_campaigns/wmf_campaigns.module
+++ b/sites/all/modules/wmf_campaigns/wmf_campaigns.module
@@ -198,57 +198,62 @@
 }
 
 /**
- * implementation of hook_wmf_civicrm_contribution_message_import
+ * Implements hook_civicrm_post
  */
-function wmf_campaigns_wmf_civicrm_contribution_message_import( $msg, 
$contribution ) {
-    if ( empty( $msg['utm_campaign'] ) ) {
-        watchdog( 'wmf_campaigns', "No campaign for message", NULL, 
WATCHDOG_INFO );
-        return;
-    }
+function wmf_campaigns_civicrm_post( $action, $type, $id, &$contribution ) {
+    switch ( $action ) {
+    case 'create':
+    case 'edit':
+        if ( $type === 'Contribution' ) {
+            if ( empty( $msg['utm_campaign'] ) ) {
+                watchdog( 'wmf_campaigns', "No campaign for message", NULL, 
WATCHDOG_INFO );
+                return;
+            }
 
-    $campaignKey = $msg['utm_campaign'];
-    $campaign = WmfCampaign::fromKey( $campaignKey );
-    if ( !$campaign || !$campaign->getNotificationEmail() ) {
-        watchdog( 'wmf_campaigns', "No special behaviors configured for 
campaign {$campaignKey}", NULL, WATCHDOG_INFO );
-        return;
-    }
+            $campaignKey = $msg['utm_campaign'];
+            $campaign = WmfCampaign::fromKey( $campaignKey );
+            if ( !$campaign || !$campaign->getNotificationEmail() ) {
+                watchdog( 'wmf_campaigns', "No special behaviors configured 
for campaign {$campaignKey}", NULL, WATCHDOG_INFO );
+                return;
+            }
 
-    $email = $campaign->getNotificationEmail();
-    watchdog( 'wmf_campaigns', "Notifying {$email} about match on campaign 
{$campaignKey}", NULL, WATCHDOG_INFO );
+            $email = $campaign->getNotificationEmail();
+            watchdog( 'wmf_campaigns', "Notifying {$email} about match on 
campaign {$campaignKey}", NULL, WATCHDOG_INFO );
 
-    //TODO: template this POS. So inconvenient.
-    $letter = array(
-        'from_address' => "[email protected]",
-        'from_name' => "Campaign notifer",
-        'reply_to' => "[email protected]",
-        'subject' => "Campaign donation: {$campaignKey}",
-    );
-    $civiUrl = CRM_Utils_System::url( 'civicrm/contact/view/contribution',
-        array(
-            'reset' => 1,
-            'id' => $contribution['id'],
-            'cid' => $contribution['contact_id'],
-            'action' => 'view',
-        ),
-        true // absolute link
-    );
-    $letter['html'] = <<<EOS
-<p>Contribution for campaign {$campaignKey}: Contact ID 
{$contribution['contact_id']}, [link to <a href="{$civiUrl}">Civi 
console</a>]</p>
-EOS;
+            //TODO: template this POS. So inconvenient.
+            $letter = array(
+                'from_address' => "[email protected]",
+                'from_name' => "Campaign notifer",
+                'reply_to' => "[email protected]",
+                'subject' => "Campaign donation: {$campaignKey}",
+            );
+            $civiUrl = CRM_Utils_System::url( 
'civicrm/contact/view/contribution',
+                array(
+                    'reset' => 1,
+                    'id' => $contribution['id'],
+                    'cid' => $contribution['contact_id'],
+                    'action' => 'view',
+                ),
+                true // absolute link
+            );
+            $letter['html'] = <<<EOS
+        <p>Contribution for campaign {$campaignKey}: Contact ID 
{$contribution['contact_id']}, [link to <a href="{$civiUrl}">Civi 
console</a>]</p>
+        EOS;
 
-    $civiUrl = html_entity_decode( $civiUrl );
-    $letter['plaintext'] = <<<EOS
-Contribution for campaign {$campaignKey}: Contact ID 
{$contribution['contact_id']}
+            $civiUrl = html_entity_decode( $civiUrl );
+            $letter['plaintext'] = <<<EOS
+        Contribution for campaign {$campaignKey}: Contact ID 
{$contribution['contact_id']}
 
-{$civiUrl}
-EOS;
+        {$civiUrl}
+        EOS;
 
-    $addressees = explode( ",", $campaign->getNotificationEmail() );
-    $mailer = Mailer::getDefault();
-    foreach ( $addressees as $recipient ) {
-        $letter['to_address'] = trim( $recipient );
-        $letter['to_name'] = t( "Dear donor services" );
+            $addressees = explode( ",", $campaign->getNotificationEmail() );
+            $mailer = Mailer::getDefault();
+            foreach ( $addressees as $recipient ) {
+                $letter['to_address'] = trim( $recipient );
+                $letter['to_name'] = t( "Dear donor services" );
 
-        $mailer->send( $letter );
+                $mailer->send( $letter );
+            }
     }
 }
diff --git a/sites/all/modules/wmf_communication/Mailer.php 
b/sites/all/modules/wmf_communication/Mailer.php
index 88a9f46..48de8dc 100644
--- a/sites/all/modules/wmf_communication/Mailer.php
+++ b/sites/all/modules/wmf_communication/Mailer.php
@@ -108,7 +108,16 @@
 </body>
 </html>";
     }
-}
+
+    function normalizeContent( &$email ) {
+        $converter = new Html2Text( $email['html'], false, array( 'do_links' 
=> 'table' ) );
+        $email['plaintext'] = $converter->get_text();
+
+        if ( $email['plaintext'] === false ) {
+            watchdog( 'thank_you', "Text rendering of template failed in 
{$email['locale']}.", array(), WATCHDOG_ERROR );
+            throw new WmfException( 'RENDER', "Could not render plaintext" );
+        }
+    }
 
 /**
  * Use the PHPMailer engine
@@ -140,7 +149,13 @@
         $mailer->SetFrom( $email['from_address'], $email['from_name'] );
         $mailer->set( 'Sender', $email['reply_to'] );
 
-        $mailer->AddAddress( $email['to_address'], $email['to_name'] );
+        if ( isset( $email['to'] ) ) {
+            foreach ( $email['to'] as $to ) {
+                $mailer->AddAddress( $to );
+            }
+        } else {
+            $mailer->AddAddress( $email['to_address'], $email['to_name'] );
+        }
 
                foreach ($headers as $header => $value) {
                        $mailer->AddCustomHeader( "$header: $value" );
diff --git a/sites/all/modules/wmf_communication/Templating.php 
b/sites/all/modules/wmf_communication/Templating.php
index 9dbf10d..f05cdfe 100644
--- a/sites/all/modules/wmf_communication/Templating.php
+++ b/sites/all/modules/wmf_communication/Templating.php
@@ -94,8 +94,7 @@
     protected function loadTemplate() {
         $language = $this->language;
         do {
-            // TODO: encapsulate path strategy in a function so it can be 
overridden
-            $template = $this->loadTemplateFile( 
"{$this->format}/{$this->template_name}.{$language}.{$this->format}" );
+            $template = $this->loadTemplateFile( $language );
             if ( $template ) {
                 return $template;
             }
@@ -110,22 +109,19 @@
             $language = Translation::next_fallback( $language );
         } while ( $language );
 
-        watchdog( 'wmf_communication',
-            "Using universal language fallback for template :key...",
-            array( ':key' => $this->key() ),
-            WATCHDOG_INFO
-        );
-        return $this->loadTemplateFile( 
"{$this->format}/{$this->template_name}.{$this->format}" );
+        throw new Exception( "No fallbacks for template 
{$this->template_name}, from {$this->language}" );
     }
 
     /**
      * Load a Twig template from the given filesystem path
      *
-     * @param string $path absolute path, or path relative to configured Twig 
include dirs
+     * @param string $language
      *
      * @return Twig_Template
      */
-    protected function loadTemplateFile( $path ) {
+    protected function loadTemplateFile( $language ) {
+        $path = 
"{$this->format}/{$this->template_name}.{$language}.{$this->format}";
+
         watchdog( 'wmf_communication',
             "Searching for template file at :path",
             array( ':path' => $path ),

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

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

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

Reply via email to