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) { | |
42 | return crmApi('CaseType', 'get', {}); | |
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', { | |
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 | }]; | |
87dcd909 | 72 | } |
9625aad1 | 73 | return crmApi(reqs); |
4d74de55 TO |
74 | } |
75 | } | |
4c58e251 TO |
76 | }); |
77 | } | |
78 | ]); | |
79 | ||
95fd24c0 TO |
80 | // Add a new record by name. |
81 | // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" /> | |
8fc6fba7 | 82 | crmCaseType.directive('crmAddName', function() { |
95fd24c0 TO |
83 | return { |
84 | restrict: 'AE', | |
bafce1db TO |
85 | template: '<input class="add-activity" 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 | }); | |
110 | $(input).on('select2-selecting', function(e) { | |
111 | scope[attrs.crmVar] = e.val; | |
112 | scope.$evalAsync(attrs.crmOnAdd); | |
113 | scope.$evalAsync('_resetSelection()'); | |
114 | e.preventDefault(); | |
115 | }); | |
60dd172b TO |
116 | |
117 | scope.$watch(attrs.crmOptions, function(value) { | |
118 | $(input).select2('data', getFormattedOptions); | |
119 | $(input).select2('val', ''); | |
120 | }); | |
bafce1db | 121 | } |
95fd24c0 TO |
122 | }; |
123 | }); | |
124 | ||
9625aad1 | 125 | crmCaseType.controller('CaseTypeCtrl', function($scope, crmApi, apiCalls) { |
5d8901af | 126 | var ts = $scope.ts = CRM.ts(null); |
76e4acb8 | 127 | |
9625aad1 TO |
128 | $scope.activityStatuses = _.values(apiCalls.actStatuses.values); |
129 | $scope.activityTypes = apiCalls.actTypes.values; | |
130 | $scope.activityTypeNames = _.pluck(apiCalls.actTypes.values, 'name'); | |
131 | $scope.relationshipTypeNames = _.pluck(apiCalls.relTypes.values, CRM.crmCaseType.REL_TYPE_CNAME); // CRM_Case_XMLProcessor::REL_TYPE_CNAME | |
1317b266 | 132 | $scope.locks = {caseTypeName: true, activitySetName: true}; |
dc7a657e | 133 | |
76e4acb8 TO |
134 | $scope.workflows = { |
135 | 'timeline': 'Timeline', | |
b387506c | 136 | 'sequence': 'Sequence' |
76e4acb8 TO |
137 | }; |
138 | ||
9625aad1 | 139 | $scope.caseType = apiCalls.caseType ? apiCalls.caseType : _.cloneDeep(newCaseTypeTemplate); |
87dcd909 | 140 | $scope.caseType.definition = $scope.caseType.definition || []; |
4d74de55 TO |
141 | $scope.caseType.definition.activityTypes = $scope.caseType.definition.activityTypes || []; |
142 | $scope.caseType.definition.activitySets = $scope.caseType.definition.activitySets || []; | |
143 | $scope.caseType.definition.caseRoles = $scope.caseType.definition.caseRoles || []; | |
dc7a657e | 144 | window.ct = $scope.caseType; |
4c58e251 | 145 | |
76e4acb8 TO |
146 | $scope.addActivitySet = function(workflow) { |
147 | var activitySet = {}; | |
148 | activitySet[workflow] = '1'; | |
149 | activitySet.activityTypes = []; | |
150 | ||
151 | var offset = 1; | |
152 | var names = _.pluck($scope.caseType.definition.activitySets, 'name'); | |
153 | while (_.contains(names, workflow + '_' + offset)) offset++; | |
154 | activitySet.name = workflow + '_' + offset; | |
155 | activitySet.label = (offset == 1 ) ? $scope.workflows[workflow] : ($scope.workflows[workflow] + ' #' + offset); | |
156 | ||
157 | $scope.caseType.definition.activitySets.push(activitySet); | |
158 | _.defer(function() { | |
159 | $('.crmCaseType-acttab').tabs('refresh').tabs({active: -1}); | |
160 | }); | |
161 | }; | |
162 | ||
d7c25f6c TO |
163 | /// Add a new activity entry to an activity-set |
164 | $scope.addActivity = function(activitySet, activityType) { | |
165 | activitySet.activityTypes.push({ | |
183241d8 | 166 | name: activityType, |
167 | status: 'Scheduled', | |
168 | reference_activity: 'Open Case', | |
169 | reference_offset: '1', | |
170 | reference_select: 'newest' | |
d7c25f6c | 171 | }); |
60dd172b TO |
172 | if (!_.contains($scope.activityTypeNames, activityType)) { |
173 | $scope.activityTypeNames.push(activityType); | |
174 | } | |
d7c25f6c TO |
175 | }; |
176 | ||
177 | /// Add a new top-level activity-type entry | |
178 | $scope.addActivityType = function(activityType) { | |
179 | var names = _.pluck($scope.caseType.definition.activityTypes, 'name'); | |
180 | if (!_.contains(names, activityType)) { | |
181 | $scope.caseType.definition.activityTypes.push({ | |
182 | name: activityType | |
183 | }); | |
60dd172b TO |
184 | |
185 | } | |
186 | if (!_.contains($scope.activityTypeNames, activityType)) { | |
187 | $scope.activityTypeNames.push(activityType); | |
d7c25f6c TO |
188 | } |
189 | }; | |
190 | ||
8c7e0ae8 TO |
191 | /// Add a new role |
192 | $scope.addRole = function(roles, roleName) { | |
bafce1db TO |
193 | var names = _.pluck($scope.caseType.definition.caseRoles, 'name'); |
194 | if (!_.contains(names, roleName)) { | |
195 | roles.push({ | |
196 | name: roleName | |
197 | }); | |
198 | } | |
60dd172b TO |
199 | if (!_.contains($scope.relationshipTypeNames, roleName)) { |
200 | $scope.relationshipTypeNames.push(roleName); | |
201 | } | |
8c7e0ae8 TO |
202 | }; |
203 | ||
4c58e251 TO |
204 | $scope.onManagerChange = function(managerRole) { |
205 | angular.forEach($scope.caseType.definition.caseRoles, function(caseRole) { | |
206 | if (caseRole != managerRole) { | |
207 | caseRole.manager = '0'; | |
208 | } | |
209 | }); | |
210 | }; | |
211 | ||
212 | $scope.removeItem = function(array, item) { | |
213 | var idx = _.indexOf(array, item); | |
214 | if (idx != -1) { | |
215 | array.splice(idx, 1); | |
216 | } | |
217 | }; | |
218 | ||
b40b4114 | 219 | $scope.isForkable = function() { |
f2bad133 | 220 | return !$scope.caseType.id || $scope.caseType.is_forkable; |
b40b4114 TO |
221 | }; |
222 | ||
5d973e24 TO |
223 | $scope.isNewActivitySetAllowed = function(workflow) { |
224 | switch (workflow) { | |
225 | case 'timeline': | |
226 | return true; | |
b387506c | 227 | case 'sequence': |
b04e5ffb | 228 | return 0 === _.where($scope.caseType.definition.activitySets, {sequence: '1'}).length; |
5d973e24 | 229 | default: |
bba9b4f0 | 230 | CRM.console('warn', 'Denied access to unrecognized workflow: (' + workflow + ')'); |
5d973e24 TO |
231 | return false; |
232 | } | |
233 | }; | |
234 | ||
259a7652 TO |
235 | $scope.isActivityRemovable = function(activitySet, activity) { |
236 | if (activitySet.name == 'standard_timeline' && activity.name == 'Open Case') { | |
237 | return false; | |
238 | } else { | |
239 | return true; | |
240 | } | |
241 | }; | |
242 | ||
f42b448f TO |
243 | $scope.isValidName = function(name) { |
244 | return !name || name.match(/^[a-zA-Z0-9_]+$/); | |
245 | }; | |
246 | ||
4c58e251 | 247 | $scope.getWorkflowName = function(activitySet) { |
76e4acb8 TO |
248 | var result = 'Unknown'; |
249 | _.each($scope.workflows, function(value, key) { | |
250 | if (activitySet[key]) result = value; | |
251 | }); | |
252 | return result; | |
4c58e251 TO |
253 | }; |
254 | ||
255 | /** | |
256 | * Determine which HTML partial to use for a particular | |
257 | * | |
258 | * @return string URL of the HTML partial | |
259 | */ | |
260 | $scope.activityTableTemplate = function(activitySet) { | |
261 | if (activitySet.timeline) { | |
ef5d18a1 | 262 | return '~/crmCaseType/timelineTable.html'; |
b387506c | 263 | } else if (activitySet.sequence) { |
ef5d18a1 | 264 | return '~/crmCaseType/sequenceTable.html'; |
4c58e251 TO |
265 | } else { |
266 | return ''; | |
267 | } | |
268 | }; | |
269 | ||
270 | $scope.dump = function() { | |
271 | console.log($scope.caseType); | |
76e4acb8 TO |
272 | }; |
273 | ||
aa1a7c2e | 274 | $scope.save = function() { |
c7bccb5f | 275 | var result = crmApi('CaseType', 'create', $scope.caseType, true); |
3140a415 | 276 | result.then(function(data) { |
b04e5ffb | 277 | if (data.is_error === 0 || data.is_error == '0') { |
c7bccb5f | 278 | $scope.caseType.id = data.id; |
1ab5b88e | 279 | window.location.href = '#/caseType'; |
c7bccb5f | 280 | } |
281 | }); | |
aa1a7c2e TO |
282 | }; |
283 | ||
76e4acb8 TO |
284 | $scope.$watchCollection('caseType.definition.activitySets', function() { |
285 | _.defer(function() { | |
8fc6fba7 | 286 | $('.crmCaseType-acttab').tabs('refresh'); |
76e4acb8 TO |
287 | }); |
288 | }); | |
685acae4 | 289 | |
290 | var updateCaseTypeName = function () { | |
291 | if (!$scope.caseType.id && $scope.locks.caseTypeName) { | |
292 | // Should we do some filtering? Lowercase? Strip whitespace? | |
a5ca1f48 | 293 | var t = $scope.caseType.title ? $scope.caseType.title : ''; |
294 | $scope.caseType.name = t.replace(/ /g, '_').replace(/[^a-zA-Z0-9_]/g, '').toLowerCase(); | |
685acae4 | 295 | } |
296 | }; | |
297 | $scope.$watch('locks.caseTypeName', updateCaseTypeName); | |
298 | $scope.$watch('caseType.title', updateCaseTypeName); | |
b40b4114 TO |
299 | |
300 | if (!$scope.isForkable()) { | |
301 | CRM.alert(ts('The CiviCase XML file for this case-type prohibits editing the definition.')); | |
302 | } | |
4c58e251 TO |
303 | }); |
304 | ||
b75c2546 | 305 | crmCaseType.controller('CaseTypeListCtrl', function($scope, crmApi, caseTypes) { |
7abbf317 CW |
306 | var ts = $scope.ts = CRM.ts(null); |
307 | ||
b75c2546 | 308 | $scope.caseTypes = caseTypes.values; |
4b8c8b42 TO |
309 | $scope.toggleCaseType = function (caseType) { |
310 | caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; | |
311 | crmApi('CaseType', 'create', caseType, true) | |
c99f1a0a TO |
312 | .catch(function (data) { |
313 | caseType.is_active = (caseType.is_active == '1') ? '0' : '1'; // revert | |
314 | $scope.$digest(); | |
4b8c8b42 TO |
315 | }); |
316 | }; | |
317 | $scope.deleteCaseType = function (caseType) { | |
eb8e4c2d TO |
318 | crmApi('CaseType', 'delete', {id: caseType.id}, { |
319 | error: function (data) { | |
7abbf317 | 320 | CRM.alert(data.error_message, ts('Error'), 'error'); |
eb8e4c2d TO |
321 | } |
322 | }) | |
4b8c8b42 | 323 | .then(function (data) { |
c99f1a0a TO |
324 | delete caseTypes.values[caseType.id]; |
325 | $scope.$digest(); | |
4b8c8b42 TO |
326 | }); |
327 | }; | |
470a458e TO |
328 | $scope.revertCaseType = function (caseType) { |
329 | caseType.definition = 'null'; | |
330 | caseType.is_forked = '0'; | |
331 | crmApi('CaseType', 'create', caseType, true) | |
c99f1a0a TO |
332 | .catch(function (data) { |
333 | caseType.is_forked = '1'; // restore | |
334 | $scope.$digest(); | |
470a458e TO |
335 | }); |
336 | }; | |
b75c2546 TO |
337 | }); |
338 | ||
bba9b4f0 | 339 | })(angular, CRM.$, CRM._); |