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