Merge pull request #7469 from lcdservices/CRM-17427
[civicrm-core.git] / ang / crmCxn / ManageCtrl.js
CommitLineData
098de400
TO
1(function(angular, $, _) {
2
ac61750f 3 angular.module('crmCxn').controller('CrmCxnManageCtrl', function CrmCxnManageCtrl($scope, apiCalls, crmApi, crmUiAlert, crmBlocker, crmStatus, $timeout, dialogService) {
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
15 $scope.filter = {};
16 var block = $scope.block = crmBlocker();
17
18 _.each($scope.alerts, function(alert){
19 crmUiAlert({text: alert.message, title: alert.title, type: 'error'});
20 });
21
22 $scope.findCxnByAppId = function(appId) {
23 var result = _.where($scope.cxns, {
24 app_guid: appId
25 });
26 switch (result.length) {
27 case 0:
28 return null;
29 case 1:
30 return result[0];
31 default:
32 throw "Error: Too many connections for appId: " + appId;
33 }
34 };
35
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)) {
40 return true;
41 }
42 }
43 return false;
44 };
45
46 $scope.refreshCxns = function() {
47 crmApi('Cxn', 'get', {sequential: 1}).then(function(result) {
48 $timeout(function(){
49 $scope.cxns = result.values;
50 });
51 });
52 };
53
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));
57 };
58
e7041933
TO
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));
62 };
63
098de400
TO
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));
67 };
68
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;
72 });
73 return block(crmStatus({start: ts('Saving...'), success: ts('Saved')}, reg));
74 };
ac61750f
TO
75
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) {
80 case 'iframe':
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);
84 break;
85 case 'popup':
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');
88 break;
89 case 'redirect':
90 window.location = result.values.url;
91 break;
92 default:
93 CRM.alert(ts('Cannot open link. Unrecognized mode.'), '', 'error');
94 }
95 });
96 return block(crmStatus({start: ts('Opening...'), success: ''}, promise));
97 };
98
99 // @param Object options -- see dialogService.open
100 $scope.openIframe = function openIframe(url, options) {
101 var model = {
102 url: url
103 };
104 options = CRM.utils.adjustDialogDefaults(angular.extend(
105 {
106 autoOpen: false,
107 height: 'auto',
108 width: '40%',
109 title: ts('External Link')
110 },
111 options
112 ));
113 return dialogService.open('cxnLinkDialog', '~/crmCxn/LinkDialogCtrl.html', model, options)
114 .then(function(item) {
115 mailing.msg_template_id = item.id;
116 return item;
117 });
118 };
098de400
TO
119 });
120
121})(angular, CRM.$, CRM._);