From 8593859d5e8bf56db5934eeeae0503b8cc6f2ab3 Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Sun, 29 Oct 2017 15:37:02 +0530 Subject: [PATCH] refactor getRecipients and respective adjustments --- CRM/Mailing/BAO/Mailing.php | 566 +++++---------------------- CRM/Mailing/BAO/MailingAB.php | 2 +- CRM/Mailing/BAO/MailingJob.php | 6 +- CRM/SMS/Form/Group.php | 7 +- ang/crmMailing/services.js | 2 - tests/phpunit/api/v3/MailingTest.php | 4 - 6 files changed, 93 insertions(+), 494 deletions(-) diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index 8f62259831bc..eaf2cf4fa24d 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -96,29 +96,33 @@ public function __construct() { /** * @deprecated * - * @param int $job_id - * @param int $mailing_id + * @param int $mailingID * * @return int */ - public static function getRecipientsCount($job_id, $mailing_id = NULL) { - // need this for backward compatibility, so we can get count for old mailings - // please do not use this function if possible. - $eq = self::getRecipients($job_id, $mailing_id); - return $eq->N; + public static function getRecipientsCount($mailingID) { + //rebuild the recipients + self::getRecipients($mailingID); + + return civicrm_api3('MailingRecipients', 'getcount', array('mailing_id' => $mailingID)); } /** * This function retrieve recipients of selected mailing groups. * - * @param CRM_Mailing_DAO_Mailing $mailingObj + * @param int $mailingID * * @return void */ - public static function getMailRecipients($mailingObj) { + public static function getRecipients($mailingID) { + // load mailing object + $mailingObj = new self(); + $mailingObj->id = $mailingID; + $mailingObj->find(TRUE); + $mailing = CRM_Mailing_BAO_Mailing::getTableName(); $contact = CRM_Contact_DAO_Contact::getTableName(); - $mailingID = $mailingObj->id; + $isSMSmode = (!CRM_Utils_System::isNull($mailingObj->sms_provider_id)); $mailingGroup = new CRM_Mailing_DAO_MailingGroup(); $recipientsGroup = $excludeSmartGroupIDs = $includeSmartGroupIDs = array(); @@ -173,7 +177,7 @@ public static function getMailRecipients($mailingObj) { if (!empty($recipientsGroup['Exclude'])) { CRM_Utils_SQL_Select::from('civicrm_group_contact') ->select('DISTINCT contact_id') - ->where('status = "Added" AND id IN (#groups)') + ->where('status = "Added" AND group_id IN (#groups)') ->param('#groups', $recipientsGroup['Exclude']) ->insertInto($excludeTempTablename, array('contact_id')) ->execute(); @@ -197,11 +201,12 @@ public static function getMailRecipients($mailingObj) { ->execute(); } + $tempColumn = $isSMSmode ? 'phone_id' : 'email_id'; $email = CRM_Core_DAO_Email::getTableName(); // Get all the group contacts we want to include. $mailingGroup->query( "CREATE TEMPORARY TABLE $includedTempTablename - (contact_id int primary key, email_id int) + (contact_id int primary key, $tempColumn int) ENGINE=HEAP" ); @@ -213,22 +218,50 @@ public static function getMailRecipients($mailingObj) { $location_filter, "$email.email IS NOT NULL", "$email.email != ''", - 'temp.contact_id IS null', + 'temp.contact_id IS NULL', ); - // Get the group contacts, but only those which are not in the - // exclusion temp table. - CRM_Utils_SQL_Select::from($email) - ->select("$contact.id as contact_id, $email.id as email_id") - ->join($contact, " INNER JOIN $contact ON $email.contact_id = $contact.id ") - ->join('gc', " INNER JOIN civicrm_group_contact gc ON gc.contact_id = $contact.id ") - ->join('mg', " INNER JOIN civicrm_mailing_group mg ON gc.group_id = mg.entity_id AND mg.search_id IS NULL ") - ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ") - ->where('gc.group_id IN (#groups) AND gc.status = "Added"') - ->where($includeFilters) - ->groupBy(array("$email.id", "$contact.id")) - ->replaceInto($includedTempTablename, array('contact_id', 'email_id')) - ->param('#groups', $recipientsGroup['Include']) - ->execute(); + + if ($isSMSmode) { + $phone = CRM_Core_DAO_Phone::getTableName(); + CRM_Utils_SQL_Select::from($phone) + ->select(" DISTINCT $phone.id as phone_id ") + ->join($contact, " INNER JOIN $contact ON $phone.contact_id = $contact.id ") + ->join('gc', " INNER JOIN civicrm_group_contact gc ON $contact.id = gc.contact_id ") + ->join('mg', " INNER JOIN civicrm_mailing_group mg ON gc.group_id = mg.entity_id AND mg.entity_table = 'civicrm_group' ") + ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ") + ->where(array( + "mg.group_type = 'Include'", + 'mg.search_id IS NULL', + "gc.status = 'Added'", + "$contact.do_not_sms", + "$contact.is_opt_out = 0", + "$contact.is_deceased <> 1", + "$phone.phone_type_id = " . CRM_Utils_Array::value('Mobile', CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name')), + "$phone.phone IS NOT NULL", + "$phone.phone != ''", + "mg.mailing_id = #mailingID", + 'temp.contact_id IS null', + )) + ->param('#mailingID', $mailingID) + ->replaceInto($includedTempTablename, array('contact_id', 'phone_id')) + ->execute(); + } + else { + // Get the group contacts, but only those which are not in the + // exclusion temp table. + CRM_Utils_SQL_Select::from($email) + ->select("$contact.id as contact_id, $email.id as email_id") + ->join($contact, " INNER JOIN $contact ON $email.contact_id = $contact.id ") + ->join('gc', " INNER JOIN civicrm_group_contact gc ON gc.contact_id = $contact.id ") + ->join('mg', " INNER JOIN civicrm_mailing_group mg ON gc.group_id = mg.entity_id AND mg.search_id IS NULL ") + ->join('temp', " LEFT JOIN $excludeTempTablename temp ON $contact.id = temp.contact_id ") + ->where('gc.group_id IN (#groups) AND gc.status = "Added"') + ->where($includeFilters) + ->groupBy(array("$contact.id", "$email.id")) + ->replaceInto($includedTempTablename, array('contact_id', 'email_id')) + ->param('#groups', $recipientsGroup['Include']) + ->execute(); + } if (count($includeSmartGroupIDs)) { CRM_Utils_SQL_Select::from($contact) @@ -255,7 +288,7 @@ public static function getMailRecipients($mailingObj) { $dao->search_args, $dao->entity_id ); - $query = "REPLACE INTO {$includedTempTablename} (email_id, contact_id) {$customSQL} "; + $query = "REPLACE INTO {$includedTempTablename} ($tempColumn, contact_id) {$customSQL} "; $mailingGroup->query($query); } @@ -264,17 +297,16 @@ public static function getMailRecipients($mailingObj) { // clear all the mailing recipients before populating CRM_Core_DAO::executeQuery(" DELETE FROM civicrm_mailing_recipients WHERE mailing_id = %1 ", array(1 => array($mailingID, 'Integer'))); - $selectClause = array('#mailingID', 'i.contact_id', "i.email_id"); + $selectClause = array('#mailingID', 'i.contact_id', "i.$tempColumn"); // CRM-3975 - $orderBy = array("i.contact_id", "i.email_id"); + $orderBy = array("i.contact_id", "i.$tempColumn"); $query = CRM_Utils_SQL_Select::from('civicrm_contact contact_a')->join('i', " INNER JOIN {$includedTempTablename} i ON contact_a.id = i.contact_id "); if ($mailingObj->dedupe_email) { - $orderBy = array("MIN(i.contact_id)", "MIN(i.email_id)"); - $query = $query->join('e', " INNER JOIN civicrm_email e ON e.id = i.email_id ")->groupBy("i.email_id"); + $orderBy = array("MIN(i.contact_id)", "MIN(i.$tempColumn)"); + $query = $query->join('e', " INNER JOIN civicrm_email e ON e.id = i.email_id ")->groupBy("e.email"); if (CRM_Utils_SQL::supportsFullGroupBy()) { - $k = array_search("i.email_id", $selectClause); - $selectClause[$k] = "ANY_VALUE(i.email_id)"; + $selectClause[] = "e.email"; } } @@ -286,9 +318,23 @@ public static function getMailRecipients($mailingObj) { $query = $query->where($aclWhere); } - $query->insertInto('civicrm_mailing_recipients', array('mailing_id', 'contact_id', 'email_id')) - ->param('#mailingID', $mailingID) - ->execute(); + // this mean if dedupe_email AND the mysql 5.7 supports ONLY_FULL_GROUP_BY mode then as + // SELECT must contain 'email' column as its used in GROUP BY, so in order to resolve This + // here the whole SQL code is wrapped up in FROM table i and not selecting email column for INSERT + if ($key = array_search('e.email', $selectClause)) { + unset($selectClause[$key]); + $sql = $query->toSQL(); + CRM_Utils_SQL_Select::from("( $sql ) AS i ") + ->select($selectClause) + ->insertInto('civicrm_mailing_recipients', array('mailing_id', 'contact_id', $tempColumn)) + ->param('#mailingID', $mailingID) + ->execute(); + } + else { + $query->insertInto('civicrm_mailing_recipients', array('mailing_id', 'contact_id', $tempColumn)) + ->param('#mailingID', $mailingID) + ->execute(); + } // if we need to add all emails marked bulk, do it as a post filter // on the mailing recipients table @@ -355,435 +401,6 @@ public static function getLocationFilterAndOrderBy($email_selection_method, $loc return array($location_filter, $orderBy); } - /** - * note that $job_id is used only as a variable in the temp table construction - * and does not play a role in the queries generated. - * @param int $job_id - * (misnomer) a nonce value used to name temporary tables. - * @param int $mailing_id - * @param bool $storeRecipients - * @param bool $dedupeEmail - * @param null $mode - * - * @return CRM_Mailing_Event_BAO_Queue|string - */ - public static function getRecipients( - $job_id, - $mailing_id = NULL, - $storeRecipients = FALSE, - $dedupeEmail = FALSE, - $mode = NULL) { - $mailingGroup = new CRM_Mailing_DAO_MailingGroup(); - - $mailing = CRM_Mailing_BAO_Mailing::getTableName(); - $job = CRM_Mailing_BAO_MailingJob::getTableName(); - $mg = CRM_Mailing_DAO_MailingGroup::getTableName(); - $eq = CRM_Mailing_Event_DAO_Queue::getTableName(); - - $email = CRM_Core_DAO_Email::getTableName(); - if ($mode == 'sms') { - $phone = CRM_Core_DAO_Phone::getTableName(); - } - $contact = CRM_Contact_DAO_Contact::getTableName(); - - $group = CRM_Contact_DAO_Group::getTableName(); - $g2contact = CRM_Contact_DAO_GroupContact::getTableName(); - - $m = new CRM_Mailing_DAO_Mailing(); - $m->id = $mailing_id; - $m->find(TRUE); - - list($location_filter, $order_by) = self::getLocationFilterAndOrderBy($m->email_selection_method, $m->location_type_id); - - // Create a temp table for contact exclusion. - $mailingGroup->query( - "CREATE TEMPORARY TABLE X_$job_id - (contact_id int primary key) - ENGINE=HEAP" - ); - - // Add all the members of groups excluded from this mailing to the temp - // table. - - $excludeSubGroup = "INSERT INTO X_$job_id (contact_id) - SELECT DISTINCT $g2contact.contact_id - FROM $g2contact - INNER JOIN $mg - ON $g2contact.group_id = $mg.entity_id AND $mg.entity_table = '$group' - WHERE - $mg.mailing_id = {$mailing_id} - AND $g2contact.status = 'Added' - AND $mg.group_type = 'Exclude'"; - $mailingGroup->query($excludeSubGroup); - - // Add all unsubscribe members of base group from this mailing to the temp - // table. - - $unSubscribeBaseGroup = "INSERT INTO X_$job_id (contact_id) - SELECT DISTINCT $g2contact.contact_id - FROM $g2contact - INNER JOIN $mg - ON $g2contact.group_id = $mg.entity_id AND $mg.entity_table = '$group' - WHERE - $mg.mailing_id = {$mailing_id} - AND $g2contact.status = 'Removed' - AND $mg.group_type = 'Base'"; - $mailingGroup->query($unSubscribeBaseGroup); - - // Add all the (intended) recipients of an excluded prior mailing to - // the temp table. - - $excludeSubMailing = "INSERT IGNORE INTO X_$job_id (contact_id) - SELECT DISTINCT $eq.contact_id - FROM $eq - INNER JOIN $job - ON $eq.job_id = $job.id - INNER JOIN $mg - ON $job.mailing_id = $mg.entity_id AND $mg.entity_table = '$mailing' - WHERE - $mg.mailing_id = {$mailing_id} - AND $mg.group_type = 'Exclude'"; - $mailingGroup->query($excludeSubMailing); - - // get all the saved searches AND hierarchical groups - // and load them in the cache - $sql = " -SELECT $group.id, $group.cache_date, $group.saved_search_id, $group.children -FROM $group -INNER JOIN $mg ON $mg.entity_id = $group.id -WHERE $mg.entity_table = '$group' - AND $mg.group_type = 'Exclude' - AND $mg.mailing_id = {$mailing_id} - AND ( saved_search_id != 0 - OR saved_search_id IS NOT NULL - OR children IS NOT NULL ) -"; - - $groupDAO = CRM_Core_DAO::executeQuery($sql); - while ($groupDAO->fetch()) { - if ($groupDAO->cache_date == NULL) { - CRM_Contact_BAO_GroupContactCache::load($groupDAO); - } - - $smartGroupExclude = " -INSERT IGNORE INTO X_$job_id (contact_id) -SELECT c.contact_id -FROM civicrm_group_contact_cache c -WHERE c.group_id = {$groupDAO->id} -"; - $mailingGroup->query($smartGroupExclude); - } - - $tempColumn = 'email_id'; - if ($mode == 'sms') { - $tempColumn = 'phone_id'; - } - - // Get all the group contacts we want to include. - - $mailingGroup->query( - "CREATE TEMPORARY TABLE I_$job_id - ($tempColumn int, contact_id int primary key) - ENGINE=HEAP" - ); - - // Get the group contacts, but only those which are not in the - // exclusion temp table. - - $query = "REPLACE INTO I_$job_id (email_id, contact_id) - - SELECT $email.id as email_id, - $contact.id as contact_id - FROM $email - INNER JOIN $contact - ON $email.contact_id = $contact.id - INNER JOIN $g2contact - ON $contact.id = $g2contact.contact_id - INNER JOIN $mg - ON $g2contact.group_id = $mg.entity_id - AND $mg.entity_table = '$group' - LEFT JOIN X_$job_id - ON $contact.id = X_$job_id.contact_id - WHERE - ($mg.group_type = 'Include') - AND $mg.search_id IS NULL - AND $g2contact.status = 'Added' - AND $contact.do_not_email = 0 - AND $contact.is_opt_out = 0 - AND $contact.is_deceased <> 1 - AND $location_filter - AND $email.email IS NOT NULL - AND $email.email != '' - AND $email.on_hold = 0 - AND $mg.mailing_id = {$mailing_id} - AND X_$job_id.contact_id IS null - GROUP BY $email.id, $contact.id - ORDER BY " . implode(', ', $order_by); - - if ($mode == 'sms') { - $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name'); - $query = "REPLACE INTO I_$job_id (phone_id, contact_id) - - SELECT DISTINCT $phone.id as phone_id, - $contact.id as contact_id - FROM $phone - INNER JOIN $contact - ON $phone.contact_id = $contact.id - INNER JOIN $g2contact - ON $contact.id = $g2contact.contact_id - INNER JOIN $mg - ON $g2contact.group_id = $mg.entity_id - AND $mg.entity_table = '$group' - LEFT JOIN X_$job_id - ON $contact.id = X_$job_id.contact_id - WHERE - ($mg.group_type = 'Include') - AND $mg.search_id IS NULL - AND $g2contact.status = 'Added' - AND $contact.do_not_sms = 0 - AND $contact.is_opt_out = 0 - AND $contact.is_deceased <> 1 - AND $phone.phone_type_id = {$phoneTypes['Mobile']} - AND $phone.phone IS NOT NULL - AND $phone.phone != '' - AND $mg.mailing_id = {$mailing_id} - AND X_$job_id.contact_id IS null"; - } - $mailingGroup->query($query); - - // Query prior mailings. - - $query = "REPLACE INTO I_$job_id (email_id, contact_id) - SELECT $email.id as email_id, - $contact.id as contact_id - FROM $email - INNER JOIN $contact - ON $email.contact_id = $contact.id - INNER JOIN $eq - ON $eq.contact_id = $contact.id - INNER JOIN $job - ON $eq.job_id = $job.id - INNER JOIN $mg - ON $job.mailing_id = $mg.entity_id AND $mg.entity_table = '$mailing' - LEFT JOIN X_$job_id - ON $contact.id = X_$job_id.contact_id - WHERE - ($mg.group_type = 'Include') - AND $contact.do_not_email = 0 - AND $contact.is_opt_out = 0 - AND $contact.is_deceased <> 1 - AND $location_filter - AND $email.on_hold = 0 - AND $mg.mailing_id = {$mailing_id} - AND X_$job_id.contact_id IS null - GROUP BY $email.id, $contact.id - ORDER BY " . implode(', ', $order_by); - - if ($mode == 'sms') { - $query = "REPLACE INTO I_$job_id (phone_id, contact_id) - SELECT DISTINCT $phone.id as phone_id, - $contact.id as contact_id - FROM $phone - INNER JOIN $contact - ON $phone.contact_id = $contact.id - INNER JOIN $eq - ON $eq.contact_id = $contact.id - INNER JOIN $job - ON $eq.job_id = $job.id - INNER JOIN $mg - ON $job.mailing_id = $mg.entity_id AND $mg.entity_table = '$mailing' - LEFT JOIN X_$job_id - ON $contact.id = X_$job_id.contact_id - WHERE - ($mg.group_type = 'Include') - AND $contact.do_not_sms = 0 - AND $contact.is_opt_out = 0 - AND $contact.is_deceased <> 1 - AND $phone.phone_type_id = {$phoneTypes['Mobile']} - AND $mg.mailing_id = {$mailing_id} - AND X_$job_id.contact_id IS null"; - } - $mailingGroup->query($query); - - $sql = " -SELECT $group.id, $group.cache_date, $group.saved_search_id, $group.children -FROM $group -INNER JOIN $mg ON $mg.entity_id = $group.id -WHERE $mg.entity_table = '$group' - AND $mg.group_type = 'Include' - AND $mg.search_id IS NULL - AND $mg.mailing_id = {$mailing_id} - AND ( saved_search_id != 0 - OR saved_search_id IS NOT NULL - OR children IS NOT NULL ) -"; - - $groupDAO = CRM_Core_DAO::executeQuery($sql); - while ($groupDAO->fetch()) { - if ($groupDAO->cache_date == NULL) { - CRM_Contact_BAO_GroupContactCache::load($groupDAO); - } - - $smartGroupInclude = " -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 -INNER JOIN civicrm_group_contact_cache gc ON gc.contact_id = c.id -LEFT JOIN X_$job_id ON X_$job_id.contact_id = c.id -WHERE gc.group_id = {$groupDAO->id} - AND c.do_not_email = 0 - AND c.is_opt_out = 0 - AND c.is_deceased <> 1 - AND $location_filter - AND civicrm_email.on_hold = 0 - AND X_$job_id.contact_id IS null -ORDER BY " . implode(', ', $order_by); - if ($mode == 'sms') { - $smartGroupInclude = " -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 -INNER JOIN civicrm_group_contact_cache gc ON gc.contact_id = c.id -LEFT JOIN X_$job_id ON X_$job_id.contact_id = c.id -WHERE gc.group_id = {$groupDAO->id} - AND c.do_not_sms = 0 - AND c.is_opt_out = 0 - AND c.is_deceased <> 1 - AND p.phone_type_id = {$phoneTypes['Mobile']} - AND X_$job_id.contact_id IS null"; - } - $mailingGroup->query($smartGroupInclude); - } - - // Construct the filtered search queries. - $query = " -SELECT search_id, search_args, entity_id -FROM $mg -WHERE $mg.search_id IS NOT NULL -AND $mg.mailing_id = {$mailing_id} -"; - $dao = CRM_Core_DAO::executeQuery($query); - while ($dao->fetch()) { - $customSQL = CRM_Contact_BAO_SearchCustom::civiMailSQL($dao->search_id, - $dao->search_args, - $dao->entity_id - ); - $query = "REPLACE INTO I_$job_id ({$tempColumn}, contact_id) - $customSQL"; - $mailingGroup->query($query); - } - - // Get the emails with only location override. - - $query = "REPLACE INTO I_$job_id (email_id, contact_id) - SELECT $email.id as local_email_id, - $contact.id as contact_id - FROM $email - INNER JOIN $contact - ON $email.contact_id = $contact.id - INNER JOIN $g2contact - ON $contact.id = $g2contact.contact_id - INNER JOIN $mg - ON $g2contact.group_id = $mg.entity_id - LEFT JOIN X_$job_id - ON $contact.id = X_$job_id.contact_id - WHERE - $mg.entity_table = '$group' - AND $mg.group_type = 'Include' - AND $g2contact.status = 'Added' - AND $contact.do_not_email = 0 - AND $contact.is_opt_out = 0 - AND $contact.is_deceased <> 1 - AND $location_filter - AND $email.on_hold = 0 - AND $mg.mailing_id = {$mailing_id} - AND X_$job_id.contact_id IS null - GROUP BY $email.id, $contact.id - ORDER BY " . implode(', ', $order_by); - if ($mode == "sms") { - $query = "REPLACE INTO I_$job_id (phone_id, contact_id) - SELECT DISTINCT $phone.id as phone_id, - $contact.id as contact_id - FROM $phone - INNER JOIN $contact - ON $phone.contact_id = $contact.id - INNER JOIN $g2contact - ON $contact.id = $g2contact.contact_id - INNER JOIN $mg - ON $g2contact.group_id = $mg.entity_id - LEFT JOIN X_$job_id - ON $contact.id = X_$job_id.contact_id - WHERE - $mg.entity_table = '$group' - AND $mg.group_type = 'Include' - AND $g2contact.status = 'Added' - AND $contact.do_not_sms = 0 - AND $contact.is_opt_out = 0 - AND $contact.is_deceased <> 1 - AND $phone.phone_type_id = {$phoneTypes['Mobile']} - AND $mg.mailing_id = {$mailing_id} - AND X_$job_id.contact_id IS null"; - } - $mailingGroup->query($query); - - $eq = new CRM_Mailing_Event_BAO_Queue(); - - list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause(); - $aclWhere = $aclWhere ? "WHERE {$aclWhere}" : ''; - - if ($storeRecipients && $mailing_id) { - $sql = " -DELETE -FROM civicrm_mailing_recipients -WHERE mailing_id = %1 -"; - $params = array(1 => array($mailing_id, 'Integer')); - CRM_Core_DAO::executeQuery($sql, $params); - - $selectClause = array('%1', 'i.contact_id', "i.{$tempColumn}"); - $select = "SELECT " . implode(', ', $selectClause); - // CRM-3975 - $groupBy = $groupJoin = ''; - $orderBy = "i.contact_id, i.{$tempColumn}"; - if ($dedupeEmail) { - $orderBy = "MIN(i.contact_id), MIN(i.{$tempColumn})"; - $groupJoin = " INNER JOIN civicrm_email e ON e.id = i.email_id"; - $groupBy = " GROUP BY e.email "; - $select = CRM_Contact_BAO_Query::appendAnyValueToSelect($selectClause, 'e.email'); - } - - $sql = " -INSERT INTO civicrm_mailing_recipients ( mailing_id, contact_id, {$tempColumn} ) -{$select} -FROM civicrm_contact contact_a -INNER JOIN I_$job_id i ON contact_a.id = i.contact_id - $groupJoin - {$aclFrom} - {$aclWhere} - $groupBy -ORDER BY {$orderBy} -"; - - CRM_Core_DAO::executeQuery($sql, $params); - - // if we need to add all emails marked bulk, do it as a post filter - // on the mailing recipients table - if (CRM_Core_BAO_Email::isMultipleBulkMail()) { - self::addMultipleEmails($mailing_id); - } - } - - // Delete the temp table. - - $mailingGroup->reset(); - $mailingGroup->query("DROP TEMPORARY TABLE X_$job_id"); - $mailingGroup->query("DROP TEMPORARY TABLE I_$job_id"); - - return $eq; - } - /** * @param string $type * @@ -1983,7 +1600,7 @@ public static function create(&$params, $ids = array()) { // Create parent job if not yet created. // Condition on the existence of a scheduled date. - if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && empty($params['_skip_evil_bao_auto_schedule_'])) { + if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null') { $job = new CRM_Mailing_BAO_MailingJob(); $job->mailing_id = $mailing->id; // If we are creating a new Completed mailing (e.g. import from another system) set the job to completed. @@ -1996,19 +1613,14 @@ public static function create(&$params, $ids = array()) { $job->scheduled_date = NULL; $job->save(); } - - // Populate the recipients. - if (empty($params['_skip_evil_bao_auto_recipients_'])) { - // check if it's sms - $mode = $mailing->sms_provider_id && $mailing->sms_provider_id != 'null' ? 'sms' : NULL; - self::getRecipients($job->id, $mailing->id, TRUE, $mailing->dedupe_email, $mode); - } // Schedule the job now that it has recipients. $job->scheduled_date = $params['scheduled_date']; $job->save(); } - elseif (!empty($params['get_recipents'])) { - self::getMailRecipients($mailing); + + // Populate the recipients. + if (empty($params['_skip_evil_bao_auto_recipients_'])) { + self::getRecipients($mailing->id); } return $mailing; @@ -2407,7 +2019,7 @@ public static function &report($id, $skipDetails = FALSE, $isSMS = FALSE) { $report['event_totals']['queue'] = $newTableSize; } else { - $report['event_totals']['queue'] = self::getRecipientsCount($mailing_id, $mailing_id); + $report['event_totals']['queue'] = self::getRecipientsCount($mailing_id); } if (!empty($report['event_totals']['queue'])) { diff --git a/CRM/Mailing/BAO/MailingAB.php b/CRM/Mailing/BAO/MailingAB.php index 21a80ada5554..535593646ce0 100644 --- a/CRM/Mailing/BAO/MailingAB.php +++ b/CRM/Mailing/BAO/MailingAB.php @@ -147,7 +147,7 @@ public static function del($id) { * @param CRM_Mailing_DAO_MailingAB $dao */ public static function distributeRecipients(CRM_Mailing_DAO_MailingAB $dao) { - CRM_Mailing_BAO_Mailing::getRecipients($dao->mailing_id_a, $dao->mailing_id_a, TRUE); + CRM_Mailing_BAO_Mailing::getRecipients($dao->mailing_id_a); //calculate total number of random recipients for mail C from group_percentage selected $totalCount = CRM_Mailing_BAO_Recipients::mailingSize($dao->mailing_id_a); diff --git a/CRM/Mailing/BAO/MailingJob.php b/CRM/Mailing/BAO/MailingJob.php index 01251872e584..c1e4c25f9f05 100644 --- a/CRM/Mailing/BAO/MailingJob.php +++ b/CRM/Mailing/BAO/MailingJob.php @@ -69,10 +69,8 @@ static public function create($params) { $job->scheduled_date = $params['scheduled_date']; $job->is_test = $params['is_test']; $job->save(); - $mailing = new CRM_Mailing_BAO_Mailing(); - $mailing->id = $params['mailing_id']; - if ($mailing->id && $mailing->find(TRUE)) { - $mailing->getRecipients($job->id, $params['mailing_id'], TRUE, $mailing->dedupe_email); + if ($params['mailing_id']) { + CRM_Mailing_BAO_Mailing::getRecipients($params['mailing_id']); return $job; } else { diff --git a/CRM/SMS/Form/Group.php b/CRM/SMS/Form/Group.php index 6f1941b4aecf..8c4bd8a63c45 100644 --- a/CRM/SMS/Form/Group.php +++ b/CRM/SMS/Form/Group.php @@ -266,12 +266,7 @@ public function postProcess() { $this->set('mailing_id', $mailing->id); // also compute the recipients and store them in the mailing recipients table - CRM_Mailing_BAO_Mailing::getRecipients($mailing->id, - $mailing->id, - TRUE, - FALSE, - 'sms' - ); + CRM_Mailing_BAO_Mailing::getRecipients($mailing->id); $count = CRM_Mailing_BAO_Recipients::mailingSize($mailing->id); $this->set('count', $count); diff --git a/ang/crmMailing/services.js b/ang/crmMailing/services.js index 78eeff9701b0..1c749b53dada 100644 --- a/ang/crmMailing/services.js +++ b/ang/crmMailing/services.js @@ -292,7 +292,6 @@ // get the resulting recipients -- then rollback any changes. var params = angular.extend({}, mailing.recipients, { id: mailing.id, - get_recipents: 1, 'api.MailingRecipients.get': { mailing_id: '$value.id', options: {limit: previewLimit}, @@ -313,7 +312,6 @@ // get the resulting recipients -- then rollback any changes. var params = angular.extend({}, mailing.recipients, { id: mailing.id, - get_recipents: 1, 'api.MailingRecipients.getcount': { mailing_id: '$value.id' } diff --git a/tests/phpunit/api/v3/MailingTest.php b/tests/phpunit/api/v3/MailingTest.php index 10de7b838568..efa5e658d762 100644 --- a/tests/phpunit/api/v3/MailingTest.php +++ b/tests/phpunit/api/v3/MailingTest.php @@ -288,7 +288,6 @@ public function testMailerPreviewRecipients() { $params['mailings']['include'] = array(); $params['mailings']['exclude'] = array(); $params['options']['force_rollback'] = 1; - $params['api.mailing_job.create'] = 1; $params['api.MailingRecipients.get'] = array( 'mailing_id' => '$value.id', 'api.contact.getvalue' => array( @@ -302,12 +301,10 @@ public function testMailerPreviewRecipients() { $maxIDs = array( 'mailing' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'), - 'job' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'), 'group' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'), ); $create = $this->callAPIAndDocument('Mailing', 'create', $params, __FUNCTION__, __FILE__); $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records' - $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record' $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records' $preview = $create['values'][$create['id']]['api.MailingRecipients.get']; @@ -346,7 +343,6 @@ public function testMailerPreviewRecipientsDeduplicate() { $params['mailings']['include'] = array(); $params['options']['force_rollback'] = 1; $params['dedupe_email'] = 1; - $params['api.mailing_job.create'] = 1; $params['api.MailingRecipients.get'] = array( 'mailing_id' => '$value.id', 'api.contact.getvalue' => array(