From 0496e9b25902c70506e760e5e75495e18887afe0 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 4 May 2017 21:23:31 +1000 Subject: [PATCH] CRM-20521 Convert returning groups and mailings for CiviMail new mailing screen into an API request rather than loading in the DOM --- CRM/Mailing/Info.php | 22 +---- ang/crmMailing/BlockRecipients.html | 9 +- ang/crmMailing/Recipients.html | 20 ---- ang/crmMailing/Recipients.js | 139 +++++++++++++++++++++++++++- ang/crmMailing/ViewRecipCtrl.js | 48 ++++++---- 5 files changed, 168 insertions(+), 70 deletions(-) delete mode 100644 ang/crmMailing/Recipients.html diff --git a/CRM/Mailing/Info.php b/CRM/Mailing/Info.php index f902b526c7f..26ae3b565d7 100644 --- a/CRM/Mailing/Info.php +++ b/CRM/Mailing/Info.php @@ -112,30 +112,12 @@ public function getAngularModules() { $session = CRM_Core_Session::singleton(); $contactID = $session->get('userID'); - // Get past mailings. - // CRM-16155 - Limit to a reasonable number. - $civiMails = civicrm_api3('Mailing', 'get', array( - 'is_completed' => 1, - 'mailing_type' => array('IN' => array('standalone', 'winner')), - 'domain_id' => CRM_Core_Config::domainID(), - 'return' => array('id', 'name', 'scheduled_date'), - 'sequential' => 1, - 'options' => array( - 'limit' => 500, - 'sort' => 'is_archived asc, scheduled_date desc', - ), - )); // Generic params. $params = array( 'options' => array('limit' => 0), 'sequential' => 1, ); - $groupNames = civicrm_api3('Group', 'get', $params + array( - 'is_active' => 1, - 'check_permissions' => TRUE, - 'return' => array('title', 'visibility', 'group_type', 'is_hidden'), - )); $headerfooterList = civicrm_api3('MailingComponent', 'get', $params + array( 'is_active' => 1, 'return' => array('name', 'component_type', 'is_default', 'body_html', 'body_text'), @@ -167,9 +149,9 @@ public function getAngularModules() { ->addSetting(array( 'crmMailing' => array( 'templateTypes' => CRM_Mailing_BAO_Mailing::getTemplateTypes(), - 'civiMails' => $civiMails['values'], + 'civiMails' => array(), 'campaignEnabled' => in_array('CiviCampaign', $config->enableComponents), - 'groupNames' => $groupNames['values'], + 'groupNames' => array(), 'headerfooterList' => $headerfooterList['values'], 'mesTemplate' => $mesTemplate['values'], 'emailAdd' => $emailAdd['values'], diff --git a/ang/crmMailing/BlockRecipients.html b/ang/crmMailing/BlockRecipients.html index 3a82623aa34..579a1ea9bd4 100644 --- a/ang/crmMailing/BlockRecipients.html +++ b/ang/crmMailing/BlockRecipients.html @@ -4,16 +4,13 @@ {{getRecipientsEstimate()}} - + ng-required="true"/> diff --git a/ang/crmMailing/Recipients.html b/ang/crmMailing/Recipients.html deleted file mode 100644 index 5fb611731f1..00000000000 --- a/ang/crmMailing/Recipients.html +++ /dev/null @@ -1,20 +0,0 @@ - - diff --git a/ang/crmMailing/Recipients.js b/ang/crmMailing/Recipients.js index 60cd1b88e21..211c072cea1 100644 --- a/ang/crmMailing/Recipients.js +++ b/ang/crmMailing/Recipients.js @@ -6,9 +6,6 @@ restrict: 'AE', require: 'ngModel', scope: { - crmAvailGroups: '@', // available groups - crmAvailMailings: '@', // available mailings - crmMandatoryGroups: '@', // hard-coded/mandatory groups ngRequired: '@' }, templateUrl: '~/crmMailing/Recipients.html', @@ -116,14 +113,148 @@ } } + var rcpAjaxState = { + input: '', + entity: 'civicrm_group', + type: 'include', + page_n: 0, + page_i: 0, + }; + $(element).select2({ + width: '36em', dropdownAutoWidth: true, placeholder: "Groups or Past Recipients", formatResult: formatItem, formatSelection: formatItem, escapeMarkup: function(m) { return m; - } + }, + multiple: true, + initSelection: function(el, cb) { + var values = el.val().split(','); + + var gids = []; + var mids = []; + + for (var i in values) { + var dv = convertValueToObj(values[i]); + if (dv.entity_type == 'civicrm_group') { + gids.push(dv.entity_id); + } + else if (dv.entity_type == 'civicrm_mailing') { + mids.push(dv.entity_id); + } + } + + CRM.api3('Group', 'getlist', { params: { id: { IN: gids } }, extra: ["is_hidden"] }).then( + function(glist) { + CRM.api3('Mailing', 'getlist', { params: { id: { IN: mids } } }).then( + function(mlist) { + var datamap = []; + + var groupNames = []; + var civiMails = []; + + $(glist.values).each(function (idx, group) { + var key = group.id + ' civicrm_group include'; + groupNames.push({id: parseInt(group.id), title: group.label, is_hidden: group.extra.is_hidden}); + + if (values.indexOf(key) >= 0) { + datamap.push({id: key, text: group.label}); + } + + key = group.id + ' civicrm_group exclude'; + if (values.indexOf(key) >= 0) { + datamap.push({id: key, text: group.label}); + } + }); + + $(mlist.values).each(function (idx, group) { + var key = group.id + ' civicrm_mailing include'; + civiMails.push({id: parseInt(group.id), name: group.label}); + + if (values.indexOf(key) >= 0) { + datamap.push({id: key, text: group.label}); + } + + key = group.id + ' civicrm_mailing exclude'; + if (values.indexOf(key) >= 0) { + datamap.push({id: key, text: group.label}); + } + }); + + scope.$parent.crmMailingConst.groupNames = groupNames; + scope.$parent.crmMailingConst.civiMails = civiMails; + + refreshMandatory(); + + cb(datamap); + }); + }); + }, + ajax: { + url: CRM.url('civicrm/ajax/rest'), + quietMillis: 300, + data: function(input, page_num) { + if (page_num <= 1) { + rcpAjaxState = { + input: input, + entity: 'civicrm_group', + type: 'include', + page_n: 0, + }; + } + + rcpAjaxState.page_i = page_num - rcpAjaxState.page_n; + + var params = { + input: input, + page_num: rcpAjaxState.page_i, + params: { is_hidden: 0 }, + }; + return params; + }, + transport: function(params) { + switch(rcpAjaxState.entity) { + case 'civicrm_group': + CRM.api3('Group', 'getlist', params.data).then(params.success, params.error); + break; + + case 'civicrm_mailing': + params.data.params.options = { sort: "is_archived asc, scheduled_date desc" }; + CRM.api3('Mailing', 'getlist', params.data).then(params.success, params.error); + break; + } + }, + results: function(data) { + results = { + children: $.map(data.values, function(obj) { + return { id: obj.id + ' ' + rcpAjaxState.entity + ' ' + rcpAjaxState.type, + text: obj.label }; + }) + }; + + if (rcpAjaxState.page_i == 1 && data.count) { + results.text = ts((rcpAjaxState.type == 'include'? 'Include ' : 'Exclude ') + + (rcpAjaxState.entity == 'civicrm_group'? 'Group' : 'Mailing')); + } + + more = data.more_results || !(rcpAjaxState.entity == 'civicrm_mailing' && rcpAjaxState.type == 'exclude'); + + if (more && !data.more_results) { + if (rcpAjaxState.type == 'include') { + rcpAjaxState.type = 'exclude'; + } else { + rcpAjaxState.type = 'include'; + rcpAjaxState.entity = 'civicrm_mailing'; + } + rcpAjaxState.page_n += rcpAjaxState.page_i; + } + + return { more: more, results: [ results ] }; + }, + }, }); $(element).on('select2-selecting', function(e) { diff --git a/ang/crmMailing/ViewRecipCtrl.js b/ang/crmMailing/ViewRecipCtrl.js index d07bb83e71a..652b89e3a88 100644 --- a/ang/crmMailing/ViewRecipCtrl.js +++ b/ang/crmMailing/ViewRecipCtrl.js @@ -5,20 +5,24 @@ var first = true; var names = ''; _.each(mailing.recipients.groups.include, function(id) { - if (!first) { - names = names + ', '; - } var group = _.where(CRM.crmMailing.groupNames, {id: '' + id}); - names = names + group[0].title; - first = false; + if (group.length) { + if (!first) { + names = names + ', '; + } + names = names + group[0].title; + first = false; + } }); _.each(mailing.recipients.mailings.include, function(id) { - if (!first) { - names = names + ', '; - } var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id}); - names = names + oldMailing[0].name; - first = false; + if (oldMailing.length) { + if (!first) { + names = names + ', '; + } + names = names + oldMailing[0].name; + first = false; + } }); return names; }; @@ -26,20 +30,24 @@ var first = true; var names = ''; _.each(mailing.recipients.groups.exclude, function(id) { - if (!first) { - names = names + ', '; - } var group = _.where(CRM.crmMailing.groupNames, {id: '' + id}); - names = names + group[0].title; - first = false; + if (group.length) { + if (!first) { + names = names + ', '; + } + names = names + group[0].title; + first = false; + } }); _.each(mailing.recipients.mailings.exclude, function(id) { - if (!first) { - names = names + ', '; - } var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id}); - names = names + oldMailing[0].name; - first = false; + if (oldMailing.length) { + if (!first) { + names = names + ', '; + } + names = names + oldMailing[0].name; + first = false; + } }); return names; };