1 (function(angular
, $, _
) {
2 //partials for the html pages
3 var partialUrl = function(relPath
) {
4 return CRM
.resourceUrls
['civicrm'] + '/partials/crmMailingType/' + relPath
;
7 var crmMailing
= angular
.module('crmMailing', ['ngRoute', 'ui.utils']);
10 //-------------------------------------------------------------------------------------------------------
11 crmMailing
.config(['$routeProvider',
12 function($routeProvider
) {
13 $routeProvider
.when('/mailing', {
14 templateUrl
: partialUrl('mailingList.html'),
15 controller
: 'mailingListCtrl',
17 mailingList: function($route
, crmApi
) {
18 return crmApi('Mailing', 'get', {});
22 }); //This route is used for generating the list of mails created.
26 $routeProvider
.when('/mailing/:id', {
27 templateUrl
: partialUrl('main.html'),
28 controller
: 'mailingCtrl',
30 selectedMail: function($route
, crmApi
) {
31 if ( $route
.current
.params
.id
!== 'new') {
32 return crmApi('Mailing', 'getsingle', {id
: $route
.current
.params
.id
});
35 //created_id has been set to my id. Does not save without created_id. Needs to made generic based on the user
36 return {name
: "New Mail", visibility
: "Public Pages", url_tracking
:"1", forward_replies
:"0", created_id
: "202", auto_responder
:"0", open_tracking
:"1",
41 }); //This route is used for creating new mails and editing the current mails
44 //-----------------------------------------
47 //This controller is used in creating new mail and editing current mails
48 crmMailing
.controller('mailingCtrl', function($scope
, crmApi
, selectedMail
) {
50 //Making some dummy api to see if my from email, reply to email works. To see if all options come in select box
53 'from_mail': 'rajgo94@gmail.com',
54 'reply_mail': 'rajgo94@gmail.com' },
56 'from_mail': 'rajgo94@gmail_2.com',
57 'reply_mail': 'rajgo94@gmail_2.com'},
59 'from_mail': 'rajgo94@gmail_3.com',
60 'reply_mail': 'rajgo94@gmail_3.com'}
62 //setting variables to the values we have got to the api
63 $scope
.partialUrl
= partialUrl
;
64 $scope
.campaignList
= CRM
.crmMailing
.campNames
;
65 $scope
.mailList
= CRM
.crmMailing
.civiMails
;
66 $scope
.mailNameList
= _
.pluck(CRM
.crmCaseType
.civiMails
, 'name');
67 $scope
.groupNamesList
= CRM
.crmMailing
.groupNames
;
68 $scope
.headerfooter
= CRM
.crmMailing
.headerfooterList
;
69 $scope
.eMailing
= CRM
.crmMailing
.emailAdd
;
70 $scope
.tmpList
= CRM
.crmMailing
.mesTemplate
;
71 $scope
.currentMailing
= selectedMail
;
72 $scope
.testGroup
= "";
73 window
.ct
= $scope
.currentMailing
;
75 //initializing variables we will use for checkboxes, or for purpose of ng-show
78 $scope
.trackreplies
="0";
81 //to split the value of selectedMail.scheduled_date into the date and time separately
83 $scope
.scheddate
.date
= "";
84 $scope
.scheddate
.time
= "";
87 // To split the scheduled_date into date and time. The date format is not accepting
88 if(selectedMail
.scheduled_date
!= null){
89 $scope
.ans
= selectedMail
.scheduled_date
.split(" ");
90 $scope
.scheddate
.date
=$scope
.ans
[0];
91 $scope
.scheddate
.time
=$scope
.ans
[1];
92 console
.log("scheddate.date is " + $scope
.scheddate
.date
);
93 console
.log("scheddate.time is " + $scope
.scheddate
.time
);
96 console
.log(selectedMail
);
98 //changing the screen from compose on screen to upload content
99 $scope
.upldChange= function(composeS
){
106 //filter so we only get headers from mailing component
107 $scope
.isHeader= function(hf
){
108 return hf
.component_type
== "Header";
110 //filter so we only get footers from mailing component
111 $scope
.isFooter= function(f
){
112 return f
.component_type
== "Footer";
114 //filter so we only get auto-Responders from mailing component
115 $scope
.isAuto= function(au
){
116 return au
.component_type
== "Reply";
118 //filter so we only get userDriven message templates
119 $scope
.isUserDriven= function(mstemp
){
120 return (parseInt(mstemp
.id
)>58);
122 //used for ng-show when trackreplies is selected. Only then we show forward replies and auto-responders options
123 $scope
.trackr= function(trackreplies
){
124 if(trackreplies
=="1"){
131 $scope
.isGrp= function(grp
){
132 return grp
.visibility
== "Public Pages";
135 $scope
.isCompMail= function(ml
){
136 return ml
.is_completed
== 1;
139 $scope
.save = function() {
144 console
.log($scope
.testGroup
+ " test group");
145 console
.log(incGroup
);
147 for(req_id
in incGroup
){
148 $scope
.answer
= incGroup
[req_id
].split(" ");
150 if($scope
.answer
[1] == "mail" && $scope
.answer
[2]=="include"){
151 $scope
.incMail
.push($scope
.answer
[0]);
153 else if($scope
.answer
[1] == "mail" && $scope
.answer
[2]=="exclude"){
154 $scope
.excMail
.push($scope
.answer
[0]);
156 if($scope
.answer
[1] == "group" && $scope
.answer
[2]=="include"){
157 $scope
.incGrp
.push($scope
.answer
[0]);
159 else if($scope
.answer
[1] == "group" && $scope
.answer
[2]=="exclude"){
160 $scope
.excGrp
.push($scope
.answer
[0]);
164 console
.log($scope
.incMail
+ " inc mail");
165 console
.log($scope
.excMail
+ " exc mail");
166 console
.log($scope
.incGrp
+ " inc group");
167 console
.log($scope
.excGrp
+ " exc group");
169 for(a
in $scope
.incMail
){
170 for(b
in $scope
.excMail
){
171 if($scope
.excMail
[b
]==$scope
.incMail
[a
]){
172 console
.log("should not happen same mail with id " + $scope
.incMail
[a
] + " excluded and included");
177 for(a
in $scope
.incGrp
){
178 for(b
in $scope
.excGrp
){
179 if($scope
.excGrp
[b
]==$scope
.incGrp
[a
]){
180 console
.log("should not happen same group with id " + $scope
.incGrp
[a
] + " excluded and included");
185 $scope
.currentMailing
.scheduled_date
= $scope
.scheddate
.date
+ " " + $scope
.scheddate
.time
;
186 if($scope
.currentMailing
.scheduled_date
!=" "){
187 $scope
.currentMailing
.scheduled_id
= "202";
190 $scope
.currentMailing
.scheduled_date
= null;
192 var result
= crmApi('Mailing', 'create', {
193 id
: $scope
.currentMailing
.id
,
194 name
: $scope
.currentMailing
.name
,
195 visibility
: $scope
.currentMailing
.visibility
,
196 created_id
: $scope
.currentMailing
.created_id
,
197 subject
: $scope
.currentMailing
.subject
,
198 msg_template_id
: $scope
.currentMailing
.msg_template_id
,
199 open_tracking
: $scope
.currentMailing
.open_tracking
,
200 url_tracking
: $scope
.currentMailing
.url_tracking
,
201 forward_replies
: $scope
.currentMailing
.forward_replies
,
202 auto_responder
: $scope
.currentMailing
.auto_responder
,
203 from_name
: $scope
.currentMailing
.from_name
,
204 from_email
: $scope
.currentMailing
.from_email
,
205 replyto_email
: $scope
.currentMailing
.replyto_email
,
206 unsubscribe_id
: $scope
.currentMailing
.unsubscribe_id
,
207 resubscribe_id
: $scope
.currentMailing
.resubscribe_id
,
208 body_html
: $scope
.currentMailing
.body_html
,
209 body_text
: $scope
.currentMailing
.body_text
,
210 scheduled_date
: $scope
.currentMailing
.scheduled_date
,
211 scheduled_id
: $scope
.currentMailing
.scheduled_id
,
212 campaign_id
: $scope
.currentMailing
.campaign_id
,
213 header_id
: $scope
.currentMailing
.header_id
,
214 footer_id
: $scope
.currentMailing
.footer_id
,
215 groups
: {include
: $scope
.incGrp
,
216 exclude
: $scope
.excGrp
218 mailings
: {include
: $scope
.incMail
,
219 exclude
: $scope
.excMail
221 is_completed
: $scope
.currentMailing
.is_completed
,
224 //var result = crmApi('Mailing', 'create', $scope.currentMailing, true);
225 result
.success(function(data
) {
226 if (data
.is_error
== 0) {
227 $scope
.currentMailing
.id
= data
.id
;
236 // Directive to go to the next tab
237 crmMailing
.directive('nexttab', function() {
240 link: function(scope
, element
, attrs
) {
241 $(element
).parent().parent().tabs();
242 $(element
).on("click",function() {
243 scope
.acttab
=scope
.acttab
+1;
244 $(element
).parent().parent().tabs({active
:scope
.acttab
});
251 // Directive to go to the previous tab
252 crmMailing
.directive('prevtab', function() {
255 link: function(scope
, element
, attrs
) {
256 $(element
).parent().parent().tabs();
257 $(element
).on("click",function() {
258 scope
.acttab
=scope
.acttab
-1;
259 $(element
).parent().parent().tabs({active
:scope
.acttab
});
266 // Select 2 Widget for selecting the group
267 crmMailing
.directive('chsgroup',function(){
270 link: function(scope
,element
, attrs
){
273 placeholder
: "Choose Recipients",
275 $(element
).on('select2-selecting', function(e
) {
276 incGroup
.push(e
.val
);
277 console
.log(incGroup
);
283 // Used for the select date option. This is used for giving scheduled_date its date value
284 crmMailing
.directive('chsdate',function(){
290 link: function(scope
,element
,attrs
){
291 $(element
).datepicker({
292 dateFormat
: "yy-mm-dd",
293 onSelect: function(date
) {
294 $(".ui-datepicker a").removeAttr("href");
303 //browsing controller. to add selected files. not working currently
304 crmMailing.controller('browse', function($scope){
305 $scope.fileList = [];
306 $('#fileupload').bind('fileuploadadd', function(e, data){
307 // Add the files to the list
308 numFiles = $scope.fileList.length
309 for (var i=0; i < data.files.length; ++i) {
310 var file = data.files[i];
311 // .$apply to update angular when something else makes changes
313 $scope.fileList.push({name: file.name})
316 // Begin upload immediately
321 //adding directive. to add selected files. not working currently
322 crmMailing.directive('add',function(){
325 link: function(scope,element, attrs){
326 $(document).ready(function(){
327 $('#fileupload').fileupload({
335 //This controller is used for creating the mailing list. Simply gets all the mailing data from civiAPI
336 crmMailing
.controller('mailingListCtrl', function($scope
, crmApi
, mailingList
) {
337 $scope
.mailingList
= mailingList
.values
;
338 $scope
.mailStatus
= _
.pluck(CRM
.crmMailing
.mailStatus
, 'status');
341 })(angular
, CRM
.$, CRM
._
);