INFRA-132 - CRM/Event - phpcbf
[civicrm-core.git] / CRM / Mailing / BAO / MailingJob.php
index 8d0410783029e595ea2009754d8cd5c2705ea388..4f98ac1e95d7702f69d73b7047948ab40e358725 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -39,12 +39,21 @@ require_once 'Mail.php';
  * Class CRM_Mailing_BAO_MailingJob
  */
 class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob {
-  CONST MAX_CONTACTS_TO_PROCESS = 1000;
+  const MAX_CONTACTS_TO_PROCESS = 1000;
 
   /**
-   * class constructor
+   * (Dear God Why) Keep a global count of mails processed within the current
+   * request.
+   *
+   * @static
+   * @var int
+   */
+  static $mailsProcessed = 0;
+
+  /**
+   * Class constructor
    */
-  function __construct() {
+  public function __construct() {
     parent::__construct();
   }
 
@@ -68,11 +77,10 @@ class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob {
   /**
    * Initiate all pending/ready jobs
    *
-   * @param null $testParams
+   * @param array $testParams
    * @param null $mode
    *
    * @return void
-   * @access public
    * @static
    */
   public static function runJobs($testParams = NULL, $mode = NULL) {
@@ -421,7 +429,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
   }
 
   /**
-   * @param null $testParams
+   * @param array $testParams
    */
   public function queue($testParams = NULL) {
     $mailing = new CRM_Mailing_BAO_Mailing();
@@ -471,12 +479,12 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
   /**
    * Send the mailing
    *
-   * @param object $mailer A Mail object to send the messages
+   * @param object $mailer
+   *   A Mail object to send the messages.
    *
-   * @param null $testParams
+   * @param array $testParams
    *
    * @return void
-   * @access public
    */
   public function deliver(&$mailer, $testParams = NULL) {
     $mailing = new CRM_Mailing_BAO_Mailing();
@@ -530,12 +538,12 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
                     WHERE       $eqTable.job_id = " . $this->id . "
                         AND     $edTable.id IS null
                         AND     $ebTable.id IS null
-                        AND    $contactTable.is_opt_out = 0";
+                        AND    ( $contactTable.is_opt_out = 0
+                        OR       $contactTable.do_not_sms = 0 )";
     }
     $eq->query($query);
 
-    static $config = NULL;
-    static $mailsProcessed = 0;
+    $config = NULL;
 
     if ($config == NULL) {
       $config = CRM_Core_Config::singleton();
@@ -570,7 +578,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
 
       if (
         $config->mailerBatchLimit > 0 &&
-        $mailsProcessed >= $config->mailerBatchLimit
+        self::$mailsProcessed >= $config->mailerBatchLimit
       ) {
         if (!empty($fields)) {
           $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments);
@@ -578,7 +586,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
         $eq->free();
         return FALSE;
       }
-      $mailsProcessed++;
+      self::$mailsProcessed++;
 
       $fields[] = array(
         'id' => $eq->id,
@@ -627,6 +635,15 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
     $params           = $targetParams = $deliveredParams = array();
     $count            = 0;
 
+    /**
+     * CRM-15702: Sending bulk sms to contacts without e-mail addres 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
+    }
+
     foreach ($fields as $key => $field) {
       $params[] = $field['contact_id'];
     }
@@ -634,7 +651,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
     $details = CRM_Utils_Token::getTokenDetails(
       $params,
       $returnProperties,
-      TRUE, TRUE, NULL,
+      $skipOnHold, TRUE, NULL,
       $mailing->getFlattenedTokens(),
       get_class($this),
       $this->id
@@ -800,9 +817,10 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
   }
 
   /**
-   * cancel a mailing
+   * Cancel a mailing
    *
-   * @param int $mailingId  the id of the mailing to be canceled
+   * @param int $mailingId
+   *   The id of the mailing to be canceled.
    * @static
    */
   public static function cancel($mailingId) {
@@ -848,10 +866,10 @@ AND    status IN ( 'Scheduled', 'Running', 'Paused' )
   /**
    * Return a translated status enum string
    *
-   * @param string $status        The status enum
+   * @param string $status
+   *   The status enum.
    *
    * @return string               The translated version
-   * @access public
    * @static
    */
   public static function status($status) {
@@ -874,7 +892,6 @@ AND    status IN ( 'Scheduled', 'Running', 'Paused' )
    * to only process jobs that are approved.
    *
    * @return string        For use in a WHERE clause
-   * @access public
    * @static
    */
   public static function workflowClause() {
@@ -893,8 +910,8 @@ AND    status IN ( 'Scheduled', 'Running', 'Paused' )
   }
 
   /**
-   * @param $deliveredParams
-   * @param $targetParams
+   * @param array $deliveredParams
+   * @param array $targetParams
    * @param $mailing
    * @param $job_date
    *
@@ -1017,4 +1034,3 @@ AND    record_type_id = $targetRecordID
     return $result;
   }
 }
-