CRM-13966 - Replace pcp autocomplete with select2
[civicrm-core.git] / templates / CRM / Contribute / Form / SoftCredit.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.4 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2013 |
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 {* template for adding form elements for soft credit form*}
27 {if $honor_block_is_active}
28 {crmRegion name="contribution-soft-credit-block"}
29 <legend>{$honor_block_title}</legend>
30 <div class="crm-section honor_block_text-section">
31 {$honor_block_text}
32 </div>
33 {if $form.soft_credit_type_id.html}
34 <div class="crm-section {$form.soft_credit_type_id.name}-section">
35 <div class="content" >
36 {$form.soft_credit_type_id.html}
37 <div class="description">{ts}Select an option to reveal honoree information fields.{/ts}</div>
38 </div>
39 </div>
40 {/if}
41 {/crmRegion}
42 {else}
43 <table class="form-layout-compressed crm-soft-credit-block">
44 {section name='i' start=1 loop=$rowCount}
45 {assign var='rowNumber' value=$smarty.section.i.index}
46 <tr id="soft-credit-row-{$rowNumber}"
47 class="crm-contribution-form-block-soft_credit_to {if $rowNumber gte $showSoftCreditRow}hiddenElement{/if}">
48 <td>
49 {$form.soft_credit_contact_id.$rowNumber.label}&nbsp;{$form.soft_credit_contact_id.$rowNumber.html|crmAddClass:twenty}
50 </td>
51 <td>
52 {$form.soft_credit_amount.$rowNumber.label}&nbsp;{$form.soft_credit_amount.$rowNumber.html|crmAddClass:eight}
53 </td>
54 <td>
55 {$form.soft_credit_type.$rowNumber.label}&nbsp;{$form.soft_credit_type.$rowNumber.html}
56 &nbsp;<a class="crm-hover-button soft-credit-delete-link" href="#"><span class="icon delete-icon"></span></a>
57 </td>
58 </tr>
59 {/section}
60 <tr>
61 <td>
62 <a href="#" class="crm-hover-button" id="addMoreSoftCredit"><span class="icon add-icon"></span> {ts}another soft credit{/ts}</a>
63 </td>
64 </tr>
65 </table>
66 {/if}
67 {literal}
68 <script type="text/javascript">
69 cj(function($) {
70 $('#showPCP, #showSoftCredit').click(function(){
71 return showHideSoftCreditAndPCP();
72 });
73
74 function showHideSoftCreditAndPCP() {
75 $('.crm-contribution-pcp-block').toggle();
76 $('.crm-contribution-pcp-block-link').toggle();
77 $('.crm-contribution-form-block-soft_credit_to').toggle();
78 return false;
79 }
80
81 $('#addMoreSoftCredit').on('click', function () {
82 if ($('tr.crm-contribution-form-block-soft_credit_to').hasClass("hiddenElement")) {
83 $('.crm-contribution-form-block-soft_credit_to tr.hiddenElement').filter(':first').show().removeClass('hiddenElement');
84 }
85 if ($('.crm-soft-credit-block tr.hiddenElement').length < 1) {
86 $('#addMoreSoftCredit').hide();
87 }
88 return false;
89 });
90
91 // FIXME: This could be much simpler as an entityRef field but pcp doesn't have a searchable api :(
92 var pcpURL = CRM.url('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getPCPList&json=1&context=contact&reset=1');
93 $('#pcp_made_through_id').crmSelect2({
94 placeholder: {/literal}'{ts escape="js"}- select -{/ts}'{literal},
95 minimumInputLength: 1,
96 ajax: {
97 url: pcpURL,
98 data: function(term) {
99 return {term: term};
100 },
101 results: function(response) {
102 return {results: response};
103 }
104 },
105 initSelection: function(el, callback) {
106 callback({id: $(el).val(), text: $('#pcp_made_through').val()});
107 }
108 });
109
110 $('.crm-soft-credit-block tr span').each(function () {
111 if ($(this).hasClass('crm-error')) {
112 $(this).parents('tr').show();
113 }
114 });
115
116 $('.soft-credit-delete-link').click(function(){
117 $(this).closest('tr').addClass('hiddenElement').removeAttr('style');
118 $('#addMoreSoftCredit').show();
119 return false;
120 });
121
122 $('input[name^="soft_credit_contact["]').change(function(){
123 var rowNum = $(this).prop('id').replace('soft_credit_contact_','');
124 var totalAmount = $('#total_amount').val();
125 //assign total amount as default soft credit amount
126 $('#soft_credit_amount_'+ rowNum).val(totalAmount);
127 var thousandMarker = {/literal}{$config->monetaryThousandSeparator|json_encode}{literal};
128 $('#soft_credit_type_'+ rowNum).val($('#sct_default_id').val());
129 totalAmount = Number(totalAmount.replace(thousandMarker,''));
130 if (rowNum > 1) {
131 var scAmount = Number($('#soft_credit_amount_'+ (rowNum - 1)).val().replace(thousandMarker,''));
132 if (scAmount < totalAmount) {
133 //if user enters less than the total amount and adds another soft credit row,
134 //the soft credit amount default will be left empty
135 $('#soft_credit_amount_'+ rowNum).val('');
136 }
137 }
138 });
139
140 });
141 </script>
142 {/literal}