CRM-15578 - Mailing API - Extend test for "preview" action:
[civicrm-core.git] / js / angular-Mailing.js
CommitLineData
40f603d5 1(function (angular, $, _) {
ab953923 2//partials for the html pages
40f603d5 3 var partialUrl = function (relPath) {
ab953923
SR
4 return CRM.resourceUrls['civicrm'] + '/partials/crmMailing/' + relPath;
5 };
6
40f603d5 7 var crmMailing = angular.module('crmMailing', ['ngRoute', 'ui.utils', 'ngSanitize']);
ab953923 8 var chck = []; //to fill the group variable $scope.incGroup
40f603d5 9 var chck2 = []; // to get id and text in the required format
f97e2426
SR
10 var mltokens = []; //we store list of the tokens in this
11 var global = 0; //use this to reload mailingList page once
4198cf0b 12
ab953923
SR
13//-------------------------------------------------------------------------------------------------------
14 crmMailing.config(['$routeProvider',
40f603d5 15 function ($routeProvider) {
ab953923
SR
16 $routeProvider.when('/mailing', {
17 templateUrl: partialUrl('mailingList.html'),
18 controller: 'mailingListCtrl',
19 resolve: {
40f603d5 20 mailingList: function ($route, crmApi) {
ab953923
SR
21 return crmApi('Mailing', 'get', {});
22 }
23
24 }
25 }); //This route is used for generating the list of mails created.
26
ab953923
SR
27 $routeProvider.when('/mailing/:id', {
28 templateUrl: partialUrl('main.html'),
29 controller: 'mailingCtrl',
30 resolve: {
40f603d5
TO
31 selectedMail: function ($route, crmApi) {
32 if ($route.current.params.id !== 'new') {
ab953923
SR
33 return crmApi('Mailing', 'getsingle', {id: $route.current.params.id});
34 }
35 else {
f97e2426 36 //selected mail in case of a new mailing. some default values are set
40f603d5 37 return {visibility: "Public Pages", url_tracking: "1", dedupe_email: "1", forward_replies: "0", auto_responder: "0", open_tracking: "1"
ab953923
SR
38 };
39 }
40 }
41 }
42 }); //This route is used for creating new mails and editing the current mails
43 }
44 ]);
45//-----------------------------------------
46
47
48//This controller is used in creating new mail and editing current mails
40f603d5 49 crmMailing.controller('mailingCtrl', function ($scope, crmApi, selectedMail, $location, $route, $sce, $window) {
ab953923
SR
50
51 //setting variables to the values we have got to the api
ab953923 52 $scope.partialUrl = partialUrl;
40f603d5 53 $scope.campaignList = CRM.crmMailing.campNames;
ab953923
SR
54 $scope.mailList = CRM.crmMailing.civiMails;
55 $scope.mailNameList = _.pluck(CRM.crmCaseType.civiMails, 'name');
56 $scope.groupNamesList = CRM.crmMailing.groupNames;
57 $scope.headerfooter = CRM.crmMailing.headerfooterList;
58 $scope.fromAddress = CRM.crmMailing.fromAddress;
59 $scope.tmpList = CRM.crmMailing.mesTemplate;
60 $scope.mailingGrp = CRM.crmMailing.mailGrp;
61 $scope.user_id = CRM.crmMailing.contactid;
f97e2426
SR
62 mltokens = CRM.crmMailing.mailTokens;
63 //set currentMailing to selectedMail
ab953923
SR
64 $scope.currentMailing = selectedMail;
65 $scope.currentMailing.created_id = $scope.user_id;
f97e2426 66 //when pre is true, preview mailing is opened
ab953923 67 $scope.pre = false;
f97e2426 68 //counts number of recipients
ab953923 69 $scope.noOfRecipients = 0;
f97e2426 70 //object testMailing for testMailing.name and testMailing.group
ab953923
SR
71 $scope.testMailing = {};
72 $scope.testMailing.name = "";
73 $scope.testMailing.group = "";
74 window.ct = $scope.currentMailing;
f97e2426 75 //previewbody_html stores value of HTML in editor, similar functionality of text and subject based on name
ab953923
SR
76 $scope.previewbody_html = "";
77 $scope.previewbody_text = "";
78 $scope.preview_subject = "";
f97e2426 79 //object for tokens
ab953923 80 $scope.token = "";
f97e2426 81 //chck and chck2 are used for mailing groups
ab953923
SR
82 chck = [];
83 chck2 = [];
f97e2426
SR
84 //from.name stores name of the sender, email stores the mail address of the sender, total stores the concatenation of the two
85 //reply.email stores the reply to email address
ab953923
SR
86 $scope.from = {};
87 $scope.from.name = "";
88 $scope.from.email = "";
89 $scope.from.total = "";
40f603d5 90 $scope.reply = {};
ab953923 91 $scope.reply.email = "";
f97e2426 92 //replyaddress is the array used to save the set of fromemailAddress of the person
ab953923 93 $scope.replyaddress = [];
40f603d5 94 for (var a in $scope.fromAddress) {
ab953923
SR
95 var b = {};
96 var splt = $scope.fromAddress[a].label.split(" ");
40f603d5 97 splt = splt[1].substring(1, (splt[1].length - 1));
ab953923
SR
98 b.email = splt;
99 $scope.replyaddress.push(b);
100 }
f97e2426 101 //from.total retrieves the from.name + from.email value so it can be read by the select2 widget
ab953923
SR
102 if ($scope.currentMailing.from_name != null) {
103 $scope.from.name = $scope.currentMailing.from_name;
104 $scope.from.email = $scope.currentMailing.from_email;
40f603d5 105 $scope.from.total = '"' + $scope.from.name + '"' + " <" + $scope.from.email + ">";
ab953923
SR
106 $scope.reply.email = $scope.currentMailing.replyto_email;
107 }
108
109 $scope.mailid = [];
f97e2426 110 //putting all the ids of mails corresponding to the current mailing in mailid.
ab953923 111 for (var a in $scope.mailingGrp) {
40f603d5
TO
112 if ($scope.mailingGrp[a].mailing_id == $scope.currentMailing.id) {
113 var b = $scope.mailingGrp[a].entity_id + " " + $scope.mailingGrp[a].entity_table + " " + $scope.mailingGrp[a].group_type;
ab953923
SR
114 var c = $scope.mailingGrp[a].id;
115 chck.push(b);
116 $scope.mailid.push(c);
117 }
118 }
119
f97e2426
SR
120 //used to put the Template id in tst so it can be used
121
40f603d5
TO
122 if ($scope.currentMailing.msg_template_id != null) {
123 $scope.tst = $scope.currentMailing.msg_template_id;
ab953923 124 }
f97e2426 125 //Making the object for data in the mailing group related directive
ab953923
SR
126
127 for (var a in chck) {
40f603d5 128 var b = {};
ab953923
SR
129 b.id = chck[a];
130 var splt = chck[a].split(" ");
131
40f603d5
TO
132 if (splt[1] == "civicrm_group") {
133 for (var c in $scope.groupNamesList) {
134 if ($scope.groupNamesList[c].id == splt[0]) {
ab953923
SR
135 b.text = $scope.groupNamesList[c].title;
136 }
137 }
138 }
40f603d5
TO
139 if (splt[1] == "civicrm_mailing") {
140 for (var c in $scope.mailList) {
141 if ($scope.mailList[c].id == splt[0]) {
ab953923
SR
142 b.text = $scope.mailList[c].name;
143 }
144 }
145 }
146 chck2.push(b);
147 }
148 $scope.incGroup = chck2;
f97e2426 149 //tabact is set to 0. stores value of active tab
4198cf0b 150 $scope.tabact = 0;
f97e2426
SR
151 //all tabs with tab index greater than maxtab will be disabled. if not a new mail, no tab will be disabled
152 //otherwise all except first tab disabled
40f603d5 153 if ($scope.currentMailing.id != null)
4198cf0b
SR
154 $scope.maxtab = 3;
155 $scope.maxtab = 0;
156
f97e2426 157 //increments active tab value and maxtab when next is clicked
40f603d5 158 $scope.tabupdate = function () {
4198cf0b 159 $scope.tabact = $scope.tabact + 1;
40f603d5 160 $scope.maxtab = Math.max($scope.maxtab, $scope.tabact);
4198cf0b 161 }
f97e2426 162 //decrements active tab value
40f603d5 163 $scope.prevtabupdate = function () {
4198cf0b 164 $scope.tabact = $scope.tabact - 1;
4198cf0b 165 }
f97e2426 166 //set active tab to 0 when recipient is clicked
40f603d5 167 $scope.recclicked = function () {
f97e2426 168 $scope.tabact = 0;
ab953923 169
ab953923 170 };
f97e2426
SR
171 //set active tab to 1 when content is clicked
172 //also call appropriate save function if clicked from recipient tab
40f603d5
TO
173 $scope.conclicked = function () {
174 if ($scope.tabact == 0)
f97e2426
SR
175 $scope.save_next_page1();
176 $scope.tabact = 1;
177 };
178 //set active tab to 2 when schedule and send is clicked
179 //also call appropriate save functions if clicked from recipient or content tab
40f603d5
TO
180 $scope.schedclicked = function () {
181 if ($scope.tabact == 0)
f97e2426 182 $scope.save_next_page1();
40f603d5 183 else if ($scope.tabact == 1)
f97e2426
SR
184 $scope.save_next_page2();
185 $scope.tabact = 2;
186 };
187 //goes to the mailing list page
40f603d5
TO
188 $scope.back = function () {
189 $window.location.href = "#/mailing";
ab953923
SR
190 $route.reload();
191 };
f97e2426 192 //on changing the selected template, updates subject and body_html of currentMailing
40f603d5
TO
193 $scope.tmp = function (tst) {
194 $scope.currentMailing.msg_template_id = tst;
195 if ($scope.currentMailing.msg_template_id == null) {
196 $scope.currentMailing.body_html = "";
197 $scope.currentMailing.subject = "";
ab953923 198 }
40f603d5
TO
199 else {
200 for (var a in $scope.tmpList) {
ab953923 201
40f603d5
TO
202 if ($scope.tmpList[a].id == $scope.currentMailing.msg_template_id) {
203 $scope.currentMailing.body_html = $scope.tmpList[a].msg_html;
204 $scope.currentMailing.subject = $scope.tmpList[a].msg_subject;
ab953923
SR
205 }
206 }
207 }
208 };
209 //initializing variables we will use for checkboxes, or for purpose of ng-show
40f603d5
TO
210 $scope.composeS = "1";
211 if ($scope.currentMailing.forward_replies == 0 && $scope.currentMailing.auto_responder == 0) {
212 $scope.trackreplies = "0";
ab953923
SR
213 }
214 else {
40f603d5 215 $scope.trackreplies = "1";
ab953923 216 }
40f603d5
TO
217 if ($scope.currentMailing.scheduled_date == null || $scope.currentMailing.scheduled_date == "")
218 $scope.now = "1";
ab953923 219 else
40f603d5 220 $scope.now = "0";
ab953923 221
40f603d5
TO
222 $scope.reply = function () {
223 if ($scope.trackreplies == 0) {
224 $scope.trackreplies = 1;
ab953923 225 }
40f603d5
TO
226 else {
227 $scope.trackreplies = 0;
228 $scope.currentMailing.forward_replies = 0;
229 $scope.currentMailing.auto_responder = 0;
ab953923
SR
230 }
231 }
232
f97e2426 233 //joining scheddate.date and scheddate.time gives us scheduled date of current mailing
40f603d5 234 $scope.scheddate = {};
ab953923
SR
235 $scope.scheddate.date = "";
236 $scope.scheddate.time = "";
f97e2426 237 //checkNow decides whether we see the date picker or not. Depends on send immediately being on or off
40f603d5
TO
238 $scope.checkNow = function () {
239 if ($scope.now == 1) {
ab953923
SR
240 $scope.now = 0;
241 }
40f603d5 242 else {
ab953923
SR
243 $scope.now = 1;
244 $scope.currentMailing.scheduled_date = null;
245 $scope.currentMailing.scheduled_id = null;
246 $scope.scheddate.date = "";
247 $scope.scheddate.time = "";
248 }
f97e2426 249 };
ab953923 250 //changing the screen from compose on screen to upload content
40f603d5
TO
251 $scope.upldChange = function (composeS) {
252 if (composeS == "1") {
ab953923
SR
253 return true;
254 }
255 else
256 return false;
257 };
258 //filter so we only get headers from mailing component
40f603d5 259 $scope.isHeader = function (hf) {
ab953923
SR
260 return hf.component_type == "Header";
261 };
262 //filter so we only get footers from mailing component
40f603d5 263 $scope.isFooter = function (f) {
ab953923
SR
264 return f.component_type == "Footer";
265 };
266 //filter so we only get auto-Responders from mailing component
40f603d5 267 $scope.isAuto = function (au) {
ab953923
SR
268 return au.component_type == "Reply";
269 };
270 //filter so we only get userDriven message templates
40f603d5
TO
271 $scope.isUserDriven = function (mstemp) {
272 return (parseInt(mstemp.id) > 58);
ab953923
SR
273 };
274 //used for ng-show when trackreplies is selected. Only then we show forward replies and auto-responders options
40f603d5
TO
275 $scope.trackr = function (trackreplies) {
276 if (trackreplies == "1") {
ab953923
SR
277 return true;
278 }
279 else
280 return false;
281 }
f97e2426 282 //we should only see groups with appropriate mailing visibility
40f603d5 283 $scope.isGrp = function (grp) {
ab953923
SR
284 return grp.visibility == "Public Pages";
285 };
f97e2426 286 //only completed mails are shown in the groupings
40f603d5 287 $scope.isCompMail = function (ml) {
ab953923
SR
288 return ml.is_completed == 1;
289 };
f97e2426 290 //This is used to open the update the values in preview mailing
40f603d5
TO
291 $scope.preview_update = function () {
292 var resulta = crmApi('Mailing', 'preview', {id: $scope.currentMailing.id});
293 resulta.success(function (data) {
ab953923 294 if (data.is_error == 0) {
40f603d5
TO
295 $scope.previewbody_html = data.values.html;
296 $scope.previewbody_text = data.values.text;
297 $scope.preview_subject = data.values.subject;
ab953923
SR
298 $scope.pre = true;
299 $scope.$digest();
300 $scope.$apply();
301 }
302 });
303 };
f97e2426 304 //checks if body_text is empty or not
40f603d5
TO
305 $scope.isBody_text = function () {
306 if ($scope.currentMailing.body_text == null || $scope.currentMailing.body_text == "")
ab953923
SR
307 return false;
308 else
309 return true;
310 };
f97e2426 311 //parses html
40f603d5 312 $scope.deliberatelyTrustDangerousSnippet = function () {
ab953923
SR
313 return $sce.trustAsHtml($scope.previewbody_html);
314 };
315
40f603d5 316 $scope.deliberatelyTrustDangerousSnippet2 = function () {
ab953923
SR
317 return $sce.trustAsHtml($scope.previewbody_text);
318 };
319
40f603d5 320 $scope.deliberatelyTrustDangerousSnippet3 = function () {
ab953923
SR
321 return $sce.trustAsHtml($scope.preview_subject);
322 };
f97e2426 323 //gets number of mailing recipients
40f603d5
TO
324 $scope.mailing_recipients = function () {
325 var resulta = crmApi('MailingRecipients', 'get', {mailing_id: $scope.currentMailing.id, options: {limit: 1000}});
326 resulta.success(function (data) {
ab953923 327 if (data.is_error == 0) {
40f603d5 328 $scope.noOfRecipients = data.count;
ab953923
SR
329 $scope.$digest();
330 $scope.$apply();
331 }
332 });
333 }
f97e2426 334 //gets mailing groups associated with current mailing id. formats it in the required way
40f603d5
TO
335 $scope.mailingGroup = function () {
336 var resulta = crmApi('MailingGroup', 'get', {mailing_id: $scope.currentMailing.id, options: {limit: 1000}})
337 resulta.success(function (data) {
ab953923
SR
338 $scope.mailid = [];
339 chck = [];
40f603d5
TO
340 angular.forEach(data.values, function (value, key) {
341 var b = value.entity_id + " " + value.entity_table + " " + value.group_type;
ab953923
SR
342 var c = value.id;
343 chck.push(b);
344 $scope.mailid.push(c);
345 });
40f603d5 346 for (var a in chck)
7e6e8bd6 347 {
40f603d5 348 var b = {}
ab953923
SR
349 b.id = chck[a];
350 var splt = chck[a].split(" ");
351
40f603d5
TO
352 if (splt[1] == "civicrm_group") {
353 for (var c in $scope.groupNamesList) {
354 if ($scope.groupNamesList[c].id == splt[0]) {
ab953923
SR
355 b.text = $scope.groupNamesList[c].title;
356 }
357 }
358 }
40f603d5
TO
359 if (splt[1] == "civicrm_mailing") {
360 for (var c in $scope.mailList) {
361 if ($scope.mailList[c].id == splt[0]) {
ab953923
SR
362 b.text = $scope.mailList[c].name;
363 }
364 }
365 }
366 chck2.push(b);
367 }
368
369 $scope.incGroup = chck2;
370 $scope.mailing_recipients();
371 });
372 }
f97e2426 373 //save repeat is a function that is repeated in all versions of save
40f603d5 374 $scope.save_repeat = function () {
ab953923
SR
375 if ($scope.from.total != "") {
376 var splt = $scope.from.total.split(" ");
40f603d5
TO
377 var splta = splt[1].substring(1, (splt[1].length - 1));
378 var spltb = splt[0].substring(1, (splt[0].length - 1));
ab953923
SR
379 $scope.currentMailing.from_email = splta;
380 $scope.currentMailing.from_name = spltb;
381 }
ab953923 382 }
f97e2426 383 //save_next is used in recipient page. stores mailing groups
40f603d5 384 $scope.save_next = function () {
ab953923 385 $scope.save_repeat();
f97e2426 386 //splits the groups based on include exclude mailing and groups
40f603d5
TO
387 $scope.incGrp = [];
388 $scope.excGrp = [];
389 $scope.incMail = [];
390 $scope.excMail = [];
391 $scope.answer = "";
392 for (req_id in chck) {
ab953923
SR
393 $scope.answer = chck[req_id].split(" ");
394
40f603d5 395 if ($scope.answer[1] == "civicrm_mailing" && $scope.answer[2] == "include") {
ab953923
SR
396 $scope.incMail.push($scope.answer[0]);
397 }
40f603d5 398 else if ($scope.answer[1] == "civicrm_mailing" && $scope.answer[2] == "exclude") {
ab953923
SR
399 $scope.excMail.push($scope.answer[0]);
400 }
40f603d5 401 if ($scope.answer[1] == "civicrm_group" && $scope.answer[2] == "include") {
ab953923
SR
402 $scope.incGrp.push($scope.answer[0]);
403 }
40f603d5 404 else if ($scope.answer[1] == "civicrm_group" && $scope.answer[2] == "exclude") {
ab953923
SR
405 $scope.excGrp.push($scope.answer[0]);
406 }
407 }
f97e2426 408 //deletes existing mailing groups of that id
ab953923
SR
409 if ($scope.mailid != null) {
410 for (var a in $scope.mailid) {
40f603d5 411 var result_2 = crmApi('MailingGroup', 'delete', {
ab953923
SR
412 id: $scope.mailid[a]
413 });
414 }
415 }
416
417 var result = crmApi('Mailing', 'create', {
40f603d5
TO
418 id: $scope.currentMailing.id,
419 name: $scope.currentMailing.name,
420 visibility: $scope.currentMailing.visibility,
421 created_id: $scope.currentMailing.created_id,
422 subject: $scope.currentMailing.subject,
423 msg_template_id: $scope.currentMailing.msg_template_id == null ? "" : $scope.currentMailing.msg_template_id,
424 open_tracking: $scope.currentMailing.open_tracking,
425 url_tracking: $scope.currentMailing.url_tracking,
426 forward_replies: $scope.currentMailing.forward_replies,
427 auto_responder: $scope.currentMailing.auto_responder,
428 from_name: $scope.currentMailing.from_name,
429 from_email: $scope.currentMailing.from_email,
430 replyto_email: $scope.currentMailing.replyto_email,
431 unsubscribe_id: $scope.currentMailing.unsubscribe_id,
432 resubscribe_id: $scope.currentMailing.resubscribe_id,
433 body_html: $scope.currentMailing.body_html,
434 body_text: $scope.currentMailing.body_text,
435 campaign_id: $scope.currentMailing.campaign_id == null ? "" : $scope.currentMailing.campaign_id,
436 header_id: $scope.currentMailing.header_id,
437 footer_id: $scope.currentMailing.footer_id,
438 groups: {include: $scope.incGrp,
439 exclude: $scope.excGrp
ab953923 440 },
40f603d5
TO
441 mailings: {include: $scope.incMail,
442 exclude: $scope.excMail
443 },
444 is_completed: $scope.currentMailing.is_completed,
445 dedupe_email: $scope.currentMailing.dedupe_email
446 },
447 true);
ab953923 448 //var result = crmApi('Mailing', 'create', $scope.currentMailing, true);
40f603d5 449 result.success(function (data) {
ab953923
SR
450 if (data.is_error == 0) {
451 $scope.currentMailing.id = data.id;
452 $scope.mailingGroup();
453 }
454 });
455 };
f97e2426 456 //in second page save neither groups or schedule date information.
40f603d5 457 $scope.save_next_page2 = function () {
ab953923 458 $scope.save_repeat();
4066c991 459 var result = crmApi('Mailing', 'create', {
40f603d5
TO
460 id: $scope.currentMailing.id,
461 name: $scope.currentMailing.name,
462 visibility: $scope.currentMailing.visibility,
463 created_id: $scope.currentMailing.created_id,
464 subject: $scope.currentMailing.subject,
465 msg_template_id: $scope.currentMailing.msg_template_id == null ? "" : $scope.currentMailing.msg_template_id,
466 open_tracking: $scope.currentMailing.open_tracking,
467 url_tracking: $scope.currentMailing.url_tracking,
468 forward_replies: $scope.currentMailing.forward_replies,
469 auto_responder: $scope.currentMailing.auto_responder,
470 from_name: $scope.currentMailing.from_name,
471 from_email: $scope.currentMailing.from_email,
472 replyto_email: $scope.currentMailing.replyto_email,
473 unsubscribe_id: $scope.currentMailing.unsubscribe_id,
474 resubscribe_id: $scope.currentMailing.resubscribe_id,
475 body_html: $scope.currentMailing.body_html,
476 body_text: $scope.currentMailing.body_text,
477 campaign_id: $scope.currentMailing.campaign_id == null ? "" : $scope.currentMailing.campaign_id,
478 header_id: $scope.currentMailing.header_id,
479 footer_id: $scope.currentMailing.footer_id,
480 is_completed: $scope.currentMailing.is_completed,
481 dedupe_email: $scope.currentMailing.dedupe_email
482 },
483 true);
4066c991 484 //var result = crmApi('Mailing', 'create', $scope.currentMailing, true);
40f603d5 485 result.success(function (data) {
4066c991
SR
486 if (data.is_error == 0) {
487 $scope.currentMailing.id = data.id;
488 }
489 });
490 };
f97e2426 491 //save scheduling information and not groups
40f603d5 492 $scope.save_next_page3 = function () {
4066c991
SR
493 $scope.save_repeat();
494 if ($scope.currentMailing.scheduled_date == $scope.currentMailing.approval_date
40f603d5
TO
495 && $scope.currentMailing.scheduled_date != null) {
496 ;
4066c991
SR
497 }
498 else {
40f603d5
TO
499 $scope.currentMailing.scheduled_date = $scope.scheddate.date + " " + $scope.scheddate.time;
500 if ($scope.currentMailing.scheduled_date != " ") {
501 $scope.currentMailing.scheduled_id = $scope.user_id;
4066c991
SR
502 }
503 else {
40f603d5 504 $scope.currentMailing.scheduled_date = null;
4066c991
SR
505 }
506 }
ab953923 507 var result = crmApi('Mailing', 'create', {
40f603d5
TO
508 id: $scope.currentMailing.id,
509 name: $scope.currentMailing.name,
510 visibility: $scope.currentMailing.visibility,
511 created_id: $scope.currentMailing.created_id,
512 subject: $scope.currentMailing.subject,
513 msg_template_id: $scope.currentMailing.msg_template_id == null ? "" : $scope.currentMailing.msg_template_id,
514 open_tracking: $scope.currentMailing.open_tracking,
515 url_tracking: $scope.currentMailing.url_tracking,
516 forward_replies: $scope.currentMailing.forward_replies,
517 auto_responder: $scope.currentMailing.auto_responder,
518 from_name: $scope.currentMailing.from_name,
519 from_email: $scope.currentMailing.from_email,
520 replyto_email: $scope.currentMailing.replyto_email,
521 unsubscribe_id: $scope.currentMailing.unsubscribe_id,
522 resubscribe_id: $scope.currentMailing.resubscribe_id,
523 body_html: $scope.currentMailing.body_html,
524 body_text: $scope.currentMailing.body_text,
525 scheduled_date: $scope.currentMailing.scheduled_date == null ? "" : $scope.currentMailing.scheduled_date,
526 scheduled_id: $scope.currentMailing.scheduled_id == null ? "" : $scope.currentMailing.scheduled_id,
527 campaign_id: $scope.currentMailing.campaign_id == null ? "" : $scope.currentMailing.campaign_id,
528 header_id: $scope.currentMailing.header_id,
529 footer_id: $scope.currentMailing.footer_id,
530 is_completed: $scope.currentMailing.is_completed,
531 approver_id: $scope.currentMailing.approver_id,
532 approval_status_id: $scope.currentMailing.approval_status_id,
533 approval_date: $scope.currentMailing.approval_date,
534 dedupe_email: $scope.currentMailing.dedupe_email
535 },
536 true);
ab953923 537 //var result = crmApi('Mailing', 'create', $scope.currentMailing, true);
40f603d5 538 result.success(function (data) {
ab953923
SR
539 if (data.is_error == 0) {
540 $scope.currentMailing.id = data.id;
541 }
542 });
543 };
544
f97e2426 545 //call save of page2 and go to listing page
40f603d5 546 $scope.save = function () {
ab953923
SR
547 $scope.save_next_page2();
548 $scope.back();
549 };
f97e2426 550 //call save of page1 and go to listing page
40f603d5 551 $scope.save_page1 = function () {
ab953923
SR
552 $scope.save_next_page1();
553 $scope.back();
554 };
f97e2426 555 //if we save on page one, subject isnt defined. so we equate it to mailing name for now so we can save
40f603d5
TO
556 $scope.save_next_page1 = function () {
557 if ($scope.currentMailing.id == null) {
ab953923
SR
558 $scope.currentMailing.subject = $scope.currentMailing.name;
559 $scope.save_next();
560 }
561 else {
562 $scope.save_next();
563 }
564
565 };
f97e2426
SR
566 //set approval date to current time, also schedule date based on send immediately or not. then call 3rd page save
567 //go back to listing page
40f603d5 568 $scope.submitButton = function () {
ab953923
SR
569 $scope.currentMailing.approval_status_id = "1";
570 $scope.currentMailing.approver_id = $scope.user_id;
571 var currentdate = new Date();
572 var yyyy = currentdate.getFullYear();
573 var mm = currentdate.getMonth() + 1;
40f603d5 574 mm = mm < 10 ? '0' + mm : mm;
ab953923 575 var dd = currentdate.getDate();
40f603d5 576 dd = dd < 10 ? '0' + dd : dd;
ab953923 577 var hh = currentdate.getHours();
40f603d5 578 hh = hh < 10 ? '0' + hh : hh;
ab953923 579 var min = currentdate.getMinutes();
40f603d5 580 min = min < 10 ? '0' + min : min;
ab953923 581 var sec = currentdate.getSeconds();
40f603d5
TO
582 sec = sec < 10 ? '0' + sec : sec;
583 $scope.currentMailing.approval_date = yyyy + "/" + mm
584 + "/" + dd + " "
585 + hh + ":"
586 + min + ":" + sec;
587 if ($scope.now == 1) {
ab953923
SR
588 $scope.currentMailing.scheduled_date = $scope.currentMailing.approval_date;
589 }
4066c991
SR
590 $scope.save_next_page3();
591 $scope.back();
ab953923 592 };
f97e2426 593 //we use this to open the preview modal based on value of pre
40f603d5
TO
594 $scope.$watch('pre', function () {
595 if ($scope.pre == true) {
ab953923
SR
596 $('#prevmail').dialog({
597 title: 'Preview Mailing',
598 width: 1080,
599 height: 800,
600 closed: false,
601 cache: false,
602 modal: true,
40f603d5
TO
603 close: function () {
604 $scope.pre = false;
605 $scope.$apply();
ab953923
SR
606 }
607 });
608 }
40f603d5 609 }, true);
f97e2426 610 //send test api called to send the test
40f603d5
TO
611 $scope.sendTest = function () {
612 var resulta = crmApi('Mailing', 'send_test', {test_email: $scope.testMailing.name, test_group: $scope.testMailing.group,
613 mailing_id: $scope.currentMailing.id});
ab953923
SR
614 };
615 });
616
617
7e6e8bd6 618// Directive to go to the next tab
40f603d5 619 crmMailing.directive('nexttab', function () {
ab953923
SR
620 return {
621 restrict: 'A',
40f603d5 622 link: function (scope, element, attrs) {
50781b34 623 var tabselector = $(".crmMailingTabs");
40f603d5
TO
624 var myarr = new Array(1, 2);
625 if (scope.currentMailing.id == null) {
626 tabselector.tabs({disabled: myarr});
4198cf0b 627 }
40f603d5 628 $(element).on("click", function () {
4198cf0b 629 scope.tabupdate();
50781b34
SR
630 var myArray1 = new Array( );
631 scope.$apply();
40f603d5
TO
632 for (var i = scope.maxtab + 1; i < 3; i++) {
633 myArray1.push(i);
50781b34
SR
634 }
635
40f603d5
TO
636 tabselector.tabs("option", "disabled", myArray1);
637 tabselector.tabs({active: scope.tabact});
50781b34 638 scope.$apply();
ab953923
SR
639 });
640 }
641 };
50781b34 642 });
ab953923
SR
643
644 // Directive to go to the previous tab
40f603d5 645 crmMailing.directive('prevtab', function () {
ab953923
SR
646 return {
647 restrict: 'A',
40f603d5 648 link: function (scope, element, attrs) {
50781b34
SR
649 var tabselector = $(".crmMailingTabs");
650 tabselector.tabs();
40f603d5 651 $(element).on("click", function () {
4198cf0b 652 scope.prevtabupdate();
50781b34
SR
653 scope.$apply();
654 var myArray1 = new Array( );
40f603d5 655 tabselector.tabs({active: scope.tabact});
ab953923
SR
656 });
657 }
658 };
50781b34 659 });
ab953923
SR
660
661
662 // Select 2 Widget for selecting the included group
40f603d5 663 crmMailing.directive('chsgroup', function () {
ab953923 664 return {
40f603d5
TO
665 restrict: 'AE',
666 link: function (scope, element, attrs) {
ab953923 667 function format(item) {
40f603d5 668 if (!item.id) {
ab953923
SR
669 // return `text` for optgroup
670 return item.text;
671 }
672 // return item template
673 var a = item.id.split(" ");
40f603d5 674 if (a[1] == "civicrm_group" && a[2] == "include")
ab953923 675 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;
40f603d5 676 if (a[1] == "civicrm_group" && a[2] == "exclude")
ab953923 677 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;
40f603d5
TO
678 if (a[1] == "civicrm_mailing" && a[2] == "include")
679 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;
680 if (a[1] == "civicrm_mailing" && a[2] == "exclude")
ab953923
SR
681 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;
682 }
683
684 $(element).select2({
40f603d5 685 width: "400px",
ab953923
SR
686 placeholder: "Choose Recipients",
687 formatResult: format,
688 formatSelection: format,
40f603d5
TO
689 escapeMarkup: function (m) {
690 return m;
691 },
ab953923
SR
692 }).select2("data", scope.incGroup);
693
694
40f603d5
TO
695 $(element).on("select2-opening", function () {
696 scope.incGroup = chck;
ab953923
SR
697 scope.$apply();
698
699 });
700
40f603d5 701 $(element).on('select2-selecting', function (e) {
ab953923 702 chck.push(e.val);
40f603d5 703 scope.incGroup = chck;
ab953923
SR
704 scope.$apply();
705
706 });
707
40f603d5 708 $(element).on("select2-removed", function (e) {
ab953923
SR
709 var index = chck.indexOf(e.val);
710 chck.splice(index, 1);
40f603d5 711 scope.incGroup = chck;
ab953923
SR
712 scope.$apply();
713 });
714
715 }
716 };
717 });
f97e2426 718 //used for tokens select2 widget
40f603d5 719 crmMailing.directive('groupselect', function () {
ab953923 720 return {
40f603d5
TO
721 restrict: 'AE',
722 link: function (scope, element, attrs) {
723 $(element).select2({width: "200px", data: mltokens, placeholder: "Insert Token"});
724 $(element).on('select2-selecting', function (e) {
ab953923
SR
725
726 scope.$evalAsync('_resetSelection()');
727 var a = $(element).attr('id');
40f603d5
TO
728 if (a == "htgroup") {
729 if (scope.currentMailing.body_html == "" || scope.currentMailing.body_html == null)
3f4bfa00 730 scope.currentMailing.body_html = e.val;
016db4f6 731 else
3f4bfa00 732 scope.currentMailing.body_html += e.val;
ab953923 733 }
40f603d5 734 else if (a == "subgroup") {
ab953923
SR
735 var msg = document.getElementById("sub").value;
736 var cursorlen = document.getElementById("sub").selectionStart;
40f603d5 737 var textlen = msg.length;
ab953923
SR
738 document.getElementById("sub").value = msg.substring(0, cursorlen) + e.val + msg.substring(cursorlen, textlen);
739 scope.currentMailing.subject = msg.substring(0, cursorlen) + e.val + msg.substring(cursorlen, textlen);
740 var cursorPos = (cursorlen + e.val.length);
741 document.getElementById("sub").selectionStart = cursorPos;
40f603d5 742 document.getElementById("sub").selectionEnd = cursorPos;
ab953923
SR
743 document.getElementById("sub").focus();
744 }
40f603d5
TO
745 else if (a == "textgroup") {
746 var msg = document.getElementById("body_text").value;
747 var cursorlen = document.getElementById("body_text").selectionStart;
748 var textlen = msg.length;
749 document.getElementById("body_text").value = msg.substring(0, cursorlen) + e.val + msg.substring(cursorlen, textlen);
750 scope.currentMailing.body_text = msg.substring(0, cursorlen) + e.val + msg.substring(cursorlen, textlen);
751 var cursorPos = (cursorlen + e.val.length);
752 document.getElementById("body_text").selectionStart = cursorPos;
753 document.getElementById("body_text").selectionEnd = cursorPos;
754 document.getElementById("body_text").focus();
755 }
ab953923
SR
756 scope.$apply();
757 e.preventDefault();
758 })
759 }
760 };
761 });
762
f97e2426 763 //ckeditor directive
40f603d5 764 crmMailing.directive('ckedit', function () {
50781b34
SR
765 return {
766 require: '?ngModel',
40f603d5 767 link: function (scope, elm, attr, ngModel) {
50781b34
SR
768 var ck = CKEDITOR.replace(elm[0]);
769
40f603d5
TO
770 if (!ngModel)
771 return;
50781b34 772
40f603d5
TO
773 ck.on('pasteState', function () {
774 scope.$apply(function () {
50781b34
SR
775 ngModel.$setViewValue(ck.getData());
776 });
777 });
778
40f603d5 779 ngModel.$render = function (value) {
50781b34
SR
780 ck.setData(ngModel.$viewValue);
781 };
782 }
783 };
784 });
ab953923
SR
785
786 // Used for the select date option. This is used for giving scheduled_date its date value
40f603d5 787 crmMailing.directive('chsdate', function () {
ab953923
SR
788 return {
789 restrict: 'AE',
40f603d5 790 link: function (scope, element, attrs) {
ab953923
SR
791 $(element).datepicker({
792 dateFormat: "yy-mm-dd",
40f603d5 793 onSelect: function (date) {
ab953923 794 $(".ui-datepicker a").removeAttr("href");
40f603d5 795 scope.scheddate.date = date.toString();
ab953923
SR
796 scope.$apply();
797 }
798 });
799 }
800 };
801 });
f97e2426 802 //used for file upload
40f603d5 803 crmMailing.directive('file', function () {
ab953923
SR
804 return {
805 scope: {
806 file: '='
807 },
40f603d5
TO
808 link: function (scope, el, attrs) {
809 el.bind('change', function (event) {
ab953923
SR
810 var files = event.target.files;
811 var file = files[0];
812 scope.file = file ? file : undefined;
813 scope.$apply();
814 });
815 }
816 };
817 });
f97e2426 818 //used to insert the time entry
40f603d5 819 crmMailing.directive('checktimeentry', function () {
ab953923 820 return {
40f603d5 821 restrict: 'AE',
ab953923 822 link: function (scope, element, attrs) {
40f603d5 823 $(element).timeEntry({show24Hours: true});
ab953923
SR
824 }
825 }
826 });
827
50781b34 828
40f603d5
TO
829 //This controller is used for creating the mailing list. Simply gets all the mailing data from civiAPI
830 crmMailing.controller('mailingListCtrl', function ($scope, crmApi, mailingList, $route) {
ab953923
SR
831 if (global == 0) {
832 global = global + 1;
833 $route.reload();
834 }
835 $scope.mailingList = mailingList.values;
40f603d5
TO
836 $scope.checkEmpty = function () {
837 if ($scope.mailingList == "")
838 return true;
839 else
840 return false;
ab953923
SR
841 }
842 $scope.deleteMail = function (mail) {
843 crmApi('Mailing', 'delete', {id: mail.id}, {
844 error: function (data) {
845 CRM.alert(data.error_message, ts('Error'));
846 }
847 })
40f603d5
TO
848 .then(function (data) {
849 if (!data.is_error) {
850 delete mailingList.values[mail.id];
851 $scope.$digest();
852 $route.reload();
853 }
854 });
ab953923
SR
855
856 };
857
40f603d5 858 $scope.edit = function () {
ab953923
SR
859 global = global - 1;
860 };
861
862 });
863
864})(angular, CRM.$, CRM._);
865
866/* example of params
867 [attachFile_1] => Array ( [uri] => /var/www/siddhant/drupal-7.27/sites/default/files/civicrm/custom/blog_2_odt_2c622a7b5e32415a92e81ed97d6554c7.unknown [type] => application/vnd.oasis.opendocument.text [location] => /var/www/siddhant/drupal-7.27/sites/default/files/civicrm/custom/blog_2_odt_2c622a7b5e32415a92e81ed97d6554c7.unknown [description] => dasdas [upload_date] => 20140706105804 [tag] => Array ( ) [attachment_taglist] => Array ( ) )
868 */