| 1 | (function(angular, $, _) { |
| 2 | |
| 3 | var crmCaseType = angular.module('crmCaseType', ['ngRoute', 'ui.utils', 'crmUi', 'unsavedChanges', 'crmUtil']); |
| 4 | |
| 5 | // Note: This template will be passed to cloneDeep(), so don't put any funny stuff in here! |
| 6 | var newCaseTypeTemplate = { |
| 7 | title: "", |
| 8 | name: "", |
| 9 | is_active: "1", |
| 10 | weight: "1", |
| 11 | definition: { |
| 12 | activityTypes: [ |
| 13 | {name: 'Open Case', max_instances: 1}, |
| 14 | {name: 'Email'}, |
| 15 | {name: 'Follow up'}, |
| 16 | {name: 'Meeting'}, |
| 17 | {name: 'Phone Call'} |
| 18 | ], |
| 19 | activitySets: [ |
| 20 | { |
| 21 | name: 'standard_timeline', |
| 22 | label: 'Standard Timeline', |
| 23 | timeline: '1', // Angular won't bind checkbox correctly with numeric 1 |
| 24 | activityTypes: [ |
| 25 | {name: 'Open Case', status: 'Completed' } |
| 26 | ] |
| 27 | } |
| 28 | ], |
| 29 | caseRoles: [ |
| 30 | { name: 'Case Coordinator', creator: '1', manager: '1'} |
| 31 | ] |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | crmCaseType.config(['$routeProvider', |
| 36 | function($routeProvider) { |
| 37 | $routeProvider.when('/caseType', { |
| 38 | templateUrl: '~/crmCaseType/list.html', |
| 39 | controller: 'CaseTypeListCtrl', |
| 40 | resolve: { |
| 41 | caseTypes: function($route, crmApi) { |
| 42 | return crmApi('CaseType', 'get', {options: {limit: 0}}); |
| 43 | } |
| 44 | } |
| 45 | }); |
| 46 | $routeProvider.when('/caseType/:id', { |
| 47 | templateUrl: '~/crmCaseType/edit.html', |
| 48 | controller: 'CaseTypeCtrl', |
| 49 | resolve: { |
| 50 | apiCalls: function($route, crmApi) { |
| 51 | var reqs = {}; |
| 52 | reqs.actStatuses = ['OptionValue', 'get', { |
| 53 | option_group_id: 'activity_status' |
| 54 | }]; |
| 55 | reqs.actTypes = ['OptionValue', 'get', { |
| 56 | option_group_id: 'activity_type', |
| 57 | options: { |
| 58 | sort: 'name', |
| 59 | limit: 0 |
| 60 | } |
| 61 | }]; |
| 62 | reqs.relTypes = ['RelationshipType', 'get', { |
| 63 | options: { |
| 64 | sort: CRM.crmCaseType.REL_TYPE_CNAME, |
| 65 | limit: 0 |
| 66 | } |
| 67 | }]; |
| 68 | if ($route.current.params.id !== 'new') { |
| 69 | reqs.caseType = ['CaseType', 'getsingle', { |
| 70 | id: $route.current.params.id |
| 71 | }]; |
| 72 | } |
| 73 | return crmApi(reqs); |
| 74 | } |
| 75 | } |
| 76 | }); |
| 77 | } |
| 78 | ]); |
| 79 | |
| 80 | // Add a new record by name. |
| 81 | // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" /> |
| 82 | crmCaseType.directive('crmAddName', function() { |
| 83 | return { |
| 84 | restrict: 'AE', |
| 85 | template: '<input class="add-activity crm-action-menu fa-plus" type="hidden" />', |
| 86 | link: function(scope, element, attrs) { |
| 87 | /// Format list of options for select2's "data" |
| 88 | var getFormattedOptions = function() { |
| 89 | return { |
| 90 | results: _.map(scope[attrs.crmOptions], function(option){ |
| 91 | return {id: option, text: option}; |
| 92 | }) |
| 93 | }; |
| 94 | }; |
| 95 | |
| 96 | var input = $('input', element); |
| 97 | |
| 98 | scope._resetSelection = function() { |
| 99 | $(input).select2('close'); |
| 100 | $(input).select2('val', ''); |
| 101 | scope[attrs.crmVar] = ''; |
| 102 | }; |
| 103 | |
| 104 | $(input).select2({ |
| 105 | data: getFormattedOptions, |
| 106 | createSearchChoice: function(term) { |
| 107 | return {id: term, text: term}; |
| 108 | }, |
| 109 | placeholder: attrs.placeholder |
| 110 | }); |
| 111 | $(input).on('select2-selecting', function(e) { |
| 112 | scope[attrs.crmVar] = e.val; |
| 113 | scope.$evalAsync(attrs.crmOnAdd); |
| 114 | scope.$evalAsync('_resetSelection()'); |
| 115 | e.preventDefault(); |
| 116 | }); |
| 117 | |
| 118 | scope.$watch(attrs.crmOptions, function(value) { |
| 119 | $(input).select2('data', getFormattedOptions); |
| 120 | $(input).select2('val', ''); |
| 121 | }); |
| 122 | } |
| 123 | }; |
| 124 | }); |
| 125 | |
| 126 | crmCaseType.controller('CaseTypeCtrl', function($scope, crmApi, apiCalls) { |
| 127 | var ts = $scope.ts = CRM.ts(null); |
| 128 | |
| 129 | $scope.activityStatuses = _.values(apiCalls.actStatuses.values); |
| 130 | $scope.activityTypes = apiCalls.actTypes.values; |
| 131 | $scope.activityTypeNames = _.pluck(apiCalls.actTypes.values, 'name'); |
| 132 | $scope.activityTypes = apiCalls.actTypes.values; |
| 133 | $scope.relationshipTypeNames = _.pluck(apiCalls.relTypes.values, CRM.crmCaseType.REL_TYPE_CNAME); // CRM_Case_XMLProcessor::REL_TYPE_CNAME |
| 134 | $scope.locks = {caseTypeName: true, activitySetName: true}; |
| 135 | |
| 136 | $scope.workflows = { |
| 137 | 'timeline': 'Timeline', |
| 138 | 'sequence': 'Sequence' |
| 139 | }; |
| 140 | |
| 141 | $scope.caseType = apiCalls.caseType ? apiCalls.caseType : _.cloneDeep(newCaseTypeTemplate); |
| 142 | $scope.caseType.definition = $scope.caseType.definition || []; |
| 143 | $scope.caseType.definition.activityTypes = $scope.caseType.definition.activityTypes || []; |
| 144 | $scope.caseType.definition.activitySets = $scope.caseType.definition.activitySets || []; |
| 145 | $scope.caseType.definition.caseRoles = $scope.caseType.definition.caseRoles || []; |
| 146 | window.ct = $scope.caseType; |
| 147 | |
| 148 | $scope.addActivitySet = function(workflow) { |
| 149 | var activitySet = {}; |
| 150 | activitySet[workflow] = '1'; |
| 151 | activitySet.activityTypes = []; |
| 152 | |
| 153 | var offset = 1; |
| 154 | var names = _.pluck($scope.caseType.definition.activitySets, 'name'); |
| 155 | while (_.contains(names, workflow + '_' + offset)) offset++; |
| 156 | activitySet.name = workflow + '_' + offset; |
| 157 | activitySet.label = (offset == 1 ) ? $scope.workflows[workflow] : ($scope.workflows[workflow] + ' #' + offset); |
| 158 | |
| 159 | $scope.caseType.definition.activitySets.push(activitySet); |
| 160 | _.defer(function() { |
| 161 | $('.crmCaseType-acttab').tabs('refresh').tabs({active: -1}); |
| 162 | }); |
| 163 | }; |
| 164 | |
| 165 | /// Add a new activity entry to an activity-set |
| 166 | $scope.addActivity = function(activitySet, activityType) { |
| 167 | activitySet.activityTypes.push({ |
| 168 | name: activityType, |
| 169 | status: 'Scheduled', |
| 170 | reference_activity: 'Open Case', |
| 171 | reference_offset: '1', |
| 172 | reference_select: 'newest' |
| 173 | }); |
| 174 | if (!_.contains($scope.activityTypeNames, activityType)) { |
| 175 | $scope.activityTypeNames.push(activityType); |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | /// Add a new top-level activity-type entry |
| 180 | $scope.addActivityType = function(activityType) { |
| 181 | var names = _.pluck($scope.caseType.definition.activityTypes, 'name'); |
| 182 | if (!_.contains(names, activityType)) { |
| 183 | $scope.caseType.definition.activityTypes.push({ |
| 184 | name: activityType |
| 185 | }); |
| 186 | |
| 187 | } |
| 188 | if (!_.contains($scope.activityTypeNames, activityType)) { |
| 189 | $scope.activityTypeNames.push(activityType); |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | /// Add a new role |
| 194 | $scope.addRole = function(roles, roleName) { |
| 195 | var names = _.pluck($scope.caseType.definition.caseRoles, 'name'); |
| 196 | if (!_.contains(names, roleName)) { |
| 197 | roles.push({ |
| 198 | name: roleName |
| 199 | }); |
| 200 | } |
| 201 | if (!_.contains($scope.relationshipTypeNames, roleName)) { |
| 202 | $scope.relationshipTypeNames.push(roleName); |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | $scope.onManagerChange = function(managerRole) { |
| 207 | angular.forEach($scope.caseType.definition.caseRoles, function(caseRole) { |
| 208 | if (caseRole != managerRole) { |
| 209 | caseRole.manager = '0'; |
| 210 | } |
| 211 | }); |
| 212 | }; |
| 213 | |
| 214 | $scope.removeItem = function(array, item) { |
| 215 | var idx = _.indexOf(array, item); |
| 216 | if (idx != -1) { |
| 217 | array.splice(idx, 1); |
| 218 | } |
| 219 | }; |
| 220 | |
| 221 | $scope.isForkable = function() { |
| 222 | return !$scope.caseType.id || $scope.caseType.is_forkable; |
| 223 | }; |
| 224 | |
| 225 | $scope.isNewActivitySetAllowed = function(workflow) { |
| 226 | switch (workflow) { |
| 227 | case 'timeline': |
| 228 | return true; |
| 229 | case 'sequence': |
| 230 | return 0 === _.where($scope.caseType.definition.activitySets, {sequence: '1'}).length; |
| 231 | default: |
| 232 | CRM.console('warn', 'Denied access to unrecognized workflow: (' + workflow + ')'); |
| 233 | return false; |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | $scope.isActivityRemovable = function(activitySet, activity) { |
| 238 | if (activitySet.name == 'standard_timeline' && activity.name == 'Open Case') { |
| 239 | return false; |
| 240 | } else { |
| 241 | return true; |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | $scope.isValidName = function(name) { |
| 246 | return !name || name.match(/^[a-zA-Z0-9_]+$/); |
| 247 | }; |
| 248 | |
| 249 | $scope.getWorkflowName = function(activitySet) { |
| 250 | var result = 'Unknown'; |
| 251 | _.each($scope.workflows, function(value, key) { |
| 252 | if (activitySet[key]) result = value; |
| 253 | }); |
| 254 | return result; |
| 255 | }; |
| 256 | |
| 257 | /** |
| 258 | * Determine which HTML partial to use for a particular |
| 259 | * |
| 260 | * @return string URL of the HTML partial |
| 261 | */ |
| 262 | $scope.activityTableTemplate = function(activitySet) { |
| 263 | if (activitySet.timeline) { |
| 264 | return '~/crmCaseType/timelineTable.html'; |
| 265 | } else if (activitySet.sequence) { |
| 266 | return '~/crmCaseType/sequenceTable.html'; |
| 267 | } else { |
| 268 | return ''; |
| 269 | } |
| 270 | }; |
| 271 | |
| 272 | $scope.dump = function() { |
| 273 | console.log($scope.caseType); |
| 274 | }; |
| 275 | |
| 276 | $scope.save = function() { |
| 277 | var result = crmApi('CaseType', 'create', $scope.caseType, true); |
| 278 | result.then(function(data) { |
| 279 | if (data.is_error === 0 || data.is_error == '0') { |
| 280 | $scope.caseType.id = data.id; |
| 281 | window.location.href = '#/caseType'; |
| 282 | } |
| 283 | }); |
| 284 | }; |
| 285 | |
| 286 | $scope.$watchCollection('caseType.definition.activitySets', function() { |
| 287 | _.defer(function() { |
| 288 | $('.crmCaseType-acttab').tabs('refresh'); |
| 289 | }); |
| 290 | }); |
| 291 | |
| 292 | var updateCaseTypeName = function () { |
| 293 | if (!$scope.caseType.id && $scope.locks.caseTypeName) { |
| 294 | // Should we do some filtering? Lowercase? Strip whitespace? |
| 295 | var t = $scope.caseType.title ? $scope.caseType.title : ''; |
| 296 | $scope.caseType.name = t.replace(/ /g, '_').replace(/[^a-zA-Z0-9_]/g, '').toLowerCase(); |
| 297 | } |
| 298 | }; |
| 299 | $scope.$watch('locks.caseTypeName', updateCaseTypeName); |
| 300 | $scope.$watch('caseType.title', updateCaseTypeName); |
| 301 | |
| 302 | if (!$scope.isForkable()) { |
| 303 | CRM.alert(ts('The CiviCase XML file for this case-type prohibits editing the definition.')); |
| 304 | } |
| 305 | }); |
| 306 | |
| 307 | crmCaseType.controller('CaseTypeListCtrl', function($scope, crmApi, caseTypes) { |
| 308 | var ts = $scope.ts = CRM.ts(null); |
| 309 | |
| 310 | $scope.caseTypes = caseTypes.values; |
| 311 | $scope.toggleCaseType = function (caseType) { |
| 312 | caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; |
| 313 | crmApi('CaseType', 'create', caseType, true) |
| 314 | .catch(function (data) { |
| 315 | caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; // revert |
| 316 | $scope.$digest(); |
| 317 | }); |
| 318 | }; |
| 319 | $scope.deleteCaseType = function (caseType) { |
| 320 | crmApi('CaseType', 'delete', {id: caseType.id}, { |
| 321 | error: function (data) { |
| 322 | CRM.alert(data.error_message, ts('Error'), 'error'); |
| 323 | } |
| 324 | }) |
| 325 | .then(function (data) { |
| 326 | delete caseTypes.values[caseType.id]; |
| 327 | $scope.$digest(); |
| 328 | }); |
| 329 | }; |
| 330 | $scope.revertCaseType = function (caseType) { |
| 331 | caseType.definition = 'null'; |
| 332 | caseType.is_forked = '0'; |
| 333 | crmApi('CaseType', 'create', caseType, true) |
| 334 | .catch(function (data) { |
| 335 | caseType.is_forked = '1'; // restore |
| 336 | $scope.$digest(); |
| 337 | }); |
| 338 | }; |
| 339 | }); |
| 340 | |
| 341 | })(angular, CRM.$, CRM._); |