$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'),
->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'],
restrict: 'AE',
require: 'ngModel',
scope: {
- crmAvailGroups: '@', // available groups
- crmAvailMailings: '@', // available mailings
- crmMandatoryGroups: '@', // hard-coded/mandatory groups
ngRequired: '@'
},
templateUrl: '~/crmMailing/Recipients.html',
}
}
+ 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) {
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;
};
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;
};