Skip to content

Commit

Permalink
CRM-20521 Convert returning groups and mailings for CiviMail new mail…
Browse files Browse the repository at this point in the history
…ing screen into an API request rather than loading in the DOM
  • Loading branch information
seamuslee001 committed May 4, 2017
1 parent de840a0 commit 0496e9b
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 70 deletions.
22 changes: 2 additions & 20 deletions CRM/Mailing/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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'],
Expand Down
9 changes: 3 additions & 6 deletions ang/crmMailing/BlockRecipients.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
<a href="" ng-click="previewRecipients()" title="{{ts('Preview a List of Recipients')}}">{{getRecipientsEstimate()}}</a>
</div>
</div>
<select
<input
type="hidden"
crm-mailing-recipients
ng-model="mailing.recipients"
crm-mandatory-groups="crmMailingConst.groupNames | filter:{is_hidden:1}"
crm-avail-groups="crmMailingConst.groupNames | filter:isMailingList | filter:{is_hidden:0}"
crm-avail-mailings="crmMailingConst.civiMails"
crm-ui-id="{{crmMailingBlockRecipients.id}}"
name="{{crmMailingBlockRecipients.name}}"
ng-required="true"
multiple>
</select>
ng-required="true"/>
<a crm-icon="fa-wrench" ng-click="editOptions(mailing)" class="crm-hover-button" title="{{ts('Edit Recipient Options')}}"></a>
</div>
20 changes: 0 additions & 20 deletions ang/crmMailing/Recipients.html

This file was deleted.

139 changes: 135 additions & 4 deletions ang/crmMailing/Recipients.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down
48 changes: 28 additions & 20 deletions ang/crmMailing/ViewRecipCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,49 @@
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;
};
$scope.getExcludesAsString = function(mailing) {
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;
};
Expand Down

0 comments on commit 0496e9b

Please sign in to comment.