CRM-15578 - Add crmMailingAB2 module (based on crmMailingAB). Add skeletal list/edit...
[civicrm-core.git] / js / angular-crmMailingAB.js
CommitLineData
983052fe 1/**
2 * Created by aditya on 6/12/14.
3 */
5c691259 4(function (angular, $, _) {
5
fd843187 6 var partialUrl = function (relPath) {
1df2e9f7 7 return CRM.resourceUrls['civicrm'] + '/partials/crmMailingAB/' + relPath;
fd843187 8 };
7811a84b 9 var crmMailingAB = angular.module('crmMailingAB', ['ngRoute', 'ui.utils', 'ngSanitize']);
c2ede705 10
fd843187 11 crmMailingAB.run(function ($rootScope, $templateCache) {
12 $rootScope.$on('$viewContentLoaded', function () {
13 $templateCache.removeAll();
14 });
15 });
fd843187 16 crmMailingAB.config([
17 '$routeProvider',
18 function ($routeProvider) {
1df2e9f7 19 $routeProvider.when('/abtest', {
fd843187 20 templateUrl: partialUrl('list.html'),
425723d9 21 controller: 'ABListingCtrl',
fd843187 22 resolve: {
c2ede705 23 mailingABList: function ($route, crmApi) {
349b6fac 24 return crmApi('MailingAB', 'get', {rowCount: 0});
fd843187 25 }
26 }
27 });
1df2e9f7 28 $routeProvider.when('/abtest/report/:id', {
0838af8e 29 templateUrl: partialUrl('report.html'),
30 controller: 'ReportCtrl',
31 resolve: {
7811a84b 32 selectedABTest: function ($route, crmApi) {
33 return crmApi('MailingAB', 'getsingle', {id: $route.current.params.id});
0838af8e 34 }
35 }
0838af8e 36 });
1df2e9f7 37 $routeProvider.when('/abtest/:id', {
fd843187 38 templateUrl: partialUrl('main.html'),
7811a84b 39 controller: 'crmABTestingTabsCtrl',
fd843187 40 resolve: {
7811a84b 41 selectedABTest: function ($route, crmApi) {
42 if ($route.current.params.id !== 'new') {
493b91cf 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
7811a84b 48 return { just_created: "1"
493b91cf 49 };
50 }
fd843187 51 }
52 }
53 });
54 }
55 ]);
f2719425 56
be5751d1 57 //-----------------------------------------
fd843187 58 // Add a new record by name.
59 // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" />
fe17cd42
V
60 /**
61 * This controler lists the existing ABtests
1df2e9f7 62 * used on /partials/abtest/list.html
fe17cd42
V
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 */
35565345 66 crmMailingAB.controller('ABListingCtrl', function ($scope, crmApi, mailingABList) {
c2ede705 67 $scope.mailingABList = mailingABList.values;
b9926828
V
68 $scope.testing_criteria = {
69 '1': "Subject lines",
70 '2': "From names",
71 '3': "Two different emails"
72 };
425723d9 73 });
7811a84b 74
7811a84b 75 crmMailingAB.controller('crmABTestingTabsCtrl', function ($scope, crmApi, selectedABTest, $sce) {
64ccb448 76 $scope.partialUrl = partialUrl;
77 $scope.abId = "";
7811a84b 78 $scope.whatnext = 2;
79 $scope.currentABTest = selectedABTest;
fd843187 80 $scope.groups = CRM.crmMailing.groupNames;
81 $scope.mailList = CRM.crmMailing.civiMails;
493b91cf 82 $scope.eMailing = CRM.crmMailing.emailAdd;
83 $scope.tmpList = CRM.crmMailing.mesTemplate;
f27a0a8e 84 $scope.mailingGrp = CRM.crmMailing.mailGrp;
493b91cf 85 $scope.headerfooter = CRM.crmMailing.headerfooterList;
35565345 86 $scope.sparestuff = {};
467cd00c 87 $scope.sparestuff.emailadd = "";
88 $scope.sparestuff.winnercriteria = "";
18882677 89 $scope.sparestuff.isnew = false;
f27a0a8e 90 $scope.sparestuff.allgroups = "";
91 $scope.mailid = [];
679b3923 92 $scope.preventsubmit = false;
93
7811a84b 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];
41f6e155 98 }
467cd00c 99 $scope.token = [];
c2ede705 100
7811a84b 101 if ($scope.currentABTest.just_created != 1) {
102 $scope.abId = $scope.currentABTest.id;
18882677 103 $scope.sparestuff.isnew = false;
f27a0a8e 104
7811a84b 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 {
18882677 125 $scope.sparestuff.isnew = true;
7811a84b 126 $scope.mailA = {};
127 $scope.mailB = {};
128 $scope.mailC = {};
129 }
64ccb448 130
7811a84b 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 }
0838af8e 141
fbbff1a0 142 $scope.templates =
143 [
7811a84b 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}
fbbff1a0 147 ];
5cd34d46 148
7811a84b 149 if ($scope.currentABTest.just_created != 1) {
467cd00c 150 $scope.sparestuff.template = $scope.templates[$scope.currentABTest.testing_criteria_id - 1];
7811a84b 151 }
152 else {
467cd00c 153 $scope.sparestuff.template = $scope.templates[0];
fbbff1a0 154 }
493b91cf 155
467cd00c 156
7811a84b 157 $scope.deliberatelyTrustDangerousSnippeta = function () {
467cd00c 158 return $sce.trustAsHtml($scope.sparestuff.previewa);
fe6f9f2f 159 };
160
7811a84b 161 $scope.deliberatelyTrustDangerousSnippetb = function () {
467cd00c 162 return $sce.trustAsHtml($scope.sparestuff.previewb);
fe6f9f2f 163 };
fd843187 164
165 $scope.tab_val = 0;
16ee60ab 166 $scope.max_tab = ($scope.sparestuff.isnew == true) ? 0 : 4;
121d1705 167
cc3a8925
TO
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];
fd843187 175 }
cc3a8925 176 }
7811a84b 177
ef643544 178 $scope.preview = false;
fd843187 179 $scope.slide_value = 0;
180
181 $scope.setifyes = function (val) {
16ee60ab 182 $scope.ifyes = val == 1;
fd843187 183 };
184
35565345
TO
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 */
7811a84b 196 $scope.isAuto = function (au) {
493b91cf 197 return au.component_type == "Reply";
198 };
199
7811a84b 200 $scope.trackr = function (trackreplies) {
16ee60ab
TO
201 return trackreplies == "1";
202 };
493b91cf 203
7811a84b 204 $scope.sendTestMailing = function () {
205 $scope.sendtest = true;
16ee60ab 206 };
493b91cf 207
7811a84b 208 $scope.isHeader = function (hf) {
493b91cf 209 return hf.component_type == "Header";
210 };
211 //filter so we only get footers from mailing component
7811a84b 212 $scope.isFooter = function (f) {
493b91cf 213 return f.component_type == "Footer";
214 };
215
fd843187 216 $scope.send_date = "01/01/2000";
fd843187 217 $scope.dt = "";
218
493b91cf 219 $scope.savea = function (dat) {
fd843187 220
221 var result = crmApi('Mailing', 'create', dat, true);
7811a84b 222 result.success(function (data) {
493b91cf 223 if (data.is_error == 0) {
224 $scope.mailA.id = data.id;
7811a84b 225 $scope.currentABTest.mailing_id_a = $scope.mailA.id;
493b91cf 226 }
227 });
228 };
229
7811a84b 230 $scope.append_mails = function () {
16ee60ab
TO
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 });
7811a84b 237 $scope.currentABTest.id = $scope.abId;
493b91cf 238 };
239
240 $scope.saveb = function (dat) {
493b91cf 241 var result = crmApi('Mailing', 'create', dat, true);
7811a84b 242 result.success(function (data) {
493b91cf 243 if (data.is_error == 0) {
244 $scope.mailB.id = data.id;
7811a84b 245 $scope.currentABTest.mailing_id_b = $scope.mailB.id;
679b3923 246 //$scope.append_mails();
493b91cf 247 }
248 });
fd843187 249 };
250
ef643544 251 $scope.savec = function (dat) {
ef643544 252 var result = crmApi('Mailing', 'create', dat, true);
679b3923 253
7811a84b 254 result.success(function (data) {
ef643544 255 if (data.is_error == 0) {
256 $scope.mailC.id = data.id;
7811a84b 257 $scope.currentABTest.mailing_id_c = $scope.mailC.id;
ef643544 258 $scope.append_mails();
ef643544 259 }
260 });
ef643544 261 };
ef643544 262
467cd00c 263 $scope.sparestuff.previewa = "";
7811a84b 264 $scope.pre = function () {
265 $scope.preview = true;
16ee60ab 266 };
fd843187 267
7811a84b 268 $scope.init = function (par) {
269 if (par == "3") {
467cd00c 270 $scope.sparestuff.template.url = partialUrl('from_name.html');
7811a84b 271 }
272 else {
273 if (par == "2") {
467cd00c 274 $scope.sparestuff.template.url = partialUrl('subject_lines.html');
7811a84b 275 }
276 else {
467cd00c 277 $scope.sparestuff.template.url = partialUrl('two_emails.html');
7811a84b 278 }
279 }
16ee60ab 280 $scope.whatnext = par.toString();
fd843187 281 };
7811a84b 282
283 $scope.tab_upd = function () {
64ccb448 284 $scope.tab_val = $scope.tab_val + 1;
7811a84b 285 $scope.max_tab = Math.max($scope.tab_val, $scope.max_tab);
16ee60ab 286 };
64ccb448 287
7811a84b 288 $scope.tab_upd_dec = function () {
64ccb448 289 $scope.tab_val = $scope.tab_val - 1;
16ee60ab 290 };
7811a84b 291
fd843187 292 $scope.setdate = function (par) {
fd843187 293 $scope.send_date = par;
294 $scope.dt = par;
295 $scope.apply();
296 };
297
7811a84b 298 $scope.testmailid = "";
fd843187 299 $scope.incGroup = [];
300 $scope.excGroup = [];
7811a84b 301 $scope.incGroupids = [];
302 $scope.excGroupids = [];
ef643544 303 $scope.tp1 = {};
7811a84b 304 $scope.create_abtest = function () {
493b91cf 305 var result;
467cd00c 306 $scope.currentABTest.testing_criteria_id = $scope.sparestuff.template.val;
493b91cf 307
7811a84b 308 if ($scope.abId == "") {
467cd00c 309 result = crmApi('MailingAB', 'create', {name: $scope.currentABTest.name, testing_criteria_id: $scope.sparestuff.template.val});
7811a84b 310 }
311 else {
312 if (typeof $scope.currentABTest.mailing_id_a == 'undefined') {
35565345 313 result = crmApi('MailingAB', 'create', {name: $scope.currentABTest.name, id: $scope.abId, testing_criteria_id: $scope.sparestuff.template.val});
7811a84b 314 }
315 else {
35565345 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});
493b91cf 317 }
493b91cf 318 }
319
7811a84b 320 result.success(function (data) {
493b91cf 321 if (data.is_error == 0) {
322 $scope.abId = data.id;
493b91cf 323 }
324 });
325 };
35565345 326 $scope.tokenfunc = function (elem, e, chng) {
467cd00c 327 var msg = document.getElementById(elem).value;
328 var cursorlen = document.getElementById(elem).selectionStart;
35565345 329 var textlen = msg.length;
467cd00c 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;
35565345 334 document.getElementById(elem).selectionEnd = cursorPos;
467cd00c 335 document.getElementById(elem).focus();
336 };
493b91cf 337
467cd00c 338 $scope.sparestuff.ingrps = "";
339 $scope.sparestuff.excgrps = "";
7811a84b 340 $scope.a_b_update = function () {
341 $scope.tp1.include = $scope.incGroupids;
342 $scope.tp1.exclude = $scope.excGroupids;
202ebbab 343 console.log($scope.tp1);
16ee60ab 344 crmApi('MailingAB', 'recipients_update', {
7811a84b 345 id: $scope.currentABTest.id,
ef643544 346 groups: $scope.tp1
347 });
348
7811a84b 349 var resulta = crmApi('Mailing', 'preview', {id: $scope.currentABTest.mailing_id_a});
ef643544 350
7811a84b 351 resulta.success(function (data) {
ef643544 352 if (data.is_error == 0) {
467cd00c 353 $scope.sparestuff.previewa = data.values.html;
ef643544 354 }
355 });
356
7811a84b 357 resulta = crmApi('Mailing', 'preview', {id: $scope.currentABTest.mailing_id_b});
ef643544 358
7811a84b 359 resulta.success(function (data) {
ef643544 360 if (data.is_error == 0) {
467cd00c 361 $scope.sparestuff.previewb = data.values.html;
ef643544 362 }
363 });
467cd00c 364
35565345 365 $scope.startabtest = function () {
2e84f38d 366 if (typeof $scope.sparestuff.date == 'undefined') {
367 $scope.sparestuff.date = 'now';
368 }
16ee60ab 369 crmApi('MailingAB', 'send_mail', {id: $scope.abId,
35565345 370 scheduled_date: $scope.sparestuff.date, scheduled_date_time: $scope.currentABTest.latertime});
16ee60ab 371 };
467cd00c 372
35565345
TO
373 angular.forEach($scope.incGroup, function (value) {
374 $scope.sparestuff.ingrps += value.toString() + ", ";
467cd00c 375 });
35565345
TO
376 angular.forEach($scope.excGroup, function (value) {
377 $scope.sparestuff.excgrps += value.toString() + ", ";
467cd00c 378 });
35565345
TO
379 if ($scope.sparestuff.ingrps.length != 0) {
380 $scope.sparestuff.ingrps = $scope.sparestuff.ingrps.substr(0, $scope.sparestuff.ingrps.length - 2);
467cd00c 381 }
35565345
TO
382 if ($scope.sparestuff.excgrps.length != 0) {
383 $scope.sparestuff.excgrps = $scope.sparestuff.excgrps.substr(0, $scope.sparestuff.excgrps.length - 2);
467cd00c 384 }
16ee60ab 385 };
202ebbab 386
7811a84b 387 $scope.update_abtest = function () {
388 $scope.currentABTest.declare_winning_time = $scope.currentABTest.date + " " + $scope.currentABTest.time;
16ee60ab 389 crmApi('MailingAB', 'create', {
2e84f38d 390 id: $scope.abId,
467cd00c 391 testing_criteria_id: $scope.sparestuff.template.val,
7811a84b 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,
35565345 395 specific_url: $scope.currentABTest.acturl,
7811a84b 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 });
41f6e155 400 };
467cd00c 401 $scope.currentABTest.latertime = "";
7811a84b 402 $scope.tmp = function (tst, aorb) {
403 if (aorb == 1) {
404 $scope.mailA.msg_template_id = tst;
7811a84b 405 if ($scope.mailA.msg_template_id == null) {
406 $scope.mailA.body_html = "";
35565345 407 $scope.mailA.subject = "";
41f6e155 408 }
7811a84b 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;
35565345
TO
413 if (typeof $scope.mailA.subject == 'undefined' || $scope.mailA.subject.length == 0) {
414 $scope.mailA.subject = $scope.tmpList[a].msg_subject;
415 }
41f6e155 416 }
417 }
418 }
419 }
7811a84b 420 else {
421 if (aorb == 2) {
422 $scope.mailB.msg_template_id = tst;
7811a84b 423 if ($scope.mailB.msg_template_id == null) {
424 $scope.mailB.body_html = "";
35565345 425 $scope.mailB.subject = "";
7811a84b 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;
35565345
TO
431 if (typeof $scope.mailB.subject == 'undefined' || $scope.mailB.subject.length == 0) {
432 $scope.mailB.subject = $scope.tmpList[a].msg_subject;
433 }
18882677 434
7811a84b 435 }
41f6e155 436 }
437 }
438 }
7811a84b 439 else {
440 $scope.mailA.msg_template_id = tst;
7811a84b 441 if ($scope.mailA.msg_template_id == null) {
442 $scope.mailA.body_html = "";
35565345 443 $scope.mailA.subject = "";
7811a84b 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;
35565345
TO
449 if (typeof $scope.mailA.subject == 'undefined' || $scope.mailA.subject.length == 0) {
450 $scope.mailA.subject = $scope.tmpList[a].msg_subject;
451 }
7811a84b 452 }
41f6e155 453 }
493b91cf 454 }
41f6e155 455
7811a84b 456 $scope.mailB.msg_template_id = tst;
7811a84b 457 if ($scope.mailB.msg_template_id == null) {
458 $scope.mailB.body_html = "";
35565345 459 $scope.mailB.subject = "";
18882677 460
7811a84b 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;
35565345
TO
466 if (typeof $scope.mailB.subject == 'undefined' || $scope.mailB.subject.length == 0) {
467 $scope.mailB.subject = $scope.tmpList[a].msg_subject;
468 }
18882677 469
7811a84b 470 }
41f6e155 471 }
472 }
473 }
7811a84b 474 }
16ee60ab 475 };
41f6e155 476
18882677 477 /*$scope.tmp = function (tst){
35565345
TO
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 };*/
18882677 496
202ebbab 497
7811a84b 498 $scope.$watch('preview', function () {
7811a84b 499 if ($scope.preview == true) {
500 $('#prevmail').dialog({
501 title: 'Preview Mailing',
008e95c9
V
502 width: 1000,
503 height: 500,
7811a84b 504 closed: false,
505 cache: false,
506 modal: true,
35565345 507 position: {
467cd00c 508 my: 'left',
509 at: 'top',
510 of: $(".crmABTestingAllTabs")
467cd00c 511 },
512
7811a84b 513 close: function () {
7811a84b 514 $scope.preview = false;
515 $scope.$apply();
516 }
517 });
467cd00c 518
519 $("#prevmail").dialog('option', 'position', [300, 50]);
493b91cf 520 }
467cd00c 521
7811a84b 522 }, true);
523
35565345 524 $scope.call = function () {
467cd00c 525 $scope.$apply();
16ee60ab 526 crmApi('Mailing', 'send_test', {
35565345
TO
527 mailing_id: $scope.currentABTest.mailing_id_a,
528 test_email: $scope.sparestuff.emailadd
467cd00c 529 });
530
16ee60ab 531 crmApi('Mailing', 'send_test', {
35565345
TO
532 mailing_id: $scope.currentABTest.mailing_id_b,
533 test_email: $scope.sparestuff.emailadd
467cd00c 534 })
16ee60ab 535 };
467cd00c 536
2e84f38d 537 $scope.$watch('sendtest', function () {
7811a84b 538 if ($scope.sendtest == true) {
539 $('#sendtest').dialog({
540 title: 'Send Test Mails',
467cd00c 541 width: 300,
542 height: 150,
7811a84b 543 closed: false,
544 cache: false,
545 modal: true,
546 buttons: {
547 'Send': function () {
467cd00c 548 $scope.call();
98a676d2 549 $scope.sendtest = false;
7811a84b 550 $('#sendtest').dialog("close");
493b91cf 551
7811a84b 552 }
553 },
554 close: function () {
98a676d2 555 $scope.sendtest = false;
7811a84b 556 $scope.$apply()
557 }
558 });
559 }
560 });
fd843187 561 });
562
679b3923 563
fd843187 564 crmMailingAB.directive('nexttab', function () {
565 return {
566 // Restrict it to be an attribute in this case
567 restrict: 'A',
fd843187 568 priority: 500,
569 // responsible for registering DOM listeners as well as updating the DOM
570 link: function (scope, element, attrs) {
571
7811a84b 572 var tabselector = $(".crmABTestingAllTabs");
573 tabselector.tabs(scope.$eval(attrs.nexttab));
7811a84b 574
575 // disable remaining tabs
35565345 576 if (scope.sparestuff.isnew == true) {
16ee60ab 577 tabselector.tabs({disabled: [1, 2, 3]});
35565345 578 }
fd843187 579
580 $(element).on("click", function () {
7811a84b 581 if (scope.tab_val == 0) {
493b91cf 582 scope.create_abtest();
583 }
7811a84b 584 else {
585 if (scope.tab_val == 2) {
586 scope.update_abtest();
587 if (scope.currentABTest.winner_criteria_id == 1) {
467cd00c 588 scope.sparestuff.winnercriteria = "Open";
589 scope.$apply();
41f6e155 590 }
7811a84b 591 else {
592 if (scope.currentABTest.winner_criteria_id == 2) {
467cd00c 593 scope.sparestuff.winnercriteria = " Total Unique Clicks";
594 scope.$apply();
7811a84b 595 }
596 else {
597 if (scope.currentABTest.winner_criteria_id == 3) {
467cd00c 598 scope.sparestuff.winnercriteria = "Total Clicks on a particular link";
599 scope.$apply();
7811a84b 600 }
601 }
602 }
603 scope.a_b_update();
604 }
41f6e155 605 }
64ccb448 606 scope.tab_upd();
fd843187 607
16ee60ab 608 var myArray1 = [];
fd843187 609 for (var i = scope.max_tab + 1; i < 4; i++) {
610 myArray1.push(i);
611 }
7811a84b 612 tabselector.tabs("option", "disabled", myArray1);
613 tabselector.tabs("option", "active", scope.tab_val);
fd843187 614 scope.$apply();
fd843187 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) {
fd843187 627 $(element).on("click", function () {
628 var temp = scope.tab_val - 1;
64ccb448 629 scope.tab_upd_dec();
630 scope.$apply();
7811a84b 631 if (temp != 3) {
632 $(".crmABTestingAllTabs").tabs("option", "active", temp);
fd843187 633 }
fd843187 634 scope.$apply();
fd843187 635 });
636 }
637 };
638 });
639
493b91cf 640 crmMailingAB.directive('chsgroup', function () {
fd843187 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(" ");
f27a0a8e 651 if (a[1] == "civicrm_group" && a[2] == "include") {
fd843187 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 }
f27a0a8e 654 if (a[1] == "civicrm_group" && a[2] == "exclude") {
fd843187 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 }
f27a0a8e 657 if (a[1] == "civicrm_mailing" && a[2] == "include") {
fd843187 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 }
f27a0a8e 660 if (a[1] == "civicrm_mailing" && a[2] == "exclude") {
fd843187 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
f27a0a8e 665
fd843187 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 }
f27a0a8e 674 }).select2("data", scope.sparestuff.allgroups);
675
fd843187 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);
ef643544 687 scope.incGroupids.push(a[0]);
fd843187 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);
ef643544 699 scope.excGroupids.push(a[0]);
fd843187 700 scope.$apply();
701 }
c2ede705 702
f27a0a8e 703 scope.$apply();
fd843187 704
705 });
706 $(element).on("select2-removed", function (e) {
707 if (e.val.split(" ")[2] == "exclude") {
16ee60ab
TO
708 var excIndex = scope.excGroup.indexOf(e.val.split(" ")[3]);
709 scope.excGroup.splice(excIndex, 1);
710 scope.excGroupids.splice(excIndex, 1);
fd843187 711 scope.$apply();
712 }
713 else {
16ee60ab
TO
714 var incIndex = scope.incGroup.indexOf(e.val.split(" ")[3]);
715 scope.incGroup.splice(incIndex, 1);
716 scope.incGroupids.splice(incIndex, 1);
fd843187 717 scope.$apply();
718 }
c2ede705 719
f27a0a8e 720 scope.$apply();
fd843187 721 });
722 }
723 };
fd843187 724 });
725
726 crmMailingAB.directive('sliderbar', function () {
727 return{
728 restrict: 'AE',
729 link: function (scope, element, attrs) {
7811a84b 730 if (typeof scope.currentABTest.group_percentage != 'undefined') {
7811a84b 731 $(element).slider({value: scope.currentABTest.group_percentage});
41f6e155 732 }
fd843187 733 $(element).slider({min: 1});
734 $(element).slider({
735 slide: function (event, ui) {
41f6e155 736 scope.currentABTest.group_percentage = ui.value;
fd843187 737 scope.$apply();
738 }
739 });
740 }
741 };
742 });
743
fd843187 744 crmMailingAB.directive('datepick', function () {
745 return {
fd843187 746 restrict: 'AE',
747 link: function (scope, element, attrs) {
748 $(element).datepicker({
467cd00c 749 dateFormat: "dd-mm-yy",
fd843187 750 onSelect: function (date) {
751 $(".ui-datepicker a").removeAttr("href");
467cd00c 752 scope.sparestuff.date = date.toString();
fd843187 753 scope.$apply();
fd843187 754 }
fd843187 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 () {
493b91cf 766 scope.savea({
767 id: scope.mailA.id,
e217e2ef 768 name: scope.currentABTest.name + ' (AB test A)',
7811a84b 769 visibility: scope.mailA.visibility,
493b91cf 770 created_id: 1,
771 subject: scope.mailA.subject,
7811a84b 772 msg_template_id: scope.mailA.msg_template_id == null ? "" : scope.mailA.msg_template_id,
493b91cf 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,
7811a84b 786 campaign_id: scope.mailA.campaign_id == null ? "" : scope.mailA.campaign_id,
787 header_id: scope.mailA.header_id,
493b91cf 788 footer_id: scope.mailA.footer_id,
493b91cf 789 is_completed: scope.mailA.is_completed
fd843187 790 });
0838af8e 791
fd843187 792 if (scope.whatnext == "3") {
7811a84b 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;
7811a84b 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;
fd843187 813 }
814 else {
815 if (scope.whatnext == "2") {
816 scope.mailB.fromEmail = scope.mailA.fromEmail;
7811a84b 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;
7811a84b 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;
fd843187 836 }
837 }
493b91cf 838 scope.saveb({
493b91cf 839 id: scope.mailB.id,
e217e2ef 840 name: scope.currentABTest.name + ' (AB test B)',
7811a84b 841 visibility: scope.mailB.visibility,
493b91cf 842 created_id: 1,
843 subject: scope.mailB.subject,
7811a84b 844 msg_template_id: scope.mailB.msg_template_id == null ? "" : scope.mailB.msg_template_id,
493b91cf 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,
493b91cf 856 scheduled_id: scope.mailB.scheduled_id,
7811a84b 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,
493b91cf 860 is_completed: scope.mailA.is_completed
fd843187 861 });
7811a84b 862
ef643544 863 scope.savec({
864 id: scope.mailC.id,
e217e2ef 865 name: scope.currentABTest.name + ' (AB test winner)',
7811a84b 866 visibility: scope.mailB.visibility,
ef643544 867 created_id: 1,
868 subject: scope.mailB.subject,
7811a84b 869 msg_template_id: scope.mailB.msg_template_id == null ? "" : scope.mailB.msg_template_id,
ef643544 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,
7811a84b 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,
679b3923 884 is_completed: scope.mailA.is_completed,
202ebbab 885 'api.mailing_job.create': 0
ef643544 886 });
fd843187 887 });
888 }
889 };
fd843187 890 });
891
7811a84b 892 crmMailingAB.directive('chsdate', function () {
fd843187 893 return {
894 restrict: 'AE',
7811a84b 895 link: function (scope, element, attrs) {
41f6e155 896 $(element).datepicker({
897 dateFormat: "yy-mm-dd",
7811a84b 898 onSelect: function (date) {
41f6e155 899 $(".ui-datepicker a").removeAttr("href");
7811a84b 900 scope.currentABTest.date = date.toString();
41f6e155 901 scope.$apply();
41f6e155 902 }
903 });
904 }
905 };
906 });
907
fd843187 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>'
fd843187 916 };
917 });
ec57ad65 918
35565345 919 crmMailingAB.directive('groupselect', function () {
467cd00c 920 return {
35565345
TO
921 restrict: 'AE',
922 link: function (scope, element, attrs) {
008e95c9
V
923 $(element).select2({
924 width: "200px",
925 data: CRM.crmMailing.mailTokens,
926 placeholder: "Insert Token"
927 });
35565345 928 $(element).on('select2-selecting', function (e) {
467cd00c 929
121d1705 930 scope.$evalAsync('_resetSelection()');
467cd00c 931 var a = $(element).attr('id');
35565345
TO
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 }
467cd00c 985
467cd00c 986 scope.$apply();
987 e.preventDefault();
988 })
989 }
990 };
991 });
992
64e42c42
JP
993 crmMailingAB.directive('replytoselect',function(){
994 return {
995 restrict : 'AE',
996 link: function(scope, element, attrs){
7e6e8bd6
TO
997
998 function format(item){
64e42c42
JP
999 return item.label.replace(/&/g, '&amp;')
1000 .replace(/"/g, '&quot;')
1001 .replace(/</g, '&lt;')
1002 .replace(/>/g, '&gt;');
1003 }
7e6e8bd6 1004
64e42c42
JP
1005 CRM.api3('OptionGroup', 'get', {
1006 "sequential": 1,
1007 "name": "from_email_address"
1008 }).done(function(result) {
1009 var emailGroupId = result.id;
64e42c42
JP
1010 CRM.api3('OptionValue', 'get', {
1011 "sequential": 1,
1012 "option_group_id": result.id
1013 }).done(function(orgEmails) {
7e6e8bd6 1014
64e42c42 1015 //$sce.trustAsHtml(
7e6e8bd6 1016
008e95c9
V
1017 $(element).select2({
1018 class: "abtesting-form-element",
1019 data: orgEmails.values,
1020 formatResult: format,
64e42c42
JP
1021 formatSelection: format,
1022 placeholder:"Select reply to address"});
64e42c42 1023
7e6e8bd6
TO
1024 });
1025 });
64e42c42
JP
1026 }
1027 };
1028 });
1029
1030 crmMailingAB.directive('stopa',function(){
202ebbab 1031 return {
35565345
TO
1032 restrict: 'AE',
1033 link: function (scope, element, attrs) {
202ebbab 1034 scope.$watch('aastop', function () {
35565345 1035 if (scope.aastop == true) {
202ebbab 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 },
35565345
TO
1049 'No': function () {
1050 scope.aastop = false;
202ebbab 1051 $(element).dialog("close");
1052 }
1053 },
1054 close: function () {
35565345 1055 scope.aastop = false;
202ebbab 1056
1057 scope.$apply();
1058 }
1059 });
1060 }
1061 });
1062 }
1063 }
1064 });
1065
35565345 1066 crmMailingAB.directive('stopb', function () {
202ebbab 1067 return {
35565345
TO
1068 restrict: 'AE',
1069 link: function (scope, element, attrs) {
202ebbab 1070
35565345
TO
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");
202ebbab 1089 }
35565345
TO
1090 },
1091 close: function () {
1092 scope.bbstop = false;
1093
1094 scope.$apply();
1095 }
1096 });
1097 }
1098 });
202ebbab 1099 }
1100 }
1101 });
1102
1103
35565345 1104 crmMailingAB.directive('checktimeentry', function () {
467cd00c 1105 return {
35565345 1106 restrict: 'AE',
467cd00c 1107 link: function (scope, element, attrs) {
008e95c9
V
1108 $(element).timeEntry({
1109 show24Hours: true,
1110 showSeconds: true,
1111 });
467cd00c 1112 }
1113 }
202ebbab 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 }
467cd00c 1178
1179 });
1180
ec57ad65 1181})(angular, CRM.$, CRM._);