Commit | Line | Data |
---|---|---|
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 | ||
9625aad1 | 240 | crmCaseType.controller('CaseTypeCtrl', function($scope, crmApi, apiCalls) { |
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); | |
248 | $scope.locks = { caseTypeName: true, activitySetName: true }; | |
249 | $scope.workflows = { timeline: 'Timeline', sequence: 'Sequence' }; | |
250 | defaultAssigneeDefaultValue = _.find(apiCalls.defaultAssigneeTypes.values, { is_default: '1' }) || {}; | |
251 | ||
252 | storeApiCallsResults(); | |
253 | initCaseType(); | |
254 | initCaseTypeDefinition(); | |
255 | initSelectedStatuses(); | |
256 | })(); | |
257 | ||
258 | /// Stores the api calls results in the $scope object | |
259 | function storeApiCallsResults() { | |
260 | $scope.activityStatuses = apiCalls.actStatuses.values; | |
261 | $scope.caseStatuses = _.indexBy(apiCalls.caseStatuses.values, 'name'); | |
262 | $scope.activityTypes = _.indexBy(apiCalls.actTypes.values, 'name'); | |
263 | $scope.activityTypeOptions = _.map(apiCalls.actTypes.values, formatActivityTypeOption); | |
264 | $scope.defaultAssigneeTypes = apiCalls.defaultAssigneeTypes.values; | |
265 | $scope.relationshipTypeOptions = _.map(apiCalls.relTypes.values, function(type) { | |
266 | return {id: type[REL_TYPE_CNAME], text: type.label_b_a}; | |
267 | }); | |
68098e7b | 268 | $scope.defaultRelationshipTypeOptions = getDefaultRelationshipTypeOptions(); |
ad8d1ce3 RO |
269 | // stores the default assignee values indexed by their option name: |
270 | $scope.defaultAssigneeTypeValues = _.chain($scope.defaultAssigneeTypes) | |
271 | .indexBy('name').mapValues('value').value(); | |
272 | } | |
273 | ||
68098e7b RO |
274 | /// Returns the default relationship type options. If the relationship is |
275 | /// bidirectional (Ex: Spouse of) it adds a single option otherwise it adds | |
276 | /// two options representing the relationship type directions | |
277 | /// (Ex: Employee of, Employer is) | |
278 | function getDefaultRelationshipTypeOptions() { | |
279 | return _.transform(apiCalls.relTypes.values, function(result, relType) { | |
280 | var isBidirectionalRelationship = relType.label_a_b === relType.label_b_a; | |
281 | ||
282 | result.push({ | |
283 | label: relType.label_b_a, | |
284 | value: relType.id + '_b_a' | |
285 | }); | |
286 | ||
287 | if (!isBidirectionalRelationship) { | |
288 | result.push({ | |
289 | label: relType.label_a_b, | |
290 | value: relType.id + '_a_b' | |
291 | }); | |
292 | } | |
293 | }, []); | |
294 | } | |
295 | ||
ad8d1ce3 RO |
296 | /// initializes the case type object |
297 | function initCaseType() { | |
298 | var isNewCaseType = !apiCalls.caseType; | |
299 | ||
300 | if (isNewCaseType) { | |
301 | $scope.caseType = _.cloneDeep(newCaseTypeTemplate); | |
302 | } else { | |
303 | $scope.caseType = apiCalls.caseType; | |
304 | } | |
305 | } | |
76e4acb8 | 306 | |
ad8d1ce3 RO |
307 | /// initializes the case type definition object |
308 | function initCaseTypeDefinition() { | |
309 | $scope.caseType.definition = $scope.caseType.definition || []; | |
310 | $scope.caseType.definition.activityTypes = $scope.caseType.definition.activityTypes || []; | |
311 | $scope.caseType.definition.activitySets = $scope.caseType.definition.activitySets || []; | |
312 | $scope.caseType.definition.caseRoles = $scope.caseType.definition.caseRoles || []; | |
313 | $scope.caseType.definition.statuses = $scope.caseType.definition.statuses || []; | |
314 | $scope.caseType.definition.timelineActivityTypes = $scope.caseType.definition.timelineActivityTypes || []; | |
06f21064 TO |
315 | $scope.caseType.definition.restrictActivityAsgmtToCmsUser = $scope.caseType.definition.restrictActivityAsgmtToCmsUser || 0; |
316 | $scope.caseType.definition.activityAsgmtGrps = $scope.caseType.definition.activityAsgmtGrps || []; | |
ad8d1ce3 RO |
317 | |
318 | _.each($scope.caseType.definition.activitySets, function (set) { | |
319 | _.each(set.activityTypes, function (type, name) { | |
320 | var isDefaultAssigneeTypeUndefined = _.isUndefined(type.default_assignee_type); | |
1e921db0 RO |
321 | var typeDefinition = $scope.activityTypes[type.name]; |
322 | type.label = (typeDefinition && typeDefinition.label) || type.name; | |
ad8d1ce3 RO |
323 | |
324 | if (isDefaultAssigneeTypeUndefined) { | |
325 | type.default_assignee_type = defaultAssigneeDefaultValue.value; | |
326 | } | |
327 | }); | |
be5aae33 | 328 | }); |
ad8d1ce3 | 329 | } |
7c2b40d1 | 330 | |
ad8d1ce3 RO |
331 | /// initializes the selected statuses |
332 | function initSelectedStatuses() { | |
333 | $scope.selectedStatuses = {}; | |
093f1cfd | 334 | |
ad8d1ce3 RO |
335 | _.each(apiCalls.caseStatuses.values, function (status) { |
336 | $scope.selectedStatuses[status.name] = !$scope.caseType.definition.statuses.length || $scope.caseType.definition.statuses.indexOf(status.name) > -1; | |
337 | }); | |
338 | } | |
4c58e251 | 339 | |
76e4acb8 TO |
340 | $scope.addActivitySet = function(workflow) { |
341 | var activitySet = {}; | |
342 | activitySet[workflow] = '1'; | |
343 | activitySet.activityTypes = []; | |
344 | ||
345 | var offset = 1; | |
346 | var names = _.pluck($scope.caseType.definition.activitySets, 'name'); | |
347 | while (_.contains(names, workflow + '_' + offset)) offset++; | |
348 | activitySet.name = workflow + '_' + offset; | |
349 | activitySet.label = (offset == 1 ) ? $scope.workflows[workflow] : ($scope.workflows[workflow] + ' #' + offset); | |
350 | ||
351 | $scope.caseType.definition.activitySets.push(activitySet); | |
352 | _.defer(function() { | |
353 | $('.crmCaseType-acttab').tabs('refresh').tabs({active: -1}); | |
354 | }); | |
355 | }; | |
356 | ||
4324b8d7 CW |
357 | function formatActivityTypeOption(type) { |
358 | return {id: type.name, text: type.label, icon: type.icon}; | |
359 | } | |
360 | ||
361 | function addActivityToSet(activitySet, activityTypeName) { | |
0f25eb9c SA |
362 | activitySet.activityTypes = activitySet.activityTypes || []; |
363 | var activity = { | |
093f1cfd AP |
364 | name: activityTypeName, |
365 | label: $scope.activityTypes[activityTypeName].label, | |
366 | status: 'Scheduled', | |
367 | reference_activity: 'Open Case', | |
368 | reference_offset: '1', | |
ad8d1ce3 RO |
369 | reference_select: 'newest', |
370 | default_assignee_type: $scope.defaultAssigneeTypeValues.NONE | |
093f1cfd AP |
371 | }; |
372 | activitySet.activityTypes.push(activity); | |
373 | if(typeof activitySet.timeline !== "undefined" && activitySet.timeline == "1") { | |
374 | $scope.caseType.definition.timelineActivityTypes.push(activity); | |
375 | } | |
376 | } | |
377 | ||
378 | function resetTimelineActivityTypes() { | |
379 | $scope.caseType.definition.timelineActivityTypes = []; | |
380 | angular.forEach($scope.caseType.definition.activitySets, function(activitySet) { | |
381 | angular.forEach(activitySet.activityTypes, function(activityType) { | |
382 | $scope.caseType.definition.timelineActivityTypes.push(activityType); | |
383 | }); | |
384 | }); | |
4324b8d7 CW |
385 | } |
386 | ||
387 | function createActivity(name, callback) { | |
388 | CRM.loadForm(CRM.url('civicrm/admin/options/activity_type', {action: 'add', reset: 1, label: name, component_id: 7})) | |
389 | .on('crmFormSuccess', function(e, data) { | |
390 | $scope.activityTypes[data.optionValue.name] = data.optionValue; | |
391 | $scope.activityTypeOptions.push(formatActivityTypeOption(data.optionValue)); | |
392 | callback(data.optionValue); | |
393 | $scope.$digest(); | |
394 | }); | |
395 | } | |
396 | ||
397 | // Add a new activity entry to an activity-set | |
398 | $scope.addActivity = function(activitySet, activityType) { | |
399 | if ($scope.activityTypes[activityType]) { | |
400 | addActivityToSet(activitySet, activityType); | |
401 | } else { | |
402 | createActivity(activityType, function(newActivity) { | |
403 | addActivityToSet(activitySet, newActivity.name); | |
404 | }); | |
60dd172b | 405 | } |
d7c25f6c TO |
406 | }; |
407 | ||
408 | /// Add a new top-level activity-type entry | |
409 | $scope.addActivityType = function(activityType) { | |
410 | var names = _.pluck($scope.caseType.definition.activityTypes, 'name'); | |
411 | if (!_.contains(names, activityType)) { | |
4324b8d7 CW |
412 | // Add an activity type that exists |
413 | if ($scope.activityTypes[activityType]) { | |
414 | $scope.caseType.definition.activityTypes.push({name: activityType}); | |
415 | } else { | |
416 | createActivity(activityType, function(newActivity) { | |
417 | $scope.caseType.definition.activityTypes.push({name: newActivity.name}); | |
418 | }); | |
419 | } | |
d7c25f6c TO |
420 | } |
421 | }; | |
422 | ||
ad8d1ce3 RO |
423 | /// Clears the activity's default assignee values for relationship and contact |
424 | $scope.clearActivityDefaultAssigneeValues = function(activity) { | |
425 | activity.default_assignee_relationship = null; | |
426 | activity.default_assignee_contact = null; | |
427 | }; | |
428 | ||
8c7e0ae8 TO |
429 | /// Add a new role |
430 | $scope.addRole = function(roles, roleName) { | |
bafce1db TO |
431 | var names = _.pluck($scope.caseType.definition.caseRoles, 'name'); |
432 | if (!_.contains(names, roleName)) { | |
4324b8d7 CW |
433 | if (_.where($scope.relationshipTypeOptions, {id: roleName}).length) { |
434 | roles.push({name: roleName}); | |
435 | } else { | |
436 | CRM.loadForm(CRM.url('civicrm/admin/reltype', {action: 'add', reset: 1, label_a_b: roleName, label_b_a: roleName})) | |
437 | .on('crmFormSuccess', function(e, data) { | |
438 | roles.push({name: data.relationshipType[REL_TYPE_CNAME]}); | |
439 | $scope.relationshipTypeOptions.push({id: data.relationshipType[REL_TYPE_CNAME], text: data.relationshipType.label_b_a}); | |
440 | $scope.$digest(); | |
441 | }); | |
442 | } | |
60dd172b | 443 | } |
8c7e0ae8 TO |
444 | }; |
445 | ||
4c58e251 TO |
446 | $scope.onManagerChange = function(managerRole) { |
447 | angular.forEach($scope.caseType.definition.caseRoles, function(caseRole) { | |
448 | if (caseRole != managerRole) { | |
449 | caseRole.manager = '0'; | |
450 | } | |
451 | }); | |
452 | }; | |
453 | ||
454 | $scope.removeItem = function(array, item) { | |
455 | var idx = _.indexOf(array, item); | |
456 | if (idx != -1) { | |
457 | array.splice(idx, 1); | |
093f1cfd | 458 | resetTimelineActivityTypes(); |
4c58e251 TO |
459 | } |
460 | }; | |
461 | ||
b40b4114 | 462 | $scope.isForkable = function() { |
f2bad133 | 463 | return !$scope.caseType.id || $scope.caseType.is_forkable; |
b40b4114 TO |
464 | }; |
465 | ||
7c2b40d1 CW |
466 | $scope.newStatus = function() { |
467 | CRM.loadForm(CRM.url('civicrm/admin/options/case_status', {action: 'add', reset: 1})) | |
468 | .on('crmFormSuccess', function(e, data) { | |
469 | $scope.caseStatuses[data.optionValue.name] = data.optionValue; | |
470 | $scope.selectedStatuses[data.optionValue.name] = true; | |
471 | $scope.$digest(); | |
472 | }); | |
473 | }; | |
474 | ||
5d973e24 TO |
475 | $scope.isNewActivitySetAllowed = function(workflow) { |
476 | switch (workflow) { | |
477 | case 'timeline': | |
478 | return true; | |
b387506c | 479 | case 'sequence': |
b04e5ffb | 480 | return 0 === _.where($scope.caseType.definition.activitySets, {sequence: '1'}).length; |
5d973e24 | 481 | default: |
bba9b4f0 | 482 | CRM.console('warn', 'Denied access to unrecognized workflow: (' + workflow + ')'); |
5d973e24 TO |
483 | return false; |
484 | } | |
485 | }; | |
486 | ||
259a7652 | 487 | $scope.isActivityRemovable = function(activitySet, activity) { |
12b84ade | 488 | return true; |
259a7652 TO |
489 | }; |
490 | ||
f42b448f TO |
491 | $scope.isValidName = function(name) { |
492 | return !name || name.match(/^[a-zA-Z0-9_]+$/); | |
493 | }; | |
494 | ||
4c58e251 | 495 | $scope.getWorkflowName = function(activitySet) { |
76e4acb8 TO |
496 | var result = 'Unknown'; |
497 | _.each($scope.workflows, function(value, key) { | |
498 | if (activitySet[key]) result = value; | |
499 | }); | |
500 | return result; | |
4c58e251 TO |
501 | }; |
502 | ||
503 | /** | |
504 | * Determine which HTML partial to use for a particular | |
505 | * | |
506 | * @return string URL of the HTML partial | |
507 | */ | |
508 | $scope.activityTableTemplate = function(activitySet) { | |
509 | if (activitySet.timeline) { | |
ef5d18a1 | 510 | return '~/crmCaseType/timelineTable.html'; |
b387506c | 511 | } else if (activitySet.sequence) { |
ef5d18a1 | 512 | return '~/crmCaseType/sequenceTable.html'; |
4c58e251 TO |
513 | } else { |
514 | return ''; | |
515 | } | |
516 | }; | |
517 | ||
518 | $scope.dump = function() { | |
519 | console.log($scope.caseType); | |
76e4acb8 TO |
520 | }; |
521 | ||
aa1a7c2e | 522 | $scope.save = function() { |
7c2b40d1 CW |
523 | // Add selected statuses |
524 | var selectedStatuses = []; | |
525 | _.each($scope.selectedStatuses, function(v, k) { | |
526 | if (v) selectedStatuses.push(k); | |
527 | }); | |
528 | // Ignore if ALL or NONE selected | |
529 | $scope.caseType.definition.statuses = selectedStatuses.length == _.size($scope.selectedStatuses) ? [] : selectedStatuses; | |
06f21064 TO |
530 | |
531 | if ($scope.caseType.definition.activityAsgmtGrps) { | |
532 | $scope.caseType.definition.activityAsgmtGrps = $scope.caseType.definition.activityAsgmtGrps.toString().split(","); | |
533 | } | |
534 | ||
c7bccb5f | 535 | var result = crmApi('CaseType', 'create', $scope.caseType, true); |
3140a415 | 536 | result.then(function(data) { |
b04e5ffb | 537 | if (data.is_error === 0 || data.is_error == '0') { |
c7bccb5f | 538 | $scope.caseType.id = data.id; |
1ab5b88e | 539 | window.location.href = '#/caseType'; |
c7bccb5f | 540 | } |
541 | }); | |
aa1a7c2e TO |
542 | }; |
543 | ||
76e4acb8 TO |
544 | $scope.$watchCollection('caseType.definition.activitySets', function() { |
545 | _.defer(function() { | |
8fc6fba7 | 546 | $('.crmCaseType-acttab').tabs('refresh'); |
76e4acb8 TO |
547 | }); |
548 | }); | |
685acae4 | 549 | |
550 | var updateCaseTypeName = function () { | |
551 | if (!$scope.caseType.id && $scope.locks.caseTypeName) { | |
552 | // Should we do some filtering? Lowercase? Strip whitespace? | |
a5ca1f48 | 553 | var t = $scope.caseType.title ? $scope.caseType.title : ''; |
554 | $scope.caseType.name = t.replace(/ /g, '_').replace(/[^a-zA-Z0-9_]/g, '').toLowerCase(); | |
685acae4 | 555 | } |
556 | }; | |
557 | $scope.$watch('locks.caseTypeName', updateCaseTypeName); | |
558 | $scope.$watch('caseType.title', updateCaseTypeName); | |
b40b4114 TO |
559 | |
560 | if (!$scope.isForkable()) { | |
561 | CRM.alert(ts('The CiviCase XML file for this case-type prohibits editing the definition.')); | |
562 | } | |
093f1cfd | 563 | |
4c58e251 TO |
564 | }); |
565 | ||
b75c2546 | 566 | crmCaseType.controller('CaseTypeListCtrl', function($scope, crmApi, caseTypes) { |
7abbf317 CW |
567 | var ts = $scope.ts = CRM.ts(null); |
568 | ||
b75c2546 | 569 | $scope.caseTypes = caseTypes.values; |
4b8c8b42 TO |
570 | $scope.toggleCaseType = function (caseType) { |
571 | caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; | |
572 | crmApi('CaseType', 'create', caseType, true) | |
c99f1a0a TO |
573 | .catch(function (data) { |
574 | caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; // revert | |
575 | $scope.$digest(); | |
4b8c8b42 TO |
576 | }); |
577 | }; | |
578 | $scope.deleteCaseType = function (caseType) { | |
eb8e4c2d TO |
579 | crmApi('CaseType', 'delete', {id: caseType.id}, { |
580 | error: function (data) { | |
7abbf317 | 581 | CRM.alert(data.error_message, ts('Error'), 'error'); |
eb8e4c2d TO |
582 | } |
583 | }) | |
4b8c8b42 | 584 | .then(function (data) { |
c99f1a0a | 585 | delete caseTypes.values[caseType.id]; |
4b8c8b42 TO |
586 | }); |
587 | }; | |
470a458e TO |
588 | $scope.revertCaseType = function (caseType) { |
589 | caseType.definition = 'null'; | |
590 | caseType.is_forked = '0'; | |
591 | crmApi('CaseType', 'create', caseType, true) | |
c99f1a0a TO |
592 | .catch(function (data) { |
593 | caseType.is_forked = '1'; // restore | |
594 | $scope.$digest(); | |
470a458e TO |
595 | }); |
596 | }; | |
b75c2546 TO |
597 | }); |
598 | ||
bba9b4f0 | 599 | })(angular, CRM.$, CRM._); |