Merge pull request #8483 from colemanw/CRM-18742
[civicrm-core.git] / templates / CRM / common / enableDisableApi.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.7 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2016 |
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">
29 CRM.$(function($) {
30 var $a, $row, info, enabled, fieldLabel;
31
32 function successMsg() {
33 {/literal} {* client-side variable substitutions in smarty are AWKWARD! *}
34 var msg = enabled ? '{ts escape="js" 1="<em>%1</em>"}%1 Disabled{/ts}' : '{ts escape="js" 1="<em>%1</em>"}%1 Enabled{/ts}'{literal};
35 return ts(msg, {1: fieldLabel});
36 }
37
38 function refresh() {
39 $a.trigger('crmPopupFormSuccess');
40 CRM.refreshParent($row);
41 }
42
43 function save() {
44 $row.closest('table').block();
45 var params = {id: info.id};
46 if (info.action == 'setvalue') {
47 params.field = 'is_active';
48 params.value = enabled ? 0 : 1;
49 } else {
50 params.is_active = enabled ? 0 : 1;
51 }
52 CRM.api3(info.entity, info.action, params, {success: successMsg}).done(refresh);
53 }
54
55 function checkResponse(e, response) {
56 if (response.illegal) {
57 $(this).dialog('option', 'buttons', [
58 {text: {/literal}'{ts escape="js"}Close{/ts}'{literal}, click: function() {$(this).dialog('close');}, icons: {primary: 'fa-times'}}
59 ]);
60 }
61 }
62
63 function enableDisable() {
64 $a = $(this);
65 $row = $a.closest('.crm-entity');
66 info = $a.crmEditableEntity();
67 fieldLabel = info.label || info.title || info.display_name || info.name || {/literal}'{ts escape="js"}Record{/ts}'{literal};
68 enabled = !$row.hasClass('disabled');
69 if (enabled) {
70 CRM.confirm({
71 url: CRM.url('civicrm/ajax/statusmsg', {entity: info.entity, id: info.id}),
72 title: ts('{/literal}{ts escape="js" 1='%1'}Disable %1{/ts}{literal}', {1: fieldLabel}),
73 options: {{/literal}yes: '{ts escape="js"}Yes{/ts}', no: '{ts escape="js"}No{/ts}'{literal}},
74 width: 300,
75 height: 'auto'
76 })
77 .on('crmLoad', checkResponse)
78 .on('crmConfirm:yes', save);
79 } else {
80 save();
81 }
82 return false;
83 }
84
85 // Because this is an inline script it may get added to the document more than once, so remove handler before adding
86 $('body')
87 .off('.crmEnableDisable')
88 .on('click.crmEnableDisable', '.action-item.crm-enable-disable', enableDisable);
89 });
90 </script>
91 {/literal}