Merge pull request #12603 from jitendrapurohit/core-295
[civicrm-core.git] / ang / crmCaseType.js
CommitLineData
4c58e251
TO
1(function(angular, $, _) {
2
0b199194 3 var crmCaseType = angular.module('crmCaseType', CRM.angRequires('crmCaseType'));
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 }];
ad8d1ce3
RO
70 reqs.defaultAssigneeTypes = ['OptionValue', 'get', {
71 option_group_id: 'activity_default_assignee',
72 sequential: 1,
73 options: {
74 limit: 0
75 }
76 }];
9625aad1 77 reqs.relTypes = ['RelationshipType', 'get', {
4324b8d7 78 sequential: 1,
9625aad1
TO
79 options: {
80 sort: CRM.crmCaseType.REL_TYPE_CNAME,
81 limit: 0
82 }
83 }];
84 if ($route.current.params.id !== 'new') {
85 reqs.caseType = ['CaseType', 'getsingle', {
86 id: $route.current.params.id
87 }];
87dcd909 88 }
9625aad1 89 return crmApi(reqs);
4d74de55
TO
90 }
91 }
4c58e251
TO
92 });
93 }
94 ]);
95
95fd24c0
TO
96 // Add a new record by name.
97 // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" />
8fc6fba7 98 crmCaseType.directive('crmAddName', function() {
95fd24c0
TO
99 return {
100 restrict: 'AE',
9597c394 101 template: '<input class="add-activity crm-action-menu fa-plus" type="hidden" />',
bafce1db 102 link: function(scope, element, attrs) {
bafce1db
TO
103
104 var input = $('input', element);
105
106 scope._resetSelection = function() {
107 $(input).select2('close');
108 $(input).select2('val', '');
109 scope[attrs.crmVar] = '';
110 };
111
4324b8d7 112 $(input).crmSelect2({
c0bb8bd4
DB
113 data: function () {
114 return { results: scope[attrs.crmOptions] };
115 },
bafce1db 116 createSearchChoice: function(term) {
4324b8d7 117 return {id: term, text: term + ' (' + ts('new') + ')'};
00eee619 118 },
4324b8d7 119 createSearchChoicePosition: 'bottom',
00eee619 120 placeholder: attrs.placeholder
bafce1db
TO
121 });
122 $(input).on('select2-selecting', function(e) {
123 scope[attrs.crmVar] = e.val;
124 scope.$evalAsync(attrs.crmOnAdd);
125 scope.$evalAsync('_resetSelection()');
126 e.preventDefault();
127 });
128 }
95fd24c0
TO
129 };
130 });
131
d7a470db
DB
132 crmCaseType.directive('crmEditableTabTitle', function($timeout) {
133 return {
134 restrict: 'AE',
135 link: function(scope, element, attrs) {
136 element.addClass('crm-editable crm-editable-enabled');
137 var titleLabel = $(element).find('span');
138 var penIcon = $('<i class="crm-i fa-pencil crm-editable-placeholder"></i>').prependTo(element);
139 var saveButton = $('<button type="button"><i class="crm-i fa-check"></i></button>').appendTo(element);
140 var cancelButton = $('<button type="cancel"><i class="crm-i fa-times"></i></button>').appendTo(element);
141 $('button', element).wrapAll('<div class="crm-editable-form" style="display:none" />');
142 var buttons = $('.crm-editable-form', element);
143 titleLabel.on('click', startEditMode);
144 penIcon.on('click', startEditMode);
145
146 function detectEscapeKeyPress (event) {
147 var isEscape = false;
148
149 if ("key" in event) {
150 isEscape = (event.key == "Escape" || event.key == "Esc");
151 } else {
152 isEscape = (event.keyCode == 27);
153 }
154
155 return isEscape;
156 }
157
158 function detectEnterKeyPress (event) {
159 var isEnter = false;
160
161 if ("key" in event) {
162 isEnter = (event.key == "Enter");
163 } else {
164 isEnter = (event.keyCode == 13);
165 }
166
167 return isEnter;
168 }
169
170 function startEditMode () {
171 if (titleLabel.is(":focus")) {
172 return;
173 }
174
175 penIcon.hide();
176 buttons.show();
177
178 saveButton.click(function () {
179 updateTextValue();
180 stopEditMode();
181 });
182
183 cancelButton.click(function () {
184 revertTextValue();
185 stopEditMode();
186 });
187
188 $(element).addClass('crm-editable-editing');
189
190 titleLabel
191 .attr("contenteditable", "true")
192 .focus()
193 .focusout(function (event) {
194 $timeout(function () {
195 revertTextValue();
196 stopEditMode();
197 }, 500);
198 })
199 .keydown(function(event) {
200 event.stopImmediatePropagation();
201
202 if(detectEscapeKeyPress(event)) {
203 revertTextValue();
204 stopEditMode();
205 } else if(detectEnterKeyPress(event)) {
206 event.preventDefault();
207 updateTextValue();
208 stopEditMode();
209 }
210 });
211 }
212
213 function stopEditMode () {
214 titleLabel.removeAttr("contenteditable").off("focusout");
215 titleLabel.off("keydown");
216 saveButton.off("click");
217 cancelButton.off("click");
218 $(element).removeClass('crm-editable-editing');
219
220 penIcon.show();
221 buttons.hide();
222 }
223
224 function revertTextValue () {
225 titleLabel.text(scope.activitySet.label);
226 }
227
228 function updateTextValue () {
229 var updatedTitle = titleLabel.text();
230
231 scope.$evalAsync(function () {
232 scope.activitySet.label = updatedTitle;
233 });
234 }
235 }
236 };
237 });
238
9625aad1 239 crmCaseType.controller('CaseTypeCtrl', function($scope, crmApi, apiCalls) {
ad8d1ce3
RO
240 var REL_TYPE_CNAME, defaultAssigneeDefaultValue, ts;
241
242 (function init () {
243 // CRM_Case_XMLProcessor::REL_TYPE_CNAME
244 REL_TYPE_CNAME = CRM.crmCaseType.REL_TYPE_CNAME;
245
246 ts = $scope.ts = CRM.ts(null);
247 $scope.locks = { caseTypeName: true, activitySetName: true };
248 $scope.workflows = { timeline: 'Timeline', sequence: 'Sequence' };
249 defaultAssigneeDefaultValue = _.find(apiCalls.defaultAssigneeTypes.values, { is_default: '1' }) || {};
250
251 storeApiCallsResults();
252 initCaseType();
253 initCaseTypeDefinition();
254 initSelectedStatuses();
255 })();
256
257 /// Stores the api calls results in the $scope object
258 function storeApiCallsResults() {
259 $scope.activityStatuses = apiCalls.actStatuses.values;
260 $scope.caseStatuses = _.indexBy(apiCalls.caseStatuses.values, 'name');
261 $scope.activityTypes = _.indexBy(apiCalls.actTypes.values, 'name');
262 $scope.activityTypeOptions = _.map(apiCalls.actTypes.values, formatActivityTypeOption);
263 $scope.defaultAssigneeTypes = apiCalls.defaultAssigneeTypes.values;
264 $scope.relationshipTypeOptions = _.map(apiCalls.relTypes.values, function(type) {
265 return {id: type[REL_TYPE_CNAME], text: type.label_b_a};
266 });
68098e7b 267 $scope.defaultRelationshipTypeOptions = getDefaultRelationshipTypeOptions();
ad8d1ce3
RO
268 // stores the default assignee values indexed by their option name:
269 $scope.defaultAssigneeTypeValues = _.chain($scope.defaultAssigneeTypes)
270 .indexBy('name').mapValues('value').value();
271 }
272
68098e7b
RO
273 /// Returns the default relationship type options. If the relationship is
274 /// bidirectional (Ex: Spouse of) it adds a single option otherwise it adds
275 /// two options representing the relationship type directions
276 /// (Ex: Employee of, Employer is)
277 function getDefaultRelationshipTypeOptions() {
278 return _.transform(apiCalls.relTypes.values, function(result, relType) {
279 var isBidirectionalRelationship = relType.label_a_b === relType.label_b_a;
280
281 result.push({
282 label: relType.label_b_a,
283 value: relType.id + '_b_a'
284 });
285
286 if (!isBidirectionalRelationship) {
287 result.push({
288 label: relType.label_a_b,
289 value: relType.id + '_a_b'
290 });
291 }
292 }, []);
293 }
294
ad8d1ce3
RO
295 /// initializes the case type object
296 function initCaseType() {
297 var isNewCaseType = !apiCalls.caseType;
298
299 if (isNewCaseType) {
300 $scope.caseType = _.cloneDeep(newCaseTypeTemplate);
301 } else {
302 $scope.caseType = apiCalls.caseType;
303 }
304 }
76e4acb8 305
ad8d1ce3
RO
306 /// initializes the case type definition object
307 function initCaseTypeDefinition() {
308 $scope.caseType.definition = $scope.caseType.definition || [];
309 $scope.caseType.definition.activityTypes = $scope.caseType.definition.activityTypes || [];
310 $scope.caseType.definition.activitySets = $scope.caseType.definition.activitySets || [];
311 $scope.caseType.definition.caseRoles = $scope.caseType.definition.caseRoles || [];
312 $scope.caseType.definition.statuses = $scope.caseType.definition.statuses || [];
313 $scope.caseType.definition.timelineActivityTypes = $scope.caseType.definition.timelineActivityTypes || [];
314
315 _.each($scope.caseType.definition.activitySets, function (set) {
316 _.each(set.activityTypes, function (type, name) {
317 var isDefaultAssigneeTypeUndefined = _.isUndefined(type.default_assignee_type);
318 type.label = $scope.activityTypes[type.name].label;
319
320 if (isDefaultAssigneeTypeUndefined) {
321 type.default_assignee_type = defaultAssigneeDefaultValue.value;
322 }
323 });
be5aae33 324 });
ad8d1ce3 325 }
7c2b40d1 326
ad8d1ce3
RO
327 /// initializes the selected statuses
328 function initSelectedStatuses() {
329 $scope.selectedStatuses = {};
093f1cfd 330
ad8d1ce3
RO
331 _.each(apiCalls.caseStatuses.values, function (status) {
332 $scope.selectedStatuses[status.name] = !$scope.caseType.definition.statuses.length || $scope.caseType.definition.statuses.indexOf(status.name) > -1;
333 });
334 }
4c58e251 335
76e4acb8
TO
336 $scope.addActivitySet = function(workflow) {
337 var activitySet = {};
338 activitySet[workflow] = '1';
339 activitySet.activityTypes = [];
340
341 var offset = 1;
342 var names = _.pluck($scope.caseType.definition.activitySets, 'name');
343 while (_.contains(names, workflow + '_' + offset)) offset++;
344 activitySet.name = workflow + '_' + offset;
345 activitySet.label = (offset == 1 ) ? $scope.workflows[workflow] : ($scope.workflows[workflow] + ' #' + offset);
346
347 $scope.caseType.definition.activitySets.push(activitySet);
348 _.defer(function() {
349 $('.crmCaseType-acttab').tabs('refresh').tabs({active: -1});
350 });
351 };
352
4324b8d7
CW
353 function formatActivityTypeOption(type) {
354 return {id: type.name, text: type.label, icon: type.icon};
355 }
356
357 function addActivityToSet(activitySet, activityTypeName) {
093f1cfd
AP
358 var activity = {
359 name: activityTypeName,
360 label: $scope.activityTypes[activityTypeName].label,
361 status: 'Scheduled',
362 reference_activity: 'Open Case',
363 reference_offset: '1',
ad8d1ce3
RO
364 reference_select: 'newest',
365 default_assignee_type: $scope.defaultAssigneeTypeValues.NONE
093f1cfd
AP
366 };
367 activitySet.activityTypes.push(activity);
368 if(typeof activitySet.timeline !== "undefined" && activitySet.timeline == "1") {
369 $scope.caseType.definition.timelineActivityTypes.push(activity);
370 }
371 }
372
373 function resetTimelineActivityTypes() {
374 $scope.caseType.definition.timelineActivityTypes = [];
375 angular.forEach($scope.caseType.definition.activitySets, function(activitySet) {
376 angular.forEach(activitySet.activityTypes, function(activityType) {
377 $scope.caseType.definition.timelineActivityTypes.push(activityType);
378 });
379 });
4324b8d7
CW
380 }
381
382 function createActivity(name, callback) {
383 CRM.loadForm(CRM.url('civicrm/admin/options/activity_type', {action: 'add', reset: 1, label: name, component_id: 7}))
384 .on('crmFormSuccess', function(e, data) {
385 $scope.activityTypes[data.optionValue.name] = data.optionValue;
386 $scope.activityTypeOptions.push(formatActivityTypeOption(data.optionValue));
387 callback(data.optionValue);
388 $scope.$digest();
389 });
390 }
391
392 // Add a new activity entry to an activity-set
393 $scope.addActivity = function(activitySet, activityType) {
394 if ($scope.activityTypes[activityType]) {
395 addActivityToSet(activitySet, activityType);
396 } else {
397 createActivity(activityType, function(newActivity) {
398 addActivityToSet(activitySet, newActivity.name);
399 });
60dd172b 400 }
d7c25f6c
TO
401 };
402
403 /// Add a new top-level activity-type entry
404 $scope.addActivityType = function(activityType) {
405 var names = _.pluck($scope.caseType.definition.activityTypes, 'name');
406 if (!_.contains(names, activityType)) {
4324b8d7
CW
407 // Add an activity type that exists
408 if ($scope.activityTypes[activityType]) {
409 $scope.caseType.definition.activityTypes.push({name: activityType});
410 } else {
411 createActivity(activityType, function(newActivity) {
412 $scope.caseType.definition.activityTypes.push({name: newActivity.name});
413 });
414 }
d7c25f6c
TO
415 }
416 };
417
ad8d1ce3
RO
418 /// Clears the activity's default assignee values for relationship and contact
419 $scope.clearActivityDefaultAssigneeValues = function(activity) {
420 activity.default_assignee_relationship = null;
421 activity.default_assignee_contact = null;
422 };
423
8c7e0ae8
TO
424 /// Add a new role
425 $scope.addRole = function(roles, roleName) {
bafce1db
TO
426 var names = _.pluck($scope.caseType.definition.caseRoles, 'name');
427 if (!_.contains(names, roleName)) {
4324b8d7
CW
428 if (_.where($scope.relationshipTypeOptions, {id: roleName}).length) {
429 roles.push({name: roleName});
430 } else {
431 CRM.loadForm(CRM.url('civicrm/admin/reltype', {action: 'add', reset: 1, label_a_b: roleName, label_b_a: roleName}))
432 .on('crmFormSuccess', function(e, data) {
433 roles.push({name: data.relationshipType[REL_TYPE_CNAME]});
434 $scope.relationshipTypeOptions.push({id: data.relationshipType[REL_TYPE_CNAME], text: data.relationshipType.label_b_a});
435 $scope.$digest();
436 });
437 }
60dd172b 438 }
8c7e0ae8
TO
439 };
440
4c58e251
TO
441 $scope.onManagerChange = function(managerRole) {
442 angular.forEach($scope.caseType.definition.caseRoles, function(caseRole) {
443 if (caseRole != managerRole) {
444 caseRole.manager = '0';
445 }
446 });
447 };
448
449 $scope.removeItem = function(array, item) {
450 var idx = _.indexOf(array, item);
451 if (idx != -1) {
452 array.splice(idx, 1);
093f1cfd 453 resetTimelineActivityTypes();
4c58e251
TO
454 }
455 };
456
b40b4114 457 $scope.isForkable = function() {
f2bad133 458 return !$scope.caseType.id || $scope.caseType.is_forkable;
b40b4114
TO
459 };
460
7c2b40d1
CW
461 $scope.newStatus = function() {
462 CRM.loadForm(CRM.url('civicrm/admin/options/case_status', {action: 'add', reset: 1}))
463 .on('crmFormSuccess', function(e, data) {
464 $scope.caseStatuses[data.optionValue.name] = data.optionValue;
465 $scope.selectedStatuses[data.optionValue.name] = true;
466 $scope.$digest();
467 });
468 };
469
5d973e24
TO
470 $scope.isNewActivitySetAllowed = function(workflow) {
471 switch (workflow) {
472 case 'timeline':
473 return true;
b387506c 474 case 'sequence':
b04e5ffb 475 return 0 === _.where($scope.caseType.definition.activitySets, {sequence: '1'}).length;
5d973e24 476 default:
bba9b4f0 477 CRM.console('warn', 'Denied access to unrecognized workflow: (' + workflow + ')');
5d973e24
TO
478 return false;
479 }
480 };
481
259a7652 482 $scope.isActivityRemovable = function(activitySet, activity) {
12b84ade 483 return true;
259a7652
TO
484 };
485
f42b448f
TO
486 $scope.isValidName = function(name) {
487 return !name || name.match(/^[a-zA-Z0-9_]+$/);
488 };
489
4c58e251 490 $scope.getWorkflowName = function(activitySet) {
76e4acb8
TO
491 var result = 'Unknown';
492 _.each($scope.workflows, function(value, key) {
493 if (activitySet[key]) result = value;
494 });
495 return result;
4c58e251
TO
496 };
497
498 /**
499 * Determine which HTML partial to use for a particular
500 *
501 * @return string URL of the HTML partial
502 */
503 $scope.activityTableTemplate = function(activitySet) {
504 if (activitySet.timeline) {
ef5d18a1 505 return '~/crmCaseType/timelineTable.html';
b387506c 506 } else if (activitySet.sequence) {
ef5d18a1 507 return '~/crmCaseType/sequenceTable.html';
4c58e251
TO
508 } else {
509 return '';
510 }
511 };
512
513 $scope.dump = function() {
514 console.log($scope.caseType);
76e4acb8
TO
515 };
516
aa1a7c2e 517 $scope.save = function() {
7c2b40d1
CW
518 // Add selected statuses
519 var selectedStatuses = [];
520 _.each($scope.selectedStatuses, function(v, k) {
521 if (v) selectedStatuses.push(k);
522 });
523 // Ignore if ALL or NONE selected
524 $scope.caseType.definition.statuses = selectedStatuses.length == _.size($scope.selectedStatuses) ? [] : selectedStatuses;
c7bccb5f 525 var result = crmApi('CaseType', 'create', $scope.caseType, true);
3140a415 526 result.then(function(data) {
b04e5ffb 527 if (data.is_error === 0 || data.is_error == '0') {
c7bccb5f 528 $scope.caseType.id = data.id;
1ab5b88e 529 window.location.href = '#/caseType';
c7bccb5f 530 }
531 });
aa1a7c2e
TO
532 };
533
76e4acb8
TO
534 $scope.$watchCollection('caseType.definition.activitySets', function() {
535 _.defer(function() {
8fc6fba7 536 $('.crmCaseType-acttab').tabs('refresh');
76e4acb8
TO
537 });
538 });
685acae4 539
540 var updateCaseTypeName = function () {
541 if (!$scope.caseType.id && $scope.locks.caseTypeName) {
542 // Should we do some filtering? Lowercase? Strip whitespace?
a5ca1f48 543 var t = $scope.caseType.title ? $scope.caseType.title : '';
544 $scope.caseType.name = t.replace(/ /g, '_').replace(/[^a-zA-Z0-9_]/g, '').toLowerCase();
685acae4 545 }
546 };
547 $scope.$watch('locks.caseTypeName', updateCaseTypeName);
548 $scope.$watch('caseType.title', updateCaseTypeName);
b40b4114
TO
549
550 if (!$scope.isForkable()) {
551 CRM.alert(ts('The CiviCase XML file for this case-type prohibits editing the definition.'));
552 }
093f1cfd 553
4c58e251
TO
554 });
555
b75c2546 556 crmCaseType.controller('CaseTypeListCtrl', function($scope, crmApi, caseTypes) {
7abbf317
CW
557 var ts = $scope.ts = CRM.ts(null);
558
b75c2546 559 $scope.caseTypes = caseTypes.values;
4b8c8b42
TO
560 $scope.toggleCaseType = function (caseType) {
561 caseType.is_active = (caseType.is_active == '1') ? '0' : '1';
562 crmApi('CaseType', 'create', caseType, true)
c99f1a0a
TO
563 .catch(function (data) {
564 caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; // revert
565 $scope.$digest();
4b8c8b42
TO
566 });
567 };
568 $scope.deleteCaseType = function (caseType) {
eb8e4c2d
TO
569 crmApi('CaseType', 'delete', {id: caseType.id}, {
570 error: function (data) {
7abbf317 571 CRM.alert(data.error_message, ts('Error'), 'error');
eb8e4c2d
TO
572 }
573 })
4b8c8b42 574 .then(function (data) {
c99f1a0a 575 delete caseTypes.values[caseType.id];
4b8c8b42
TO
576 });
577 };
470a458e
TO
578 $scope.revertCaseType = function (caseType) {
579 caseType.definition = 'null';
580 caseType.is_forked = '0';
581 crmApi('CaseType', 'create', caseType, true)
c99f1a0a
TO
582 .catch(function (data) {
583 caseType.is_forked = '1'; // restore
584 $scope.$digest();
470a458e
TO
585 });
586 };
b75c2546
TO
587 });
588
bba9b4f0 589})(angular, CRM.$, CRM._);