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