f2dec4c9d7bce4c022c2d3d540fa2685cdd6803f
[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 //-------------------------------------------------------------------------------------------------------
12 crmMailing.config(['$routeProvider',
13 function($routeProvider) {
14 $routeProvider.when('/mailing', {
15 templateUrl: partialUrl('mailingList.html'),
16 controller: 'mailingListCtrl',
17 resolve: {
18 mailingList: function($route, crmApi) {
19 return crmApi('Mailing', 'get', {});
20 }
21
22 }
23 }); //This route is used for generating the list of mails created.
24
25
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", forward_replies:"0", created_id: "202", 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) {
50
51 //Making some dummy api to see if my from email, reply to email works. To see if all options come in select box
52 $scope.cool_api= [
53 {'name': 'rajgo94',
54 'from_mail': 'rajgo94@gmail.com',
55 'reply_mail': 'rajgo94@gmail.com' },
56 {'name': 'rajgo94_2',
57 'from_mail': 'rajgo94@gmail_2.com',
58 'reply_mail': 'rajgo94@gmail_2.com'},
59 {'name': 'rajgo94_3',
60 'from_mail': 'rajgo94@gmail_3.com',
61 'reply_mail': 'rajgo94@gmail_3.com'}
62 ];
63 //setting variables to the values we have got to the api
64 $scope.submitted = false;
65 $scope.partialUrl = partialUrl;
66 $scope.campaignList = CRM.crmMailing.campNames;
67 $scope.mailList = CRM.crmMailing.civiMails;
68 $scope.mailNameList = _.pluck(CRM.crmCaseType.civiMails, 'name');
69 $scope.groupNamesList = CRM.crmMailing.groupNames;
70 $scope.headerfooter = CRM.crmMailing.headerfooterList;
71 $scope.eMailing = CRM.crmMailing.emailAdd;
72 $scope.tmpList = CRM.crmMailing.mesTemplate;
73 $scope.currentMailing = selectedMail;
74 $scope.testGroup = "";
75 window.ct = $scope.currentMailing;
76 $scope.param = {};
77 $scope.tst=""
78 if($scope.currentMailing.msg_template_id!=null){
79 $scope.tst=$scope.currentMailing.msg_template_id;
80 }
81 console.log($scope.tst);
82
83 $scope.mailingForm = function() {
84 if ($scope.mailing_form.$valid) {
85 // Submit as normal
86 } else {
87 $scope.mailing_form.submitted = true;
88 }
89 };
90
91 $scope.back = function (){
92 $location.path( "mailing" );
93 };
94
95 $scope.tmp = function (tst){
96 console.log(tst);
97 $scope.currentMailing.msg_template_id=tst;
98 if($scope.currentMailing.msg_template_id == ""){
99 $scope.currentMailing.body_html="";
100 }
101 else{
102 for(var a in $scope.tmpList){
103
104 if($scope.tmpList[a].id==$scope.currentMailing.msg_template_id){
105 $scope.currentMailing.body_html=$scope.tmpList[a].msg_html;
106 }
107 }
108 }
109 };
110
111
112 //initializing variables we will use for checkboxes, or for purpose of ng-show
113 $scope.acttab=0;
114 $scope.composeS="1";
115 if($scope.currentMailing.forward_replies==0 && $scope.currentMailing.auto_responder==0){
116 $scope.trackreplies="0";
117 }
118 else {
119 $scope.trackreplies="1";
120 }
121
122 $scope.now="1";
123
124 $scope.reply = function(){
125 if($scope.trackreplies==0){
126 $scope.trackreplies=1;
127 }
128 else{
129 $scope.trackreplies=0;
130 $scope.currentMailing.forward_replies=0;
131 $scope.currentMailing.auto_responder=0;
132 }
133 }
134
135 $scope.recclicked = function(){
136 $scope.acttab=0;
137 };
138
139 $scope.conclicked = function(){
140 $scope.acttab=1;
141 };
142
143 $scope.schedclicked = function(){
144 $scope.acttab=2;
145 };
146
147 //to split the value of selectedMail.scheduled_date into the date and time separately
148 $scope.scheddate={};
149 $scope.scheddate.date = "";
150 $scope.scheddate.time = "";
151 $scope.ans="";
152
153 // To split the scheduled_date into date and time. The date format is not accepting
154 if(selectedMail.scheduled_date != null){
155 $scope.ans= selectedMail.scheduled_date.split(" ");
156 $scope.scheddate.date=$scope.ans[0];
157 $scope.scheddate.time=$scope.ans[1];
158 console.log("scheddate.date is " + $scope.scheddate.date);
159 console.log("scheddate.time is " + $scope.scheddate.time);
160 }
161
162 console.log(selectedMail);
163
164 //changing the screen from compose on screen to upload content
165 $scope.upldChange= function(composeS){
166 if(composeS=="1"){
167 return true;
168 }
169 else
170 return false;
171 };
172 //filter so we only get headers from mailing component
173 $scope.isHeader= function(hf){
174 return hf.component_type == "Header";
175 };
176 //filter so we only get footers from mailing component
177 $scope.isFooter= function(f){
178 return f.component_type == "Footer";
179 };
180 //filter so we only get auto-Responders from mailing component
181 $scope.isAuto= function(au){
182 return au.component_type == "Reply";
183 };
184 //filter so we only get userDriven message templates
185 $scope.isUserDriven= function(mstemp){
186 return (parseInt(mstemp.id)>58);
187 };
188 //used for ng-show when trackreplies is selected. Only then we show forward replies and auto-responders options
189 $scope.trackr= function(trackreplies){
190 if(trackreplies=="1"){
191 return true;
192 }
193 else
194 return false;
195 }
196
197 $scope.isGrp= function(grp){
198 return grp.visibility == "Public Pages";
199 };
200
201 $scope.isCompMail= function(ml){
202 return ml.is_completed == 1;
203 };
204
205 $scope.upload = function(){
206 console.log($scope.param.file);
207 };
208
209 $scope.upload_2 = function(){
210 console.log($scope.param.file_2);
211 };
212
213 $scope.upload_3 = function(){
214 console.log($scope.param.file_3);
215 };
216
217
218 $scope.save = function() {
219 $scope.incGrp=[];
220 $scope.excGrp=[];
221 $scope.incMail=[];
222 $scope.excMail=[];
223 console.log($scope.testGroup + " test group");
224 console.log(incGroup);
225 $scope.answer="";
226 for(req_id in incGroup){
227 $scope.answer = incGroup[req_id].split(" ");
228
229 if($scope.answer[1] == "mail" && $scope.answer[2]=="include"){
230 $scope.incMail.push($scope.answer[0]);
231 }
232 else if($scope.answer[1] == "mail" && $scope.answer[2]=="exclude"){
233 $scope.excMail.push($scope.answer[0]);
234 }
235 if($scope.answer[1] == "group" && $scope.answer[2]=="include"){
236 $scope.incGrp.push($scope.answer[0]);
237 }
238 else if($scope.answer[1] == "group" && $scope.answer[2]=="exclude"){
239 $scope.excGrp.push($scope.answer[0]);
240 }
241 }
242
243 console.log($scope.incMail + " inc mail");
244 console.log($scope.excMail + " exc mail");
245 console.log($scope.incGrp + " inc group");
246 console.log($scope.excGrp + " exc group");
247
248 for(a in $scope.incMail){
249 for(b in $scope.excMail){
250 if($scope.excMail[b]==$scope.incMail[a]){
251 console.log("should not happen same mail with id " + $scope.incMail[a] + " excluded and included");
252 }
253 }
254 }
255
256 for(a in $scope.incGrp){
257 for(b in $scope.excGrp){
258 if($scope.excGrp[b]==$scope.incGrp[a]){
259 console.log("should not happen same group with id " + $scope.incGrp[a] + " excluded and included");
260 }
261 }
262 }
263
264 $scope.currentMailing.scheduled_date= $scope.scheddate.date + " " + $scope.scheddate.time ;
265 if($scope.currentMailing.scheduled_date!=" "){
266 $scope.currentMailing.scheduled_id= "202";
267 }
268 else {
269 $scope.currentMailing.scheduled_date= null;
270 }
271 var result = crmApi('Mailing', 'create', {
272 id: $scope.currentMailing.id,
273 name: $scope.currentMailing.name,
274 visibility: $scope.currentMailing.visibility,
275 created_id: $scope.currentMailing.created_id,
276 subject: $scope.currentMailing.subject,
277 msg_template_id: $scope.currentMailing.msg_template_id,
278 open_tracking: $scope.currentMailing.open_tracking,
279 url_tracking: $scope.currentMailing.url_tracking,
280 forward_replies: $scope.currentMailing.forward_replies,
281 auto_responder: $scope.currentMailing.auto_responder,
282 from_name: $scope.currentMailing.from_name,
283 from_email: $scope.currentMailing.from_email,
284 replyto_email: $scope.currentMailing.replyto_email,
285 unsubscribe_id: $scope.currentMailing.unsubscribe_id,
286 resubscribe_id: $scope.currentMailing.resubscribe_id,
287 body_html: $scope.currentMailing.body_html,
288 body_text: $scope.currentMailing.body_text,
289 scheduled_date: $scope.currentMailing.scheduled_date,
290 scheduled_id: $scope.currentMailing.scheduled_id,
291 campaign_id: $scope.currentMailing.campaign_id,
292 header_id: $scope.currentMailing.header_id,
293 footer_id: $scope.currentMailing.footer_id,
294 groups: {include: $scope.incGrp,
295 exclude: $scope.excGrp
296 },
297 mailings: {include: $scope.incMail,
298 exclude: $scope.excMail
299 },
300 is_completed: $scope.currentMailing.is_completed,
301 },
302 true);
303 //var result = crmApi('Mailing', 'create', $scope.currentMailing, true);
304 result.success(function(data) {
305 if (data.is_error == 0) {
306 $scope.currentMailing.id = data.id;
307 console.log("OK");
308 }
309 console.log("OK2");
310 });
311 };
312 });
313
314
315 // Directive to go to the next tab
316 crmMailing.directive('nexttab', function() {
317 return {
318 restrict: 'A',
319 link: function(scope, element, attrs) {
320 $(element).parent().parent().tabs();
321 $(element).on("click",function() {
322 scope.acttab=scope.acttab +1;
323 $(element).parent().parent().tabs({active:scope.acttab});
324 console.log("sid");
325 });
326 }
327 };
328 });
329
330 // Directive to go to the previous tab
331 crmMailing.directive('prevtab', function() {
332 return {
333 restrict: 'A',
334 link: function(scope, element, attrs) {
335 $(element).parent().parent().tabs();
336 $(element).on("click",function() {
337 scope.acttab=scope.acttab -1;
338 $(element).parent().parent().tabs({active:scope.acttab});
339 console.log("sid");
340 });
341 }
342 };
343 });
344
345 // Select 2 Widget for selecting the group
346 crmMailing.directive('chsgroup',function(){
347 return {
348 restrict : 'AE',
349 link: function(scope,element, attrs){
350 function format(item) {
351 if(!item.id) {
352 // return `text` for optgroup
353 return item.text;
354 }
355 // return item template
356 var a = item.id.split(" ");
357 if(a[1]=="group" && a[2]=="include")
358 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;
359 if(a[1]=="group" && a[2]=="exclude")
360 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;
361 if(a[1]=="mail" && a[2]=="include")
362 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;
363 if(a[1]=="mail" && a[2]=="exclude")
364 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;
365 }
366
367 $(element).select2({
368 width:"400px",
369 placeholder: "Choose Recipients",
370 formatResult: format,
371 formatSelection: format,
372 escapeMarkup: function(m) { return m; }
373 });
374 $(element).on('select2-selecting', function(e) {
375 incGroup.push(e.val);
376 console.log(incGroup);
377 });
378 $(element).on("select2-removed", function(e) {
379 var index = incGroup.indexOf(e.val);
380 incGroup.splice(index, 1);
381 console.log(incGroup);
382 });
383 }
384 };
385 });
386
387 // Used for the select date option. This is used for giving scheduled_date its date value
388 crmMailing.directive('chsdate',function(){
389 return {
390 restrict: 'AE',
391 link: function(scope,element,attrs){
392 $(element).datepicker({
393 dateFormat: "yy-mm-dd",
394 onSelect: function(date) {
395 $(".ui-datepicker a").removeAttr("href");
396 var ngModelName = this.attributes['ng-model'].value;
397 scope[ngModelName] = date;
398 scope.$apply();
399 }
400 });
401 }
402 };
403 });
404
405 crmMailing.directive('file', function(){
406 return {
407 scope: {
408 file: '='
409 },
410 link: function(scope, el, attrs){
411 el.bind('change', function(event){
412 var files = event.target.files;
413 var file = files[0];
414 scope.file = file ? file.name : undefined;
415 scope.$apply();
416 });
417 }
418 };
419 });
420
421
422 //This controller is used for creating the mailing list. Simply gets all the mailing data from civiAPI
423 crmMailing.controller('mailingListCtrl', function($scope, crmApi, mailingList) {
424 $scope.mailingList = mailingList.values;
425 $scope.mailStatus = _.pluck(CRM.crmMailing.mailStatus, 'status');
426 });
427
428 })(angular, CRM.$, CRM._);
429
430 /* example of params
431 [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 ( ) )
432 */