groups working
[civicrm-core.git] / js / angular-newMailing.js
1 (function(angular, $, _) {
2 //partials for the html pages
3 var partialUrl = function(relPath) {
4 return CRM.resourceUrls['civicrm'] + '/partials/crmMailingType/' + relPath;
5 };
6
7 var crmMailing = angular.module('crmMailing', ['ngRoute', 'ui.utils']);
8 var incGroup = [];
9
10 //-------------------------------------------------------------------------------------------------------
11 crmMailing.config(['$routeProvider',
12 function($routeProvider) {
13 $routeProvider.when('/mailing', {
14 templateUrl: partialUrl('mailingList.html'),
15 controller: 'mailingListCtrl',
16 resolve: {
17 mailingList: function($route, crmApi) {
18 return crmApi('Mailing', 'get', {});
19 }
20
21 }
22 }); //This route is used for generating the list of mails created.
23
24
25
26 $routeProvider.when('/mailing/:id', {
27 templateUrl: partialUrl('main.html'),
28 controller: 'mailingCtrl',
29 resolve: {
30 selectedMail: function($route, crmApi) {
31 if ( $route.current.params.id !== 'new') {
32 return crmApi('Mailing', 'getsingle', {id: $route.current.params.id});
33 }
34 else {
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",
37 };
38 }
39 }
40 }
41 }); //This route is used for creating new mails and editing the current mails
42 }
43 ]);
44 //-----------------------------------------
45
46
47 //This controller is used in creating new mail and editing current mails
48 crmMailing.controller('mailingCtrl', function($scope, crmApi, selectedMail) {
49
50 //Making some dummy api to see if my from email, reply to email works. To see if all options come in select box
51 $scope.cool_api= [
52 {'name': 'rajgo94',
53 'from_mail': 'rajgo94@gmail.com',
54 'reply_mail': 'rajgo94@gmail.com' },
55 {'name': 'rajgo94_2',
56 'from_mail': 'rajgo94@gmail_2.com',
57 'reply_mail': 'rajgo94@gmail_2.com'},
58 {'name': 'rajgo94_3',
59 'from_mail': 'rajgo94@gmail_3.com',
60 'reply_mail': 'rajgo94@gmail_3.com'}
61 ];
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;
74
75 //initializing variables we will use for checkboxes, or for purpose of ng-show
76 $scope.acttab=0;
77 $scope.composeS="1";
78 $scope.trackreplies="0";
79 $scope.now="1";
80
81 //to split the value of selectedMail.scheduled_date into the date and time separately
82 $scope.scheddate={};
83 $scope.scheddate.date = "";
84 $scope.scheddate.time = "";
85 $scope.ans="";
86
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);
94 }
95
96 console.log(selectedMail);
97
98 //changing the screen from compose on screen to upload content
99 $scope.upldChange= function(composeS){
100 if(composeS=="1"){
101 return true;
102 }
103 else
104 return false;
105 }
106 //filter so we only get headers from mailing component
107 $scope.isHeader= function(hf){
108 return hf.component_type == "Header";
109 };
110 //filter so we only get footers from mailing component
111 $scope.isFooter= function(f){
112 return f.component_type == "Footer";
113 };
114 //filter so we only get auto-Responders from mailing component
115 $scope.isAuto= function(au){
116 return au.component_type == "Reply";
117 };
118 //filter so we only get userDriven message templates
119 $scope.isUserDriven= function(mstemp){
120 return (parseInt(mstemp.id)>58);
121 };
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"){
125 return true;
126 }
127 else
128 return false;
129 }
130
131 $scope.isGrp= function(grp){
132 return grp.visibility == "Public Pages";
133 };
134
135 $scope.isCompMail= function(ml){
136 return ml.is_completed == 1;
137 };
138
139 $scope.save = function() {
140 $scope.incGrp=[];
141 $scope.excGrp=[];
142 $scope.incMail=[];
143 $scope.excMail=[];
144 console.log($scope.testGroup + " test group");
145 console.log(incGroup);
146 $scope.answer="";
147 for(req_id in incGroup){
148 $scope.answer = incGroup[req_id].split(" ");
149
150 if($scope.answer[1] == "mail" && $scope.answer[2]=="include"){
151 $scope.incMail.push($scope.answer[0]);
152 }
153 else if($scope.answer[1] == "mail" && $scope.answer[2]=="exclude"){
154 $scope.excMail.push($scope.answer[0]);
155 }
156 if($scope.answer[1] == "group" && $scope.answer[2]=="include"){
157 $scope.incGrp.push($scope.answer[0]);
158 }
159 else if($scope.answer[1] == "group" && $scope.answer[2]=="exclude"){
160 $scope.excGrp.push($scope.answer[0]);
161 }
162 }
163
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");
168
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");
173 }
174 }
175 }
176
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");
181 }
182 }
183 }
184
185 $scope.currentMailing.scheduled_date= $scope.scheddate.date + " " + $scope.scheddate.time ;
186 if($scope.currentMailing.scheduled_date!=" "){
187 $scope.currentMailing.scheduled_id= "202";
188 }
189 else {
190 $scope.currentMailing.scheduled_date= null;
191 }
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
217 },
218 mailings: {include: $scope.incMail,
219 exclude: $scope.excMail
220 },
221 is_completed: $scope.currentMailing.is_completed,
222 },
223 true);
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;
228 console.log("OK");
229 }
230 console.log("OK2");
231 });
232 };
233 });
234
235
236 // Directive to go to the next tab
237 crmMailing.directive('nexttab', function() {
238 return {
239 restrict: 'A',
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});
245 console.log("sid");
246 });
247 }
248 };
249 });
250
251 // Directive to go to the previous tab
252 crmMailing.directive('prevtab', function() {
253 return {
254 restrict: 'A',
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});
260 console.log("sid");
261 });
262 }
263 };
264 });
265
266 // Select 2 Widget for selecting the group
267 crmMailing.directive('chsgroup',function(){
268 return {
269 restrict : 'AE',
270 link: function(scope,element, attrs){
271 $(element).select2({
272 width:"400px",
273 placeholder: "Choose Recipients",
274 });
275 $(element).on('select2-selecting', function(e) {
276 incGroup.push(e.val);
277 console.log(incGroup);
278 });
279 }
280 };
281 });
282
283 // Used for the select date option. This is used for giving scheduled_date its date value
284 crmMailing.directive('chsdate',function(){
285 return {
286 scope :{
287 dat : '=send_date'
288 },
289 restrict: 'AE',
290 link: function(scope,element,attrs){
291 $(element).datepicker({
292 dateFormat: "yy-mm-dd",
293 onSelect: function(date) {
294 $(".ui-datepicker a").removeAttr("href");
295 scope.dat =date;
296 }
297 });
298 }
299 };
300 });
301
302 /*
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
312 $scope.$apply(
313 $scope.fileList.push({name: file.name})
314 );
315 }
316 // Begin upload immediately
317 data.submit();
318 });
319 });
320
321 //adding directive. to add selected files. not working currently
322 crmMailing.directive('add',function(){
323 return {
324 restrict : 'AE',
325 link: function(scope,element, attrs){
326 $(document).ready(function(){
327 $('#fileupload').fileupload({
328 dataType: 'json'
329 });
330 });
331 }
332 };
333 });
334 */
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');
339 });
340
341 })(angular, CRM.$, CRM._);
342
343
344