Merge pull request #16792 from colemanw/dashboardContact
[civicrm-core.git] / templates / CRM / common / enableDisableApi.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | Copyright CiviCRM LLC. All rights reserved. |
4 | |
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 |
8 +--------------------------------------------------------------------+
9 *}
10 {* handle common enable/disable actions *}
11 {literal}
12 <script type="text/javascript">
13 CRM.$(function($) {
14 var $a, $row, info, enabled, fieldLabel;
15
16 function successMsg() {
17 {/literal} {* client-side variable substitutions in smarty are AWKWARD! *}
18 var msg = enabled ? '{ts escape="js" 1="%1"}%1 Disabled{/ts}' : '{ts escape="js" 1="%1"}%1 Enabled{/ts}'{literal};
19 return ts(msg, {1: fieldLabel});
20 }
21
22 function refresh() {
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 });
30 CRM.refreshParent($row);
31 }
32
33 function save() {
34 $row.closest('table').block();
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);
43 }
44
45 function checkResponse(e, response) {
46 if (response.illegal) {
47 $(this).dialog('option', 'buttons', [
48 {text: {/literal}'{ts escape="js"}Close{/ts}'{literal}, click: function() {$(this).dialog('close');}, icons: {primary: 'fa-times'}}
49 ]);
50 }
51 }
52
53 function enableDisable() {
54 $row = $a.closest('.crm-entity');
55 info = $a.crmEditableEntity();
56 fieldLabel = info.label || info.title || info.display_name || info.name || {/literal}'{ts escape="js"}Record{/ts}'{literal};
57 enabled = !$row.hasClass('disabled');
58 if (enabled) {
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}},
63 width: 300,
64 height: 'auto'
65 })
66 .on('crmLoad', checkResponse)
67 .on('crmConfirm:yes', save);
68 } else {
69 save();
70 }
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')
75 .off('.crmEnableDisable')
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 });
81 });
82 </script>
83 {/literal}