Skip to content

Commit

Permalink
CRM-21260: CiviMail compose UI very slow to initialize, CRM-21316: Si…
Browse files Browse the repository at this point in the history
…mplify getRecipients fn
  • Loading branch information
monishdeb committed Dec 19, 2017
1 parent c865af6 commit 6dd717a
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 463 deletions.
693 changes: 256 additions & 437 deletions CRM/Mailing/BAO/Mailing.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion CRM/Mailing/BAO/MailingAB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions CRM/Mailing/BAO/MailingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions CRM/SMS/Form/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 31 additions & 1 deletion CRM/Utils/SQL/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class CRM_Utils_SQL_Select implements ArrayAccess {

private $mode = NULL;
private $insertInto = NULL;
private $insertVerb = 'INSERT INTO ';
private $insertIntoFields = array();
private $selects = array();
private $from;
Expand Down Expand Up @@ -403,6 +404,34 @@ public function insertInto($table, $fields = array()) {
return $this;
}

/**
* Wrapper function of insertInto fn but sets insertVerb = "INSERT IGNORE INTO "
*
* @param string $table
* The name of the other table (which receives new data).
* @param array $fields
* The fields to fill in the other table (in order).
* @return CRM_Utils_SQL_Select
*/
public function insertIgnoreInto($table, $fields = array()) {
$this->insertVerb = "INSERT IGNORE INTO ";
return $this->insertInto($table, $fields);
}

/**
* Wrapper function of insertInto fn but sets insertVerb = "REPLACE INTO "
*
* @param string $table
* The name of the other table (which receives new data).
* @param array $fields
* The fields to fill in the other table (in order).
*/
public function replaceInto($table, $fields = array()) {
$this->insertVerb = "REPLACE INTO ";
return $this->insertInto($table, $fields);
}


/**
* @param array $fields
* The fields to fill in the other table (in order).
Expand Down Expand Up @@ -550,10 +579,11 @@ public function escapeString($value) {
public function toSQL() {
$sql = '';
if ($this->insertInto) {
$sql .= 'INSERT INTO ' . $this->insertInto . ' (';
$sql .= $this->insertVerb . $this->insertInto . ' (';
$sql .= implode(', ', $this->insertIntoFields);
$sql .= ")\n";
}

if ($this->selects) {
$sql .= 'SELECT ' . $this->distinct . implode(', ', $this->selects) . "\n";
}
Expand Down
3 changes: 3 additions & 0 deletions ang/crmMailing/Recipients.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
mids.push(dv.entity_id);
}
}
if (gids.length === 0) {
return;
}

CRM.api3('Group', 'getlist', { params: { id: { IN: gids }, options: { limit: 0 } }, extra: ["is_hidden"] } ).then(
function(glist) {
Expand Down
14 changes: 4 additions & 10 deletions ang/crmMailing/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
else {
// Protect against races in saving and previewing by chaining create+preview.
var params = angular.extend({}, mailing, mailing.recipients, {
options: {force_rollback: 1},
id: mailing.id,
'api.Mailing.preview': {
id: '$value.id'
}
Expand All @@ -290,11 +290,8 @@
previewRecipients: function previewRecipients(mailing, previewLimit) {
// To get list of recipients, we tentatively save the mailing and
// get the resulting recipients -- then rollback any changes.
var params = angular.extend({}, mailing, mailing.recipients, {
name: 'placeholder', // for previewing recipients on new, incomplete mailing
subject: 'placeholder', // for previewing recipients on new, incomplete mailing
options: {force_rollback: 1},
'api.mailing_job.create': 1, // note: exact match to API default
var params = angular.extend({}, mailing.recipients, {
id: mailing.id,
'api.MailingRecipients.get': {
mailing_id: '$value.id',
options: {limit: previewLimit},
Expand All @@ -317,10 +314,7 @@
// To get list of recipients, we tentatively save the mailing and
// get the resulting recipients -- then rollback any changes.
var params = angular.extend({}, mailing, mailing.recipients, {
name: 'placeholder', // for previewing recipients on new, incomplete mailing
subject: 'placeholder', // for previewing recipients on new, incomplete mailing
options: {force_rollback: 1},
'api.mailing_job.create': 1, // note: exact match to API default
id: mailing.id,
'api.MailingRecipients.getcount': {
mailing_id: '$value.id'
}
Expand Down
4 changes: 0 additions & 4 deletions tests/phpunit/api/v3/MailingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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'];
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 6dd717a

Please sign in to comment.