CRM-16572 - CiviMail sending to primary instead of bulk email account for smart groups
[civicrm-core.git] / CRM / Mailing / BAO / Mailing.php
index 8fb0bb55b03147f2f4c79672edb7273c2bba35a3..288403b295ebb695ea7ddeea08e7cc109001c7b3 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
@@ -425,7 +425,7 @@ WHERE      $mg.entity_table = '$group'
       }
 
       $smartGroupInclude = "
-INSERT IGNORE INTO I_$job_id (email_id, contact_id)
+REPLACE INTO I_$job_id (email_id, contact_id)
 SELECT     civicrm_email.id as email_id, c.id as contact_id
 FROM       civicrm_contact c
 INNER JOIN civicrm_email                ON civicrm_email.contact_id         = c.id
@@ -442,7 +442,7 @@ $order_by
 ";
       if ($mode == 'sms') {
         $smartGroupInclude = "
-INSERT IGNORE INTO I_$job_id (phone_id, contact_id)
+REPLACE INTO I_$job_id (phone_id, contact_id)
 SELECT     p.id as phone_id, c.id as contact_id
 FROM       civicrm_contact c
 INNER JOIN civicrm_phone p                ON p.contact_id         = c.id
@@ -631,7 +631,7 @@ ORDER BY   i.contact_id, i.{$tempColumn}
 
     $protos = '(https?|ftp)';
     $letters = '\w';
-    $gunk = '\{\}/#~:.?+=&;%@!\,\-';
+    $gunk = '\{\}/#~:.?+=&;%@!\,\-\|\(\)\*';
     $punc = '.:?\-';
     $any = "{$letters}{$gunk}{$punc}";
     if ($onlyHrefs) {
@@ -1330,6 +1330,9 @@ ORDER BY   civicrm_email.is_bulkmail DESC
     );
     $mailParams['toEmail'] = $email;
 
+    // Add job ID to mailParams for external email delivery service to utilise
+    $mailParams['job_id'] = $job_id;
+
     CRM_Utils_Hook::alterMailParams($mailParams, 'civimail');
 
     // CRM-10699 support custom email headers
@@ -1469,7 +1472,7 @@ ORDER BY   civicrm_email.is_bulkmail DESC
       if ($this->url_tracking) {
         $data = CRM_Mailing_BAO_TrackableURL::getTrackerURL($token, $this->id, $event_queue_id);
         if (!empty($html)) {
-          $data = htmlentities($data);
+          $data = htmlentities($data, ENT_NOQUOTES);
         }
       }
       else {
@@ -1608,6 +1611,10 @@ ORDER BY   civicrm_email.is_bulkmail DESC
    * @throws \Exception
    */
   public static function create(&$params, $ids = array()) {
+    // WTH $ids
+    if (empty($ids) && isset($params['id'])) {
+      $ids['mailing_id'] = $ids['id'] = $params['id'];
+    }
 
     // CRM-12430
     // Do the below only for an insert
@@ -1745,7 +1752,7 @@ ORDER BY   civicrm_email.is_bulkmail DESC
 
       // Populate the recipients.
       if (empty($params['_skip_evil_bao_auto_recipients_'])) {
-        self::getRecipients($job->id, $mailing->id, NULL, NULL, TRUE, FALSE);
+        self::getRecipients($job->id, $mailing->id, NULL, NULL, TRUE, $mailing->dedupe_email);
       }
     }
 
@@ -3128,4 +3135,39 @@ AND        m.id = %1
     return civicrm_api('MailingContact', 'getcount', $params);
   }
 
+  /**
+   * Get a list of permissions required for CRUD'ing each field
+   * (when workflow is enabled).
+   *
+   * @return array
+   *   Array (string $fieldName => string $permName)
+   */
+  public static function getWorkflowFieldPerms() {
+    $fieldNames = array_keys(CRM_Mailing_DAO_Mailing::fields());
+    $fieldPerms = array();
+    foreach ($fieldNames as $fieldName) {
+      if ($fieldName == 'id') {
+        $fieldPerms[$fieldName] = array(
+          array('access CiviMail', 'schedule mailings', 'approve mailings', 'create mailings'), // OR
+        );
+      }
+      elseif (in_array($fieldName, array('scheduled_date', 'scheduled_id'))) {
+        $fieldPerms[$fieldName] = array(
+          array('access CiviMail', 'schedule mailings'), // OR
+        );
+      }
+      elseif (in_array($fieldName, array('approval_date', 'approver_id', 'approval_status_id', 'approval_note'))) {
+        $fieldPerms[$fieldName] = array(
+          array('access CiviMail', 'approve mailings'), // OR
+        );
+      }
+      else {
+        $fieldPerms[$fieldName] = array(
+          array('access CiviMail', 'create mailings'), // OR
+        );
+      }
+    }
+    return $fieldPerms;
+  }
+
 }