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