Merge pull request #7452 from seamuslee001/sl_export_tidy
[civicrm-core.git] / ang / crmCxn / ManageCtrl.js
1 (function(angular, $, _) {
2
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) {
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 }
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 // Convert array [x] to x|null|error
23 function asOne(result, msg) {
24 switch (result.length) {
25 case 0:
26 return null;
27 case 1:
28 return result[0];
29 default:
30 throw msg;
31 }
32 }
33
34 $scope.findCxnByAppId = function(appId) {
35 var result = _.where($scope.cxns, {
36 app_guid: appId
37 });
38 return asOne(result, "Error: Too many connections for appId: " + appId);
39 };
40
41 $scope.findAppByAppId = function(appId) {
42 var result = _.where($scope.appMetas, {
43 appId: appId
44 });
45 return asOne(result, "Error: Too many apps for appId: " + appId);
46 };
47
48 $scope.hasAvailApps = function() {
49 // This should usu return after the 1st or 2nd item, but in testing with small# apps, we may exhaust the list.
50 for (var i = 0; i< $scope.appMetas.length; i++) {
51 if (!$scope.findCxnByAppId($scope.appMetas[i].appId)) {
52 return true;
53 }
54 }
55 return false;
56 };
57
58 $scope.refreshCxns = function() {
59 crmApi('Cxn', 'get', {sequential: 1}).then(function(result) {
60 $timeout(function(){
61 $scope.cxns = result.values;
62 });
63 });
64 };
65
66 $scope.register = function(appMeta) {
67 var reg = crmApi('Cxn', 'register', {app_guid: appMeta.appId}).then($scope.refreshCxns);
68 return block(crmStatus({start: ts('Connecting...'), success: ts('Connected')}, reg));
69 };
70
71 $scope.reregister = function(appMeta) {
72 var reg = crmApi('Cxn', 'register', {app_guid: appMeta.appId}).then($scope.refreshCxns);
73 return block(crmStatus({start: ts('Reconnecting...'), success: ts('Reconnected')}, reg));
74 };
75
76 $scope.unregister = function(appMeta) {
77 var reg = crmApi('Cxn', 'unregister', {app_guid: appMeta.appId, debug: 1}).then($scope.refreshCxns);
78 return block(crmStatus({start: ts('Disconnecting...'), success: ts('Disconnected')}, reg));
79 };
80
81 $scope.toggleCxn = function toggleCxn(cxn) {
82 var reg = crmApi('Cxn', 'create', {id: cxn.id, is_active: !cxn.is_active, debug: 1}).then(function(){
83 cxn.is_active = !cxn.is_active;
84 });
85 return block(crmStatus({start: ts('Saving...'), success: ts('Saved')}, reg));
86 };
87
88 $scope.openLink = function openLink(appMeta, page, options) {
89 var promise = crmApi('Cxn', 'getlink', {app_guid: appMeta.appId, page: page}).then(function(result) {
90 var mode = result.values.mode ? result.values.mode : 'popup';
91 switch (result.values.mode) {
92 case 'iframe':
93 var passThrus = ['height', 'width']; // Options influenced by remote server.
94 options = angular.extend(_.pick(result.values, passThrus), options);
95 $scope.openIframe(result.values.url, options);
96 break;
97 case 'popup':
98 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');
99 window.open(result.values.url, 'cxnSettings', 'resizable,scrollbars,status');
100 break;
101 case 'redirect':
102 window.location = result.values.url;
103 break;
104 default:
105 CRM.alert(ts('Cannot open link. Unrecognized mode.'), '', 'error');
106 }
107 });
108 return block(crmStatus({start: ts('Opening...'), success: ''}, promise));
109 };
110
111 // @param Object options -- see dialogService.open
112 $scope.openIframe = function openIframe(url, options) {
113 var model = {
114 url: url
115 };
116 options = CRM.utils.adjustDialogDefaults(angular.extend(
117 {
118 autoOpen: false,
119 height: 'auto',
120 width: '40%',
121 title: ts('External Link')
122 },
123 options
124 ));
125 return dialogService.open('cxnLinkDialog', '~/crmCxn/LinkDialogCtrl.html', model, options)
126 .then(function(item) {
127 mailing.msg_template_id = item.id;
128 return item;
129 });
130 };
131 });
132
133 })(angular, CRM.$, CRM._);