Commit | Line | Data |
---|---|---|
12798ddc CW |
1 | {* |
2 | +--------------------------------------------------------------------+ | |
fee14197 | 3 | | CiviCRM version 5 | |
12798ddc | 4 | +--------------------------------------------------------------------+ |
6b83d5bd | 5 | | Copyright CiviCRM LLC (c) 2004-2019 | |
12798ddc CW |
6 | +--------------------------------------------------------------------+ |
7 | | This file is a part of CiviCRM. | | |
8 | | | | |
9 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
10 | | under the terms of the GNU Affero General Public License | | |
11 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
12 | | | | |
13 | | CiviCRM is distributed in the hope that it will be useful, but | | |
14 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
16 | | See the GNU Affero General Public License for more details. | | |
17 | | | | |
18 | | You should have received a copy of the GNU Affero General Public | | |
19 | | License and the CiviCRM Licensing Exception along | | |
20 | | with this program; if not, contact CiviCRM LLC | | |
21 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
22 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
23 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
24 | +--------------------------------------------------------------------+ | |
25 | *} | |
26 | {* handle common enable/disable actions *} | |
27 | {literal} | |
28 | <script type="text/javascript"> | |
3cc60a06 | 29 | CRM.$(function($) { |
369f685e | 30 | var $a, $row, info, enabled, fieldLabel; |
12798ddc | 31 | |
e11b84ce CW |
32 | function successMsg() { |
33 | {/literal} {* client-side variable substitutions in smarty are AWKWARD! *} | |
d0aaa02e | 34 | var msg = enabled ? '{ts escape="js" 1="%1"}%1 Disabled{/ts}' : '{ts escape="js" 1="%1"}%1 Enabled{/ts}'{literal}; |
e11b84ce CW |
35 | return ts(msg, {1: fieldLabel}); |
36 | } | |
37 | ||
12798ddc | 38 | function refresh() { |
a2f1945a MD |
39 | // the opposite of the current status based on row class |
40 | var newStatus = $row.hasClass('disabled'); | |
41 | $a.trigger('crmPopupFormSuccess', { | |
42 | 'entity': info.entity, | |
43 | 'id': info.id, | |
44 | 'enabled': newStatus | |
45 | }); | |
554b1768 | 46 | CRM.refreshParent($row); |
12798ddc CW |
47 | } |
48 | ||
49 | function save() { | |
554b1768 | 50 | $row.closest('table').block(); |
c6c860d0 CW |
51 | var params = {id: info.id}; |
52 | if (info.action == 'setvalue') { | |
53 | params.field = 'is_active'; | |
54 | params.value = enabled ? 0 : 1; | |
55 | } else { | |
56 | params.is_active = enabled ? 0 : 1; | |
57 | } | |
58 | CRM.api3(info.entity, info.action, params, {success: successMsg}).done(refresh); | |
12798ddc CW |
59 | } |
60 | ||
369f685e CW |
61 | function checkResponse(e, response) { |
62 | if (response.illegal) { | |
63 | $(this).dialog('option', 'buttons', [ | |
f62db3ac | 64 | {text: {/literal}'{ts escape="js"}Close{/ts}'{literal}, click: function() {$(this).dialog('close');}, icons: {primary: 'fa-times'}} |
369f685e CW |
65 | ]); |
66 | } | |
12798ddc CW |
67 | } |
68 | ||
69 | function enableDisable() { | |
369f685e CW |
70 | $row = $a.closest('.crm-entity'); |
71 | info = $a.crmEditableEntity(); | |
e2046b33 | 72 | fieldLabel = info.label || info.title || info.display_name || info.name || {/literal}'{ts escape="js"}Record{/ts}'{literal}; |
12798ddc CW |
73 | enabled = !$row.hasClass('disabled'); |
74 | if (enabled) { | |
369f685e CW |
75 | CRM.confirm({ |
76 | url: CRM.url('civicrm/ajax/statusmsg', {entity: info.entity, id: info.id}), | |
77 | title: ts('{/literal}{ts escape="js" 1='%1'}Disable %1{/ts}{literal}', {1: fieldLabel}), | |
78 | options: {{/literal}yes: '{ts escape="js"}Yes{/ts}', no: '{ts escape="js"}No{/ts}'{literal}}, | |
9f324020 CW |
79 | width: 300, |
80 | height: 'auto' | |
369f685e CW |
81 | }) |
82 | .on('crmLoad', checkResponse) | |
83 | .on('crmConfirm:yes', save); | |
12798ddc CW |
84 | } else { |
85 | save(); | |
86 | } | |
12798ddc CW |
87 | } |
88 | ||
89 | // Because this is an inline script it may get added to the document more than once, so remove handler before adding | |
90 | $('body') | |
fdf3f783 | 91 | .off('.crmEnableDisable') |
30b7642c CW |
92 | .on('click.crmEnableDisable', '.action-item.crm-enable-disable', function(e) { |
93 | e.preventDefault(); | |
94 | $a = $(this); | |
95 | CRM.loadScript(CRM.config.resourceBase + 'js/jquery/jquery.crmEditable.js').done(enableDisable); | |
96 | }); | |
12798ddc CW |
97 | }); |
98 | </script> | |
99 | {/literal} |