CRM-15578 - MsgTemplateCtrl - Don't hardcode dependence on upstream "scope.mailing"
[civicrm-core.git] / js / angular-crmMailingAB.js
1 /**
2 * Created by aditya on 6/12/14.
3 */
4 (function (angular, $, _) {
5
6 var partialUrl = function (relPath) {
7 return CRM.resourceUrls['civicrm'] + '/partials/crmMailingAB/' + relPath;
8 };
9 var crmMailingAB = angular.module('crmMailingAB', ['ngRoute', 'ui.utils', 'ngSanitize']);
10
11 crmMailingAB.run(function ($rootScope, $templateCache) {
12 $rootScope.$on('$viewContentLoaded', function () {
13 $templateCache.removeAll();
14 });
15 });
16 crmMailingAB.config([
17 '$routeProvider',
18 function ($routeProvider) {
19 $routeProvider.when('/abtest', {
20 templateUrl: partialUrl('list.html'),
21 controller: 'ABListingCtrl',
22 resolve: {
23 mailingABList: function ($route, crmApi) {
24 return crmApi('MailingAB', 'get', {rowCount: 0});
25 }
26 }
27 });
28 $routeProvider.when('/abtest/report/:id', {
29 templateUrl: partialUrl('report.html'),
30 controller: 'ReportCtrl',
31 resolve: {
32 selectedABTest: function ($route, crmApi) {
33 return crmApi('MailingAB', 'getsingle', {id: $route.current.params.id});
34 }
35 }
36 });
37 $routeProvider.when('/abtest/:id', {
38 templateUrl: partialUrl('main.html'),
39 controller: 'crmABTestingTabsCtrl',
40 resolve: {
41 selectedABTest: function ($route, crmApi) {
42 if ($route.current.params.id !== 'new') {
43
44 return crmApi('MailingAB', 'getsingle', {id: $route.current.params.id});
45 }
46 else {
47 //created_id has been set to my id. Does not save without created_id. Needs to made generic based on the user
48 return { just_created: "1"
49 };
50 }
51 }
52 }
53 });
54 }
55 ]);
56
57 //-----------------------------------------
58 // Add a new record by name.
59 // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" />
60 /**
61 * This controler lists the existing ABtests
62 * used on /partials/abtest/list.html
63 * @returns mailingABList - object that contains the existing AB mailings
64 * @returns testing_criteria - array that has the name of the different test types
65 */
66 crmMailingAB.controller('ABListingCtrl', function ($scope, crmApi, mailingABList) {
67 $scope.mailingABList = mailingABList.values;
68 $scope.testing_criteria = {
69 '1': "Subject lines",
70 '2': "From names",
71 '3': "Two different emails"
72 };
73 });
74
75 crmMailingAB.controller('crmABTestingTabsCtrl', function ($scope, crmApi, selectedABTest, $sce) {
76 $scope.partialUrl = partialUrl;
77 $scope.abId = "";
78 $scope.whatnext = 2;
79 $scope.currentABTest = selectedABTest;
80 $scope.groups = CRM.crmMailing.groupNames;
81 $scope.mailList = CRM.crmMailing.civiMails;
82 $scope.eMailing = CRM.crmMailing.emailAdd;
83 $scope.tmpList = CRM.crmMailing.mesTemplate;
84 $scope.mailingGrp = CRM.crmMailing.mailGrp;
85 $scope.headerfooter = CRM.crmMailing.headerfooterList;
86 $scope.sparestuff = {};
87 $scope.sparestuff.emailadd = "";
88 $scope.sparestuff.winnercriteria = "";
89 $scope.sparestuff.isnew = false;
90 $scope.sparestuff.allgroups = "";
91 $scope.mailid = [];
92 $scope.preventsubmit = false;
93
94 if ($scope.currentABTest.declare_winning_time != null) {
95 $scope.ans = $scope.currentABTest.declare_winning_time.split(" ");
96 $scope.currentABTest.date = $scope.ans[0];
97 $scope.currentABTest.time = $scope.ans[1];
98 }
99 $scope.token = [];
100
101 if ($scope.currentABTest.just_created != 1) {
102 $scope.abId = $scope.currentABTest.id;
103 $scope.sparestuff.isnew = false;
104
105 var abmailA = crmApi('Mailing', 'getsingle', {id: $scope.currentABTest.mailing_id_a});
106 var abmailB = crmApi('Mailing', 'getsingle', {id: $scope.currentABTest.mailing_id_b});
107 var abmailC = crmApi('Mailing', 'getsingle', {id: $scope.currentABTest.mailing_id_c});
108 abmailA.success(function (data) {
109 if (data.is_error == 0) {
110 $scope.mailA = data;
111 }
112 });
113 abmailB.success(function (data) {
114 if (data.is_error == 0) {
115 $scope.mailB = data;
116 }
117 });
118 abmailC.success(function (data) {
119 if (data.is_error == 0) {
120 $scope.mailC = data;
121 }
122 });
123 }
124 else {
125 $scope.sparestuff.isnew = true;
126 $scope.mailA = {};
127 $scope.mailB = {};
128 $scope.mailC = {};
129 }
130
131 $scope.sendtest = false;
132 if (typeof $scope.mailA == 'undefined') {
133 $scope.mailA = {};
134 }
135 if (typeof $scope.mailB == 'undefined') {
136 $scope.mailB = {};
137 }
138 if (typeof $scope.mailB == 'undefined') {
139 $scope.mailC = {};
140 }
141
142 $scope.templates =
143 [
144 { name: 'Subject Lines', url: partialUrl('subject_lines.html'), val: 1},
145 { name: 'From Name', url: partialUrl('from_name.html'), val: 2},
146 { name: 'Two different Emails', url: partialUrl('two_emails.html'), val: 3}
147 ];
148
149 if ($scope.currentABTest.just_created != 1) {
150 $scope.sparestuff.template = $scope.templates[$scope.currentABTest.testing_criteria_id - 1];
151 }
152 else {
153 $scope.sparestuff.template = $scope.templates[0];
154 }
155
156
157 $scope.deliberatelyTrustDangerousSnippeta = function () {
158 return $sce.trustAsHtml($scope.sparestuff.previewa);
159 };
160
161 $scope.deliberatelyTrustDangerousSnippetb = function () {
162 return $sce.trustAsHtml($scope.sparestuff.previewb);
163 };
164
165 $scope.tab_val = 0;
166 $scope.max_tab = ($scope.sparestuff.isnew == true) ? 0 : 4;
167
168 /**
169 * Example: gotoTab('campaign')
170 */
171 $scope.gotoTab = function(name) {
172 var TABS = {campaign: 0, compose: 1, rec: 2, preview: 3}; // FIXME
173 if ($scope.max_tab >= TABS[name]) {
174 $scope.tab_val = TABS[name];
175 }
176 }
177
178 $scope.preview = false;
179 $scope.slide_value = 0;
180
181 $scope.setifyes = function (val) {
182 $scope.ifyes = val == 1;
183 };
184
185 /* $scope.reply = function () {
186 if ($scope.trackreplies == 0) {
187 $scope.trackreplies = 1;
188 }
189 else {
190 $scope.trackreplies = 0;
191 $scope.mailA.forward_replies = 0;
192 $scope.mailA.auto_responder = 0;
193 }
194 }
195 */
196 $scope.isAuto = function (au) {
197 return au.component_type == "Reply";
198 };
199
200 $scope.trackr = function (trackreplies) {
201 return trackreplies == "1";
202 };
203
204 $scope.sendTestMailing = function () {
205 $scope.sendtest = true;
206 };
207
208 $scope.isHeader = function (hf) {
209 return hf.component_type == "Header";
210 };
211 //filter so we only get footers from mailing component
212 $scope.isFooter = function (f) {
213 return f.component_type == "Footer";
214 };
215
216 $scope.send_date = "01/01/2000";
217 $scope.dt = "";
218
219 $scope.savea = function (dat) {
220
221 var result = crmApi('Mailing', 'create', dat, true);
222 result.success(function (data) {
223 if (data.is_error == 0) {
224 $scope.mailA.id = data.id;
225 $scope.currentABTest.mailing_id_a = $scope.mailA.id;
226 }
227 });
228 };
229
230 $scope.append_mails = function () {
231 crmApi('MailingAB', 'create', {
232 id: $scope.abId,
233 mailing_id_a: $scope.mailA.id,
234 mailing_id_b: $scope.mailB.id,
235 mailing_id_c: $scope.mailC.id
236 });
237 $scope.currentABTest.id = $scope.abId;
238 };
239
240 $scope.saveb = function (dat) {
241 var result = crmApi('Mailing', 'create', dat, true);
242 result.success(function (data) {
243 if (data.is_error == 0) {
244 $scope.mailB.id = data.id;
245 $scope.currentABTest.mailing_id_b = $scope.mailB.id;
246 //$scope.append_mails();
247 }
248 });
249 };
250
251 $scope.savec = function (dat) {
252 var result = crmApi('Mailing', 'create', dat, true);
253
254 result.success(function (data) {
255 if (data.is_error == 0) {
256 $scope.mailC.id = data.id;
257 $scope.currentABTest.mailing_id_c = $scope.mailC.id;
258 $scope.append_mails();
259 }
260 });
261 };
262
263 $scope.sparestuff.previewa = "";
264 $scope.pre = function () {
265 $scope.preview = true;
266 };
267
268 $scope.init = function (par) {
269 if (par == "3") {
270 $scope.sparestuff.template.url = partialUrl('from_name.html');
271 }
272 else {
273 if (par == "2") {
274 $scope.sparestuff.template.url = partialUrl('subject_lines.html');
275 }
276 else {
277 $scope.sparestuff.template.url = partialUrl('two_emails.html');
278 }
279 }
280 $scope.whatnext = par.toString();
281 };
282
283 $scope.tab_upd = function () {
284 $scope.tab_val = $scope.tab_val + 1;
285 $scope.max_tab = Math.max($scope.tab_val, $scope.max_tab);
286 };
287
288 $scope.tab_upd_dec = function () {
289 $scope.tab_val = $scope.tab_val - 1;
290 };
291
292 $scope.setdate = function (par) {
293 $scope.send_date = par;
294 $scope.dt = par;
295 $scope.apply();
296 };
297
298 $scope.testmailid = "";
299 $scope.incGroup = [];
300 $scope.excGroup = [];
301 $scope.incGroupids = [];
302 $scope.excGroupids = [];
303 $scope.tp1 = {};
304 $scope.create_abtest = function () {
305 var result;
306 $scope.currentABTest.testing_criteria_id = $scope.sparestuff.template.val;
307
308 if ($scope.abId == "") {
309 result = crmApi('MailingAB', 'create', {name: $scope.currentABTest.name, testing_criteria_id: $scope.sparestuff.template.val});
310 }
311 else {
312 if (typeof $scope.currentABTest.mailing_id_a == 'undefined') {
313 result = crmApi('MailingAB', 'create', {name: $scope.currentABTest.name, id: $scope.abId, testing_criteria_id: $scope.sparestuff.template.val});
314 }
315 else {
316 result = crmApi('MailingAB', 'create', {name: $scope.currentABTest.name, id: $scope.abId, testing_criteria_id: $scope.sparestuff.template.val, mailing_id_a: $scope.currentABTest.mailing_id_a, mailing_id_b: $scope.currentABTest.mailing_id_b});
317 }
318 }
319
320 result.success(function (data) {
321 if (data.is_error == 0) {
322 $scope.abId = data.id;
323 }
324 });
325 };
326 $scope.tokenfunc = function (elem, e, chng) {
327 var msg = document.getElementById(elem).value;
328 var cursorlen = document.getElementById(elem).selectionStart;
329 var textlen = msg.length;
330 document.getElementById(elem).value = msg.substring(0, cursorlen) + e.val + msg.substring(cursorlen, textlen);
331 chng = msg.substring(0, cursorlen) + e.val + msg.substring(cursorlen, textlen);
332 var cursorPos = (cursorlen + e.val.length);
333 document.getElementById(elem).selectionStart = cursorPos;
334 document.getElementById(elem).selectionEnd = cursorPos;
335 document.getElementById(elem).focus();
336 };
337
338 $scope.sparestuff.ingrps = "";
339 $scope.sparestuff.excgrps = "";
340 $scope.a_b_update = function () {
341 $scope.tp1.include = $scope.incGroupids;
342 $scope.tp1.exclude = $scope.excGroupids;
343 console.log($scope.tp1);
344 crmApi('MailingAB', 'recipients_update', {
345 id: $scope.currentABTest.id,
346 groups: $scope.tp1
347 });
348
349 var resulta = crmApi('Mailing', 'preview', {id: $scope.currentABTest.mailing_id_a});
350
351 resulta.success(function (data) {
352 if (data.is_error == 0) {
353 $scope.sparestuff.previewa = data.values.html;
354 }
355 });
356
357 resulta = crmApi('Mailing', 'preview', {id: $scope.currentABTest.mailing_id_b});
358
359 resulta.success(function (data) {
360 if (data.is_error == 0) {
361 $scope.sparestuff.previewb = data.values.html;
362 }
363 });
364
365 $scope.startabtest = function () {
366 if (typeof $scope.sparestuff.date == 'undefined') {
367 $scope.sparestuff.date = 'now';
368 }
369 crmApi('MailingAB', 'send_mail', {id: $scope.abId,
370 scheduled_date: $scope.sparestuff.date, scheduled_date_time: $scope.currentABTest.latertime});
371 };
372
373 angular.forEach($scope.incGroup, function (value) {
374 $scope.sparestuff.ingrps += value.toString() + ", ";
375 });
376 angular.forEach($scope.excGroup, function (value) {
377 $scope.sparestuff.excgrps += value.toString() + ", ";
378 });
379 if ($scope.sparestuff.ingrps.length != 0) {
380 $scope.sparestuff.ingrps = $scope.sparestuff.ingrps.substr(0, $scope.sparestuff.ingrps.length - 2);
381 }
382 if ($scope.sparestuff.excgrps.length != 0) {
383 $scope.sparestuff.excgrps = $scope.sparestuff.excgrps.substr(0, $scope.sparestuff.excgrps.length - 2);
384 }
385 };
386
387 $scope.update_abtest = function () {
388 $scope.currentABTest.declare_winning_time = $scope.currentABTest.date + " " + $scope.currentABTest.time;
389 crmApi('MailingAB', 'create', {
390 id: $scope.abId,
391 testing_criteria_id: $scope.sparestuff.template.val,
392 mailing_id_a: $scope.currentABTest.mailing_id_a,
393 mailing_id_b: $scope.currentABTest.mailing_id_b,
394 mailing_id_c: $scope.currentABTest.mailing_id_c,
395 specific_url: $scope.currentABTest.acturl,
396 winner_criteria_id: $scope.currentABTest.winner_criteria_id,
397 group_percentage: $scope.currentABTest.group_percentage,
398 declare_winning_time: $scope.currentABTest.declare_winning_time
399 });
400 };
401 $scope.currentABTest.latertime = "";
402 $scope.tmp = function (tst, aorb) {
403 if (aorb == 1) {
404 $scope.mailA.msg_template_id = tst;
405 if ($scope.mailA.msg_template_id == null) {
406 $scope.mailA.body_html = "";
407 $scope.mailA.subject = "";
408 }
409 else {
410 for (var a in $scope.tmpList) {
411 if ($scope.tmpList[a].id == $scope.mailA.msg_template_id) {
412 $scope.mailA.body_html = $scope.tmpList[a].msg_html;
413 if (typeof $scope.mailA.subject == 'undefined' || $scope.mailA.subject.length == 0) {
414 $scope.mailA.subject = $scope.tmpList[a].msg_subject;
415 }
416 }
417 }
418 }
419 }
420 else {
421 if (aorb == 2) {
422 $scope.mailB.msg_template_id = tst;
423 if ($scope.mailB.msg_template_id == null) {
424 $scope.mailB.body_html = "";
425 $scope.mailB.subject = "";
426 }
427 else {
428 for (var a in $scope.tmpList) {
429 if ($scope.tmpList[a].id == $scope.mailB.msg_template_id) {
430 $scope.mailB.body_html = $scope.tmpList[a].msg_html;
431 if (typeof $scope.mailB.subject == 'undefined' || $scope.mailB.subject.length == 0) {
432 $scope.mailB.subject = $scope.tmpList[a].msg_subject;
433 }
434
435 }
436 }
437 }
438 }
439 else {
440 $scope.mailA.msg_template_id = tst;
441 if ($scope.mailA.msg_template_id == null) {
442 $scope.mailA.body_html = "";
443 $scope.mailA.subject = "";
444 }
445 else {
446 for (var a in $scope.tmpList) {
447 if ($scope.tmpList[a].id == $scope.mailA.msg_template_id) {
448 $scope.mailA.body_html = $scope.tmpList[a].msg_html;
449 if (typeof $scope.mailA.subject == 'undefined' || $scope.mailA.subject.length == 0) {
450 $scope.mailA.subject = $scope.tmpList[a].msg_subject;
451 }
452 }
453 }
454 }
455
456 $scope.mailB.msg_template_id = tst;
457 if ($scope.mailB.msg_template_id == null) {
458 $scope.mailB.body_html = "";
459 $scope.mailB.subject = "";
460
461 }
462 else {
463 for (var a in $scope.tmpList) {
464 if ($scope.tmpList[a].id == $scope.mailB.msg_template_id) {
465 $scope.mailB.body_html = $scope.tmpList[a].msg_html;
466 if (typeof $scope.mailB.subject == 'undefined' || $scope.mailB.subject.length == 0) {
467 $scope.mailB.subject = $scope.tmpList[a].msg_subject;
468 }
469
470 }
471 }
472 }
473 }
474 }
475 };
476
477 /*$scope.tmp = function (tst){
478 $scope.currentMailing.msg_template_id=tst;
479 console.log($scope.currentMailing.msg_template_id+ "sasas");
480 if($scope.currentMailing.msg_template_id == null){
481 $scope.currentMailing.body_html="";
482 $scope.currentMailing.subject="";
483 }
484 else{
485 for(var a in $scope.tmpList){
486
487 if($scope.tmpList[a].id==$scope.currentMailing.msg_template_id){
488 $scope.currentMailing.body_html=$scope.tmpList[a].msg_html;
489 console.log($scope.tmpList[a].msg_subject);
490 $scope.currentMailing.subject=$scope.tmpList[a].msg_subject;
491 console.log($scope.currentMailing.subject);
492 }
493 }
494 }
495 };*/
496
497
498 $scope.$watch('preview', function () {
499 if ($scope.preview == true) {
500 $('#prevmail').dialog({
501 title: 'Preview Mailing',
502 width: 1000,
503 height: 500,
504 closed: false,
505 cache: false,
506 modal: true,
507 position: {
508 my: 'left',
509 at: 'top',
510 of: $(".crmABTestingAllTabs")
511 },
512
513 close: function () {
514 $scope.preview = false;
515 $scope.$apply();
516 }
517 });
518
519 $("#prevmail").dialog('option', 'position', [300, 50]);
520 }
521
522 }, true);
523
524 $scope.call = function () {
525 $scope.$apply();
526 crmApi('Mailing', 'send_test', {
527 mailing_id: $scope.currentABTest.mailing_id_a,
528 test_email: $scope.sparestuff.emailadd
529 });
530
531 crmApi('Mailing', 'send_test', {
532 mailing_id: $scope.currentABTest.mailing_id_b,
533 test_email: $scope.sparestuff.emailadd
534 })
535 };
536
537 $scope.$watch('sendtest', function () {
538 if ($scope.sendtest == true) {
539 $('#sendtest').dialog({
540 title: 'Send Test Mails',
541 width: 300,
542 height: 150,
543 closed: false,
544 cache: false,
545 modal: true,
546 buttons: {
547 'Send': function () {
548 $scope.call();
549 $scope.sendtest = false;
550 $('#sendtest').dialog("close");
551
552 }
553 },
554 close: function () {
555 $scope.sendtest = false;
556 $scope.$apply()
557 }
558 });
559 }
560 });
561 });
562
563
564 crmMailingAB.directive('nexttab', function () {
565 return {
566 // Restrict it to be an attribute in this case
567 restrict: 'A',
568 priority: 500,
569 // responsible for registering DOM listeners as well as updating the DOM
570 link: function (scope, element, attrs) {
571
572 var tabselector = $(".crmABTestingAllTabs");
573 tabselector.tabs(scope.$eval(attrs.nexttab));
574
575 // disable remaining tabs
576 if (scope.sparestuff.isnew == true) {
577 tabselector.tabs({disabled: [1, 2, 3]});
578 }
579
580 $(element).on("click", function () {
581 if (scope.tab_val == 0) {
582 scope.create_abtest();
583 }
584 else {
585 if (scope.tab_val == 2) {
586 scope.update_abtest();
587 if (scope.currentABTest.winner_criteria_id == 1) {
588 scope.sparestuff.winnercriteria = "Open";
589 scope.$apply();
590 }
591 else {
592 if (scope.currentABTest.winner_criteria_id == 2) {
593 scope.sparestuff.winnercriteria = " Total Unique Clicks";
594 scope.$apply();
595 }
596 else {
597 if (scope.currentABTest.winner_criteria_id == 3) {
598 scope.sparestuff.winnercriteria = "Total Clicks on a particular link";
599 scope.$apply();
600 }
601 }
602 }
603 scope.a_b_update();
604 }
605 }
606 scope.tab_upd();
607
608 var myArray1 = [];
609 for (var i = scope.max_tab + 1; i < 4; i++) {
610 myArray1.push(i);
611 }
612 tabselector.tabs("option", "disabled", myArray1);
613 tabselector.tabs("option", "active", scope.tab_val);
614 scope.$apply();
615 });
616 }
617 };
618 });
619
620 crmMailingAB.directive('prevtab', function () {
621 return {
622 // Restrict it to be an attribute in this case
623 restrict: 'A',
624 priority: 500,
625 // responsible for registering DOM listeners as well as updating the DOM
626 link: function (scope, element, attrs) {
627 $(element).on("click", function () {
628 var temp = scope.tab_val - 1;
629 scope.tab_upd_dec();
630 scope.$apply();
631 if (temp != 3) {
632 $(".crmABTestingAllTabs").tabs("option", "active", temp);
633 }
634 scope.$apply();
635 });
636 }
637 };
638 });
639
640 crmMailingAB.directive('chsgroup', function () {
641 return {
642 restrict: 'AE',
643 link: function (scope, element, attrs) {
644 function format(item) {
645 if (!item.id) {
646 // return `text` for optgroup
647 return item.text;
648 }
649 // return item template
650 var a = item.id.split(" ");
651 if (a[1] == "civicrm_group" && a[2] == "include") {
652 return "<img src='../../sites/all/modules/civicrm/i/include.jpeg' height=12 width=12/>" + " " + "<img src='../../sites/all/modules/civicrm/i/group.png' height=12 width=12/>" + item.text;
653 }
654 if (a[1] == "civicrm_group" && a[2] == "exclude") {
655 return "<img src='../../sites/all/modules/civicrm/i/Error.gif' height=12 width=12/>" + " " + "<img src='../../sites/all/modules/civicrm/i/group.png' height=12 width=12/>" + item.text;
656 }
657 if (a[1] == "civicrm_mailing" && a[2] == "include") {
658 return "<img src='../../sites/all/modules/civicrm/i/include.jpeg' height=12 width=12/>" + " " + "<img src='../../sites/all/modules/civicrm/i/EnvelopeIn.gif' height=12 width=12/>" + item.text;
659 }
660 if (a[1] == "civicrm_mailing" && a[2] == "exclude") {
661 return "<img src='../../sites/all/modules/civicrm/i/Error.gif' height=12 width=12/>" + " " + "<img src='../../sites/all/modules/civicrm/i/EnvelopeIn.gif' height=12 width=12/>" + item.text;
662 }
663 }
664
665
666 $(element).select2({
667 width: "400px",
668 placeholder: "Select the groups you wish to include",
669 formatResult: format,
670 formatSelection: format,
671 escapeMarkup: function (m) {
672 return m;
673 }
674 }).select2("data", scope.sparestuff.allgroups);
675
676
677 $(element).on('select2-selecting', function (e) {
678 var a = e.val.split(" ");
679 var l = a.length;
680 if (a[2] == "include") {
681 var str = "";
682 for (i = 3; i < l; i++) {
683 str += a[i];
684 str += " ";
685 }
686 scope.incGroup.push(str);
687 scope.incGroupids.push(a[0]);
688 scope.$apply();
689 }
690
691 else {
692 var str = "";
693 for (i = 3; i < l; i++) {
694 str += a[i];
695 str += " ";
696 }
697
698 scope.excGroup.push(str);
699 scope.excGroupids.push(a[0]);
700 scope.$apply();
701 }
702
703 scope.$apply();
704
705 });
706 $(element).on("select2-removed", function (e) {
707 if (e.val.split(" ")[2] == "exclude") {
708 var excIndex = scope.excGroup.indexOf(e.val.split(" ")[3]);
709 scope.excGroup.splice(excIndex, 1);
710 scope.excGroupids.splice(excIndex, 1);
711 scope.$apply();
712 }
713 else {
714 var incIndex = scope.incGroup.indexOf(e.val.split(" ")[3]);
715 scope.incGroup.splice(incIndex, 1);
716 scope.incGroupids.splice(incIndex, 1);
717 scope.$apply();
718 }
719
720 scope.$apply();
721 });
722 }
723 };
724 });
725
726 crmMailingAB.directive('sliderbar', function () {
727 return{
728 restrict: 'AE',
729 link: function (scope, element, attrs) {
730 if (typeof scope.currentABTest.group_percentage != 'undefined') {
731 $(element).slider({value: scope.currentABTest.group_percentage});
732 }
733 $(element).slider({min: 1});
734 $(element).slider({
735 slide: function (event, ui) {
736 scope.currentABTest.group_percentage = ui.value;
737 scope.$apply();
738 }
739 });
740 }
741 };
742 });
743
744 crmMailingAB.directive('datepick', function () {
745 return {
746 restrict: 'AE',
747 link: function (scope, element, attrs) {
748 $(element).datepicker({
749 dateFormat: "dd-mm-yy",
750 onSelect: function (date) {
751 $(".ui-datepicker a").removeAttr("href");
752 scope.sparestuff.date = date.toString();
753 scope.$apply();
754 }
755 });
756 }
757 };
758 });
759
760 crmMailingAB.directive('submitform', function () {
761 return {
762 restrict: 'A',
763 priority: 1000,
764 link: function (scope, element, attrs) {
765 $(element).on("click", function () {
766 scope.savea({
767 id: scope.mailA.id,
768 name: scope.currentABTest.name + ' (AB test A)',
769 visibility: scope.mailA.visibility,
770 created_id: 1,
771 subject: scope.mailA.subject,
772 msg_template_id: scope.mailA.msg_template_id == null ? "" : scope.mailA.msg_template_id,
773 open_tracking: scope.mailA.open_tracking,
774 url_tracking: scope.mailA.url_tracking,
775 forward_replies: scope.mailA.forward_replies,
776 auto_responder: scope.mailA.auto_responder,
777 from_name: scope.mailA.from_name,
778 from_email: scope.mailA.from_email,
779 replyto_email: scope.mailA.replyto_email,
780 unsubscribe_id: scope.mailA.unsubscribe_id,
781 resubscribe_id: scope.mailA.resubscribe_id,
782 body_html: scope.mailA.body_html,
783 body_text: scope.mailA.body_text,
784 scheduled_date: scope.mailA.scheduled_date,
785 scheduled_id: scope.mailA.scheduled_id,
786 campaign_id: scope.mailA.campaign_id == null ? "" : scope.mailA.campaign_id,
787 header_id: scope.mailA.header_id,
788 footer_id: scope.mailA.footer_id,
789 is_completed: scope.mailA.is_completed
790 });
791
792 if (scope.whatnext == "3") {
793 scope.mailB.name = scope.mailA.name;
794 scope.mailB.visibility = scope.mailA.visibility;
795 scope.mailB.created_id = scope.mailA.created_id;
796 scope.mailB.subject = scope.mailA.subject;
797 scope.mailB.msg_template_id = scope.mailA.msg_template_id == null ? "" : scope.mailA.msg_template_id;
798 scope.mailB.open_tracking = scope.mailA.open_tracking;
799 scope.mailB.url_tracking = scope.mailA.url_tracking;
800 scope.mailB.forward_replies = scope.mailA.forward_replies;
801 scope.mailB.auto_responder = scope.mailA.auto_responder;
802 scope.mailB.from_name = scope.mailA.from_name;
803 scope.mailB.replyto_email = scope.mailA.replyto_email;
804 scope.mailB.unsubscribe_id = scope.mailA.unsubscribe_id;
805 scope.mailB.resubscribe_id = scope.mailA.resubscribe_id;
806 scope.mailB.body_html = scope.mailA.body_html;
807 scope.mailB.body_text = scope.mailA.body_text;
808 scope.mailB.scheduled_id = scope.mailA.scheduled_id;
809 scope.mailB.campaign_id = scope.mailA.campaign_id == null ? "" : scope.mailA.campaign_id;
810 scope.mailB.header_id = scope.mailA.header_id;
811 scope.mailB.footer_id = scope.mailA.footer_id;
812 scope.mailB.is_completed = scope.mailA.is_completed;
813 }
814 else {
815 if (scope.whatnext == "2") {
816 scope.mailB.fromEmail = scope.mailA.fromEmail;
817 scope.mailB.name = scope.mailA.name;
818 scope.mailB.visibility = scope.mailA.visibility;
819 scope.mailB.created_id = scope.mailA.created_id;
820 scope.mailB.msg_template_id = scope.mailA.msg_template_id == null ? "" : scope.mailA.msg_template_id;
821 scope.mailB.open_tracking = scope.mailA.open_tracking;
822 scope.mailB.url_tracking = scope.mailA.url_tracking;
823 scope.mailB.forward_replies = scope.mailA.forward_replies;
824 scope.mailB.auto_responder = scope.mailA.auto_responder;
825 scope.mailB.from_name = scope.mailA.from_name;
826 scope.mailB.replyto_email = scope.mailA.replyto_email;
827 scope.mailB.unsubscribe_id = scope.mailA.unsubscribe_id;
828 scope.mailB.resubscribe_id = scope.mailA.resubscribe_id;
829 scope.mailB.body_html = scope.mailA.body_html;
830 scope.mailB.body_text = scope.mailA.body_text;
831 scope.mailB.scheduled_id = scope.mailA.scheduled_id;
832 scope.mailB.campaign_id = scope.mailA.campaign_id == null ? "" : scope.mailA.campaign_id;
833 scope.mailB.header_id = scope.mailA.header_id;
834 scope.mailB.footer_id = scope.mailA.footer_id;
835 scope.mailB.is_completed = scope.mailA.is_completed;
836 }
837 }
838 scope.saveb({
839 id: scope.mailB.id,
840 name: scope.currentABTest.name + ' (AB test B)',
841 visibility: scope.mailB.visibility,
842 created_id: 1,
843 subject: scope.mailB.subject,
844 msg_template_id: scope.mailB.msg_template_id == null ? "" : scope.mailB.msg_template_id,
845 open_tracking: scope.mailB.open_tracking,
846 url_tracking: scope.mailB.url_tracking,
847 forward_replies: scope.mailB.forward_replies,
848 auto_responder: scope.mailB.auto_responder,
849 from_name: scope.mailB.from_name,
850 from_email: scope.mailB.from_email,
851 replyto_email: scope.mailB.replyto_email,
852 unsubscribe_id: scope.mailB.unsubscribe_id,
853 resubscribe_id: scope.mailB.resubscribe_id,
854 body_html: scope.mailB.body_html,
855 body_text: scope.mailB.body_text,
856 scheduled_id: scope.mailB.scheduled_id,
857 campaign_id: scope.mailB.campaign_id == null ? "" : scope.mailB.campaign_id,
858 header_id: scope.mailB.header_id,
859 footer_id: scope.mailB.footer_id,
860 is_completed: scope.mailA.is_completed
861 });
862
863 scope.savec({
864 id: scope.mailC.id,
865 name: scope.currentABTest.name + ' (AB test winner)',
866 visibility: scope.mailB.visibility,
867 created_id: 1,
868 subject: scope.mailB.subject,
869 msg_template_id: scope.mailB.msg_template_id == null ? "" : scope.mailB.msg_template_id,
870 open_tracking: scope.mailB.open_tracking,
871 url_tracking: scope.mailB.url_tracking,
872 forward_replies: scope.mailB.forward_replies,
873 auto_responder: scope.mailB.auto_responder,
874 from_name: scope.mailB.from_name,
875 from_email: scope.mailB.from_email,
876 replyto_email: scope.mailB.replyto_email,
877 unsubscribe_id: scope.mailB.unsubscribe_id,
878 resubscribe_id: scope.mailB.resubscribe_id,
879 body_html: scope.mailB.body_html,
880 body_text: scope.mailB.body_text,
881 campaign_id: scope.mailB.campaign_id == null ? "" : scope.mailB.campaign_id,
882 header_id: scope.mailB.header_id,
883 footer_id: scope.mailB.footer_id,
884 is_completed: scope.mailA.is_completed,
885 'api.mailing_job.create': 0
886 });
887 });
888 }
889 };
890 });
891
892 crmMailingAB.directive('chsdate', function () {
893 return {
894 restrict: 'AE',
895 link: function (scope, element, attrs) {
896 $(element).datepicker({
897 dateFormat: "yy-mm-dd",
898 onSelect: function (date) {
899 $(".ui-datepicker a").removeAttr("href");
900 scope.currentABTest.date = date.toString();
901 scope.$apply();
902 }
903 });
904 }
905 };
906 });
907
908 crmMailingAB.directive('prevbutton', function () {
909 return {
910 restrict: 'AE',
911 replace: 'true',
912 template: '<div class="crm-submit-buttons" >' +
913 '<div class = "crm-button crm-button-type-upload crm-button_qf_Contact_upload_view" >' +
914 '<input type="submit" value="Previous" id="campaignbutton _qf_Contact_upload_view-top" class="btn btn-primary" prevtab={{tab_val}}>' +
915 '</div></div>'
916 };
917 });
918
919 crmMailingAB.directive('groupselect', function () {
920 return {
921 restrict: 'AE',
922 link: function (scope, element, attrs) {
923 $(element).select2({
924 width: "200px",
925 data: CRM.crmMailing.mailTokens,
926 placeholder: "Insert Token"
927 });
928 $(element).on('select2-selecting', function (e) {
929
930 scope.$evalAsync('_resetSelection()');
931 var a = $(element).attr('id');
932 if (a == "htgroupcompose") {
933 scope.tokenfunc("body_html", e, scope.mailA.body_html);
934 }
935 else {
936 if (a == "htgroupcomposetwob") {
937 scope.tokenfunc("twomailbbody_html", e, scope.mailB.body_html);
938 }
939 else {
940 if (a == "htgroupcomposetwoa") {
941 scope.tokenfunc("twomailabody_html", e, scope.mailA.body_html);
942 }
943 else {
944 if (a == "textgroupcompose") {
945 scope.tokenfunc("body_text", e, scope.mailA.body_text);
946 }
947 else {
948 if (a == "textgroupcomposetwoa") {
949 scope.tokenfunc("twomailabody_text", e, scope.mailA.body_text);
950 }
951 else {
952 if (a == "textgroupcomposetwob") {
953 scope.tokenfunc("twomailbbody_text", e, scope.mailB.body_text);
954 }
955 else {
956 if (a == "subgroupsuba") {
957 scope.tokenfunc("suba", e, scope.mailA.subject);
958 }
959 else {
960 if (a == "subgroupsubb") {
961 scope.tokenfunc("subb", e, scope.mailB.subject);
962 }
963 else {
964 if (a == "subgroupfrom") {
965 scope.tokenfunc("subfrom", e, scope.mailA.subject);
966 }
967 else {
968 if (a == "subgrouptwoa") {
969 scope.tokenfunc("twomaila", e, scope.mailA.subject);
970 }
971 else {
972 if (a == "subgrouptwob") {
973 scope.tokenfunc("twomailb", e, scope.mailB.subject);
974 }
975 }
976 }
977 }
978 }
979 }
980 }
981 }
982 }
983 }
984 }
985
986 scope.$apply();
987 e.preventDefault();
988 })
989 }
990 };
991 });
992
993 crmMailingAB.directive('replytoselect',function(){
994 return {
995 restrict : 'AE',
996 link: function(scope, element, attrs){
997
998 function format(item){
999 return item.label.replace(/&/g, '&amp;')
1000 .replace(/"/g, '&quot;')
1001 .replace(/</g, '&lt;')
1002 .replace(/>/g, '&gt;');
1003 }
1004
1005 CRM.api3('OptionGroup', 'get', {
1006 "sequential": 1,
1007 "name": "from_email_address"
1008 }).done(function(result) {
1009 var emailGroupId = result.id;
1010 CRM.api3('OptionValue', 'get', {
1011 "sequential": 1,
1012 "option_group_id": result.id
1013 }).done(function(orgEmails) {
1014
1015 //$sce.trustAsHtml(
1016
1017 $(element).select2({
1018 class: "abtesting-form-element",
1019 data: orgEmails.values,
1020 formatResult: format,
1021 formatSelection: format,
1022 placeholder:"Select reply to address"});
1023
1024 });
1025 });
1026 }
1027 };
1028 });
1029
1030 crmMailingAB.directive('stopa',function(){
1031 return {
1032 restrict: 'AE',
1033 link: function (scope, element, attrs) {
1034 scope.$watch('aastop', function () {
1035 if (scope.aastop == true) {
1036 $(element).dialog({
1037 title: 'Confirmation',
1038 width: 300,
1039 height: 150,
1040 closed: false,
1041 cache: false,
1042 modal: true,
1043 buttons: {
1044 'Yes': function () {
1045 scope.aastop = false;
1046 scope.copyatoc();
1047 $(element).dialog("close");
1048 },
1049 'No': function () {
1050 scope.aastop = false;
1051 $(element).dialog("close");
1052 }
1053 },
1054 close: function () {
1055 scope.aastop = false;
1056
1057 scope.$apply();
1058 }
1059 });
1060 }
1061 });
1062 }
1063 }
1064 });
1065
1066 crmMailingAB.directive('stopb', function () {
1067 return {
1068 restrict: 'AE',
1069 link: function (scope, element, attrs) {
1070
1071 scope.$watch('bbstop', function () {
1072 if (scope.bbstop == true) {
1073 $(element).dialog({
1074 title: 'Confirmation',
1075 width: 300,
1076 height: 150,
1077 closed: false,
1078 cache: false,
1079 modal: true,
1080 buttons: {
1081 'Yes': function () {
1082 scope.bbstop = false;
1083 scope.sendc();
1084 $(element).dialog("close");
1085 },
1086 'No': function () {
1087 scope.bbstop = false;
1088 $(element).dialog("close");
1089 }
1090 },
1091 close: function () {
1092 scope.bbstop = false;
1093
1094 scope.$apply();
1095 }
1096 });
1097 }
1098 });
1099 }
1100 }
1101 });
1102
1103
1104 crmMailingAB.directive('checktimeentry', function () {
1105 return {
1106 restrict: 'AE',
1107 link: function (scope, element, attrs) {
1108 $(element).timeEntry({
1109 show24Hours: true,
1110 showSeconds: true,
1111 });
1112 }
1113 }
1114 });
1115
1116 crmMailingAB.directive('ckedit', function ($parse) {
1117 CKEDITOR.disableAutoInline = true;
1118 var counter = 0,
1119 prefix = '__ckd_';
1120
1121 return {
1122 restrict: 'A',
1123 link: function (scope, element, attrs, controller) {
1124 var getter = $parse(attrs.ckedit),
1125 setter = getter.assign;
1126
1127 attrs.$set('contenteditable', true); // inline ckeditor needs this
1128 if (!attrs.id) {
1129 attrs.$set('id', prefix + (++counter));
1130 }
1131
1132 // CKEditor stuff
1133 // Override the normal CKEditor save plugin
1134
1135 CKEDITOR.plugins.registered['save'] =
1136 {
1137 init: function (editor) {
1138 editor.addCommand('save',
1139 {
1140 modes: { wysiwyg: 1, source: 1 },
1141 exec: function (editor) {
1142 if (editor.checkDirty()) {
1143 var ckValue = editor.getData();
1144 scope.$apply(function () {
1145 setter(scope, ckValue);
1146 });
1147 ckValue = null;
1148 editor.resetDirty();
1149 }
1150 }
1151 }
1152 );
1153 editor.ui.addButton('Save', { label: 'Save', command: 'save', toolbar: 'document' });
1154 }
1155 };
1156 var options = {};
1157 options.on = {
1158 blur: function (e) {
1159 if (e.editor.checkDirty()) {
1160 var ckValue = e.editor.getData();
1161 scope.$apply(function () {
1162 setter(scope, ckValue);
1163 });
1164 ckValue = null;
1165 e.editor.resetDirty();
1166 }
1167 }
1168 };
1169 options.extraPlugins = 'sourcedialog';
1170 options.removePlugins = 'sourcearea';
1171 var editorangular = CKEDITOR.inline(element[0], options); //invoke
1172
1173 scope.$watch(attrs.ckedit, function (value) {
1174 editorangular.setData(value);
1175 });
1176 }
1177 }
1178
1179 });
1180
1181 })(angular, CRM.$, CRM._);