Commit | Line | Data |
---|---|---|
4c58e251 TO |
1 | (function(angular, $, _) { |
2 | ||
a8e65974 | 3 | var crmCaseType = angular.module('crmCaseType', ['ngRoute', 'ui.utils', 'crmUi', 'unsavedChanges', 'crmUtil']); |
4c58e251 | 4 | |
506cd414 | 5 | // Note: This template will be passed to cloneDeep(), so don't put any funny stuff in here! |
9be5fc34 TO |
6 | var newCaseTypeTemplate = { |
7 | title: "", | |
8 | name: "", | |
9 | is_active: "1", | |
10 | weight: "1", | |
11 | definition: { | |
12 | activityTypes: [ | |
4d8bbcf6 TO |
13 | {name: 'Open Case', max_instances: 1}, |
14 | {name: 'Email'}, | |
15 | {name: 'Follow up'}, | |
16 | {name: 'Meeting'}, | |
17 | {name: 'Phone Call'} | |
9be5fc34 TO |
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 | } | |
4d74de55 TO |
33 | }; |
34 | ||
4c58e251 TO |
35 | crmCaseType.config(['$routeProvider', |
36 | function($routeProvider) { | |
b75c2546 | 37 | $routeProvider.when('/caseType', { |
ef5d18a1 | 38 | templateUrl: '~/crmCaseType/list.html', |
b75c2546 TO |
39 | controller: 'CaseTypeListCtrl', |
40 | resolve: { | |
41 | caseTypes: function($route, crmApi) { | |
a214ce43 | 42 | return crmApi('CaseType', 'get', {options: {limit: 0}}); |
b75c2546 TO |
43 | } |
44 | } | |
45 | }); | |
4c58e251 | 46 | $routeProvider.when('/caseType/:id', { |
ef5d18a1 | 47 | templateUrl: '~/crmCaseType/edit.html', |
4d74de55 TO |
48 | controller: 'CaseTypeCtrl', |
49 | resolve: { | |
9625aad1 TO |
50 | apiCalls: function($route, crmApi) { |
51 | var reqs = {}; | |
52 | reqs.actStatuses = ['OptionValue', 'get', { | |
7c2b40d1 CW |
53 | option_group_id: 'activity_status', |
54 | sequential: 1, | |
55 | options: {limit: 0} | |
56 | }]; | |
57 | reqs.caseStatuses = ['OptionValue', 'get', { | |
58 | option_group_id: 'case_status', | |
59 | sequential: 1, | |
60 | options: {limit: 0} | |
9625aad1 TO |
61 | }]; |
62 | reqs.actTypes = ['OptionValue', 'get', { | |
63 | option_group_id: 'activity_type', | |
4324b8d7 | 64 | sequential: 1, |
9625aad1 TO |
65 | options: { |
66 | sort: 'name', | |
67 | limit: 0 | |
68 | } | |
69 | }]; | |
70 | reqs.relTypes = ['RelationshipType', 'get', { | |
4324b8d7 | 71 | sequential: 1, |
9625aad1 TO |
72 | options: { |
73 | sort: CRM.crmCaseType.REL_TYPE_CNAME, | |
74 | limit: 0 | |
75 | } | |
76 | }]; | |
77 | if ($route.current.params.id !== 'new') { | |
78 | reqs.caseType = ['CaseType', 'getsingle', { | |
79 | id: $route.current.params.id | |
80 | }]; | |
87dcd909 | 81 | } |
9625aad1 | 82 | return crmApi(reqs); |
4d74de55 TO |
83 | } |
84 | } | |
4c58e251 TO |
85 | }); |
86 | } | |
87 | ]); | |
88 | ||
95fd24c0 TO |
89 | // Add a new record by name. |
90 | // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" /> | |
8fc6fba7 | 91 | crmCaseType.directive('crmAddName', function() { |
95fd24c0 TO |
92 | return { |
93 | restrict: 'AE', | |
9597c394 | 94 | template: '<input class="add-activity crm-action-menu fa-plus" type="hidden" />', |
bafce1db | 95 | link: function(scope, element, attrs) { |
bafce1db TO |
96 | |
97 | var input = $('input', element); | |
98 | ||
99 | scope._resetSelection = function() { | |
100 | $(input).select2('close'); | |
101 | $(input).select2('val', ''); | |
102 | scope[attrs.crmVar] = ''; | |
103 | }; | |
104 | ||
4324b8d7 CW |
105 | $(input).crmSelect2({ |
106 | data: scope[attrs.crmOptions], | |
bafce1db | 107 | createSearchChoice: function(term) { |
4324b8d7 | 108 | return {id: term, text: term + ' (' + ts('new') + ')'}; |
00eee619 | 109 | }, |
4324b8d7 | 110 | createSearchChoicePosition: 'bottom', |
00eee619 | 111 | placeholder: attrs.placeholder |
bafce1db TO |
112 | }); |
113 | $(input).on('select2-selecting', function(e) { | |
114 | scope[attrs.crmVar] = e.val; | |
115 | scope.$evalAsync(attrs.crmOnAdd); | |
116 | scope.$evalAsync('_resetSelection()'); | |
117 | e.preventDefault(); | |
118 | }); | |
60dd172b TO |
119 | |
120 | scope.$watch(attrs.crmOptions, function(value) { | |
4324b8d7 | 121 | $(input).select2('data', scope[attrs.crmOptions]); |
60dd172b TO |
122 | $(input).select2('val', ''); |
123 | }); | |
bafce1db | 124 | } |
95fd24c0 TO |
125 | }; |
126 | }); | |
127 | ||
9625aad1 | 128 | crmCaseType.controller('CaseTypeCtrl', function($scope, crmApi, apiCalls) { |
4324b8d7 CW |
129 | // CRM_Case_XMLProcessor::REL_TYPE_CNAME |
130 | var REL_TYPE_CNAME = CRM.crmCaseType.REL_TYPE_CNAME, | |
131 | ||
132 | ts = $scope.ts = CRM.ts(null); | |
76e4acb8 | 133 | |
7c2b40d1 CW |
134 | $scope.activityStatuses = apiCalls.actStatuses.values; |
135 | $scope.caseStatuses = _.indexBy(apiCalls.caseStatuses.values, 'name'); | |
4324b8d7 CW |
136 | $scope.activityTypes = _.indexBy(apiCalls.actTypes.values, 'name'); |
137 | $scope.activityTypeOptions = _.map(apiCalls.actTypes.values, formatActivityTypeOption); | |
138 | $scope.relationshipTypeOptions = _.map(apiCalls.relTypes.values, function(type) { | |
139 | return {id: type[REL_TYPE_CNAME], text: type.label_b_a}; | |
140 | }); | |
1317b266 | 141 | $scope.locks = {caseTypeName: true, activitySetName: true}; |
dc7a657e | 142 | |
76e4acb8 TO |
143 | $scope.workflows = { |
144 | 'timeline': 'Timeline', | |
b387506c | 145 | 'sequence': 'Sequence' |
76e4acb8 TO |
146 | }; |
147 | ||
9625aad1 | 148 | $scope.caseType = apiCalls.caseType ? apiCalls.caseType : _.cloneDeep(newCaseTypeTemplate); |
87dcd909 | 149 | $scope.caseType.definition = $scope.caseType.definition || []; |
4d74de55 TO |
150 | $scope.caseType.definition.activityTypes = $scope.caseType.definition.activityTypes || []; |
151 | $scope.caseType.definition.activitySets = $scope.caseType.definition.activitySets || []; | |
152 | $scope.caseType.definition.caseRoles = $scope.caseType.definition.caseRoles || []; | |
7c2b40d1 CW |
153 | $scope.caseType.definition.statuses = $scope.caseType.definition.statuses || []; |
154 | ||
155 | $scope.selectedStatuses = {}; | |
156 | _.each(apiCalls.caseStatuses.values, function (status) { | |
157 | $scope.selectedStatuses[status.name] = !$scope.caseType.definition.statuses.length || $scope.caseType.definition.statuses.indexOf(status.name) > -1; | |
158 | }); | |
4c58e251 | 159 | |
76e4acb8 TO |
160 | $scope.addActivitySet = function(workflow) { |
161 | var activitySet = {}; | |
162 | activitySet[workflow] = '1'; | |
163 | activitySet.activityTypes = []; | |
164 | ||
165 | var offset = 1; | |
166 | var names = _.pluck($scope.caseType.definition.activitySets, 'name'); | |
167 | while (_.contains(names, workflow + '_' + offset)) offset++; | |
168 | activitySet.name = workflow + '_' + offset; | |
169 | activitySet.label = (offset == 1 ) ? $scope.workflows[workflow] : ($scope.workflows[workflow] + ' #' + offset); | |
170 | ||
171 | $scope.caseType.definition.activitySets.push(activitySet); | |
172 | _.defer(function() { | |
173 | $('.crmCaseType-acttab').tabs('refresh').tabs({active: -1}); | |
174 | }); | |
175 | }; | |
176 | ||
4324b8d7 CW |
177 | function formatActivityTypeOption(type) { |
178 | return {id: type.name, text: type.label, icon: type.icon}; | |
179 | } | |
180 | ||
181 | function addActivityToSet(activitySet, activityTypeName) { | |
d7c25f6c | 182 | activitySet.activityTypes.push({ |
4324b8d7 | 183 | name: activityTypeName, |
183241d8 | 184 | status: 'Scheduled', |
185 | reference_activity: 'Open Case', | |
186 | reference_offset: '1', | |
187 | reference_select: 'newest' | |
d7c25f6c | 188 | }); |
4324b8d7 CW |
189 | } |
190 | ||
191 | function createActivity(name, callback) { | |
192 | CRM.loadForm(CRM.url('civicrm/admin/options/activity_type', {action: 'add', reset: 1, label: name, component_id: 7})) | |
193 | .on('crmFormSuccess', function(e, data) { | |
194 | $scope.activityTypes[data.optionValue.name] = data.optionValue; | |
195 | $scope.activityTypeOptions.push(formatActivityTypeOption(data.optionValue)); | |
196 | callback(data.optionValue); | |
197 | $scope.$digest(); | |
198 | }); | |
199 | } | |
200 | ||
201 | // Add a new activity entry to an activity-set | |
202 | $scope.addActivity = function(activitySet, activityType) { | |
203 | if ($scope.activityTypes[activityType]) { | |
204 | addActivityToSet(activitySet, activityType); | |
205 | } else { | |
206 | createActivity(activityType, function(newActivity) { | |
207 | addActivityToSet(activitySet, newActivity.name); | |
208 | }); | |
60dd172b | 209 | } |
d7c25f6c TO |
210 | }; |
211 | ||
212 | /// Add a new top-level activity-type entry | |
213 | $scope.addActivityType = function(activityType) { | |
214 | var names = _.pluck($scope.caseType.definition.activityTypes, 'name'); | |
215 | if (!_.contains(names, activityType)) { | |
4324b8d7 CW |
216 | // Add an activity type that exists |
217 | if ($scope.activityTypes[activityType]) { | |
218 | $scope.caseType.definition.activityTypes.push({name: activityType}); | |
219 | } else { | |
220 | createActivity(activityType, function(newActivity) { | |
221 | $scope.caseType.definition.activityTypes.push({name: newActivity.name}); | |
222 | }); | |
223 | } | |
d7c25f6c TO |
224 | } |
225 | }; | |
226 | ||
8c7e0ae8 TO |
227 | /// Add a new role |
228 | $scope.addRole = function(roles, roleName) { | |
bafce1db TO |
229 | var names = _.pluck($scope.caseType.definition.caseRoles, 'name'); |
230 | if (!_.contains(names, roleName)) { | |
4324b8d7 CW |
231 | if (_.where($scope.relationshipTypeOptions, {id: roleName}).length) { |
232 | roles.push({name: roleName}); | |
233 | } else { | |
234 | CRM.loadForm(CRM.url('civicrm/admin/reltype', {action: 'add', reset: 1, label_a_b: roleName, label_b_a: roleName})) | |
235 | .on('crmFormSuccess', function(e, data) { | |
236 | roles.push({name: data.relationshipType[REL_TYPE_CNAME]}); | |
237 | $scope.relationshipTypeOptions.push({id: data.relationshipType[REL_TYPE_CNAME], text: data.relationshipType.label_b_a}); | |
238 | $scope.$digest(); | |
239 | }); | |
240 | } | |
60dd172b | 241 | } |
8c7e0ae8 TO |
242 | }; |
243 | ||
4c58e251 TO |
244 | $scope.onManagerChange = function(managerRole) { |
245 | angular.forEach($scope.caseType.definition.caseRoles, function(caseRole) { | |
246 | if (caseRole != managerRole) { | |
247 | caseRole.manager = '0'; | |
248 | } | |
249 | }); | |
250 | }; | |
251 | ||
252 | $scope.removeItem = function(array, item) { | |
253 | var idx = _.indexOf(array, item); | |
254 | if (idx != -1) { | |
255 | array.splice(idx, 1); | |
256 | } | |
257 | }; | |
258 | ||
b40b4114 | 259 | $scope.isForkable = function() { |
f2bad133 | 260 | return !$scope.caseType.id || $scope.caseType.is_forkable; |
b40b4114 TO |
261 | }; |
262 | ||
7c2b40d1 CW |
263 | $scope.newStatus = function() { |
264 | CRM.loadForm(CRM.url('civicrm/admin/options/case_status', {action: 'add', reset: 1})) | |
265 | .on('crmFormSuccess', function(e, data) { | |
266 | $scope.caseStatuses[data.optionValue.name] = data.optionValue; | |
267 | $scope.selectedStatuses[data.optionValue.name] = true; | |
268 | $scope.$digest(); | |
269 | }); | |
270 | }; | |
271 | ||
5d973e24 TO |
272 | $scope.isNewActivitySetAllowed = function(workflow) { |
273 | switch (workflow) { | |
274 | case 'timeline': | |
275 | return true; | |
b387506c | 276 | case 'sequence': |
b04e5ffb | 277 | return 0 === _.where($scope.caseType.definition.activitySets, {sequence: '1'}).length; |
5d973e24 | 278 | default: |
bba9b4f0 | 279 | CRM.console('warn', 'Denied access to unrecognized workflow: (' + workflow + ')'); |
5d973e24 TO |
280 | return false; |
281 | } | |
282 | }; | |
283 | ||
259a7652 TO |
284 | $scope.isActivityRemovable = function(activitySet, activity) { |
285 | if (activitySet.name == 'standard_timeline' && activity.name == 'Open Case') { | |
286 | return false; | |
287 | } else { | |
288 | return true; | |
289 | } | |
290 | }; | |
291 | ||
f42b448f TO |
292 | $scope.isValidName = function(name) { |
293 | return !name || name.match(/^[a-zA-Z0-9_]+$/); | |
294 | }; | |
295 | ||
4c58e251 | 296 | $scope.getWorkflowName = function(activitySet) { |
76e4acb8 TO |
297 | var result = 'Unknown'; |
298 | _.each($scope.workflows, function(value, key) { | |
299 | if (activitySet[key]) result = value; | |
300 | }); | |
301 | return result; | |
4c58e251 TO |
302 | }; |
303 | ||
304 | /** | |
305 | * Determine which HTML partial to use for a particular | |
306 | * | |
307 | * @return string URL of the HTML partial | |
308 | */ | |
309 | $scope.activityTableTemplate = function(activitySet) { | |
310 | if (activitySet.timeline) { | |
ef5d18a1 | 311 | return '~/crmCaseType/timelineTable.html'; |
b387506c | 312 | } else if (activitySet.sequence) { |
ef5d18a1 | 313 | return '~/crmCaseType/sequenceTable.html'; |
4c58e251 TO |
314 | } else { |
315 | return ''; | |
316 | } | |
317 | }; | |
318 | ||
319 | $scope.dump = function() { | |
320 | console.log($scope.caseType); | |
76e4acb8 TO |
321 | }; |
322 | ||
aa1a7c2e | 323 | $scope.save = function() { |
7c2b40d1 CW |
324 | // Add selected statuses |
325 | var selectedStatuses = []; | |
326 | _.each($scope.selectedStatuses, function(v, k) { | |
327 | if (v) selectedStatuses.push(k); | |
328 | }); | |
329 | // Ignore if ALL or NONE selected | |
330 | $scope.caseType.definition.statuses = selectedStatuses.length == _.size($scope.selectedStatuses) ? [] : selectedStatuses; | |
c7bccb5f | 331 | var result = crmApi('CaseType', 'create', $scope.caseType, true); |
3140a415 | 332 | result.then(function(data) { |
b04e5ffb | 333 | if (data.is_error === 0 || data.is_error == '0') { |
c7bccb5f | 334 | $scope.caseType.id = data.id; |
1ab5b88e | 335 | window.location.href = '#/caseType'; |
c7bccb5f | 336 | } |
337 | }); | |
aa1a7c2e TO |
338 | }; |
339 | ||
76e4acb8 TO |
340 | $scope.$watchCollection('caseType.definition.activitySets', function() { |
341 | _.defer(function() { | |
8fc6fba7 | 342 | $('.crmCaseType-acttab').tabs('refresh'); |
76e4acb8 TO |
343 | }); |
344 | }); | |
685acae4 | 345 | |
346 | var updateCaseTypeName = function () { | |
347 | if (!$scope.caseType.id && $scope.locks.caseTypeName) { | |
348 | // Should we do some filtering? Lowercase? Strip whitespace? | |
a5ca1f48 | 349 | var t = $scope.caseType.title ? $scope.caseType.title : ''; |
350 | $scope.caseType.name = t.replace(/ /g, '_').replace(/[^a-zA-Z0-9_]/g, '').toLowerCase(); | |
685acae4 | 351 | } |
352 | }; | |
353 | $scope.$watch('locks.caseTypeName', updateCaseTypeName); | |
354 | $scope.$watch('caseType.title', updateCaseTypeName); | |
b40b4114 TO |
355 | |
356 | if (!$scope.isForkable()) { | |
357 | CRM.alert(ts('The CiviCase XML file for this case-type prohibits editing the definition.')); | |
358 | } | |
4c58e251 TO |
359 | }); |
360 | ||
b75c2546 | 361 | crmCaseType.controller('CaseTypeListCtrl', function($scope, crmApi, caseTypes) { |
7abbf317 CW |
362 | var ts = $scope.ts = CRM.ts(null); |
363 | ||
b75c2546 | 364 | $scope.caseTypes = caseTypes.values; |
4b8c8b42 TO |
365 | $scope.toggleCaseType = function (caseType) { |
366 | caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; | |
367 | crmApi('CaseType', 'create', caseType, true) | |
c99f1a0a TO |
368 | .catch(function (data) { |
369 | caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; // revert | |
370 | $scope.$digest(); | |
4b8c8b42 TO |
371 | }); |
372 | }; | |
373 | $scope.deleteCaseType = function (caseType) { | |
eb8e4c2d TO |
374 | crmApi('CaseType', 'delete', {id: caseType.id}, { |
375 | error: function (data) { | |
7abbf317 | 376 | CRM.alert(data.error_message, ts('Error'), 'error'); |
eb8e4c2d TO |
377 | } |
378 | }) | |
4b8c8b42 | 379 | .then(function (data) { |
c99f1a0a TO |
380 | delete caseTypes.values[caseType.id]; |
381 | $scope.$digest(); | |
4b8c8b42 TO |
382 | }); |
383 | }; | |
470a458e TO |
384 | $scope.revertCaseType = function (caseType) { |
385 | caseType.definition = 'null'; | |
386 | caseType.is_forked = '0'; | |
387 | crmApi('CaseType', 'create', caseType, true) | |
c99f1a0a TO |
388 | .catch(function (data) { |
389 | caseType.is_forked = '1'; // restore | |
390 | $scope.$digest(); | |
470a458e TO |
391 | }); |
392 | }; | |
b75c2546 TO |
393 | }); |
394 | ||
bba9b4f0 | 395 | })(angular, CRM.$, CRM._); |