Ian province abbreviation patch - issue 724
[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 $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
59 $scope.unregister = function(appMeta) {
60 var reg = crmApi('Cxn', 'unregister', {app_guid: appMeta.appId, debug: 1}).then($scope.refreshCxns);
61 return block(crmStatus({start: ts('Disconnecting...'), success: ts('Disconnected')}, reg));
62 };
63
64 $scope.toggleCxn = function toggleCxn(cxn) {
65 var reg = crmApi('Cxn', 'create', {id: cxn.id, is_active: !cxn.is_active, debug: 1}).then(function(){
66 cxn.is_active = !cxn.is_active;
67 });
68 return block(crmStatus({start: ts('Saving...'), success: ts('Saved')}, reg));
69 };
70
71 $scope.openLink = function openLink(appMeta, page, options) {
72 var promise = crmApi('Cxn', 'getlink', {app_guid: appMeta.appId, page: page}).then(function(result) {
73 var mode = result.values.mode ? result.values.mode : 'popup';
74 switch (result.values.mode) {
75 case 'iframe':
76 var passThrus = ['height', 'width']; // Options influenced by remote server.
77 options = angular.extend(_.pick(result.values, passThrus), options);
78 $scope.openIframe(result.values.url, options);
79 break;
80 case 'popup':
81 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');
82 window.open(result.values.url, 'cxnSettings', 'resizable,scrollbars,status');
83 break;
84 case 'redirect':
85 window.location = result.values.url;
86 break;
87 default:
88 CRM.alert(ts('Cannot open link. Unrecognized mode.'), '', 'error');
89 }
90 });
91 return block(crmStatus({start: ts('Opening...'), success: ''}, promise));
92 };
93
94 // @param Object options -- see dialogService.open
95 $scope.openIframe = function openIframe(url, options) {
96 var model = {
97 url: url
98 };
99 options = CRM.utils.adjustDialogDefaults(angular.extend(
100 {
101 autoOpen: false,
102 height: 'auto',
103 width: '40%',
104 title: ts('External Link')
105 },
106 options
107 ));
108 return dialogService.open('cxnLinkDialog', '~/crmCxn/LinkDialogCtrl.html', model, options)
109 .then(function(item) {
110 mailing.msg_template_id = item.id;
111 return item;
112 });
113 };
114 });
115
116 })(angular, CRM.$, CRM._);