Merge in 5.29
[civicrm-core.git] / templates / CRM / Case / Form / CaseView.js
CommitLineData
5ffd5a35 1// https://civicrm.org/licensing
6ce08914 2(function($, CRM) {
5ffd5a35 3
c91df8b4 4 function refresh(table) {
ad280fb6 5 $('#crm-main-content-wrapper').crmSnippet('refresh');
5ffd5a35
CW
6 }
7
c91df8b4 8 function open(url, options, table) {
5ffd5a35 9 if (CRM.config.ajaxPopupsEnabled) {
c91df8b4
CW
10 CRM.loadForm(url, options).on('crmFormSuccess', function() {
11 refresh(table);
12 });
5ffd5a35
CW
13 }
14 else {
f4af7a1e 15 window.location.href = url;
5ffd5a35
CW
16 }
17 }
18
6ce08914
CW
19 function caseId() {
20 return $('.crm-entity[data-entity=case]').data('id');
21 }
22
23 function contactId() {
24 return $('.crm-entity[data-entity=case]').data('cid');
25 }
26
27 /**
28 * The CaseView form includes some extra fields which are meant to open in a dialog.
29 * We stash them initially and pop them up as needed
30 * TODO: Creating a separate form class for each of these and opening them as standard popup links would be simpler and more reusable
c91df8b4 31 * @type {pre, post}
6ce08914 32 */
c91df8b4
CW
33 var miniForms = {
34 '#manageTagsDialog': {
35 post: function(data) {
b733747a 36 var tagsChecked = $("#tags", this) ? $("#tags", this).val() : '',
6ce08914
CW
37 tagList = {},
38 url = CRM.url('civicrm/case/ajax/processtags');
c91df8b4 39 $("input[name^=case_taglist]", this).each(function() {
6ce08914
CW
40 var tsId = $(this).attr('id').split('_');
41 tagList[tsId[2]] = $(this).val();
42 });
c91df8b4 43 $.extend(data, {
6ce08914
CW
44 case_id: caseId(),
45 tag: tagsChecked,
c91df8b4
CW
46 taglist: tagList
47 });
6ce08914 48 return $.post(url, data);
c91df8b4
CW
49 }
50 },
51 '#mergeCasesDialog': {
52 post: function(data) {
2a06342c
CW
53 if ($('select#merge_case_id').val()) {
54 $('select#merge_case_id').appendTo('form#CaseView');
55 $('[name="_qf_CaseView_next_merge_case"]').click();
c91df8b4
CW
56 } else {
57 return false;
58 }
59 }
60 },
61 '#deleteCaseRoleDialog': {
62 post: function(data) {
63 data.case_id = caseId();
64 return $.post(CRM.url('civicrm/ajax/delcaserole'), data);
65 }
66 },
67 '#addCaseRoleDialog': {
68 pre: function() {
0004ae05
CW
69 var $contactField = $('[name=add_role_contact_id]', this);
70 $('[name=role_type]', this)
71 .off('.miniform')
72 .on('change.miniform', function() {
73 var val = $(this).val();
74 $contactField.val('').change().prop('disabled', !val);
75 if (val) {
9c7ffe36 76 prepareRelationshipField(val, $contactField);
0004ae05
CW
77 }
78 })
79 .val('')
80 .change();
9c7ffe36 81 $contactField.val('').crmEntityRef();
c91df8b4
CW
82 },
83 post: function(data) {
4d46efc4
CW
84 var contactID = $('[name=add_role_contact_id]', this).val(),
85 relType = $('[name=role_type]', this).val();
c91df8b4
CW
86 if (contactID && relType) {
87 $.extend(data, {
88 case_id: caseId(),
89 rel_contact: contactID,
90 rel_type: relType,
91 contact_id: contactId()
92 });
93 return $.post(CRM.url('civicrm/ajax/relation'), data);
94 }
95 return false;
96 }
97 },
98 '#editCaseRoleDialog': {
ba9be71a 99 pre: function(data) {
9c7ffe36 100 prepareRelationshipField(data.rel_type, $('[name=edit_role_contact_id]', this));
c91df8b4
CW
101 },
102 post: function(data) {
ba9be71a 103 data.rel_contact = $('[name=edit_role_contact_id]', this).val();
c91df8b4
CW
104 if (data.rel_contact) {
105 $.extend(data, {
106 case_id: caseId(),
107 contact_id: contactId()
108 });
109 return $.post(CRM.url('civicrm/ajax/relation'), data);
2a06342c 110 }
c91df8b4
CW
111 return false;
112 }
113 },
114 '#addClientDialog': {
115 pre: function() {
116 $('[name=add_client_id]', this).val('').crmEntityRef({create: true});
117 },
118 post: function(data) {
ba9be71a 119 data.contactID = $('[name=add_client_id]', this).val();
c91df8b4
CW
120 if (data.contactID) {
121 data.caseID = caseId();
122 return $.post(CRM.url('civicrm/case/ajax/addclient'), data);
123 }
124 return false;
125 }
126 },
127 '#addMembersToGroupDialog': {
128 pre: function() {
ba9be71a 129 $('[name=add_member_to_group_contact_id]', this).val('').crmEntityRef({create: true, select: {multiple: true}});
2a06342c 130 },
c91df8b4 131 post: function(data) {
ba9be71a
CW
132 var requests = [],
133 cids = $('[name=add_member_to_group_contact_id]', this).val();
134 if (cids) {
135 $.each(cids.split(','), function (k, cid) {
136 requests.push(['group_contact', 'create', $.extend({contact_id: cid}, data)]);
137 });
138 return CRM.api3(requests);
c91df8b4
CW
139 }
140 return false;
6ce08914 141 }
c91df8b4
CW
142 }
143 },
144 detached = {};
6ce08914 145
9c7ffe36
CW
146 function prepareRelationshipField(relType, $contactField) {
147 var
148 pieces = relType.split('_'),
149 rType = pieces[0],
150 target = pieces[2], // b or a
151 relationshipType = CRM.vars.relationshipTypes[rType],
152 api = {params: {}};
153 if (relationshipType['contact_type_' + target]) {
154 api.params.contact_type = relationshipType['contact_type_' + target];
155 }
156 if (relationshipType['contact_sub_type_' + target]) {
157 api.params.contact_sub_type = relationshipType['contact_sub_type_' + target];
158 }
159 if (relationshipType['group_' + target]) {
160 api.params.group = {IN: relationshipType['group_' + target]};
161 }
162 $contactField
163 .data('create-links', !relationshipType['group_' + target])
164 .data('api-params', api)
165 .data('user-filter', {})
166 .attr('placeholder', relationshipType['placeholder_' + target])
167 .change()
168 .crmEntityRef();
169 }
170
6ce08914
CW
171 function detachMiniForms() {
172 detached = {};
173 $.each(miniForms, function(selector) {
2a06342c 174 detached[selector] = $(selector).detach().removeClass('hiddenElement');
5ffd5a35 175 });
6ce08914
CW
176 }
177
178 $('#crm-container').on('crmLoad', '#crm-main-content-wrapper', detachMiniForms);
179
180 $(document).ready(function() {
181 detachMiniForms();
182 $('#crm-container')
183 .on('change', 'select[name=add_activity_type_id]', function() {
184 open($(this).val());
185 $(this).select2('val', '');
186 })
2a06342c
CW
187 .on('change', 'select[name=timeline_id]', function() {
188 if ($(this).val()) {
5fb83680 189 CRM.confirm({
2a06342c
CW
190 title: $('option:first', this).text(),
191 message: ts('Add the %1 set of scheduled activities to this case?', {1: '<em>' + $('option:selected', this).text() + '</em>'})
192 })
5fb83680 193 .on('crmConfirm:yes', function() {
2a06342c
CW
194 $('[name=_qf_CaseView_next]').click();
195 })
5fb83680 196 .on('crmConfirm:no', function() {
2a06342c
CW
197 $('select[name=timeline_id]').select2('val', '');
198 });
199 }
200 })
201 .on('change', 'select[name=report_id]', function() {
202 if ($(this).val()) {
203 var url = CRM.url('civicrm/case/report', {
204 reset: 1,
205 cid: contactId(),
206 caseid: caseId(),
207 asn: $(this).val()
208 });
209 open(url, {dialog: {width: '50%', height: 'auto'}});
210 $(this).select2('val', '');
211 }
212 })
ccfd6962
CW
213 // When changing case subject, record an activity
214 .on('crmFormSuccess', '[data-field=subject]', function(e, value) {
215 var id = caseId();
216 CRM.api3('Activity', 'create', {
217 case_id: id,
218 activity_type_id: 'Change Case Subject',
219 subject: value,
220 status_id: 'Completed'
221 }).done(function() {
222 $('#case_id_' + id).dataTable().api().draw();
223 });
224 })
5fb83680 225 .on('click', 'a.case-miniform', function(e) {
6ce08914
CW
226 var dialog,
227 $el = $(this),
228 target = $el.attr('href');
229 function submit() {
c91df8b4
CW
230 // Call post function with dialog as context and link data as param
231 var submission = miniForms[target].post.call(dialog[0], $.extend({}, $el.data()));
232 // Function should return a deferred object
2a06342c 233 if (submission) {
ba9be71a 234 dialog.block();
c91df8b4 235 submission.done(function(data) {
2a06342c 236 dialog.dialog('close');
c91df8b4
CW
237 var table = $el.closest('table.dataTable');
238 refresh(table.length ? table : $el.attr('rel'));
239 if ($.isPlainObject(data) && data.is_error && data.error_message) {
240 CRM.alert(data.error_message, ts('Error'), 'error');
241 }
2a06342c
CW
242 });
243 return false;
244 }
c91df8b4
CW
245 // Validation failed - show an error msg on empty fields
246 else if (submission === false) {
247 $(':input', dialog).not('.select2-container *').each(function() {
248 if (!$(this).val()) {
249 $(this).crmError(ts('Please select a value'));
250 }
aab589e2 251 });
c91df8b4
CW
252 }
253 return submission;
6ce08914 254 }
5fb83680 255 dialog = CRM.confirm({
2a06342c 256 title: $(this).attr('title') || $(this).text(),
6ce08914 257 message: detached[target],
a243158e 258 resizable: true,
cc1bf3ed 259 options: {yes: ts('Save'), no: ts('Cancel')},
ba9be71a 260 open: function() {
9c632128 261 if (miniForms[target].pre) miniForms[target].pre.call(this, $el.data());
ba9be71a 262 }
5fb83680
CW
263 })
264 .on('dialogclose', function() {
265 detached[target] = $(target, dialog).detach();
266 })
267 .on('crmConfirm:yes', submit);
268 e.preventDefault();
6ce08914 269 });
ca0de7b5 270
ca0de7b5
CW
271 // Keep the state of accordions when refreshing
272 var accordionStates = [];
273 $('#crm-main-content-wrapper')
274 .on('crmBeforeLoad', function(e) {
275 if ($(e.target).is(this)) {
276 accordionStates = [];
277 $('.crm-accordion-wrapper', this).each(function() {
278 accordionStates.push($(this).hasClass('collapsed'));
279 });
280 }
281 })
282 .on('crmLoad', function(e) {
283 if ($(e.target).is(this)) {
284 var $targets = $('.crm-accordion-wrapper', this);
285 $.each(accordionStates, function(i, isCollapsed) {
286 $targets.eq(i).toggleClass('collapsed', isCollapsed);
287 });
288 }
289 });
6ce08914 290 });
aab589e2 291}(cj, CRM));