Merge pull request #16546 from eileenmcnaughton/part_end2
[civicrm-core.git] / templates / CRM / common / enableDisableApi.tpl
CommitLineData
12798ddc
CW
1{*
2 +--------------------------------------------------------------------+
1188c7a8 3 | Copyright CiviCRM LLC. All rights reserved. |
12798ddc 4 | |
1188c7a8
TO
5 | This work is published under the GNU AGPLv3 license with some |
6 | permitted exceptions and without any warranty. For full license |
7 | and copyright information, see https://civicrm.org/licensing |
12798ddc
CW
8 +--------------------------------------------------------------------+
9*}
10{* handle common enable/disable actions *}
11{literal}
12<script type="text/javascript">
3cc60a06 13 CRM.$(function($) {
369f685e 14 var $a, $row, info, enabled, fieldLabel;
12798ddc 15
e11b84ce
CW
16 function successMsg() {
17 {/literal} {* client-side variable substitutions in smarty are AWKWARD! *}
d0aaa02e 18 var msg = enabled ? '{ts escape="js" 1="%1"}%1 Disabled{/ts}' : '{ts escape="js" 1="%1"}%1 Enabled{/ts}'{literal};
e11b84ce
CW
19 return ts(msg, {1: fieldLabel});
20 }
21
12798ddc 22 function refresh() {
a2f1945a
MD
23 // the opposite of the current status based on row class
24 var newStatus = $row.hasClass('disabled');
25 $a.trigger('crmPopupFormSuccess', {
26 'entity': info.entity,
27 'id': info.id,
28 'enabled': newStatus
29 });
554b1768 30 CRM.refreshParent($row);
12798ddc
CW
31 }
32
33 function save() {
554b1768 34 $row.closest('table').block();
c6c860d0
CW
35 var params = {id: info.id};
36 if (info.action == 'setvalue') {
37 params.field = 'is_active';
38 params.value = enabled ? 0 : 1;
39 } else {
40 params.is_active = enabled ? 0 : 1;
41 }
42 CRM.api3(info.entity, info.action, params, {success: successMsg}).done(refresh);
12798ddc
CW
43 }
44
369f685e
CW
45 function checkResponse(e, response) {
46 if (response.illegal) {
47 $(this).dialog('option', 'buttons', [
f62db3ac 48 {text: {/literal}'{ts escape="js"}Close{/ts}'{literal}, click: function() {$(this).dialog('close');}, icons: {primary: 'fa-times'}}
369f685e
CW
49 ]);
50 }
12798ddc
CW
51 }
52
53 function enableDisable() {
369f685e
CW
54 $row = $a.closest('.crm-entity');
55 info = $a.crmEditableEntity();
e2046b33 56 fieldLabel = info.label || info.title || info.display_name || info.name || {/literal}'{ts escape="js"}Record{/ts}'{literal};
12798ddc
CW
57 enabled = !$row.hasClass('disabled');
58 if (enabled) {
369f685e
CW
59 CRM.confirm({
60 url: CRM.url('civicrm/ajax/statusmsg', {entity: info.entity, id: info.id}),
61 title: ts('{/literal}{ts escape="js" 1='%1'}Disable %1{/ts}{literal}', {1: fieldLabel}),
62 options: {{/literal}yes: '{ts escape="js"}Yes{/ts}', no: '{ts escape="js"}No{/ts}'{literal}},
9f324020
CW
63 width: 300,
64 height: 'auto'
369f685e
CW
65 })
66 .on('crmLoad', checkResponse)
67 .on('crmConfirm:yes', save);
12798ddc
CW
68 } else {
69 save();
70 }
12798ddc
CW
71 }
72
73 // Because this is an inline script it may get added to the document more than once, so remove handler before adding
74 $('body')
fdf3f783 75 .off('.crmEnableDisable')
30b7642c
CW
76 .on('click.crmEnableDisable', '.action-item.crm-enable-disable', function(e) {
77 e.preventDefault();
78 $a = $(this);
79 CRM.loadScript(CRM.config.resourceBase + 'js/jquery/jquery.crmEditable.js').done(enableDisable);
80 });
12798ddc
CW
81 });
82</script>
83{/literal}