Commit | Line | Data |
---|---|---|
098de400 TO |
1 | (function(angular, $, _) { |
2 | ||
0b5469cd | 3 | angular.module('crmCxn').controller('CrmCxnManageCtrl', function CrmCxnManageCtrl($scope, apiCalls, crmApi, crmUiAlert, crmBlocker, crmStatus, $timeout, dialogService, crmCxnCheckAddr) { |
098de400 | 4 | var ts = $scope.ts = CRM.ts(null); |
b460b03b TO |
5 | if (apiCalls.appMetas.is_error) { |
6 | $scope.appMetas = []; | |
7 | CRM.alert(apiCalls.appMetas.error_message, ts('Application List Unavailable'), 'error'); | |
8 | } | |
9 | else { | |
10 | $scope.appMetas = apiCalls.appMetas.values; | |
11 | } | |
098de400 TO |
12 | $scope.cxns = apiCalls.cxns.values; |
13 | $scope.alerts = _.where(apiCalls.sysCheck.values, {name: 'checkCxnOverrides'}); | |
14 | ||
0b5469cd C |
15 | crmCxnCheckAddr(apiCalls.cfg.values.siteCallbackUrl).then(function(response) { |
16 | if (response.valid) return; | |
17 | crmUiAlert({ | |
18 | type: 'warning', | |
19 | title: ts('Internet Access Required'), | |
20 | templateUrl: '~/crmCxn/Connectivity.html', | |
21 | scope: $scope.$new(), | |
22 | options: {expires: false} | |
23 | }); | |
24 | }); | |
25 | ||
098de400 TO |
26 | $scope.filter = {}; |
27 | var block = $scope.block = crmBlocker(); | |
28 | ||
29 | _.each($scope.alerts, function(alert){ | |
30 | crmUiAlert({text: alert.message, title: alert.title, type: 'error'}); | |
31 | }); | |
32 | ||
ce8eaaea C |
33 | // Convert array [x] to x|null|error |
34 | function asOne(result, msg) { | |
098de400 TO |
35 | switch (result.length) { |
36 | case 0: | |
37 | return null; | |
38 | case 1: | |
39 | return result[0]; | |
40 | default: | |
ce8eaaea | 41 | throw msg; |
098de400 | 42 | } |
ce8eaaea C |
43 | } |
44 | ||
45 | $scope.findCxnByAppId = function(appId) { | |
46 | var result = _.where($scope.cxns, { | |
47 | app_guid: appId | |
48 | }); | |
49 | return asOne(result, "Error: Too many connections for appId: " + appId); | |
50 | }; | |
51 | ||
52 | $scope.findAppByAppId = function(appId) { | |
53 | var result = _.where($scope.appMetas, { | |
54 | appId: appId | |
55 | }); | |
56 | return asOne(result, "Error: Too many apps for appId: " + appId); | |
098de400 TO |
57 | }; |
58 | ||
59 | $scope.hasAvailApps = function() { | |
60 | // This should usu return after the 1st or 2nd item, but in testing with small# apps, we may exhaust the list. | |
61 | for (var i = 0; i< $scope.appMetas.length; i++) { | |
62 | if (!$scope.findCxnByAppId($scope.appMetas[i].appId)) { | |
63 | return true; | |
64 | } | |
65 | } | |
66 | return false; | |
67 | }; | |
68 | ||
69 | $scope.refreshCxns = function() { | |
70 | crmApi('Cxn', 'get', {sequential: 1}).then(function(result) { | |
71 | $timeout(function(){ | |
72 | $scope.cxns = result.values; | |
73 | }); | |
74 | }); | |
75 | }; | |
76 | ||
77 | $scope.register = function(appMeta) { | |
78 | var reg = crmApi('Cxn', 'register', {app_guid: appMeta.appId}).then($scope.refreshCxns); | |
79 | return block(crmStatus({start: ts('Connecting...'), success: ts('Connected')}, reg)); | |
80 | }; | |
81 | ||
e7041933 TO |
82 | $scope.reregister = function(appMeta) { |
83 | var reg = crmApi('Cxn', 'register', {app_guid: appMeta.appId}).then($scope.refreshCxns); | |
84 | return block(crmStatus({start: ts('Reconnecting...'), success: ts('Reconnected')}, reg)); | |
85 | }; | |
86 | ||
098de400 TO |
87 | $scope.unregister = function(appMeta) { |
88 | var reg = crmApi('Cxn', 'unregister', {app_guid: appMeta.appId, debug: 1}).then($scope.refreshCxns); | |
89 | return block(crmStatus({start: ts('Disconnecting...'), success: ts('Disconnected')}, reg)); | |
90 | }; | |
91 | ||
92 | $scope.toggleCxn = function toggleCxn(cxn) { | |
e3c37dee C |
93 | var is_active = (cxn.is_active=="1" ? 0 : 1); // we switch the flag |
94 | var reg = crmApi('Cxn', 'create', {id: cxn.id, app_guid: cxn.app_meta.appId, is_active: is_active, debug: 1}).then(function(){ | |
95 | cxn.is_active = is_active; | |
098de400 TO |
96 | }); |
97 | return block(crmStatus({start: ts('Saving...'), success: ts('Saved')}, reg)); | |
98 | }; | |
ac61750f TO |
99 | |
100 | $scope.openLink = function openLink(appMeta, page, options) { | |
b1c0b99f | 101 | var promise = crmApi('Cxn', 'getlink', {app_guid: appMeta.appId, page_name: page}).then(function(result) { |
ac61750f TO |
102 | var mode = result.values.mode ? result.values.mode : 'popup'; |
103 | switch (result.values.mode) { | |
104 | case 'iframe': | |
105 | var passThrus = ['height', 'width']; // Options influenced by remote server. | |
106 | options = angular.extend(_.pick(result.values, passThrus), options); | |
107 | $scope.openIframe(result.values.url, options); | |
108 | break; | |
109 | case 'popup': | |
110 | CRM.alert(ts('The page "%1" will open in a popup. If it does not appear automatically, check your browser for notifications.', {1: options.title}), '', 'info'); | |
111 | window.open(result.values.url, 'cxnSettings', 'resizable,scrollbars,status'); | |
112 | break; | |
113 | case 'redirect': | |
114 | window.location = result.values.url; | |
115 | break; | |
116 | default: | |
117 | CRM.alert(ts('Cannot open link. Unrecognized mode.'), '', 'error'); | |
118 | } | |
119 | }); | |
120 | return block(crmStatus({start: ts('Opening...'), success: ''}, promise)); | |
121 | }; | |
122 | ||
123 | // @param Object options -- see dialogService.open | |
124 | $scope.openIframe = function openIframe(url, options) { | |
125 | var model = { | |
126 | url: url | |
127 | }; | |
128 | options = CRM.utils.adjustDialogDefaults(angular.extend( | |
129 | { | |
130 | autoOpen: false, | |
131 | height: 'auto', | |
132 | width: '40%', | |
133 | title: ts('External Link') | |
134 | }, | |
135 | options | |
136 | )); | |
137 | return dialogService.open('cxnLinkDialog', '~/crmCxn/LinkDialogCtrl.html', model, options) | |
138 | .then(function(item) { | |
139 | mailing.msg_template_id = item.id; | |
140 | return item; | |
141 | }); | |
142 | }; | |
098de400 TO |
143 | }); |
144 | ||
145 | })(angular, CRM.$, CRM._); |