CRM-17763 postMailing hook -- use mailing ID as only param
[civicrm-core.git] / CRM / Mailing / BAO / MailingJob.php
index 4dee25c748fecaf009bdb6ddded5f411cd7ecb9f..71851049debb97b30a98813df81baf549af512f8 100644 (file)
@@ -29,8 +29,6 @@
  *
  * @package CRM
  * @copyright CiviCRM LLC (c) 2004-2015
- * $Id$
- *
  */
 
 require_once 'Mail.php';
@@ -84,8 +82,6 @@ class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob {
    *
    * @param array $testParams
    * @param null $mode
-   *
-   * @return void
    */
   public static function runJobs($testParams = NULL, $mode = NULL) {
     $job = new CRM_Mailing_BAO_MailingJob();
@@ -136,7 +132,7 @@ class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob {
 
     while ($job->fetch()) {
       // still use job level lock for each child job
-      $lock = Civi\Core\Container::singleton()->get('lockManager')->acquire("data.mailing.job.{$job->id}");
+      $lock = Civi::lockManager()->acquire("data.mailing.job.{$job->id}");
       if (!$lock->isAcquired()) {
         continue;
       }
@@ -185,7 +181,7 @@ class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob {
 
       // Get the mailer
       if ($mode === NULL) {
-        $mailer = \Civi\Core\Container::singleton()->get('pear_mail');
+        $mailer = \Civi::service('pear_mail');
       }
       elseif ($mode == 'sms') {
         $mailer = CRM_SMS_Provider::singleton(array('mailing_id' => $job->mailing_id));
@@ -198,7 +194,7 @@ class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob {
 
       // Mark the child complete
       if ($isComplete) {
-        /* Finish the job */
+        // Finish the job.
 
         $transaction = new CRM_Core_Transaction();
 
@@ -223,8 +219,8 @@ class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob {
   }
 
   /**
-   * post process to determine if the parent job.
-   * as well as the mailing is complete after the run
+   * Post process to determine if the parent job
+   * as well as the mailing is complete after the run.
    * @param null $mode
    */
   public static function runJobs_post($mode = NULL) {
@@ -289,6 +285,9 @@ class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob {
         $mailing->is_completed = TRUE;
         $mailing->save();
         $transaction->commit();
+
+        // CRM-17763
+        CRM_Utils_Hook::postMailing($job->mailing_id);
       }
     }
   }
@@ -341,7 +340,7 @@ class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob {
     // X Number of child jobs
     while ($job->fetch()) {
       // still use job level lock for each child job
-      $lock = Civi\Core\Container::singleton()->get('lockManager')->acquire("data.mailing.job.{$job->id}");
+      $lock = Civi::lockManager()->acquire("data.mailing.job.{$job->id}");
       if (!$lock->isAcquired()) {
         continue;
       }
@@ -458,7 +457,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
           $recipients->phone_id,
         );
         $count++;
-        if ($count % CRM_Core_DAO::BULK_MAIL_INSERT_COUNT == 0) {
+        if ($count % CRM_Mailing_Config::BULK_MAIL_INSERT_COUNT == 0) {
           CRM_Mailing_Event_BAO_Queue::bulkCreate($params, $now);
           $count = 0;
           $params = array();
@@ -478,8 +477,6 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
    *   A Mail object to send the messages.
    *
    * @param array $testParams
-   *
-   * @return void
    */
   public function deliver(&$mailer, $testParams = NULL) {
     $mailing = new CRM_Mailing_BAO_Mailing();
@@ -544,6 +541,10 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
       $config = CRM_Core_Config::singleton();
     }
 
+    if (property_exists($mailing, 'language') && $mailing->language && $mailing->language != 'en_US') {
+      $swapLang = CRM_Utils_AutoClean::swap('global://dbLocale?getter', 'call://i18n/setLocale', $mailing->language);
+    }
+
     $job_date = CRM_Utils_Date::isoToMysql($this->scheduled_date);
     $fields = array();
 
@@ -562,19 +563,17 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
 
     // CRM-12376
     // This handles the edge case scenario where all the mails
-    // have been delivered in prior jobs
+    // have been delivered in prior jobs.
     $isDelivered = TRUE;
 
-    // make sure that there's no more than $config->mailerBatchLimit mails processed in a run
+    // make sure that there's no more than $mailerBatchLimit mails processed in a run
+    $mailerBatchLimit = Civi::settings()->get('mailerBatchLimit');
     while ($eq->fetch()) {
       // if ( ( $mailsProcessed % 100 ) == 0 ) {
       // CRM_Utils_System::xMemory( "$mailsProcessed: " );
       // }
 
-      if (
-        $config->mailerBatchLimit > 0 &&
-        self::$mailsProcessed >= $config->mailerBatchLimit
-      ) {
+      if ($mailerBatchLimit > 0 && self::$mailsProcessed >= $mailerBatchLimit) {
         if (!empty($fields)) {
           $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
         }
@@ -632,11 +631,9 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
     $params = $targetParams = $deliveredParams = array();
     $count = 0;
 
-    /**
-     * CRM-15702: Sending bulk sms to contacts without e-mail address fails.
-     * Solution is to skip checking for on hold
-     */
-    $skipOnHold = TRUE; //do include a statement to check wether e-mail address is on hold
+    // CRM-15702: Sending bulk sms to contacts without e-mail address fails.
+    // Solution is to skip checking for on hold
+    $skipOnHold = TRUE;    //do include a statement to check wether e-mail address is on hold
     if ($mailing->sms_provider_id) {
       $skipOnHold = FALSE; //do not include a statement to check wether e-mail address is on hold
     }
@@ -661,7 +658,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
         $details[0][$contactID] = array();
       }
 
-      /* Compose the mailing */
+      // Compose the mailing.
       $recipient = $replyToEmail = NULL;
       $replyValue = strcmp($mailing->replyto_email, $mailing->from_email);
       if ($replyValue) {
@@ -681,7 +678,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
         continue;
       }
 
-      /* Send the mailing */
+      // Send the mailing.
 
       $body = &$message->get();
       $headers = &$message->headers();
@@ -738,7 +735,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
           CRM_Utils_System::civiExit();
         }
 
-        /* Register the bounce event */
+        // Register the bounce event.
 
         $params = array(
           'event_queue_id' => $field['id'],
@@ -758,12 +755,12 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
         CRM_Core_Error::debug_log_message($result->getMessage());
       }
       else {
-        /* Register the delivery event */
+        // Register the delivery event.
         $deliveredParams[] = $field['id'];
         $targetParams[] = $field['contact_id'];
 
         $count++;
-        if ($count % CRM_Core_DAO::BULK_MAIL_INSERT_COUNT == 0) {
+        if ($count % CRM_Mailing_Config::BULK_MAIL_INSERT_COUNT == 0) {
           $this->writeToDB(
             $deliveredParams,
             $targetParams,
@@ -798,8 +795,9 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
       }
 
       // If we have enabled the Throttle option, this is the time to enforce it.
-      if (isset($config->mailThrottleTime) && $config->mailThrottleTime > 0) {
-        usleep((int ) $config->mailThrottleTime);
+      $mailThrottleTime = Civi::settings()->get('mailThrottleTime');
+      if (!empty($mailThrottleTime)) {
+        usleep((int ) $mailThrottleTime);
       }
     }
 
@@ -931,12 +929,7 @@ AND    status IN ( 'Scheduled', 'Running', 'Paused' )
     }
 
     if ($writeActivity === NULL) {
-      $writeActivity = CRM_Core_BAO_Setting::getItem(
-        CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
-        'write_activity_record',
-        NULL,
-        TRUE
-      );
+      $writeActivity = Civi::settings()->get('write_activity_record');
     }
 
     if (!$writeActivity) {