1 (function (angular
, $, _
) {
2 var partialUrl
= function partialUrl(relPath
) {
3 return CRM
.resourceUrls
['civicrm'] + '/partials/crmMailing/' + relPath
;
6 angular
.module('crmMailing', [
7 'crmUtil', 'crmAttachment', 'ngRoute', 'ui.utils', 'crmUi', 'dialogService'
8 ]); // TODO ngSanitize, unsavedChanges
10 // Time to wait before triggering AJAX update to recipients list
11 var RECIPIENTS_DEBOUNCE_MS
= 100;
12 var RECIPIENTS_PREVIEW_LIMIT
= 10000;
14 angular
.module('crmMailing').config([
16 function ($routeProvider
) {
17 $routeProvider
.when('/mailing', {
18 template
: '<div></div>',
19 controller
: 'ListMailingsCtrl'
21 $routeProvider
.when('/mailing/:id', {
22 templateUrl
: partialUrl('edit.html'),
23 controller
: 'EditMailingCtrl',
25 selectedMail
: function selectedMail($route
, crmMailingMgr
) {
26 return crmMailingMgr
.getOrCreate($route
.current
.params
.id
);
30 $routeProvider
.when('/mailing/:id/unified', {
31 templateUrl
: partialUrl('edit-unified.html'),
32 controller
: 'EditMailingCtrl',
34 selectedMail
: function selectedMail($route
, crmMailingMgr
) {
35 return crmMailingMgr
.getOrCreate($route
.current
.params
.id
);
39 $routeProvider
.when('/mailing/:id/unified2', {
40 templateUrl
: partialUrl('edit-unified2.html'),
41 controller
: 'EditMailingCtrl',
43 selectedMail
: function selectedMail($route
, crmMailingMgr
) {
44 return crmMailingMgr
.getOrCreate($route
.current
.params
.id
);
48 $routeProvider
.when('/mailing/:id/wizard', {
49 templateUrl
: partialUrl('edit-wizard.html'),
50 controller
: 'EditMailingCtrl',
52 selectedMail
: function selectedMail($route
, crmMailingMgr
) {
53 return crmMailingMgr
.getOrCreate($route
.current
.params
.id
);
60 angular
.module('crmMailing').controller('ListMailingsCtrl', function ListMailingsCtrl() {
61 // We haven't implemented this in Angular, but some users may get clever
62 // about typing URLs, so we'll provide a redirect.
63 window
.location
= CRM
.url('civicrm/mailing/browse/unscheduled', {
69 angular
.module('crmMailing').controller('EditMailingCtrl', function EditMailingCtrl($scope
, selectedMail
, $location
, crmMailingMgr
, crmStatus
, CrmAttachments
, crmMailingPreviewMgr
) {
70 $scope
.mailing
= selectedMail
;
71 $scope
.attachments
= new CrmAttachments(function () {
72 return {entity_table
: 'civicrm_mailing', entity_id
: $scope
.mailing
.id
};
74 $scope
.attachments
.load();
75 $scope
.crmMailingConst
= CRM
.crmMailing
;
77 $scope
.partialUrl
= partialUrl
;
78 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
80 $scope
.isSubmitted
= function isSubmitted() {
81 return _
.size($scope
.mailing
.jobs
) > 0;
85 $scope
.previewMailing
= function previewMailing(mailing
, mode
) {
86 return crmMailingPreviewMgr
.preview(mailing
, mode
);
90 $scope
.sendTest
= function sendTest(mailing
, attachments
, recipient
) {
91 var savePromise
= crmMailingMgr
.save(mailing
)
93 return attachments
.save();
96 return crmStatus({start
: ts('Saving...'), success
: ''}, savePromise
)
98 crmMailingPreviewMgr
.sendTest(mailing
, recipient
);
103 $scope
.submit
= function submit() {
104 var promise
= crmMailingMgr
.save($scope
.mailing
)
106 // pre-condition: the mailing exists *before* saving attachments to it
107 return $scope
.attachments
.save();
110 return crmMailingMgr
.submit($scope
.mailing
);
116 return crmStatus({start
: ts('Submitting...'), success
: ts('Submitted')}, promise
);
120 $scope
.save
= function save() {
121 return crmStatus(null,
123 .save($scope
.mailing
)
125 // pre-condition: the mailing exists *before* saving attachments to it
126 return $scope
.attachments
.save();
133 $scope
.delete = function cancel() {
134 return crmStatus({start
: ts('Deleting...'), success
: ts('Deleted')},
135 crmMailingMgr
.delete($scope
.mailing
)
142 // @param string listingScreen 'archive', 'scheduled', 'unscheduled'
143 function leave(listingScreen
) {
144 switch (listingScreen
) {
146 window
.location
= CRM
.url('civicrm/mailing/browse/archived', {
151 window
.location
= CRM
.url('civicrm/mailing/browse/scheduled', {
158 window
.location
= CRM
.url('civicrm/mailing/browse/unscheduled', {
165 // Transition URL "/mailing/new" => "/mailing/123"
166 function updateUrl() {
167 var parts
= $location
.path().split('/'); // e.g. "/mailing/new" or "/mailing/123/wizard"
168 if (parts
[2] != $scope
.mailing
.id
) {
169 parts
[2] = $scope
.mailing
.id
;
170 $location
.path(parts
.join('/'));
172 // FIXME: Angular unnecessarily refreshes UI
173 // WARNING: Changing the URL triggers a full reload. Any pending AJAX operations
174 // could be inconsistently applied. Run updateUrl() after other changes complete.
179 // Controller for the edit-recipients fields (
180 // WISHLIST: Move most of this to a (cache-enabled) service
182 // - [input] mailing: object
183 // - [output] recipients: array of recipient records
184 angular
.module('crmMailing').controller('EditRecipCtrl', function EditRecipCtrl($scope
, dialogService
, crmApi
, crmMailingMgr
) {
185 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
186 $scope
.recipients
= null;
187 $scope
.getRecipientsEstimate = function () {
189 if ($scope
.recipients
== null) {
190 return ts('(Estimating)');
192 if ($scope
.recipients
.length
== 0) {
193 return ts('No recipients');
195 if ($scope
.recipients
.length
== 1) {
196 return ts('~1 recipient');
198 if (RECIPIENTS_PREVIEW_LIMIT
> 0 && $scope
.recipients
.length
>= RECIPIENTS_PREVIEW_LIMIT
) {
199 return ts('>%1 recipients', {1: RECIPIENTS_PREVIEW_LIMIT
});
201 return ts('~%1 recipients', {1: $scope
.recipients
.length
});
203 $scope
.getIncludesAsString = function () {
206 _
.each($scope
.mailing
.groups
.include
, function (id
) {
208 names
= names
+ ', ';
210 var group
= _
.where(CRM
.crmMailing
.groupNames
, {id
: '' + id
});
211 names
= names
+ group
[0].title
;
214 _
.each($scope
.mailing
.mailings
.include
, function (id
) {
216 names
= names
+ ', ';
218 var oldMailing
= _
.where(CRM
.crmMailing
.civiMails
, {id
: '' + id
});
219 names
= names
+ oldMailing
[0].name
;
224 $scope
.getExcludesAsString = function () {
227 _
.each($scope
.mailing
.groups
.exclude
, function (id
) {
229 names
= names
+ ', ';
231 var group
= _
.where(CRM
.crmMailing
.groupNames
, {id
: '' + id
});
232 names
= names
+ group
[0].title
;
235 _
.each($scope
.mailing
.mailings
.exclude
, function (id
) {
237 names
= names
+ ', ';
239 var oldMailing
= _
.where(CRM
.crmMailing
.civiMails
, {id
: '' + id
});
240 names
= names
+ oldMailing
[0].name
;
246 // We monitor four fields -- use debounce so that changes across the
247 // four fields can settle-down before AJAX.
248 var refreshRecipients
= _
.debounce(function () {
249 $scope
.$apply(function () {
250 $scope
.recipients
= null;
251 crmMailingMgr
.previewRecipients($scope
.mailing
, RECIPIENTS_PREVIEW_LIMIT
).then(function (recipients
) {
252 $scope
.recipients
= recipients
;
255 }, RECIPIENTS_DEBOUNCE_MS
);
256 $scope
.$watchCollection("mailing.groups.include", refreshRecipients
);
257 $scope
.$watchCollection("mailing.groups.exclude", refreshRecipients
);
258 $scope
.$watchCollection("mailing.mailings.include", refreshRecipients
);
259 $scope
.$watchCollection("mailing.mailings.exclude", refreshRecipients
);
261 $scope
.previewRecipients
= function previewRecipients() {
263 recipients
: $scope
.recipients
268 title
: ts('Preview (%1)', {
269 1: $scope
.getRecipientsEstimate()
272 dialogService
.open('recipDialog', partialUrl('dialog/recipients.html'), model
, options
);
276 // Controller for the "Preview Recipients" dialog
277 // Note: Expects $scope.model to be an object with properties:
278 // - recipients: array of contacts
279 angular
.module('crmMailing').controller('PreviewRecipCtrl', function ($scope
) {
280 $scope
.ts
= CRM
.ts('CiviMail');
283 // Controller for the "Preview Mailing" dialog
284 // Note: Expects $scope.model to be an object with properties:
288 angular
.module('crmMailing').controller('PreviewMailingDialogCtrl', function PreviewMailingDialogCtrl($scope
) {
289 $scope
.ts
= CRM
.ts('CiviMail');
292 // Controller for the "Preview Mailing Component" segment
293 // which displays header/footer/auto-responder
294 angular
.module('crmMailing').controller('PreviewComponentCtrl', function PreviewMailingDialogCtrl($scope
, dialogService
) {
295 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
297 $scope
.previewComponent
= function previewComponent(title
, componentId
) {
298 var component
= _
.where(CRM
.crmMailing
.headerfooterList
, {id
: "" + componentId
});
299 if (!component
|| !component
[0]) {
300 CRM
.alert(ts('Invalid component ID (%1)', {
308 title
: title
// component[0].name
310 dialogService
.open('previewComponentDialog', partialUrl('dialog/previewComponent.html'), component
[0], options
);
314 // Controller for the "Preview Mailing" dialog
315 // Note: Expects $scope.model to be an object with properties:
320 angular
.module('crmMailing').controller('PreviewComponentDialogCtrl', function PreviewMailingDialogCtrl($scope
) {
321 $scope
.ts
= CRM
.ts('CiviMail');
324 // Controller for the in-place msg-template management
325 angular
.module('crmMailing').controller('MsgTemplateCtrl', function MsgTemplateCtrl($scope
, crmMsgTemplates
, dialogService
) {
326 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
327 $scope
.crmMsgTemplates
= crmMsgTemplates
;
329 // @return Promise MessageTemplate (per APIv3)
330 $scope
.saveTemplate
= function saveTemplate(mailing
) {
332 selected_id
: mailing
.msg_template_id
,
335 msg_subject
: mailing
.subject
,
336 msg_text
: mailing
.body_text
,
337 msg_html
: mailing
.body_html
343 title
: ts('Save Template')
345 return dialogService
.open('saveTemplateDialog', partialUrl('dialog/saveTemplate.html'), model
, options
)
346 .then(function (item
) {
347 mailing
.msg_template_id
= item
.id
;
354 $scope
.loadTemplate
= function loadTemplate(mailing
, id
) {
355 return crmMsgTemplates
.get(id
).then(function (tpl
) {
356 mailing
.subject
= tpl
.msg_subject
;
357 mailing
.body_text
= tpl
.msg_text
;
358 mailing
.body_html
= tpl
.msg_html
;
363 // Controller for the "Save Message Template" dialog
365 // - [input] "model": Object
366 // - "selected_id": int
368 // - "msg_subject": string
369 // - "msg_text": string
370 // - "msg_html": string
371 angular
.module('crmMailing').controller('SaveMsgTemplateDialogCtrl', function SaveMsgTemplateDialogCtrl($scope
, crmMsgTemplates
, dialogService
) {
372 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
373 $scope
.saveOpt
= {mode
: '', newTitle
: ''};
374 $scope
.selected
= null;
376 $scope
.save
= function save() {
377 var tpl
= _
.extend({}, $scope
.model
.tpl
);
378 switch ($scope
.saveOpt
.mode
) {
380 tpl
.msg_title
= $scope
.saveOpt
.newTitle
;
383 tpl
.id
= $scope
.selected
.id
;
384 tpl
.msg_title
= $scope
.selected
.msg_title
;
387 throw 'SaveMsgTemplateDialogCtrl: Unrecognized mode: ' + $scope
.saveOpt
.mode
;
389 return crmMsgTemplates
.save(tpl
)
390 .then(function (item
) {
391 CRM
.status(ts('Saved'));
396 function scopeApply(f
) {
398 var args
= arguments
;
399 $scope
.$apply(function () {
406 crmMsgTemplates
.get($scope
.model
.selected_id
).then(
408 $scope
.saveOpt
.mode
= 'update';
409 $scope
.selected
= tpl
;
412 $scope
.saveOpt
.mode
= 'add';
413 $scope
.selected
= null;
416 // When using dialogService with a button bar, the major button actions
417 // need to be registered with the dialog widget (and not embedded in
418 // the body of the dialog).
420 buttons
[ts('Save')] = function () {
421 $scope
.save().then(function (item
) {
422 dialogService
.close('saveTemplateDialog', item
);
425 buttons
[ts('Cancel')] = function () {
426 dialogService
.cancel('saveTemplateDialog');
428 dialogService
.setButtons('saveTemplateDialog', buttons
);
431 setTimeout(scopeApply(init
), 0);
434 angular
.module('crmMailing').controller('EmailAddrCtrl', function EmailAddrCtrl($scope
, crmFromAddresses
) {
435 $scope
.crmFromAddresses
= crmFromAddresses
;
437 })(angular
, CRM
.$, CRM
._
);