commented code, preview mailing
[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
9 //-------------------------------------------------------------------------------------------------------
10 crmMailing.config(['$routeProvider',
11 function($routeProvider) {
12 $routeProvider.when('/mailing', {
13 templateUrl: partialUrl('mailingList.html'),
14 controller: 'mailingListCtrl',
15 resolve: {
16 mailingList: function($route, crmApi) {
17 return crmApi('Mailing', 'get', {});
18 }
19
20 }
21 }); //This route is used for generating the list of mails created.
22
23
24
25 $routeProvider.when('/mailing/:id', {
26 templateUrl: partialUrl('main.html'),
27 controller: 'mailingCtrl',
28 resolve: {
29 selectedMail: function($route, crmApi) {
30 if ( $route.current.params.id !== 'new') {
31 return crmApi('Mailing', 'getsingle', {id: $route.current.params.id});
32 }
33 else {
34 //created_id has been set to my id. Does not save without created_id. Needs to made generic based on the user
35 return {name: "New Mail", visibility: "Public Pages", url_tracking:"1", forward_replies:"0", created_id: "202", auto_responder:"0", open_tracking:"1",
36 };
37 }
38 }
39 }
40 }); //This route is used for creating new mails and editing the current mails
41 }
42 ]);
43 //-----------------------------------------
44
45
46 //This controller is used in creating new mail and editing current mails
47 crmMailing.controller('mailingCtrl', function($scope, crmApi, selectedMail) {
48
49 //Making some dummy api to see if my from email, reply to email works. To see if all options come in select box
50 $scope.cool_api= [
51 {'name': 'rajgo94',
52 'from_mail': 'rajgo94@gmail.com',
53 'reply_mail': 'rajgo94@gmail.com' },
54 {'name': 'rajgo94_2',
55 'from_mail': 'rajgo94@gmail_2.com',
56 'reply_mail': 'rajgo94@gmail_2.com'},
57 {'name': 'rajgo94_3',
58 'from_mail': 'rajgo94@gmail_3.com',
59 'reply_mail': 'rajgo94@gmail_3.com'}
60 ];
61 //setting variables to the values we have got to the api
62 $scope.partialUrl = partialUrl;
63 $scope.campaignList = CRM.crmMailing.campNames;
64 $scope.mailNameList = _.pluck(CRM.crmCaseType.civiMails, 'name');
65 $scope.groupNamesList = CRM.crmMailing.groupNames;
66 $scope.headerfooter = CRM.crmMailing.headerfooterList;
67 $scope.tmpList = CRM.crmMailing.mesTemplate;
68 $scope.currentMailing = selectedMail;
69 window.ct = $scope.currentMailing;
70
71 //initializing variables we will use for checkboxes, or for purpose of ng-show
72 $scope.acttab=0;
73 $scope.composeS="1";
74 $scope.trackreplies="0";
75 $scope.now="1";
76
77 //to split the value of selectedMail.scheduled_date into the date and time separately
78 $scope.scheddate={};
79 $scope.scheddate.date = "";
80 $scope.scheddate.time = "";
81 $scope.ans="";
82
83
84 $scope.mailAutoResponder="";
85 // To split the scheduled_date into date and time. The date format is not accepting
86 /* if(selectedMail.scheduled_date != ""){
87 $scope.ans= selectedMail.scheduled_date.split(" ");
88 $scope.scheddate.date=$scope.ans[0];
89 $scope.scheddate.time=$scope.ans[1];
90 }*/
91
92 console.log(selectedMail);
93
94 //changing the screen from compose on screen to upload content
95 $scope.upldChange= function(composeS){
96 if(composeS=="1"){
97 return true;
98 }
99 else
100 return false;
101 }
102 //filter so we only get headers from mailing component
103 $scope.isHeader= function(hf){
104 return hf.component_type == "Header";
105 };
106 //filter so we only get footers from mailing component
107 $scope.isFooter= function(f){
108 return f.component_type == "Footer";
109 };
110 //filter so we only get auto-Responders from mailing component
111 $scope.isAuto= function(au){
112 return au.component_type == "Reply";
113 };
114 //filter so we only get userDriven message templates
115 $scope.isUserDriven= function(mstemp){
116 return (parseInt(mstemp.id)>58);
117 };
118 //used for ng-show when trackreplies is selected. Only then we show forward replies and auto-responders options
119 $scope.trackr= function(trackreplies){
120 if(trackreplies=="1"){
121 return true;
122 }
123 else
124 return false;
125 }
126
127 $scope.isGrp= function(grp){
128 return grp.visibility == "Public Pages";
129 };
130
131
132 $scope.save = function() {
133 $scope.currentMailing.scheduled_date= $scope.scheddate.date + " " + $scope.scheddate.time ;
134 if($scope.currentMailing.scheduled_date!=" "){
135 $scope.currentMailing.scheduled_id= "202";
136 }
137 else {
138 $scope.currentMailing.scheduled_date= "";
139 }
140 var result = crmApi('Mailing', 'create', $scope.currentMailing, true);
141 result.success(function(data) {
142 if (data.is_error == 0) {
143 $scope.currentMailing.id = data.id;
144 console.log("OK");
145 }
146 console.log("OK2");
147 });
148 };
149 });
150
151
152 // Directive to go to the next tab
153 crmMailing.directive('nexttab', function() {
154 return {
155 restrict: 'A',
156 link: function(scope, element, attrs) {
157 $(element).parent().parent().tabs();
158 $(element).on("click",function() {
159 scope.acttab=scope.acttab +1;
160 $(element).parent().parent().tabs({active:scope.acttab});
161 console.log("sid");
162 });
163 }
164 };
165 });
166
167 // Directive to go to the previous tab
168 crmMailing.directive('prevtab', function() {
169 return {
170 restrict: 'A',
171 link: function(scope, element, attrs) {
172 $(element).parent().parent().tabs();
173 $(element).on("click",function() {
174 scope.acttab=scope.acttab -1;
175 $(element).parent().parent().tabs({active:scope.acttab});
176 console.log("sid");
177 });
178 }
179 };
180 });
181
182 // Select 2 Widget for selecting the group
183 crmMailing.directive('chsgroup',function(){
184 return {
185 restrict : 'AE',
186 link: function(scope,element, attrs){
187 $(element).select2({
188 width:"400px",
189 placeholder: "Include Group",
190 });
191 }
192 };
193 });
194
195 // Used for the select date option. This is used for giving scheduled_date its date value
196 crmMailing.directive('chsdate',function(){
197 return {
198 scope :{
199 dat : '=send_date'
200 },
201 restrict: 'AE',
202 link: function(scope,element,attrs){
203 $(element).datepicker({
204 dateFormat: 'yy-mm-dd',
205 onSelect: function(date) {
206 $(".ui-datepicker a").removeAttr("href");
207 scope.dat =date;
208 }
209 });
210 }
211 };
212 });
213
214 /*
215 //browsing controller. to add selected files. not working currently
216 crmMailing.controller('browse', function($scope){
217 $scope.fileList = [];
218 $('#fileupload').bind('fileuploadadd', function(e, data){
219 // Add the files to the list
220 numFiles = $scope.fileList.length
221 for (var i=0; i < data.files.length; ++i) {
222 var file = data.files[i];
223 // .$apply to update angular when something else makes changes
224 $scope.$apply(
225 $scope.fileList.push({name: file.name})
226 );
227 }
228 // Begin upload immediately
229 data.submit();
230 });
231 });
232
233 //adding directive. to add selected files. not working currently
234 crmMailing.directive('add',function(){
235 return {
236 restrict : 'AE',
237 link: function(scope,element, attrs){
238 $(document).ready(function(){
239 $('#fileupload').fileupload({
240 dataType: 'json'
241 });
242 });
243 }
244 };
245 });
246 */
247 //This controller is used for creating the mailing list. Simply gets all the mailing data from civiAPI
248 crmMailing.controller('mailingListCtrl', function($scope, crmApi, mailingList) {
249 $scope.mailingList = mailingList.values;
250 $scope.mailStatus = _.pluck(CRM.crmMailing.mailStatus, 'status');
251 });
252
253 })(angular, CRM.$, CRM._);
254
255
256