1 // https://civicrm.org/licensing
4 function refresh(table
) {
5 $('#crm-main-content-wrapper').crmSnippet('refresh');
8 function open(url
, options
, table
) {
9 if (CRM
.config
.ajaxPopupsEnabled
) {
10 CRM
.loadForm(url
, options
).on('crmFormSuccess', function() {
15 window
.location
.href
= url
;
20 return $('.crm-entity[data-entity=case]').data('id');
23 function contactId() {
24 return $('.crm-entity[data-entity=case]').data('cid');
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
34 '#manageTagsDialog': {
35 post: function(data
) {
36 var tagsChecked
= $("#tags", this) ? $("#tags", this).val() : '',
38 url
= CRM
.url('civicrm/case/ajax/processtags');
39 $("input[name^=case_taglist]", this).each(function() {
40 var tsId
= $(this).attr('id').split('_');
41 tagList
[tsId
[2]] = $(this).val();
48 return $.post(url
, data
);
51 '#mergeCasesDialog': {
52 post: function(data
) {
53 if ($('select#merge_case_id').val()) {
54 $('select#merge_case_id').appendTo('form#CaseView');
55 $('[name="_qf_CaseView_next_merge_case"]').click();
61 '#deleteCaseRoleDialog': {
62 post: function(data
) {
63 data
.case_id
= caseId();
64 return $.post(CRM
.url('civicrm/ajax/delcaserole'), data
);
67 '#addCaseRoleDialog': {
69 var $contactField
= $('[name=add_role_contact_id]', this);
70 $('[name=role_type]', this)
72 .on('change.miniform', function() {
73 var val
= $(this).val();
74 $contactField
.val('').change().prop('disabled', !val
);
76 prepareRelationshipField(val
, $contactField
);
81 $contactField
.val('').crmEntityRef();
83 post: function(data
) {
84 var contactID
= $('[name=add_role_contact_id]', this).val(),
85 relType
= $('[name=role_type]', this).val();
86 if (contactID
&& relType
) {
89 rel_contact
: contactID
,
91 contact_id
: contactId()
93 return $.post(CRM
.url('civicrm/ajax/relation'), data
);
98 '#editCaseRoleDialog': {
100 prepareRelationshipField(data
.rel_type
, $('[name=edit_role_contact_id]', this));
102 post: function(data
) {
103 data
.rel_contact
= $('[name=edit_role_contact_id]', this).val();
104 if (data
.rel_contact
) {
107 contact_id
: contactId()
109 return $.post(CRM
.url('civicrm/ajax/relation'), data
);
114 '#addClientDialog': {
116 $('[name=add_client_id]', this).val('').crmEntityRef({create
: true});
118 post: function(data
) {
119 data
.contactID
= $('[name=add_client_id]', this).val();
120 if (data
.contactID
) {
121 data
.caseID
= caseId();
122 return $.post(CRM
.url('civicrm/case/ajax/addclient'), data
);
127 '#addMembersToGroupDialog': {
129 $('[name=add_member_to_group_contact_id]', this).val('').crmEntityRef({create
: true, select
: {multiple
: true}});
131 post: function(data
) {
133 cids
= $('[name=add_member_to_group_contact_id]', this).val();
135 $.each(cids
.split(','), function (k
, cid
) {
136 requests
.push(['group_contact', 'create', $.extend({contact_id
: cid
}, data
)]);
138 return CRM
.api3(requests
);
146 function prepareRelationshipField(relType
, $contactField
) {
148 pieces
= relType
.split('_'),
150 target
= pieces
[2], // b or a
151 relationshipType
= CRM
.vars
.relationshipTypes
[rType
],
153 if (relationshipType
['contact_type_' + target
]) {
154 api
.params
.contact_type
= relationshipType
['contact_type_' + target
];
156 if (relationshipType
['contact_sub_type_' + target
]) {
157 api
.params
.contact_sub_type
= relationshipType
['contact_sub_type_' + target
];
159 if (relationshipType
['group_' + target
]) {
160 api
.params
.group
= {IN
: relationshipType
['group_' + target
]};
163 .data('create-links', !relationshipType
['group_' + target
])
164 .data('api-params', api
)
165 .data('user-filter', {})
166 .attr('placeholder', relationshipType
['placeholder_' + target
])
171 function detachMiniForms() {
173 $.each(miniForms
, function(selector
) {
174 detached
[selector
] = $(selector
).detach().removeClass('hiddenElement');
178 $('#crm-container').on('crmLoad', '#crm-main-content-wrapper', detachMiniForms
);
180 $(document
).ready(function() {
183 .on('change', 'select[name=add_activity_type_id]', function() {
185 $(this).select2('val', '');
187 .on('change', 'select[name=timeline_id]', function() {
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>'})
193 .on('crmConfirm:yes', function() {
194 $('[name=_qf_CaseView_next]').click();
196 .on('crmConfirm:no', function() {
197 $('select[name=timeline_id]').select2('val', '');
201 .on('change', 'select[name=report_id]', function() {
203 var url
= CRM
.url('civicrm/case/report', {
209 open(url
, {dialog
: {width
: '50%', height
: 'auto'}});
210 $(this).select2('val', '');
213 // When changing case subject, record an activity
214 .on('crmFormSuccess', '[data-field=subject]', function(e
, value
) {
216 CRM
.api3('Activity', 'create', {
218 activity_type_id
: 'Change Case Subject',
220 status_id
: 'Completed'
222 $('#case_id_' + id
).dataTable().api().draw();
225 .on('click', 'a.case-miniform', function(e
) {
228 target
= $el
.attr('href');
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
235 submission
.done(function(data
) {
236 dialog
.dialog('close');
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');
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'));
255 dialog
= CRM
.confirm({
256 title
: $(this).attr('title') || $(this).text(),
257 message
: detached
[target
],
259 options
: {yes
: ts('Save'), no
: ts('Cancel')},
261 if (miniForms
[target
].pre
) miniForms
[target
].pre
.call(this, $el
.data());
264 .on('dialogclose', function() {
265 detached
[target
] = $(target
, dialog
).detach();
267 .on('crmConfirm:yes', submit
);
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'));
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
);