[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / support / contact-us / contact-us-manage / contact-us-manage.controller.js
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 'use strict';\r
21 \r
22 (function () {\r
23     class ContactUsManageController {\r
24         constructor($scope,$log, message, $q, $http, conf,contactUsService,confirmBoxService) {\r
25                 $scope.contactUsList=[];\r
26                 $scope.contactUsAllAppList=[];\r
27                 $scope.errMsg='';\r
28                 $scope.newContactUs ={\r
29                                 app:'',\r
30                                 name:'',\r
31                                 email:'',\r
32                                 url:'',\r
33                                 desc:''                         \r
34                 };\r
35                 /*Get the existing contact us first, then based on the existing, filter from all apps*/\r
36                 $scope.getContactUsList = function(){\r
37                         contactUsService.getContactUs().then(res=> {\r
38                         // $log.info('getting getContactUs',res.response);\r
39                         if(res!=null && res.response!=null){\r
40                                 for(var i=0; i<res.response.length;i++){\r
41                                         if(res.response[i].appId!=1)\r
42                                                 $scope.contactUsList.push(res.response[i]);\r
43                                 }\r
44                         }\r
45                         /*get all the apps*/\r
46                         contactUsService.getListOfApp().then(res=> {\r
47                         var tableData=[];\r
48                         $scope.contactUsAllAppListTemp=[];\r
49                         // $log.info('contactUsService::getListOfApp: getting res');\r
50                         var result = (typeof(res.data) != "undefined" && res.data!=null)?res.data:null;\r
51                         // $log.info('contactUsService::getListOfApp: result',result);          \r
52                         var res1 = result;\r
53                         var realAppIndex = 0;\r
54                         $scope.contactUsAllAppList.length=0;\r
55                         for (var i = 1; i <= res1.length; i++) {\r
56                             if (!res1[i - 1].restrictedApp) {\r
57                                 var okToAdd = true;\r
58                                 for(var j =0; j<$scope.contactUsList.length;j++){\r
59                                         if(res1[i - 1].title==$scope.contactUsList[j].appName)\r
60                                                 okToAdd=false;\r
61                                 }\r
62                                 // not allowed to add(duplicate) another entry if the app is already available in the table\r
63                                 if(okToAdd){\r
64                                         if(res1[i - 1].title){\r
65                                                 $scope.contactUsAllAppList.push({\r
66                                             index: realAppIndex,\r
67                                             title: res1[i - 1].title,\r
68                                             value: res1[i - 1].index\r
69                                         });\r
70                                         }       \r
71                                         realAppIndex = realAppIndex + 1;\r
72                                 }         \r
73                             } else {\r
74                                 // $log.debug('contactUsService:getAvailableApps:: Restricted/URL only App will not be used = ' + res1[i - 1].title);\r
75                             }\r
76                         }       \r
77                     }).catch(err=> {\r
78                         $log.error('contactUsService:error:: ', err);\r
79                         \r
80                        \r
81                     }).finally(() => {\r
82                         //this.isLoadingTable = false;\r
83                     });\r
84                 });\r
85                 }\r
86                 \r
87                 $scope.getContactUsList();\r
88                 \r
89                 $scope.closeDialog = function(){\r
90                         $scope.closeThisDialog( $scope.widgetData);\r
91                 }\r
92                 \r
93                 /*Add new Contact Us*/\r
94                 $scope.newContactUsFun = function(){ \r
95                         if($scope.newContactUs.app.title==null || $scope.newContactUs.app.title=='' ){\r
96                                 confirmBoxService.showInformation('Please select an App to add').then(isConfirmed => {\r
97                                         return;\r
98                                 });     \r
99                         }\r
100                                                 \r
101                         if($scope.newContactUs.url !=null && $scope.newContactUs.url != '' && !validateUrl($scope.newContactUs.url)){\r
102                                 var warningMsg = "Please enter a valid URL";\r
103                                 confirmBoxService.showInformation(warningMsg).then(isConfirmed => {return;});\r
104                                 return;\r
105                         }\r
106                         \r
107                         contactUsService.addContactUs($scope.newContactUs).then(res=> {\r
108                                 // $log.info('contactUsService: add ContactUs successfully');\r
109                                 $scope.contactUsList.length=0;\r
110                                 // $log.info('contactUsService: refreshing the Contact US table');\r
111                                 $scope.getContactUsList();\r
112                                 $scope.errMsg='';\r
113                                 /*      confirmBoxService.showInformation('You have added a new Contact Us item').then(isConfirmed => { });*/\r
114                                 var defaultSel={\r
115                                                 index: 0,\r
116                             title: '',\r
117                             value: ''\r
118                                 }\r
119                                 $scope.newContactUs ={\r
120                                         app:defaultSel,                         \r
121                                         name:'',\r
122                                         email:'',\r
123                                         url:'',\r
124                                         desc:''                         \r
125                         };\r
126 \r
127                 }).catch(err=> {\r
128                     $log.error('contactUsService: addContactUs error:: ', err);\r
129                    // $scope.errMsg=err;\r
130                     confirmBoxService.showInformation('Add Contact Us list failed: ' + err);\r
131                    \r
132                 }).finally(() => {\r
133                     //this.isLoadingTable = false;\r
134                 });\r
135                 }\r
136                 /* Edit Contact Us*/\r
137                 $scope.editContactUsFun = function(contactObj){\r
138                         // $log.info('contactUsService: edit ContactUs save successfully', contactObj);                         \r
139                         var contactUsObj={\r
140                                 appId:contactObj.appId,\r
141                                 appName:contactObj.appName,\r
142                                 description:contactObj.description,\r
143                                 contactName:contactObj.contactName,\r
144                                 contactEmail:contactObj.contactEmail,\r
145                                 url:contactObj.url,                     \r
146                 };\r
147                         \r
148                         contactUsService.modifyContactUs(contactUsObj).then(res=> {\r
149                                 // $log.info('contactUsService: edit ContactUs successfully');\r
150                                 //      confirmBoxService.showInformation('You have saved the changes').then(isConfirmed => {});\r
151                                 $scope.errMsg='';\r
152                                 \r
153                 }).catch(err=> {\r
154                     $log.error('contactUsService: editContactUs error:: ', err);\r
155                     confirmBoxService.showInformation('Edit Contact Us list failed: ' + err);\r
156                    // $scope.errMsg=err;\r
157                 }).finally(() => {\r
158                     //this.isLoadingTable = false;\r
159                 });\r
160                         \r
161                 }\r
162                 \r
163                 $scope.$watch('newContactUs.app.value', (newVal, oldVal) => {\r
164                         for(var i=0;i<$scope.contactUsAllAppList.length;i++){\r
165                         if($scope.contactUsAllAppList[i].value==newVal){\r
166                         $scope.newContactUs.app=angular.copy($scope.contactUsAllAppList[i]);;\r
167                         }\r
168                         }\r
169                         });\r
170                 /*del Contact Us*/\r
171                 $scope.delContactUsFun = function(appObj){\r
172                         var confirmMsg = 'Are you sure you want to delete '+appObj.appName +' from the list?' + ' Press OK to delete.';\r
173                         confirmBoxService.confirm(confirmMsg).then(function (confirmed) {\r
174                     if (confirmed == true) {                            \r
175                         contactUsService.removeContactUs(appObj.appId).then(res=> {   \r
176                                 // $log.info('delContactUsFun: delete ContactUs successfully',res);\r
177                                         $scope.errMsg='';\r
178                                         $scope.contactUsList.length=0;\r
179                                         $scope.getContactUsList();\r
180                                         confirmBoxService.showInformation('Item has been deleted').then(isConfirmed => {});\r
181                         }).catch(err=> {\r
182                             $log.error('contactUsService: addContactUs error:: ', err);\r
183                             confirmBoxService.showInformation('Deletion failed: ' + err);\r
184                            // $scope.errMsg=err;\r
185                         }).finally(() => {\r
186                             //this.isLoadingTable = false;\r
187                         });\r
188                     }\r
189                 });\r
190                         \r
191                 }       \r
192                 \r
193         }        \r
194     }\r
195     ContactUsManageController.$inject = ['$scope','$log',  'message', '$q', '$http', 'conf','contactUsService','confirmBoxService'];\r
196     angular.module('ecompApp').controller('ContactUsManageController', ContactUsManageController);\r
197 \r
198  \r
199 })();\r
200 function validateUrl(value){\r
201     return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF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