CRM-15578 - crmMailing2 - Extract crmMailingMgr service
[civicrm-core.git] / js / angular-Mailing.js
1 (function (angular, $, _) {
2 //partials for the html pages
3 var partialUrl = function (relPath) {
4 return CRM.resourceUrls['civicrm'] + '/partials/crmMailing/' + relPath;
5 };
6
7 var crmMailing = angular.module('crmMailing', ['ngRoute', 'ui.utils', 'ngSanitize']);
8 var chck = []; //to fill the group variable $scope.incGroup
9 var chck2 = []; // to get id and text in the required format
10 var mltokens = []; //we store list of the tokens in this
11 var global = 0; //use this to reload mailingList page once
12
13 //-------------------------------------------------------------------------------------------------------
14 crmMailing.config(['$routeProvider',
15 function ($routeProvider) {
16 $routeProvider.when('/mailing', {
17 templateUrl: partialUrl('mailingList.html'),
18 controller: 'mailingListCtrl',
19 resolve: {
20 mailingList: function ($route, crmApi) {
21 return crmApi('Mailing', 'get', {});
22 }
23
24 }
25 }); //This route is used for generating the list of mails created.
26
27 $routeProvider.when('/mailing/:id', {
28 templateUrl: partialUrl('main.html'),
29 controller: 'mailingCtrl',
30 resolve: {
31 selectedMail: function ($route, crmApi) {
32 if ($route.current.params.id !== 'new') {
33 return crmApi('Mailing', 'getsingle', {id: $route.current.params.id});
34 }
35 else {
36 //selected mail in case of a new mailing. some default values are set
37 return {visibility: "Public Pages", url_tracking: "1", dedupe_email: "1", forward_replies: "0", auto_responder: "0", open_tracking: "1"
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
49 crmMailing.controller('mailingCtrl', function ($scope, crmApi, selectedMail, $location, $route, $sce, $window) {
50
51 //setting variables to the values we have got to the api
52 $scope.partialUrl = partialUrl;
53 $scope.campaignList = CRM.crmMailing.campNames;
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;
62 mltokens = CRM.crmMailing.mailTokens;
63 //set currentMailing to selectedMail
64 $scope.currentMailing = selectedMail;
65 $scope.currentMailing.created_id = $scope.user_id;
66 //when pre is true, preview mailing is opened
67 $scope.pre = false;
68 //counts number of recipients
69 $scope.noOfRecipients = 0;
70 //object testMailing for testMailing.name and testMailing.group
71 $scope.testMailing = {};
72 $scope.testMailing.name = "";
73 $scope.testMailing.group = "";
74 window.ct = $scope.currentMailing;
75 //previewbody_html stores value of HTML in editor, similar functionality of text and subject based on name
76 $scope.previewbody_html = "";
77 $scope.previewbody_text = "";
78 $scope.preview_subject = "";
79 //object for tokens
80 $scope.token = "";
81 //chck and chck2 are used for mailing groups
82 chck = [];
83 chck2 = [];
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
86 $scope.from = {};
87 $scope.from.name = "";
88 $scope.from.email = "";
89 $scope.from.total = "";
90 $scope.reply = {};
91 $scope.reply.email = "";
92 //replyaddress is the array used to save the set of fromemailAddress of the person
93 $scope.replyaddress = [];
94 for (var a in $scope.fromAddress) {
95 var b = {};
96 var splt = $scope.fromAddress[a].label.split(" ");
97 splt = splt[1].substring(1, (splt[1].length - 1));
98 b.email = splt;
99 $scope.replyaddress.push(b);
100 }
101 //from.total retrieves the from.name + from.email value so it can be read by the select2 widget
102 if ($scope.currentMailing.from_name != null) {
103 $scope.from.name = $scope.currentMailing.from_name;
104 $scope.from.email = $scope.currentMailing.from_email;
105 $scope.from.total = '"' + $scope.from.name + '"' + " <" + $scope.from.email + ">";
106 $scope.reply.email = $scope.currentMailing.replyto_email;
107 }
108
109 $scope.mailid = [];
110 //putting all the ids of mails corresponding to the current mailing in mailid.
111 for (var a in $scope.mailingGrp) {
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;
114 var c = $scope.mailingGrp[a].id;
115 chck.push(b);
116 $scope.mailid.push(c);
117 }
118 }
119
120 //used to put the Template id in tst so it can be used
121
122 if ($scope.currentMailing.msg_template_id != null) {
123 $scope.tst = $scope.currentMailing.msg_template_id;
124 }
125 //Making the object for data in the mailing group related directive
126
127 for (var a in chck) {
128 var b = {};
129 b.id = chck[a];
130 var splt = chck[a].split(" ");
131
132 if (splt[1] == "civicrm_group") {
133 for (var c in $scope.groupNamesList) {
134 if ($scope.groupNamesList[c].id == splt[0]) {
135 b.text = $scope.groupNamesList[c].title;
136 }
137 }
138 }
139 if (splt[1] == "civicrm_mailing") {
140 for (var c in $scope.mailList) {
141 if ($scope.mailList[c].id == splt[0]) {
142 b.text = $scope.mailList[c].name;
143 }
144 }
145 }
146 chck2.push(b);
147 }
148 $scope.incGroup = chck2;
149 //tabact is set to 0. stores value of active tab
150 $scope.tabact = 0;
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
153 if ($scope.currentMailing.id != null)
154 $scope.maxtab = 3;
155 $scope.maxtab = 0;
156
157 //increments active tab value and maxtab when next is clicked
158 $scope.tabupdate = function () {
159 $scope.tabact = $scope.tabact + 1;
160 $scope.maxtab = Math.max($scope.maxtab, $scope.tabact);
161 }
162 //decrements active tab value
163 $scope.prevtabupdate = function () {
164 $scope.tabact = $scope.tabact - 1;
165 }
166 //set active tab to 0 when recipient is clicked
167 $scope.recclicked = function () {
168 $scope.tabact = 0;
169
170 };
171 //set active tab to 1 when content is clicked
172 //also call appropriate save function if clicked from recipient tab
173 $scope.conclicked = function () {
174 if ($scope.tabact == 0)
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
180 $scope.schedclicked = function () {
181 if ($scope.tabact == 0)
182 $scope.save_next_page1();
183 else if ($scope.tabact == 1)
184 $scope.save_next_page2();
185 $scope.tabact = 2;
186 };
187 //goes to the mailing list page
188 $scope.back = function () {
189 $window.location.href = "#/mailing";
190 $route.reload();
191 };
192 //on changing the selected template, updates subject and body_html of currentMailing
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 = "";
198 }
199 else {
200 for (var a in $scope.tmpList) {
201
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;
205 }
206 }
207 }
208 };
209 //initializing variables we will use for checkboxes, or for purpose of ng-show
210 $scope.composeS = "1";
211 if ($scope.currentMailing.forward_replies == 0 && $scope.currentMailing.auto_responder == 0) {
212 $scope.trackreplies = "0";
213 }
214 else {
215 $scope.trackreplies = "1";
216 }
217 if ($scope.currentMailing.scheduled_date == null || $scope.currentMailing.scheduled_date == "")
218 $scope.now = "1";
219 else
220 $scope.now = "0";
221
222 $scope.reply = function () {
223 if ($scope.trackreplies == 0) {
224 $scope.trackreplies = 1;
225 }
226 else {
227 $scope.trackreplies = 0;
228 $scope.currentMailing.forward_replies = 0;
229 $scope.currentMailing.auto_responder = 0;
230 }
231 }
232
233 //joining scheddate.date and scheddate.time gives us scheduled date of current mailing
234 $scope.scheddate = {};
235 $scope.scheddate.date = "";
236 $scope.scheddate.time = "";
237 //checkNow decides whether we see the date picker or not. Depends on send immediately being on or off
238 $scope.checkNow = function () {
239 if ($scope.now == 1) {
240 $scope.now = 0;
241 }
242 else {
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 }
249 };
250 //changing the screen from compose on screen to upload content
251 $scope.upldChange = function (composeS) {
252 if (composeS == "1") {
253 return true;
254 }
255 else
256 return false;
257 };
258 //filter so we only get headers from mailing component
259 $scope.isHeader = function (hf) {
260 return hf.component_type == "Header";
261 };
262 //filter so we only get footers from mailing component
263 $scope.isFooter = function (f) {
264 return f.component_type == "Footer";
265 };
266 //filter so we only get auto-Responders from mailing component
267 $scope.isAuto = function (au) {
268 return au.component_type == "Reply";
269 };
270 //filter so we only get userDriven message templates
271 $scope.isUserDriven = function (mstemp) {
272 return (parseInt(mstemp.id) > 58);
273 };
274 //used for ng-show when trackreplies is selected. Only then we show forward replies and auto-responders options
275 $scope.trackr = function (trackreplies) {
276 if (trackreplies == "1") {
277 return true;
278 }
279 else
280 return false;
281 }
282 //we should only see groups with appropriate mailing visibility
283 $scope.isGrp = function (grp) {
284 return grp.visibility == "Public Pages";
285 };
286 //only completed mails are shown in the groupings
287 $scope.isCompMail = function (ml) {
288 return ml.is_completed == 1;
289 };
290 //This is used to open the update the values in preview mailing
291 $scope.preview_update = function () {
292 var resulta = crmApi('Mailing', 'preview', {id: $scope.currentMailing.id});
293 resulta.success(function (data) {
294 if (data.is_error == 0) {
295 $scope.previewbody_html = data.values.html;
296 $scope.previewbody_text = data.values.text;
297 $scope.preview_subject = data.values.subject;
298 $scope.pre = true;
299 $scope.$digest();
300 $scope.$apply();
301 }
302 });
303 };
304 //checks if body_text is empty or not
305 $scope.isBody_text = function () {
306 if ($scope.currentMailing.body_text == null || $scope.currentMailing.body_text == "")
307 return false;
308 else
309 return true;
310 };
311 //parses html
312 $scope.deliberatelyTrustDangerousSnippet = function () {
313 return $sce.trustAsHtml($scope.previewbody_html);
314 };
315
316 $scope.deliberatelyTrustDangerousSnippet2 = function () {
317 return $sce.trustAsHtml($scope.previewbody_text);
318 };
319
320 $scope.deliberatelyTrustDangerousSnippet3 = function () {
321 return $sce.trustAsHtml($scope.preview_subject);
322 };
323 //gets number of mailing recipients
324 $scope.mailing_recipients = function () {
325 var resulta = crmApi('MailingRecipients', 'get', {mailing_id: $scope.currentMailing.id, options: {limit: 1000}});
326 resulta.success(function (data) {
327 if (data.is_error == 0) {
328 $scope.noOfRecipients = data.count;
329 $scope.$digest();
330 $scope.$apply();
331 }
332 });
333 }
334 //gets mailing groups associated with current mailing id. formats it in the required way
335 $scope.mailingGroup = function () {
336 var resulta = crmApi('MailingGroup', 'get', {mailing_id: $scope.currentMailing.id, options: {limit: 1000}})
337 resulta.success(function (data) {
338 $scope.mailid = [];
339 chck = [];
340 angular.forEach(data.values, function (value, key) {
341 var b = value.entity_id + " " + value.entity_table + " " + value.group_type;
342 var c = value.id;
343 chck.push(b);
344 $scope.mailid.push(c);
345 });
346 for (var a in chck)
347 {
348 var b = {}
349 b.id = chck[a];
350 var splt = chck[a].split(" ");
351
352 if (splt[1] == "civicrm_group") {
353 for (var c in $scope.groupNamesList) {
354 if ($scope.groupNamesList[c].id == splt[0]) {
355 b.text = $scope.groupNamesList[c].title;
356 }
357 }
358 }
359 if (splt[1] == "civicrm_mailing") {
360 for (var c in $scope.mailList) {
361 if ($scope.mailList[c].id == splt[0]) {
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 }
373 //save repeat is a function that is repeated in all versions of save
374 $scope.save_repeat = function () {
375 if ($scope.from.total != "") {
376 var splt = $scope.from.total.split(" ");
377 var splta = splt[1].substring(1, (splt[1].length - 1));
378 var spltb = splt[0].substring(1, (splt[0].length - 1));
379 $scope.currentMailing.from_email = splta;
380 $scope.currentMailing.from_name = spltb;
381 }
382 }
383 //save_next is used in recipient page. stores mailing groups
384 $scope.save_next = function () {
385 $scope.save_repeat();
386 //splits the groups based on include exclude mailing and groups
387 $scope.incGrp = [];
388 $scope.excGrp = [];
389 $scope.incMail = [];
390 $scope.excMail = [];
391 $scope.answer = "";
392 for (req_id in chck) {
393 $scope.answer = chck[req_id].split(" ");
394
395 if ($scope.answer[1] == "civicrm_mailing" && $scope.answer[2] == "include") {
396 $scope.incMail.push($scope.answer[0]);
397 }
398 else if ($scope.answer[1] == "civicrm_mailing" && $scope.answer[2] == "exclude") {
399 $scope.excMail.push($scope.answer[0]);
400 }
401 if ($scope.answer[1] == "civicrm_group" && $scope.answer[2] == "include") {
402 $scope.incGrp.push($scope.answer[0]);
403 }
404 else if ($scope.answer[1] == "civicrm_group" && $scope.answer[2] == "exclude") {
405 $scope.excGrp.push($scope.answer[0]);
406 }
407 }
408 //deletes existing mailing groups of that id
409 if ($scope.mailid != null) {
410 for (var a in $scope.mailid) {
411 var result_2 = crmApi('MailingGroup', 'delete', {
412 id: $scope.mailid[a]
413 });
414 }
415 }
416
417 var result = crmApi('Mailing', 'create', {
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
440 },
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);
448 //var result = crmApi('Mailing', 'create', $scope.currentMailing, true);
449 result.success(function (data) {
450 if (data.is_error == 0) {
451 $scope.currentMailing.id = data.id;
452 $scope.mailingGroup();
453 }
454 });
455 };
456 //in second page save neither groups or schedule date information.
457 $scope.save_next_page2 = function () {
458 $scope.save_repeat();
459 var result = crmApi('Mailing', 'create', {
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);
484 //var result = crmApi('Mailing', 'create', $scope.currentMailing, true);
485 result.success(function (data) {
486 if (data.is_error == 0) {
487 $scope.currentMailing.id = data.id;
488 }
489 });
490 };
491 //save scheduling information and not groups
492 $scope.save_next_page3 = function () {
493 $scope.save_repeat();
494 if ($scope.currentMailing.scheduled_date == $scope.currentMailing.approval_date
495 && $scope.currentMailing.scheduled_date != null) {
496 ;
497 }
498 else {
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;
502 }
503 else {
504 $scope.currentMailing.scheduled_date = null;
505 }
506 }
507 var result = crmApi('Mailing', 'create', {
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);
537 //var result = crmApi('Mailing', 'create', $scope.currentMailing, true);
538 result.success(function (data) {
539 if (data.is_error == 0) {
540 $scope.currentMailing.id = data.id;
541 }
542 });
543 };
544
545 //call save of page2 and go to listing page
546 $scope.save = function () {
547 $scope.save_next_page2();
548 $scope.back();
549 };
550 //call save of page1 and go to listing page
551 $scope.save_page1 = function () {
552 $scope.save_next_page1();
553 $scope.back();
554 };
555 //if we save on page one, subject isnt defined. so we equate it to mailing name for now so we can save
556 $scope.save_next_page1 = function () {
557 if ($scope.currentMailing.id == null) {
558 $scope.currentMailing.subject = $scope.currentMailing.name;
559 $scope.save_next();
560 }
561 else {
562 $scope.save_next();
563 }
564
565 };
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
568 $scope.submitButton = function () {
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;
574 mm = mm < 10 ? '0' + mm : mm;
575 var dd = currentdate.getDate();
576 dd = dd < 10 ? '0' + dd : dd;
577 var hh = currentdate.getHours();
578 hh = hh < 10 ? '0' + hh : hh;
579 var min = currentdate.getMinutes();
580 min = min < 10 ? '0' + min : min;
581 var sec = currentdate.getSeconds();
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) {
588 $scope.currentMailing.scheduled_date = $scope.currentMailing.approval_date;
589 }
590 $scope.save_next_page3();
591 $scope.back();
592 };
593 //we use this to open the preview modal based on value of pre
594 $scope.$watch('pre', function () {
595 if ($scope.pre == true) {
596 $('#prevmail').dialog({
597 title: 'Preview Mailing',
598 width: 1080,
599 height: 800,
600 closed: false,
601 cache: false,
602 modal: true,
603 close: function () {
604 $scope.pre = false;
605 $scope.$apply();
606 }
607 });
608 }
609 }, true);
610 //send test api called to send the test
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});
614 };
615 });
616
617
618 // Directive to go to the next tab
619 crmMailing.directive('nexttab', function () {
620 return {
621 restrict: 'A',
622 link: function (scope, element, attrs) {
623 var tabselector = $(".crmMailingTabs");
624 var myarr = new Array(1, 2);
625 if (scope.currentMailing.id == null) {
626 tabselector.tabs({disabled: myarr});
627 }
628 $(element).on("click", function () {
629 scope.tabupdate();
630 var myArray1 = new Array( );
631 scope.$apply();
632 for (var i = scope.maxtab + 1; i < 3; i++) {
633 myArray1.push(i);
634 }
635
636 tabselector.tabs("option", "disabled", myArray1);
637 tabselector.tabs({active: scope.tabact});
638 scope.$apply();
639 });
640 }
641 };
642 });
643
644 // Directive to go to the previous tab
645 crmMailing.directive('prevtab', function () {
646 return {
647 restrict: 'A',
648 link: function (scope, element, attrs) {
649 var tabselector = $(".crmMailingTabs");
650 tabselector.tabs();
651 $(element).on("click", function () {
652 scope.prevtabupdate();
653 scope.$apply();
654 var myArray1 = new Array( );
655 tabselector.tabs({active: scope.tabact});
656 });
657 }
658 };
659 });
660
661
662 // Select 2 Widget for selecting the included group
663 crmMailing.directive('chsgroup', function () {
664 return {
665 restrict: 'AE',
666 link: function (scope, element, attrs) {
667 function format(item) {
668 if (!item.id) {
669 // return `text` for optgroup
670 return item.text;
671 }
672 // return item template
673 var a = item.id.split(" ");
674 if (a[1] == "civicrm_group" && a[2] == "include")
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;
676 if (a[1] == "civicrm_group" && a[2] == "exclude")
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;
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")
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({
685 width: "400px",
686 placeholder: "Choose Recipients",
687 formatResult: format,
688 formatSelection: format,
689 escapeMarkup: function (m) {
690 return m;
691 },
692 }).select2("data", scope.incGroup);
693
694
695 $(element).on("select2-opening", function () {
696 scope.incGroup = chck;
697 scope.$apply();
698
699 });
700
701 $(element).on('select2-selecting', function (e) {
702 chck.push(e.val);
703 scope.incGroup = chck;
704 scope.$apply();
705
706 });
707
708 $(element).on("select2-removed", function (e) {
709 var index = chck.indexOf(e.val);
710 chck.splice(index, 1);
711 scope.incGroup = chck;
712 scope.$apply();
713 });
714
715 }
716 };
717 });
718 //used for tokens select2 widget
719 crmMailing.directive('groupselect', function () {
720 return {
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) {
725
726 scope.$evalAsync('_resetSelection()');
727 var a = $(element).attr('id');
728 if (a == "htgroup") {
729 if (scope.currentMailing.body_html == "" || scope.currentMailing.body_html == null)
730 scope.currentMailing.body_html = e.val;
731 else
732 scope.currentMailing.body_html += e.val;
733 }
734 else if (a == "subgroup") {
735 var msg = document.getElementById("sub").value;
736 var cursorlen = document.getElementById("sub").selectionStart;
737 var textlen = msg.length;
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;
742 document.getElementById("sub").selectionEnd = cursorPos;
743 document.getElementById("sub").focus();
744 }
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 }
756 scope.$apply();
757 e.preventDefault();
758 })
759 }
760 };
761 });
762
763 //ckeditor directive
764 crmMailing.directive('ckedit', function () {
765 return {
766 require: '?ngModel',
767 link: function (scope, elm, attr, ngModel) {
768 var ck = CKEDITOR.replace(elm[0]);
769
770 if (!ngModel)
771 return;
772
773 ck.on('pasteState', function () {
774 scope.$apply(function () {
775 ngModel.$setViewValue(ck.getData());
776 });
777 });
778
779 ngModel.$render = function (value) {
780 ck.setData(ngModel.$viewValue);
781 };
782 }
783 };
784 });
785
786 // Used for the select date option. This is used for giving scheduled_date its date value
787 crmMailing.directive('chsdate', function () {
788 return {
789 restrict: 'AE',
790 link: function (scope, element, attrs) {
791 $(element).datepicker({
792 dateFormat: "yy-mm-dd",
793 onSelect: function (date) {
794 $(".ui-datepicker a").removeAttr("href");
795 scope.scheddate.date = date.toString();
796 scope.$apply();
797 }
798 });
799 }
800 };
801 });
802 //used for file upload
803 crmMailing.directive('file', function () {
804 return {
805 scope: {
806 file: '='
807 },
808 link: function (scope, el, attrs) {
809 el.bind('change', function (event) {
810 var files = event.target.files;
811 var file = files[0];
812 scope.file = file ? file : undefined;
813 scope.$apply();
814 });
815 }
816 };
817 });
818 //used to insert the time entry
819 crmMailing.directive('checktimeentry', function () {
820 return {
821 restrict: 'AE',
822 link: function (scope, element, attrs) {
823 $(element).timeEntry({show24Hours: true});
824 }
825 }
826 });
827
828
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) {
831 if (global == 0) {
832 global = global + 1;
833 $route.reload();
834 }
835 $scope.mailingList = mailingList.values;
836 $scope.checkEmpty = function () {
837 if ($scope.mailingList == "")
838 return true;
839 else
840 return false;
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 })
848 .then(function (data) {
849 if (!data.is_error) {
850 delete mailingList.values[mail.id];
851 $scope.$digest();
852 $route.reload();
853 }
854 });
855
856 };
857
858 $scope.edit = function () {
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 */