rt ticket #1057626
[civicrm-core.git] / templates / CRM / Contribute / Form / Contribution / PremiumBlock.tpl
CommitLineData
6a488035
TO
1{*
2 +--------------------------------------------------------------------+
2c4c49ca 3 | CiviCRM version 4.7 |
6a488035 4 +--------------------------------------------------------------------+
e7112fa7 5 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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{if $products}
27 <div id="premiums" class="premiums-group">
28 {if $context EQ "makeContribution"}
29 <fieldset class="crm-group premiums_select-group">
52984883 30 <legend>Premiums</legend>
6a488035
TO
31 {if $premiumBlock.premiums_intro_title}
32 <legend>{$premiumBlock.premiums_intro_title}</legend>
33 {/if}
34 {if $premiumBlock.premiums_intro_text}
35 <div id="premiums-intro" class="crm-section premiums_intro-section">
36 {$premiumBlock.premiums_intro_text}
b2603e29 37 </div>
6a488035
TO
38 {/if}
39 {/if}
40
41 {if $context EQ "confirmContribution" OR $context EQ "thankContribution"}
42 <div class="crm-group premium_display-group">
43 <div class="header-dark">
44 {if $premiumBlock.premiums_intro_title}
45 {$premiumBlock.premiums_intro_title}
46 {else}
47 {ts}Your Premium Selection{/ts}
48 {/if}
49 </div>
50 {/if}
51
52 {if $preview}
53 {assign var="showSelectOptions" value="1"}
54 {/if}
55
56 {strip}
57 <div id="premiums-listings">
58 {if $showPremium AND !$preview AND $premiumBlock.premiums_nothankyou_position EQ 1}
59 <div class="premium premium-no_thanks" id="premium_id-no_thanks" min_contribution="0">
60 <div class="premium-short">
61 <input type="checkbox" disabled="disabled" /> {$premiumBlock.premiums_nothankyou_label}
62 </div>
63 <div class="premium-full">
64 <input type="checkbox" checked="checked" disabled="disabled" /> {$premiumBlock.premiums_nothankyou_label}
65 </div>
66 </div>
b2603e29 67 {/if}
6a488035
TO
68 {foreach from=$products item=row}
69 <div class="premium {if $showPremium}premium-selectable{/if}" id="premium_id-{$row.id}" min_contribution="{$row.min_contribution}">
70 <div class="premium-short">
71 {if $row.thumbnail}<div class="premium-short-thumbnail"><img src="{$row.thumbnail}" alt="{$row.name}" /></div>{/if}
72 <div class="premium-short-content">{$row.name}</div>
73 <div style="clear:both"></div>
74 </div>
75
76 <div class="premium-full">
77 <div class="premium-full-image">{if $row.image}<img src="{$row.image}" alt="{$row.name}" />{/if}</div>
78 <div class="premium-full-content">
79 <div class="premium-full-title">{$row.name}</div>
80 <div class="premium-full-disabled">
b77dcf46 81 {ts 1=$row.min_contribution|crmMoney}You must contribute at least %1 to get this item{/ts}<br/>
d6cada7c 82 <input type="button" value="{ts 1=$row.min_contribution|crmMoney}Contribute %1 Instead{/ts}" amount="{$row.min_contribution}" />
6a488035
TO
83 </div>
84 <div class="premium-full-description">
85 {$row.description}
86 </div>
87 {if $showSelectOptions }
88 {assign var="pid" value="options_"|cat:$row.id}
89 {if $pid}
90 <div class="premium-full-options">
91 <p>{$form.$pid.html}</p>
92 </div>
93 {/if}
94 {else}
95 <div class="premium-full-options">
b2603e29 96 <p><strong>{$row.options}</strong></p>
6a488035
TO
97 </div>
98 {/if}
99 {if ( ($premiumBlock.premiums_display_min_contribution AND $context EQ "makeContribution") OR $preview EQ 1) AND $row.min_contribution GT 0 }
100 <div class="premium-full-min">{ts 1=$row.min_contribution|crmMoney}Minimum: %1{/ts}</div>
101 {/if}
102 <div style="clear:both"></div>
103 </div>
104 </div>
105 <div style="clear:both"></div>
106 </div>
107 {/foreach}
108 {if $showPremium AND !$preview AND $premiumBlock.premiums_nothankyou_position EQ 2}
109 <div class="premium premium-no_thanks" id="premium_id-no_thanks" min_contribution="0">
110 <div class="premium-short">
111 <input type="checkbox" disabled="disabled" /> {$premiumBlock.premiums_nothankyou_label}
112 </div>
113 <div class="premium-full">
114 <input type="checkbox" checked="checked" disabled="disabled" /> {$premiumBlock.premiums_nothankyou_label}
115 </div>
116 </div>
b2603e29 117 {/if}
6a488035
TO
118 </div>
119 {/strip}
120
121 {if $context EQ "makeContribution"}
122 </fieldset>
123 {elseif ! $preview} {* Close premium-display-group div for Confirm and Thank-you pages *}
124 </div>
125 {/if}
126 </div>
b2603e29 127
6a488035
TO
128 {if $context EQ "makeContribution"}
129 {literal}
b2603e29 130 <script>
3cc60a06 131 CRM.$(function($) {
6a488035
TO
132 var is_separate_payment = {/literal}{if $membershipBlock.is_separate_payment}{$membershipBlock.is_separate_payment}{else}0{/if}{literal};
133
134 // select a new premium
b2603e29 135 function select_premium(premium_id) {
ae8f569f 136 if($(premium_id).length) {
6a488035 137 // hide other active premium
ae8f569f
CW
138 $('.premium-full').hide();
139 $('.premium-short').show();
6a488035 140 // show this one
ae8f569f
CW
141 $('.premium-short', $(premium_id)).hide();
142 $('.premium-full', $(premium_id)).show();
6a488035
TO
143 // record this one
144 var id_parts = premium_id.split('-');
ae8f569f 145 $('#selectProduct').val(id_parts[1]);
6a488035
TO
146 }
147 }
148
149 // click premium to select
ae8f569f
CW
150 $('.premium-short').click(function(){
151 select_premium( '#'+$($(this).parent()).attr('id') );
6a488035
TO
152 });
153
154 // select the default premium
ae8f569f 155 var premium_id = $('#selectProduct').val();
6a488035
TO
156 if(premium_id == '') premium_id = 'no_thanks';
157 select_premium('#premium_id-'+premium_id);
158
159 // get the current amount
160 function get_amount() {
161 var amount;
162
c13cb8db 163 if (typeof totalfee !== "undefined") {
164 return totalfee;
165 }
166
6a488035 167 // see if other amount exists and has a value
ae8f569f
CW
168 if($('.other_amount-content input').length) {
169 amount = Number($('.other_amount-content input').val());
6a488035
TO
170 if(isNaN(amount))
171 amount = 0;
172 }
173
174 function check_price_set(price_set_radio_buttons) {
c13cb8db 175 if (!amount) {
ae8f569f
CW
176 $(price_set_radio_buttons).each(function(){
177 if ($(this).prop('checked')) {
178 amount = $(this).attr('data-amount');
c13cb8db 179 if (typeof amount !== "undefined") {
6a488035 180 amount = Number(amount);
c13cb8db 181 }
182 else {
183 amount = 0;
6a488035
TO
184 }
185 }
186 });
187 }
188 }
189
190 // check for additional contribution
191 var additional_amount = 0;
192 if(is_separate_payment) {
193 additional_amount = amount;
194 amount = 0;
195 }
196
6a488035
TO
197 // make sure amount is a number at this point
198 if(!amount) amount = 0;
199
c13cb8db 200 // next, check for membership/contribution level price set
201 check_price_set('#priceset input[type="radio"]');
202
6a488035
TO
203 // account for is_separate_payment
204 if(is_separate_payment && additional_amount) {
205 amount += additional_amount;
206 }
207
208 return amount;
209 }
210
211 // update premiums
212 function update_premiums() {
213 var amount = get_amount();
214
ae8f569f
CW
215 $('.premium').each(function(){
216 var min_contribution = $(this).attr('min_contribution');
6a488035 217 if(amount < min_contribution) {
ae8f569f 218 $(this).addClass('premium-disabled');
6a488035 219 } else {
ae8f569f 220 $(this).removeClass('premium-disabled');
6a488035
TO
221 }
222 });
223 }
ae8f569f
CW
224 $('.other_amount-content input').change(update_premiums);
225 $('input, #priceset').change(update_premiums);
6a488035
TO
226 update_premiums();
227
228 // build a list of price sets
229 var amounts = [];
230 var price_sets = {};
ae8f569f 231 $('input, #priceset select,#priceset').each(function(){
c13cb8db 232 if (this.tagName == 'SELECT') {
ae8f569f
CW
233 var selectID = $(this).attr('id');
234 var selectvalues = JSON.parse($(this).attr('price'));
c13cb8db 235 Object.keys(selectvalues).forEach(function (key) {
236 var option = selectvalues[key].split(optionSep);
237 amount = Number(option[0]);
238 price_sets[amount] = '#' + selectID + '-' + key;
239 amounts.push(amount);
240 });
241 }
242 else {
ae8f569f 243 var amount = Number($(this).attr('data-amount'));
c13cb8db 244 if (!isNaN(amount)) {
245 amounts.push(amount);
6a488035 246
ae8f569f 247 var id = $(this).attr('id');
c13cb8db 248 price_sets[amount] = '#'+id;
249 }
6a488035
TO
250 }
251 });
252 amounts.sort(function(a,b){return a - b});
253
254 // make contribution instead buttons work
ae8f569f
CW
255 $('.premium-full-disabled input').click(function(){
256 var amount = Number($(this).attr('amount'));
c13cb8db 257 if (price_sets[amount]) {
ae8f569f 258 if (!$(price_sets[amount]).length) {
c13cb8db 259 var option = price_sets[amount].split('-');
ae8f569f
CW
260 $(option[0]).val(option[1]);
261 $(option[0]).trigger('change');
c13cb8db 262 }
ae8f569f
CW
263 else if ($(price_sets[amount]).attr('type') == 'checkbox') {
264 $(price_sets[amount]).prop("checked",true);
c13cb8db 265 if ((typeof totalfee !== 'undefined') && (typeof display == 'function')) {
266 if (totalfee > 0) {
267 totalfee += amount;
268 }
269 else {
270 totalfee = amount;
271 }
272 display(totalfee);
273 }
274 }
275 else {
ae8f569f
CW
276 $(price_sets[amount]).click();
277 $(price_sets[amount]).trigger('click');
c13cb8db 278 }
6a488035
TO
279 } else {
280 // is there an other amount input box?
ae8f569f 281 if($('.other_amount-section input').length) {
6a488035
TO
282 // is this a membership form with separate payment?
283 if(is_separate_payment) {
284 var current_amount = 0;
ae8f569f
CW
285 if($('#priceset input[type="radio"]:checked').length) {
286 current_amount = Number($('#priceset input[type="radio"]:checked').attr('data-amount'));
6a488035
TO
287 if(!current_amount) current_amount = 0;
288 }
289 var new_amount = amount - current_amount;
ae8f569f 290 $('.other_amount-section input').val(new_amount.toFixed(2));
6a488035 291 } else {
ae8f569f
CW
292 $('.other_amount-section input').click();
293 $('.other_amount-section input').val($(this).attr('amount'));
6a488035
TO
294 }
295 } else {
296 // find the next best price set
297 var selected_price_set = false;
298 for(var i in amounts) {
299 if(amounts[i] >= amount) {
300 selected_price_set = amounts[i];
301 break;
302 }
303 }
304 if(!selected_price_set) {
305 selected_price_set = amounts[amounts.length-1];
306 }
c13cb8db 307
ae8f569f 308 if (!$(price_sets[selected_price_set]).length) {
c13cb8db 309 var option = price_sets[selected_price_set].split('-');
ae8f569f
CW
310 $(option[0]).val(option[1]);
311 $(option[0]).trigger('change');
c13cb8db 312 }
ae8f569f
CW
313 else if ($(price_sets[selected_price_set]).attr('type') == 'checkbox') {
314 $(price_sets[selected_price_set]).prop("checked",true);
c13cb8db 315 if ((typeof totalfee !== 'undefined') && (typeof display == 'function')) {
316 if (totalfee > 0) {
317 totalfee += amount;
318 }
319 else {
320 totalfee = amount;
321 }
322 display(totalfee);
323 }
324 }
325 else {
ae8f569f
CW
326 $(price_sets[selected_price_set]).click();
327 $(price_sets[selected_price_set]).trigger('click');
c13cb8db 328 }
6a488035
TO
329 }
330 }
331 update_premiums();
332 });
333
334 // validation of premiums
335 var error_message = '{/literal}{ts escape="js"}You must contribute more to get that item{/ts}{literal}';
ae8f569f
CW
336 $.validator.addMethod('premiums', function(value, element, params){
337 var premium_id = $('#selectProduct').val();
338 var premium$ = $('#premium_id-'+premium_id);
6a488035
TO
339 if(premium$.length) {
340 if(premium$.hasClass('premium-disabled')) {
341 return false;
342 }
343 }
344 return true;
345 }, error_message);
b2603e29 346
6a488035
TO
347 // add validation rules
348 CRM.validate.functions.push(function(){
ae8f569f 349 $('#selectProduct').rules('add', 'premiums');
6a488035 350 });
b2603e29 351
6a488035
TO
352 // need to use jquery validate's ignore option, so that it will not ignore hidden fields
353 CRM.validate.params['ignore'] = '.ignore';
354 });
355 </script>
356 {/literal}
357
358 {else}
359 {literal}
360 <script>
3cc60a06 361 CRM.$(function($) {
6a488035
TO
362 cj('.premium-short').hide();
363 cj('.premium-full').show();
364 });
365 </script>
366 {/literal}
367 {/if}
368{/if}
369