1 (function(angular
, $, _
) {
3 angular
.module('crmCxn').controller('CrmCxnManageCtrl', function CrmCxnManageCtrl($scope
, apiCalls
, crmApi
, crmUiAlert
, crmBlocker
, crmStatus
, $timeout
, dialogService
) {
4 var ts
= $scope
.ts
= CRM
.ts(null);
5 if (apiCalls
.appMetas
.is_error
) {
7 CRM
.alert(apiCalls
.appMetas
.error_message
, ts('Application List Unavailable'), 'error');
10 $scope
.appMetas
= apiCalls
.appMetas
.values
;
12 $scope
.cxns
= apiCalls
.cxns
.values
;
13 $scope
.alerts
= _
.where(apiCalls
.sysCheck
.values
, {name
: 'checkCxnOverrides'});
16 var block
= $scope
.block
= crmBlocker();
18 _
.each($scope
.alerts
, function(alert
){
19 crmUiAlert({text
: alert
.message
, title
: alert
.title
, type
: 'error'});
22 $scope
.findCxnByAppId = function(appId
) {
23 var result
= _
.where($scope
.cxns
, {
26 switch (result
.length
) {
32 throw "Error: Too many connections for appId: " + appId
;
36 $scope
.hasAvailApps = function() {
37 // This should usu return after the 1st or 2nd item, but in testing with small# apps, we may exhaust the list.
38 for (var i
= 0; i
< $scope
.appMetas
.length
; i
++) {
39 if (!$scope
.findCxnByAppId($scope
.appMetas
[i
].appId
)) {
46 $scope
.refreshCxns = function() {
47 crmApi('Cxn', 'get', {sequential
: 1}).then(function(result
) {
49 $scope
.cxns
= result
.values
;
54 $scope
.register = function(appMeta
) {
55 var reg
= crmApi('Cxn', 'register', {app_guid
: appMeta
.appId
}).then($scope
.refreshCxns
);
56 return block(crmStatus({start
: ts('Connecting...'), success
: ts('Connected')}, reg
));
59 $scope
.reregister = function(appMeta
) {
60 var reg
= crmApi('Cxn', 'register', {app_guid
: appMeta
.appId
}).then($scope
.refreshCxns
);
61 return block(crmStatus({start
: ts('Reconnecting...'), success
: ts('Reconnected')}, reg
));
64 $scope
.unregister = function(appMeta
) {
65 var reg
= crmApi('Cxn', 'unregister', {app_guid
: appMeta
.appId
, debug
: 1}).then($scope
.refreshCxns
);
66 return block(crmStatus({start
: ts('Disconnecting...'), success
: ts('Disconnected')}, reg
));
69 $scope
.toggleCxn
= function toggleCxn(cxn
) {
70 var reg
= crmApi('Cxn', 'create', {id
: cxn
.id
, is_active
: !cxn
.is_active
, debug
: 1}).then(function(){
71 cxn
.is_active
= !cxn
.is_active
;
73 return block(crmStatus({start
: ts('Saving...'), success
: ts('Saved')}, reg
));
76 $scope
.openLink
= function openLink(appMeta
, page
, options
) {
77 var promise
= crmApi('Cxn', 'getlink', {app_guid
: appMeta
.appId
, page
: page
}).then(function(result
) {
78 var mode
= result
.values
.mode
? result
.values
.mode
: 'popup';
79 switch (result
.values
.mode
) {
81 var passThrus
= ['height', 'width']; // Options influenced by remote server.
82 options
= angular
.extend(_
.pick(result
.values
, passThrus
), options
);
83 $scope
.openIframe(result
.values
.url
, options
);
86 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');
87 window
.open(result
.values
.url
, 'cxnSettings', 'resizable,scrollbars,status');
90 window
.location
= result
.values
.url
;
93 CRM
.alert(ts('Cannot open link. Unrecognized mode.'), '', 'error');
96 return block(crmStatus({start
: ts('Opening...'), success
: ''}, promise
));
99 // @param Object options -- see dialogService.open
100 $scope
.openIframe
= function openIframe(url
, options
) {
104 options
= CRM
.utils
.adjustDialogDefaults(angular
.extend(
109 title
: ts('External Link')
113 return dialogService
.open('cxnLinkDialog', '~/crmCxn/LinkDialogCtrl.html', model
, options
)
114 .then(function(item
) {
115 mailing
.msg_template_id
= item
.id
;
121 })(angular
, CRM
.$, CRM
._
);