| 1 | (function(angular, $, _) { |
| 2 | |
| 3 | angular.module('crmStatusPage').controller('crmStatusPageCtrl', |
| 4 | function($scope, crmApi, crmStatus, statusData) { |
| 5 | $scope.ts = CRM.ts(); |
| 6 | $scope.help = CRM.help; |
| 7 | $scope.formatDate = CRM.utils.formatDate; |
| 8 | $scope.statuses = statusData.values; |
| 9 | |
| 10 | // Refresh the list. Optionally execute api calls first. |
| 11 | function refresh(apiCalls, title) { |
| 12 | title = title || 'Untitled operation'; |
| 13 | apiCalls = (apiCalls || []).concat([['System', 'check', {sequential: 1}]]); |
| 14 | $('#crm-status-list').block(); |
| 15 | crmApi(apiCalls, true) |
| 16 | .then(function(results) { |
| 17 | $scope.statuses = results[results.length - 1].values; |
| 18 | results.forEach(function(result) { |
| 19 | if (result.is_error) { |
| 20 | var error_message = ts(result.error_message); |
| 21 | if (typeof(result.debug_information) !== 'undefined') { |
| 22 | error_message += '<div class="status-debug-information">' + |
| 23 | '<b>' + ts('Debug information') + ':</b><br>' + |
| 24 | result.debug_information + '</div>'; |
| 25 | } |
| 26 | CRM.alert(error_message, ts('Operation failed: ' + title), 'error'); |
| 27 | } |
| 28 | }); |
| 29 | $('#crm-status-list').unblock(); |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | // updates a status preference and refreshes status data |
| 34 | $scope.setPref = function(status, until, visible) { |
| 35 | refresh([ |
| 36 | ['StatusPreference', 'create', { |
| 37 | name: status.name, |
| 38 | ignore_severity: visible ? 0 : status.severity, |
| 39 | hush_until: until |
| 40 | }] |
| 41 | ], 'Set preference'); |
| 42 | }; |
| 43 | |
| 44 | $scope.countVisible = function(visibility) { |
| 45 | return _.filter($scope.statuses, function(s) { |
| 46 | return s.is_visible == visibility && s.severity_id >= 2; |
| 47 | }).length; |
| 48 | }; |
| 49 | |
| 50 | $scope.doAction = function(action) { |
| 51 | function run() { |
| 52 | switch (action.type) { |
| 53 | case 'href': |
| 54 | window.location = CRM.url(action.params.path, action.params.query, action.params.mode); |
| 55 | break; |
| 56 | |
| 57 | case 'api3': |
| 58 | refresh([action.params], action.title); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (action.confirm) { |
| 64 | CRM.confirm({ |
| 65 | title: action.title, |
| 66 | message: action.confirm |
| 67 | }).on('crmConfirm:yes', run); |
| 68 | } else { |
| 69 | run(); |
| 70 | } |
| 71 | }; |
| 72 | }); |
| 73 | |
| 74 | })(angular, CRM.$, CRM._); |