1 (function (angular
, $, _
) {
2 var partialUrl
= function partialUrl(relPath
) {
3 return CRM
.resourceUrls
['civicrm'] + '/partials/crmMailing2/' + relPath
;
6 var crmMailing2
= angular
.module('crmMailing2', [
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;
16 function ($routeProvider
) {
17 $routeProvider
.when('/mailing2', {
18 template
: '<div></div>',
19 controller
: 'ListMailingsCtrl'
21 $routeProvider
.when('/mailing2/: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('/mailing2/: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('/mailing2/: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('/mailing2/: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 crmMailing2
.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 crmMailing2
.controller('EditMailingCtrl', function EditMailingCtrl($scope
, selectedMail
, $location
, crmMailingMgr
, crmFromAddresses
, crmStatus
, CrmAttachments
) {
70 $scope
.mailing
= selectedMail
;
71 $scope
.attachments
= new CrmAttachments(function () {
72 return {entity_table
: 'civicrm_mailing', entity_id
: $scope
.mailing
.id
};
74 $scope
.crmMailingConst
= CRM
.crmMailing
;
75 $scope
.crmFromAddresses
= crmFromAddresses
;
77 $scope
.partialUrl
= partialUrl
;
78 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
81 $scope
.submit
= function submit() {
82 var promise
= crmMailingMgr
.save($scope
.mailing
)
84 // pre-condition: the mailing exists *before* saving attachments to it
85 return $scope
.attachments
.save();
88 return crmMailingMgr
.submit($scope
.mailing
);
90 return crmStatus({start
: ts('Submitting...'), success
: ts('Submitted')}, promise
);
93 $scope
.save
= function save() {
94 return crmStatus(null,
98 // pre-condition: the mailing exists *before* saving attachments to it
99 return $scope
.attachments
.save();
104 $scope
.delete = function cancel() {
105 return crmStatus({start
: ts('Deleting...'), success
: ts('Deleted')},
106 crmMailingMgr
.delete($scope
.mailing
)
109 $scope
.leave
= function leave() {
110 window
.location
= CRM
.url('civicrm/mailing/browse/unscheduled', {
116 // Transition URL "/mailing2/new" => "/mailing2/123" as soon as ID is known
117 $scope
.$watch('mailing.id', function (newValue
, oldValue
) {
118 if (newValue
&& newValue
!= oldValue
) {
119 var parts
= $location
.path().split('/'); // e.g. "/mailing2/new" or "/mailing2/123/wizard"
121 $location
.path(parts
.join('/'));
123 // FIXME: Angular unnecessarily refreshes UI
127 $scope
.fromPlaceholder
= {
128 label
: crmFromAddresses
.getByAuthorEmail($scope
.mailing
.from_name
, $scope
.mailing
.from_email
, true).label
130 $scope
.$watch('fromPlaceholder.label', function (newValue
) {
131 var addr
= crmFromAddresses
.getByLabel(newValue
);
132 $scope
.mailing
.from_name
= addr
.author
;
133 $scope
.mailing
.from_email
= addr
.email
;
137 // Controller for the edit-recipients fields (
138 // WISHLIST: Move most of this to a (cache-enabled) service
140 // - [input] mailing: object
141 // - [output] recipients: array of recipient records
142 crmMailing2
.controller('EditRecipCtrl', function EditRecipCtrl($scope
, dialogService
, crmApi
, crmMailingMgr
) {
143 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
144 $scope
.recipients
= null;
145 $scope
.getRecipientsEstimate = function () {
147 if ($scope
.recipients
== null) {
148 return ts('(Estimating)');
150 if ($scope
.recipients
.length
== 0) {
151 return ts('No recipients');
153 if ($scope
.recipients
.length
== 1) {
154 return ts('~1 recipient');
156 if (RECIPIENTS_PREVIEW_LIMIT
> 0 && $scope
.recipients
.length
>= RECIPIENTS_PREVIEW_LIMIT
) {
157 return ts('>%1 recipients', {1: RECIPIENTS_PREVIEW_LIMIT
});
159 return ts('~%1 recipients', {1: $scope
.recipients
.length
});
161 $scope
.getIncludesAsString = function () {
164 _
.each($scope
.mailing
.groups
.include
, function (id
) {
166 names
= names
+ ', ';
168 var group
= _
.where(CRM
.crmMailing
.groupNames
, {id
: '' + id
});
169 names
= names
+ group
[0].title
;
172 _
.each($scope
.mailing
.mailings
.include
, function (id
) {
174 names
= names
+ ', ';
176 var oldMailing
= _
.where(CRM
.crmMailing
.civiMails
, {id
: '' + id
});
177 names
= names
+ oldMailing
[0].name
;
182 $scope
.getExcludesAsString = function () {
185 _
.each($scope
.mailing
.groups
.exclude
, function (id
) {
187 names
= names
+ ', ';
189 var group
= _
.where(CRM
.crmMailing
.groupNames
, {id
: '' + id
});
190 names
= names
+ group
[0].title
;
193 _
.each($scope
.mailing
.mailings
.exclude
, function (id
) {
195 names
= names
+ ', ';
197 var oldMailing
= _
.where(CRM
.crmMailing
.civiMails
, {id
: '' + id
});
198 names
= names
+ oldMailing
[0].name
;
204 // We monitor four fields -- use debounce so that changes across the
205 // four fields can settle-down before AJAX.
206 var refreshRecipients
= _
.debounce(function () {
207 $scope
.$apply(function () {
208 $scope
.recipients
= null;
209 crmMailingMgr
.previewRecipients($scope
.mailing
, RECIPIENTS_PREVIEW_LIMIT
).then(function (recipients
) {
210 $scope
.recipients
= recipients
;
213 }, RECIPIENTS_DEBOUNCE_MS
);
214 $scope
.$watchCollection("mailing.groups.include", refreshRecipients
);
215 $scope
.$watchCollection("mailing.groups.exclude", refreshRecipients
);
216 $scope
.$watchCollection("mailing.mailings.include", refreshRecipients
);
217 $scope
.$watchCollection("mailing.mailings.exclude", refreshRecipients
);
219 $scope
.previewRecipients
= function previewRecipients() {
221 recipients
: $scope
.recipients
226 title
: ts('Preview (%1)', {
227 1: $scope
.getRecipientsEstimate()
230 dialogService
.open('recipDialog', partialUrl('dialog/recipients.html'), model
, options
);
234 // Controller for the "Preview Recipients" dialog
235 // Note: Expects $scope.model to be an object with properties:
236 // - recipients: array of contacts
237 crmMailing2
.controller('PreviewRecipCtrl', function ($scope
) {
238 $scope
.ts
= CRM
.ts('CiviMail');
241 // Controller for the "Preview Mailing" segment
242 // Note: Expects $scope.model to be an object with properties:
244 // - attachments: object
245 crmMailing2
.controller('PreviewMailingCtrl', function ($scope
, dialogService
, crmMailingMgr
, crmStatus
) {
246 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
248 $scope
.testContact
= {email
: CRM
.crmMailing
.defaultTestEmail
};
249 $scope
.testGroup
= {gid
: null};
251 $scope
.previewHtml
= function previewHtml() {
252 $scope
.previewDialog(partialUrl('dialog/previewHtml.html'));
254 $scope
.previewText
= function previewText() {
255 $scope
.previewDialog(partialUrl('dialog/previewText.html'));
257 $scope
.previewFull
= function previewFull() {
258 $scope
.previewDialog(partialUrl('dialog/previewFull.html'));
260 // Open a dialog with a preview of the current mailing
261 // @param template string URL of the template to use in the preview dialog
262 $scope
.previewDialog
= function previewDialog(template
) {
263 var p
= crmMailingMgr
264 .preview($scope
.mailing
)
265 .then(function (content
) {
269 title
: ts('Subject: %1', {
273 dialogService
.open('previewDialog', template
, content
, options
);
275 CRM
.status({start
: ts('Previewing'), success
: ''}, CRM
.toJqPromise(p
));
277 $scope
.sendTestToContact
= function sendTestToContact() {
278 $scope
.sendTest($scope
.mailing
, $scope
.attachments
, $scope
.testContact
.email
, null);
280 $scope
.sendTestToGroup
= function sendTestToGroup() {
281 $scope
.sendTest($scope
.mailing
, $scope
.attachments
, null, $scope
.testGroup
.gid
);
283 $scope
.sendTest
= function sendTest(mailing
, attachments
, testEmail
, testGroup
) {
284 var promise
= crmMailingMgr
.save(mailing
)
286 return attachments
.save();
289 return crmMailingMgr
.sendTest(mailing
, testEmail
, testGroup
);
291 .then(function (deliveryInfos
) {
292 var count
= Object
.keys(deliveryInfos
).length
;
294 CRM
.alert(ts('Could not identify any recipients. Perhaps the group is empty?'));
298 return crmStatus({start
: ts('Sending...'), success
: ts('Sent')}, promise
);
302 // Controller for the "Preview Mailing" dialog
303 // Note: Expects $scope.model to be an object with properties:
307 crmMailing2
.controller('PreviewMailingDialogCtrl', function PreviewMailingDialogCtrl($scope
, crmMailingMgr
) {
308 $scope
.ts
= CRM
.ts('CiviMail');
311 // Controller for the "Preview Mailing Component" segment
312 // which displays header/footer/auto-responder
313 crmMailing2
.controller('PreviewComponentCtrl', function PreviewMailingDialogCtrl($scope
, dialogService
) {
314 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
316 $scope
.previewComponent
= function previewComponent(title
, componentId
) {
317 var component
= _
.where(CRM
.crmMailing
.headerfooterList
, {id
: "" + componentId
});
318 if (!component
|| !component
[0]) {
319 CRM
.alert(ts('Invalid component ID (%1)', {
327 title
: title
// component[0].name
329 dialogService
.open('previewComponentDialog', partialUrl('dialog/previewComponent.html'), component
[0], options
);
333 // Controller for the "Preview Mailing" dialog
334 // Note: Expects $scope.model to be an object with properties:
339 crmMailing2
.controller('PreviewComponentDialogCtrl', function PreviewMailingDialogCtrl($scope
) {
340 $scope
.ts
= CRM
.ts('CiviMail');
343 // Controller for the in-place msg-template management
345 // - [input] mailing: object
346 crmMailing2
.controller('MsgTemplateCtrl', function MsgTemplateCtrl($scope
, crmMsgTemplates
, dialogService
, $parse
) {
347 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
348 $scope
.crmMsgTemplates
= crmMsgTemplates
;
350 // @return Promise MessageTemplate (per APIv3)
351 $scope
.saveTemplate
= function saveTemplate() {
353 selected_id
: $scope
.mailing
.msg_template_id
,
356 msg_subject
: $scope
.mailing
.subject
,
357 msg_text
: $scope
.mailing
.body_text
,
358 msg_html
: $scope
.mailing
.body_html
364 title
: ts('Save Template')
366 return dialogService
.open('saveTemplateDialog', partialUrl('dialog/saveTemplate.html'), model
, options
)
367 .then(function (item
) {
368 $parse('mailing.msg_template_id').assign($scope
, item
.id
);
375 $scope
.loadTemplate
= function loadTemplate(id
) {
376 return crmMsgTemplates
.get(id
).then(function (tpl
) {
377 $scope
.mailing
.subject
= tpl
.msg_subject
;
378 $scope
.mailing
.body_text
= tpl
.msg_text
;
379 $scope
.mailing
.body_html
= tpl
.msg_html
;
384 // Controller for the "Save Message Template" dialog
386 // - [input] "model": Object
387 // - "selected_id": int
389 // - "msg_subject": string
390 // - "msg_text": string
391 // - "msg_html": string
392 crmMailing2
.controller('SaveMsgTemplateDialogCtrl', function SaveMsgTemplateDialogCtrl($scope
, crmMsgTemplates
, dialogService
) {
393 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
394 $scope
.saveOpt
= {mode
: '', newTitle
: ''};
395 $scope
.selected
= null;
397 $scope
.save
= function save() {
398 var tpl
= _
.extend({}, $scope
.model
.tpl
);
399 switch ($scope
.saveOpt
.mode
) {
401 tpl
.msg_title
= $scope
.saveOpt
.newTitle
;
404 tpl
.id
= $scope
.selected
.id
;
405 tpl
.msg_title
= $scope
.selected
.msg_title
;
408 throw 'SaveMsgTemplateDialogCtrl: Unrecognized mode: ' + $scope
.saveOpt
.mode
;
410 return crmMsgTemplates
.save(tpl
)
411 .then(function (item
) {
412 CRM
.status(ts('Saved'));
417 function scopeApply(f
) {
419 var args
= arguments
;
420 $scope
.$apply(function () {
427 crmMsgTemplates
.get($scope
.model
.selected_id
).then(
429 $scope
.saveOpt
.mode
= 'update';
430 $scope
.selected
= tpl
;
433 $scope
.saveOpt
.mode
= 'add';
434 $scope
.selected
= null;
437 // When using dialogService with a button bar, the major button actions
438 // need to be registered with the dialog widget (and not embedded in
439 // the body of the dialog).
441 buttons
[ts('Save')] = function () {
442 $scope
.save().then(function (item
) {
443 dialogService
.close('saveTemplateDialog', item
);
446 buttons
[ts('Cancel')] = function () {
447 dialogService
.cancel('saveTemplateDialog');
449 dialogService
.setButtons('saveTemplateDialog', buttons
);
452 setTimeout(scopeApply(init
), 0);
455 // Controller for schedule-editing widget.
457 // - [input] mailing: object
458 // - scheduled_date: null|string(YYYY-MM-DD hh:mm)
459 crmMailing2
.controller('EditScheduleCtrl', function EditScheduleCtrl($scope
, $parse
) {
460 var schedModelExpr
= 'mailing.scheduled_date';
461 var schedModel
= $parse(schedModelExpr
);
467 var updateChildren
= (function () {
468 var sched
= schedModel($scope
);
470 $scope
.schedule
.mode
= 'at';
471 $scope
.schedule
.datetime
= sched
;
474 $scope
.schedule
.mode
= 'now';
477 var updateParent
= (function () {
478 switch ($scope
.schedule
.mode
) {
480 schedModel
.assign($scope
, null);
483 schedModel
.assign($scope
, $scope
.schedule
.datetime
);
486 throw 'Unrecognized schedule mode: ' + $scope
.schedule
.mode
;
490 $scope
.$watch(schedModelExpr
, updateChildren
);
491 $scope
.$watch('schedule.mode', updateParent
);
492 $scope
.$watch('schedule.datetime', function (newValue
, oldValue
) {
493 // automatically switch mode based on datetime entry
494 if (oldValue
!= newValue
) {
495 if (!newValue
|| newValue
== " ") {
496 $scope
.schedule
.mode
= 'now';
499 $scope
.schedule
.mode
= 'at';
506 })(angular
, CRM
.$, CRM
._
);