2c005365dc807fcd8d6e5774bd78420c92e4898e
[portal/sdk.git] /
1 appDS2.controller('selfProfileController', function($scope, $http, $modal, $routeParams, $rootScope,SelfProfileService){
2         
3         /************init values***********************/
4         $scope.tableData=[];
5         $scope.profile=[];
6         $scope.oriProfile=[];
7         $scope.ociavailableRoles=[];
8         $scope.ociTimeZones;
9         $scope.ociCountries;
10         var stateList=[];
11         $scope.availableRoles = []; 
12         $scope.timeZones = []; 
13         $scope.selectedTimeZone = {"index":'',"value":"","title":""};
14         $scope.countries = []; 
15         $scope.selectedCountry = {"index":'',"value":"","title":""};
16         
17         $scope.isUserSystemAdmin = false;
18         $scope.profileId='';
19         $scope.showLoader=false;
20         $scope.pageType=1 /*1 for slef page 2 for general profile page*/
21         $scope.pageTitle='Self Profile Detail';
22         $scope.profileIdParam = $routeParams.profileId;
23         if (typeof($scope.profileIdParam) != "undefined" && $scope.profileIdParam!=''){
24                 $scope.pageTitle='Profile Detail';
25                 $scope.pageType=2;
26         }
27         
28         
29         
30         /***********************************************************functions***********************************************/
31         $scope.getProfileDetail = function(profileIdParam){
32                 $scope.showLoader=true;
33                 SelfProfileService.getProfileDetail(profileIdParam).then(function(data){
34                         $scope.showLoader=false;
35                         var j = data;
36                         $scope.data = JSON.parse(j.data);
37                         $scope.profile =JSON.parse($scope.data.profile);                        
38                         $scope.oriProfile=JSON.parse($scope.data.profile); /*original value*/
39                         $scope.profileId = $scope.profile.id;
40                         $scope.ociavailableRoles =JSON.parse($scope.data.availableRoles);
41                         $scope.ociTimeZones=JSON.parse($scope.data.timeZones);
42                         $scope.ociCountries=JSON.parse($scope.data.countries);
43                         stateList=JSON.parse($scope.data.stateList);
44                         
45                         
46                         if($scope.ociavailableRoles)
47                                 $.each($scope.ociavailableRoles, function(i, a){ 
48                                         var availableRole = a;
49                                         availableRole.selected = false;
50                                         $.each($scope.profile.roles, function(j, b){ 
51                                                 if(a.id === b.id) {
52                                                         availableRole.selected = true;
53                                                         if(a.id === 1){
54                                                                 $scope.isUserSystemAdmin = true;
55                                                         }
56                                                 } 
57                                         });
58                                         $scope.availableRoles.push(availableRole);          
59                                 });     
60                         ;
61                         if($scope.ociTimeZones){
62                                 $.each($scope.ociTimeZones, function(i, a){ 
63                                         var timeZone = {"index":i, "value":a.value, "title":a.label};
64                                         $scope.timeZones.push(timeZone);
65                                         if($scope.profile.timeZoneId !== null && a.value === $scope.profile.timeZoneId.toString()){
66                                                 $scope.selectedTimeZone = timeZone;
67                                         }           
68                                 });     
69                         };
70                         if($scope.ociCountries)
71                                 $.each($scope.ociCountries, function(i, a){ 
72                                         var country = {"index":i, "value":a.value, "title":a.label};
73                                         $scope.countries.push(country);
74                                         if(a.value === $scope.profile.country){
75                                                 $scope.selectedCountry = country;
76                                         }           
77                                 });     
78                         ;
79                         stateList = stateList== null? []: stateList;
80                         var selectedState= $scope.profile.state ? $scope.profile.state:"";
81                         $scope.stateList = initDropdownWithLookUp(stateList,selectedState );
82                 },function(error){
83                         $scope.errorPopUp(error);
84                         $scope.showLoader=false;
85                 });
86         }
87         
88         $scope.getSelfProfileDetail = function(){
89                 $scope.showLoader=true;
90                 SelfProfileService.getSelfProfileDetail().then(function(data){
91                         $scope.showLoader=false;
92                         var j = data;
93                         $scope.data = JSON.parse(j.data);
94                         $scope.profile =JSON.parse($scope.data.profile);
95                         $scope.oriProfile=JSON.parse($scope.data.profile); /*original value*/
96                         $scope.profileId = $scope.profile.id;
97                         $scope.ociavailableRoles =JSON.parse($scope.data.availableRoles);
98                         $scope.ociTimeZones=JSON.parse($scope.data.timeZones);
99                         $scope.ociCountries=JSON.parse($scope.data.countries);
100                         stateList=JSON.parse($scope.data.stateList);
101                 
102                         if($scope.ociavailableRoles)
103                                 $.each($scope.ociavailableRoles, function(i, a){ 
104                                         var availableRole = a;
105                                         availableRole.selected = false;
106                                         $.each($scope.profile.roles, function(j, b){ 
107                                                 if(a.id === b.id) {
108                                                         availableRole.selected = true;
109                                                         if(a.id === 1){
110                                                                 $scope.isUserSystemAdmin = true;
111                                                         }
112                                                 } 
113                                         });
114                                         $scope.availableRoles.push(availableRole);          
115                                 });     
116                         ;
117                         if($scope.ociTimeZones){
118                                 $.each($scope.ociTimeZones, function(i, a){ 
119                                         var timeZone = {"index":i, "value":a.value, "title":a.label};
120                                         $scope.timeZones.push(timeZone);
121                                         if($scope.profile.timeZoneId !== null && a.value === $scope.profile.timeZoneId.toString()){
122                                                 $scope.selectedTimeZone = timeZone;
123                                         }           
124                                 });     
125                         };
126                         if($scope.ociCountries)
127                                 $.each($scope.ociCountries, function(i, a){ 
128                                         var country = {"index":i, "value":a.value, "title":a.label};
129                                         $scope.countries.push(country);
130                                         if(a.value === $scope.profile.country){
131                                                 $scope.selectedCountry = country;
132                                         }           
133                                 });     
134                         ;
135                         stateList = stateList== null? []: stateList;
136                         var selectedState= $scope.profile.state ? $scope.profile.state:"";
137                         $scope.stateList = initDropdownWithLookUp(stateList,selectedState );
138                 },function(error){
139                         $scope.errorPopUp(error);
140                         $scope.showLoader=false;
141                 });
142         }
143         
144         
145         $scope.removeRole = function(data) {
146                 var modalInstance = $modal.open({
147                         templateUrl: 'app/fusion/scripts/DS2-view-models/ds2-profile/modals/role-del-confirm.html',
148                         controller: ModalInstanceCtrl,
149                         sizeClass: 'modal-small', 
150                         resolve: {
151                 items: function () {
152                         var message = {
153                                 role:data,
154                                 roleId:$scope.profileId,
155                                 };
156                                 return message;
157                 }
158                 }
159                 });     
160         };
161         $scope.addNewRoleFunctionModalPopup = function(data) {          
162                 var modalInstance = $modal.open({
163                         templateUrl: 'app/fusion/scripts/DS2-view-models/ds2-profile/modals/role-add.html',
164                         controller: ModalInstanceCtrl,
165                         windowClass:'modal-docked',
166                         sizeClass: 'modal-medium', 
167                         resolve: {
168                 items: function () {
169                         var message = {
170                                         roleFunctions:data,
171                                         roleId:$scope.profileId,
172                                         availableRoleFunctions:$scope.ociavailableRoles
173                                                 };
174                                 return message;
175                 }
176                 }
177                 });
178         }
179         
180         
181     $scope.successPopUp = function () {
182         var modalInstance = $modal.open({
183                         templateUrl: 'app/fusion/scripts/DS2-modal/success_modal.html',
184                         controller: ModalInstanceCtrl,
185                         sizeClass: 'modal-small',
186                         resolve: {
187                 items: function () {
188                     return '';
189                 }
190                 }
191                 });
192     };
193     
194     $scope.errorPopUp = function (msg) {
195         var modalInstance = $modal.open({
196                         templateUrl: 'app/fusion/scripts/DS2-modal/error_modal.html',
197                         controller: ModalInstanceCtrl,
198                         sizeClass: 'modal-small',
199                         resolve: {
200                 items: function () {
201                     return msg;
202                 }
203                 }
204                 });
205     };
206         $scope.saveProfile = function() {
207                 $scope.errorMsg = '';
208                 if($scope.oriProfile.orgUserId != $scope.profile.orgUserId){
209                         $scope.errorPopUp('Organization User ID cannot be changed');
210                         return;
211                 }
212                 if($scope.oriProfile.orgManagerUserId != $scope.profile.orgManagerUserId){
213                         $scope.errorPopUp('Organization Manager ID cannot be changed');
214                         return;
215                 }
216                 if($scope.oriProfile.loginId != $scope.profile.loginId){
217                         $scope.errorPopUp('Login ID cannot be changed');
218                         return;
219                 }
220                 if($scope.oriProfile.loginPwd != $scope.profile.loginPwd){
221                         $scope.errorPopUp('Login Password cannot be changed');
222                         return;
223                 }
224                 
225                 var postData={
226                         profile: $scope.profile,
227                 selectedCountry:$scope.selectedCountry!=null?$scope.selectedCountry.value:"",
228                 selectedState:$scope.stateList.selected!=null?$scope.stateList.selected.value:"",
229                 selectedTimeZone:$scope.selectedTimeZone!=null?$scope.selectedTimeZone.value:""
230                 };
231                 SelfProfileService.saveProfile(postData, $scope.profileId).then(function(msg){  
232                         $scope.successPopUp();
233                 },function(error){
234                         $scope.errorPopUp(error);
235                 });
236         }
237         /* updating role tables after adding or deleting*/
238         $rootScope.$on('updateRoles',function(e,d){
239                 $scope.profile.roles = d.data;
240         })
241         /*****init call*****/
242         
243         if ($scope.pageType==2){
244                 $scope.getProfileDetail($scope.profileIdParam);
245         }else{
246                 $scope.getSelfProfileDetail();
247         }
248         
249
250         /****************************************************************popup modal*************************************************************/
251         var ModalInstanceCtrl = function ($scope, $modalInstance, items,$rootScope) {
252                 $scope.roleFun=items;
253                 $scope.msg=items;
254                 $scope.activateRoleConfirmPopUp = function (selected, availableRole) {
255                         $scope.msg.roleFun = availableRole.name;
256                         $scope.msg.selected = selected;
257                         $scope.msg.availableRole = availableRole;
258                         var modalInstance = $modal.open({
259                                 templateUrl: 'app/fusion/scripts/DS2-view-models/ds2-profile/modals/role-add-confirm.html',
260                                 controller: ModalInstanceCtrl,
261                                 sizeClass: 'modal-small', 
262                                 resolve: {
263                         items: function () {
264                             return $scope.msg;
265                         }
266                         }
267                         });
268                 }
269                 $scope.confirmRoleDel = function(role,profileId){
270                         var postData={role:role};
271                         SelfProfileService.removeRole(postData,profileId).then(function(msg){   
272                                 $scope.cancel();
273                                 $scope.roleTableRefresh(profileId);
274                         },function(error){
275                                 $scope.errorPopUp(error);
276                         });
277                 }
278                 $scope.confirmRoleFunAdd = function(availableRole,profileId){
279                         var postData={role:availableRole};              
280                         SelfProfileService.addRole(postData,profileId).then(function(msg){      
281                                 $scope.cancel();
282                                 $scope.roleTableRefresh(profileId);
283                         },function(error){
284                                 $scope.errorPopUp(error);
285                         });
286                 }
287                 
288                 // confirm Role Function Remove
289                 $scope.confirmRoleFunRemove = function(availableRole,profileId){
290                         var postData={role:availableRole};              
291                         SelfProfileService.deRole(postData,profileId).then(function(msg){       
292                                 $scope.cancel();
293                                 $scope.roleTableRefresh(profileId);
294                         },function(error){
295                                 $scope.errorPopUp(error);
296                         });
297                 }
298                 
299             $scope.cancel = function () {
300                 $modalInstance.dismiss('cancel');
301             };
302             
303             $scope.cancelRoleFunSwitch = function (msg) {
304                 $scope.msg.availableRole.selected = !$scope.msg.availableRole.selected;
305                 $modalInstance.dismiss('cancel');
306             };
307             
308             $scope.selfProfileRoleRefresh = function(){
309                 SelfProfileService.getSelfProfileDetail().then(function(data){
310                         var j = data;
311                                 $scope.data = JSON.parse(j.data);
312                                 $scope.profileTemp =JSON.parse($scope.data.profile);
313                                 $rootScope.$broadcast('updateRoles',{data:$scope.profileTemp.roles});
314                 });
315             }
316             $scope.profileRoleRefresh = function(profileId){
317                 SelfProfileService.getProfileDetail(profileId).then(function(data){
318                         var j = data;
319                                 $scope.data = JSON.parse(j.data);
320                                 $scope.profileTemp =JSON.parse($scope.data.profile);
321                                 $rootScope.$broadcast('updateRoles',{data:$scope.profileTemp.roles});
322                 });
323             }
324             $scope.roleTableRefresh = function (profileId) {
325                 if($scope.pageType==1)
326                         $scope.selfProfileRoleRefresh();
327                 else
328                          $scope.profileRoleRefresh(profileId);          
329             };
330
331         };
332
333 });
334 function initDropdownWithLookUp(arr,selectedValue){
335         var dropdownArray=[];
336         var selected = null;
337         if(arr){
338                 for(var i = 0,l = arr.length; i < l; i++) {
339                         var option = { 
340                                 "index" : i ,
341                                 "value" : arr[i].value,
342                                 "title" : arr[i].label
343                             };
344                         dropdownArray.push(option);
345                         if(arr[i].value === selectedValue){
346                                 selected = option;
347                         }
348                 }
349         }
350         var dropDown={};
351         dropDown.options = dropdownArray;
352         dropDown.selected = selected!=null?selected:{"index":'',"value":"","title":""};
353         return dropDown;
354 };