[REF] Move functions back to class that uses it
[civicrm-core.git] / templates / CRM / Member / Form / Membership.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 {* this template is used for adding/editing/deleting memberships for a contact *}
11 {if $isRecur}
12 <div class="messages status no-popup">
13 {icon icon="fa-info-circle"}{/icon}
14 <p>{ts}This membership is set to renew automatically {if $endDate}on {$endDate|crmDate}{/if}. Please be aware that any changes that you make here may not be reflected in the payment processor. Please ensure that you alter the related subscription at the payment processor.{/ts}</p>
15 {if $cancelAutoRenew}<p>{ts 1=$cancelAutoRenew}To stop the automatic renewal:
16 <a href="%1">Cancel auto-renew</a>
17 {/ts}</p>{/if}
18 </div>
19 {/if}
20 <div class="spacer"></div>
21 {if $priceSetId}
22 {include file="CRM/Price/Form/PriceSet.tpl" context="standalone" extends="Membership"}
23 {literal}
24 <script type="text/javascript">
25 CRM.$(function($) {
26 var membershipValues = [];
27 {/literal}{foreach from=$optionsMembershipTypes item=memType key=opId}{literal}
28 membershipValues[{/literal}{$opId}{literal}] = {/literal}{$memType}{literal};
29 {/literal}{/foreach}{literal}
30 processMembershipPriceset(membershipValues, {/literal}{$autoRenewOption}{literal}, 1);
31 {/literal}{if !$membershipMode}{literal}
32 enableAmountSection({/literal}{$contributionType}{literal});
33 {/literal}{/if}{literal}
34 });
35 </script>
36 {/literal}
37 {else}
38 {if $membershipMode == 'test' }
39 {assign var=registerMode value="TEST"}
40 {elseif $membershipMode == 'live'}
41 {assign var=registerMode value="LIVE"}
42 {/if}
43 {if !$emailExists and $action neq 8 and $context neq 'standalone'}
44 <div class="messages status no-popup">
45 {icon icon="fa-info-circle"}{/icon}
46 <p>{ts}You will not be able to send an automatic email receipt for this Membership because there is no email address recorded for this contact. If you want a receipt to be sent when this Membership is recorded, click Cancel and then click Edit from the Summary tab to add an email address before recording the Membership.{/ts}</p>
47 </div>
48 {/if}
49 {if $membershipMode}
50 <div class="help">
51 {ts 1=$displayName 2=$registerMode}Use this form to submit Membership Record on behalf of %1. <strong>A %2 transaction will be submitted</strong> using the selected payment processor.{/ts}
52 </div>
53 {/if}
54 <div class="crm-block crm-form-block crm-membership-form-block">
55 {if $newCredit AND $action EQ 1 AND $membershipMode EQ null}
56 <div class="action-link css_right crm-link-credit-card-mode">
57 {if $contactId}
58 {capture assign=ccModeLink}{crmURL p='civicrm/contact/view/membership' q="reset=1&action=add&cid=`$contactId`&context=`$context`&mode=live"}{/capture}
59 {else}
60 {capture assign=ccModeLink}{crmURL p='civicrm/contact/view/membership' q="reset=1&action=add&context=standalone&mode=live"}{/capture}
61 {/if}
62 <a class="open-inline-noreturn action-item crm-hover-button" href="{$ccModeLink}"><i class="crm-i fa-credit-card" aria-hidden="true"></i> {ts}submit credit card membership{/ts}</a>
63 </div>
64 {/if}
65 <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div>
66 {if $action eq 8}
67 <div class="messages status no-popup">
68 {icon icon="fa-info-circle"}{/icon}
69 {$deleteMessage}
70 </div>
71 {else}
72 <table class="form-layout-compressed">
73 <tr class="crm-membership-form-contact-id">
74 <td class="label">{$form.contact_id.label}</td>
75 <td>{$form.contact_id.html}</td>
76 </tr>
77 <tr class="crm-membership-form-block-membership_type_id">
78 <td class="label">{$form.membership_type_id.label}</td>
79 <td id="mem_type_id-readonly">
80 <span id="membership_type_id_0-readonly"></span> : <span id="membership_type_id_1-readonly"></span>
81 <span id="mem-type-override">
82 <a href="#" class="crm-hover-button action-item override-mem-type" id="show-mem-type">
83 {ts}Override organization and type{/ts}
84 </a>
85 {help id="override_membership_type"}
86 </span>
87 </td>
88 <td id="mem_type_id-editable"><span id='mem_type_id'>{$form.membership_type_id.html}</span>
89 {if $hasPriceSets}
90 <span id='totalAmountORPriceSet'> {ts}OR{/ts}</span>
91 <span id='selectPriceSet'>{$form.price_set_id.html}</span>
92 {if $buildPriceSet && $priceSet}
93 <div id="priceset"><br/>{include file="CRM/Price/Form/PriceSet.tpl" extends="Membership"}</div>
94 {else}
95 <div id="priceset" class="hiddenElement"></div>
96 {/if}
97 {/if}
98 {if $member_is_test} {ts}(test){/ts}{/if}<br />
99 <span class="description">{ts}Select Membership Organization and then Membership Type.{/ts}{if $hasPriceSets} {ts}Alternatively, you can use a price set.{/ts}{/if}</span>
100 </td>
101 </tr>
102 <tr id="maxRelated" class="crm-membership-form-block-max_related">
103 <td class="label">{$form.max_related.label}</td>
104 <td>{$form.max_related.html}<br />
105 <span class="description">{ts}Maximum number of related memberships (leave blank for unlimited).{/ts} <span id="eligibleRelated"></span></span>
106 </td>
107 </tr>
108 {if $action eq 1}
109 <tr id="num_terms_row" class="crm-membership-form-block-num_terms">
110 <td class="label">{$form.num_terms.label}</td>
111 <td>&nbsp;{$form.num_terms.html}<br />
112 <span class="description">{ts}Set the membership end date this many membership periods from now. Make sure the appropriate corresponding fee is entered below.{/ts}</span>
113 </td>
114 </tr>
115 {/if}
116 <tr class="crm-membership-form-block-source">
117 <td class="label">{$form.source.label}</td>
118 <td>&nbsp;{$form.source.html}<br />
119 <span class="description">{ts}Source of this membership. This value is searchable.{/ts}</span></td>
120 </tr>
121
122 {* CRM-7362 --add campaign to membership *}
123 {include file="CRM/Campaign/Form/addCampaignToComponent.tpl"
124 campaignTrClass="crm-membership-form-block-campaign_id"}
125
126 <tr class="crm-membership-form-block-join_date"><td class="label">{$form.join_date.label}</td><td>{$form.join_date.html}
127 <br />
128 <span class="description">{ts}When did this contact first become a member?{/ts}</span></td></tr>
129 <tr class="crm-membership-form-block-start_date"><td class="label">{$form.start_date.label}</td><td>{$form.start_date.html}
130 <br />
131 <span class="description">{ts}First day of current continuous membership period. Start Date will be automatically set based on Membership Type if you don't select a date.{/ts}</span></td></tr>
132 <tr class="crm-membership-form-block-end_date"><td class="label">{$form.end_date.label}</td>
133 <td id="end-date-readonly">
134 {$endDate|crmDate}
135 <a href="#" class="crm-hover-button action-item override-date" id="show-end-date">
136 {ts}Override end date{/ts}
137 </a>
138 {help id="override_end_date"}
139 </td>
140 <td id="end-date-editable">
141 {$form.end_date.html}
142 <br />
143 <span class="description">{ts}Latest membership period expiration date. End Date will be automatically set based on Membership Type if you don't select a date.{/ts}</span>
144 </td>
145 </tr>
146 {if !$membershipMode}
147 <tr>
148 <td class="label">{$form.is_override.label} {help id="id-status-override"}</td>
149 <td>
150 <span id="is-override">{$form.is_override.html}</span>
151 <span id="status-override-end-date">{$form.status_override_end_date.html}</span>
152 </td>
153 </tr>
154 {* Show read-only Status block - when action is UPDATE and is_override is FALSE *}
155 <tr id="memberStatus_show">
156 {if $action eq 2}
157 <td class="label">{$form.status_id.label}</td><td class="view-value">{$membershipStatus}</td>
158 {/if}
159 </tr>
160
161 {* Show editable status field when is_override is TRUE *}
162 <tr id="memberStatus"><td class="label">{$form.status_id.label}</td><td>{$form.status_id.html}<br />
163 <span class="description">{ts}When <strong>Status Override</strong> is active, the selected status will remain in force (it will NOT be subject to membership status rules) until it is cancelled or become inactive.{/ts}</span></td></tr>
164 {/if}
165
166 {if $accessContribution and !$membershipMode AND ($action neq 2 or (!$rows.0.contribution_id AND !$softCredit))}
167 <tr id="contri">
168 <td class="label">{$form.record_contribution.label}</td>
169 <td>{$form.record_contribution.html}<br />
170 <span class="description">{ts}Check this box to enter or update payment information. You will also be able to generate a customized receipt.{/ts}</span></td>
171 </tr>
172 <tr class="crm-membership-form-block-record_contribution"><td colspan="2">
173 <fieldset id="recordContribution"><legend>{ts}Membership Payment and Receipt{/ts}</legend>
174 {/if}
175 {include file="CRM/Member/Form/MembershipCommon.tpl"}
176 {if $emailExists and $isEmailEnabledForSite}
177 <tr id="send-receipt" class="crm-membership-form-block-send_receipt">
178 <td class="label">{$form.send_receipt.label}</td>
179 <td>
180 {$form.send_receipt.html}<br />
181 <span class="description">
182 {ts 1=$emailExists}Automatically email a membership confirmation and receipt to %1? OR if the payment is from a different contact, this email will only go to them.{/ts}
183 <span class="auto-renew-text">{ts}For auto-renewing memberships the emails are sent when each payment is received{/ts}</span>
184 </span>
185 </td>
186 </tr>
187 {elseif $context eq 'standalone' and $isEmailEnabledForSite}
188 <tr id="email-receipt" style="display:none;">
189 <td class="label">{$form.send_receipt.label}</td>
190 <td>
191 {$form.send_receipt.html}<br />
192 <span class="description">
193 {ts}Automatically email a membership confirmation and receipt to {/ts}<span id="email-address"></span>? {ts}OR if the payment is from a different contact, this email will only go to them.{/ts}
194 <span class="auto-renew-text">{ts}For auto-renewing memberships the emails are sent when each payment is received{/ts}</span>
195 </span>
196 </td>
197 </tr>
198 {/if}
199 <tr id="fromEmail" style="display: none" class="crm-contactEmail-form-block-fromEmailAddress crm-email-element">
200 <td class="label">{$form.from_email_address.label}</td>
201 <td>{$form.from_email_address.html} {help id="id-from_email" file="CRM/Contact/Form/Task/Email.hlp" isAdmin=$isAdmin}</td>
202 </tr>
203 <tr id='notice' style="display:none;">
204 <td class="label">{$form.receipt_text.label}</td>
205 <td class="html-adjust"><span class="description">{ts}If you need to include a special message for this member, enter it here. Otherwise, the confirmation email will include the standard receipt message configured under System Message Templates.{/ts}</span>
206 {$form.receipt_text.html|crmAddClass:huge}</td>
207 </tr>
208 </table>
209 {include file="CRM/common/customDataBlock.tpl"}
210 {if $accessContribution and $action eq 2 and $rows.0.contribution_id}
211 <div class="crm-accordion-wrapper">
212 <div class="crm-accordion-header">{ts}Related Contributions{/ts}</div>
213 <div class="crm-accordion-body">
214 {include file="CRM/Contribute/Form/Selector.tpl" context="Search"}
215 <script type="text/javascript">
216 var membershipID = {$entityID};
217 var contactID = {$contactId};
218 {literal}
219 CRM.$(function($) {
220 CRM.loadPage(
221 CRM.url(
222 'civicrm/membership/recurring-contributions',
223 {
224 reset: 1,
225 membershipID: membershipID,
226 cid: contactID
227 },
228 'back'
229 ),
230 {
231 target : '#membership-recurring-contributions',
232 dialog : false
233 }
234 );
235 });
236 {/literal}
237 </script>
238 <div id="membership-recurring-contributions"></div>
239 </div>
240 </div>
241 {/if}
242 {if $softCredit}
243 <div class="crm-accordion-wrapper">
244 <div class="crm-accordion-header">{ts}Related Soft Contributions{/ts}</div>
245 <div class="crm-accordion-body">{include file="CRM/Contribute/Page/ContributionSoft.tpl" context="membership"}</div>
246 </div>
247 {/if}
248 {/if}
249
250 <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
251 </div> <!-- end form-block -->
252
253 {if $action neq 8} {* Jscript additions not need for Delete action *}
254 {if $accessContribution and !$membershipMode AND ($action neq 2 or !$rows.0.contribution_id)}
255
256 {include file="CRM/common/showHideByFieldValue.tpl"
257 trigger_field_id ="record_contribution"
258 trigger_value =""
259 target_element_id ="recordContribution"
260 target_element_type ="table-row"
261 field_type ="radio"
262 invert = 0
263 }
264 {/if}
265
266 {literal}
267 <script type="text/javascript">
268 function setPaymentBlock(mode, checkboxEvent) {
269 if (cj('#price_set_id').length > 0 && cj('#price_set_id').val()) {
270 return;
271 }
272 var membershipTypeID = parseInt(cj('#membership_type_id_1').val());
273 if (!membershipTypeID) {
274 return;
275 }
276
277 var allMemberships = {/literal}{$allMembershipInfo}{literal};
278 membershipType = allMemberships[membershipTypeID];
279 if (!mode) {
280 //check the record_contribution checkbox if membership is a paid one
281 {/literal}{if $action eq 1}{literal}
282 if (!checkboxEvent) {
283 if (membershipType['total_amount_numeric'] > 0) {
284 cj('#record_contribution').prop('checked','checked');
285 cj('#recordContribution').show();
286 }
287 else {
288 cj('#record_contribution').prop('checked', false);
289 cj('#recordContribution').hide();
290 }
291 }
292 {/literal}{/if}{literal}
293 }
294
295 // skip this for test and live modes because financial type is set automatically
296 cj("#financial_type_id").val(membershipType['financial_type_id']);
297 var term = cj('#num_terms').val();
298 var taxTerm = {/literal}{$taxTerm|@json_encode}{literal};
299 var currency = {/literal}{$currency_symbol|@json_encode}{literal};
300 var taxExclusiveAmount = membershipType['total_amount_numeric'] * term;
301 var taxAmount = (membershipType['tax_rate']/100)*taxExclusiveAmount;
302 taxAmount = isNaN (taxAmount) ? 0:taxAmount;
303 cj("#total_amount").val(CRM.formatMoney(taxExclusiveAmount + taxAmount, true));
304
305 var taxMessage = taxAmount > 0 ? 'Includes '+taxTerm+' amount of '+currency+' '+taxAmount:'';
306 cj('.totaltaxAmount').html(taxMessage);
307 }
308
309
310 CRM.$(function($) {
311 var mode = {/literal}'{$membershipMode}'{literal};
312 if ( !mode ) {
313 // Offline form (mode = false) has the record_contribution checkbox
314 cj('#record_contribution').click( function( ) {
315 if ( cj(this).prop('checked') ) {
316 cj('#recordContribution').show( );
317 setPaymentBlock( false, true);
318 }
319 else {
320 cj('#recordContribution').hide( );
321 }
322 });
323 }
324
325 cj('#membership_type_id_1').change( function( ) {
326 setPaymentBlock(mode);
327 });
328 cj('#num_terms').change( function( ) {
329 setPaymentBlock(mode);
330 });
331
332 // show/hide different contact section
333 setDifferentContactBlock();
334 cj('#is_different_contribution_contact').change( function() {
335 setDifferentContactBlock();
336 });
337
338 // give option to override membership type for auto-renew memberships - dev/core#1331
339 {/literal}
340 {if $isRecur}
341 cj('#membership_type_id_0-readonly').text(cj('#membership_type_id_0 option:selected').text());
342 cj('#membership_type_id_1-readonly').text(cj('#membership_type_id_1 option:selected').text());
343 cj('#mem_type_id-readonly').show();
344 cj('#mem_type_id-editable').hide();
345 {else}
346 cj('#mem_type_id-readonly').hide();
347 cj('#mem_type_id-editable').show();
348 {/if}
349 {literal}
350
351 cj('#show-mem-type').click( function( e ) {
352 e.preventDefault();
353 cj('#mem_type_id-readonly').hide();
354 cj('#mem_type_id-editable').show();
355 });
356
357 // give option to override end-date for auto-renew memberships
358 {/literal}
359 {if $isRecur && $endDate}
360 cj('#end-date-readonly').show();
361 cj('#end-date-editable').hide();
362 {else}
363 cj('#end-date-readonly').hide();
364 cj('#end-date-editable').show();
365 {/if}
366 {literal}
367
368 cj('#show-end-date').click( function( e ) {
369 e.preventDefault();
370 cj('#end-date-readonly').hide();
371 cj('#end-date-editable').show();
372 });
373
374 });
375
376 function setDifferentContactBlock( ) {
377 // show/hide different contact section
378 if ( cj('#is_different_contribution_contact').prop('checked') ) {
379 cj('#record-different-contact').show();
380 }
381 else {
382 cj('#record-different-contact').hide();
383 }
384 }
385
386 function showEmailOptions() {
387 {/literal}
388 // @todo emailExists refers to the primary contact for the page.
389 // elsewhere some script determines if there is a paying contact the
390 // email should go to instead (e.g gift membership). This should be checked for here
391 // and that merged into that code as currently behaviour is inconsistent.
392 var emailExists = {$emailExists|json_encode};
393 var isStandalone = {if $context == 'standalone'}true{else}false{/if};
394 var isEmailEnabledForSite = {if $isEmailEnabledForSite}true{else}false{/if};
395
396 {literal}
397 var isEmailable = (isEmailEnabledForSite && (emailExists || isStandalone));
398
399 if (isEmailable && cj('#send_receipt').prop('checked') && !cj('#auto_renew').prop('checked')) {
400 // Hide extra message and from email for recurring as they cannot be stored until use.
401 cj('#notice').show();
402 cj('#fromEmail').show();
403 }
404 else {
405 cj('#notice').hide();
406 cj('#fromEmail').hide();
407 }
408 }
409 </script>
410 <script type="text/javascript">
411
412 {/literal}{if !$membershipMode}{literal}
413 cj( "#is_override" ).change(function() {
414 showHideMemberStatus();
415 });
416
417 showHideMemberStatus();
418 function showHideMemberStatus() {
419 var isOverride = cj( "#is_override" ).val();
420 switch (isOverride) {
421 case '0':
422 cj('#memberStatus').hide();
423 cj('#memberStatus_show').show();
424 cj('#status-override-end-date').hide();
425 break;
426 case '1':
427 cj('#memberStatus').show();
428 cj('#memberStatus_show').hide();
429 cj('#status-override-end-date').hide();
430 break;
431 case '2':
432 cj('#memberStatus').show();
433 cj('#memberStatus_show').hide();
434 cj('#status-override-end-date').show();
435 break;
436 default :
437 cj('#memberStatus').hide( );
438 cj('#memberStatus_show').show( );
439 cj('#status-override-end-date').hide();
440 break;
441 }
442 }
443 {/literal}{/if}
444
445 {if $context eq 'standalone' and $isEmailEnabledForSite }
446 {literal}
447 CRM.$(function($) {
448 var $form = $("form.{/literal}{$form.formClass}{literal}");
449 $("#contact_id", $form).change(checkEmail);
450 checkEmail( );
451
452 function checkEmail( ) {
453 var data = $("#contact_id", $form).select2('data');
454 if (data && data.extra && data.extra.email && data.extra.email.length) {
455 $("#email-receipt", $form).show();
456 if ($("#send_receipt", $form).is(':checked')) {
457 $("#notice", $form).show();
458 }
459 $("#email-address", $form).html(data.extra.email);
460 }
461 else {
462 $("#email-receipt, #notice", $form).hide();
463 }
464 }
465 });
466
467 {/literal}
468 {/if}
469
470 {literal}
471 //keep read only always checked.
472 CRM.$(function($) {
473 var $form = $("form.{/literal}{$form.formClass}{literal}");
474 var allowAutoRenew = {/literal}'{$allowAutoRenew}'{literal};
475 var alreadyAutoRenew = {/literal}'{$alreadyAutoRenew}'{literal};
476 if ( allowAutoRenew || alreadyAutoRenew ) {
477 $( "#auto_renew" ).click(function( ) {
478 if ( $(this).attr( 'readonly' ) ) {
479 $(this).prop('checked', true );
480 }
481 showEmailOptions();
482 });
483 }
484
485 {/literal}
486 {if !empty($existingContactMemberships)}
487
488 var alert, memberorgs = {$existingContactMemberships|@json_encode};
489
490 {literal}
491 $("select[name='membership_type_id[0]']").change(checkExistingMemOrg);
492
493
494
495 function checkExistingMemOrg () {
496 alert && alert.close && alert.close();
497 var selectedorg = $("select[name='membership_type_id[0]']").val();
498 if (selectedorg in memberorgs) {
499 var andEndDate = '',
500 endDate = memberorgs[selectedorg].membership_end_date,
501 org = $('option:selected', "select[name='membership_type_id[0]']").text();
502 if (endDate) {
503 andEndDate = '{/literal}{ts escape='js' 1='%1'}and end date of %1{/ts}{literal}';
504 andEndDate = ' ' + ts(andEndDate, {1:endDate});
505 }
506
507 alert = CRM.alert(
508 // Mixing client-side variables with a translated string in smarty is awkward!
509 ts({/literal}'{ts escape='js'}This contact has an existing %1 membership at %2 with %3 status%4.{/ts}'{literal}, {1:memberorgs[selectedorg].membership_type, 2: org, 3: memberorgs[selectedorg].membership_status, 4: andEndDate})
510 + '<ul><li><a href="' + memberorgs[selectedorg].renewUrl + '">'
511 + {/literal}'{ts escape='js'}Renew the existing membership instead{/ts}'
512 + '</a></li><li><a href="' + memberorgs[selectedorg].membershipTab + '">'
513 + '{ts escape='js'}View all existing and / or expired memberships for this contact{/ts}'{literal}
514 + '</a></li></ul>',
515 '{/literal}{ts escape='js'}Duplicate Membership?{/ts}{literal}', 'alert');
516 }
517 }
518 checkExistingMemOrg();
519 {/literal}
520 {/if}
521
522 {literal}
523
524 });
525 {/literal}
526
527 {if $membershipMode or $action eq 2}
528 buildAutoRenew( null, null, '{$membershipMode}');
529 {/if}
530 {literal}
531 function buildAutoRenew(membershipTypeID, processorId, mode ) {
532 var action = {/literal}'{$action}'{literal};
533
534 //for update lets hide it when not already recurring.
535 if (action == 2) {
536 //user can't cancel auto renew by unchecking.
537 if (cj("#auto_renew").prop('checked')) {
538 cj("#auto_renew").attr('readonly', true);
539 }
540 else {
541 cj("#autoRenew").hide( );
542 }
543 }
544
545 //we should do all auto renew for cc memberships.
546 if (!mode) {
547 return;
548 }
549
550 //get the required values in case missing.
551 if (!processorId) {
552 processorId = cj( '#payment_processor_id' ).val( );
553 }
554 if (!membershipTypeID) {
555 membershipTypeID = parseInt( cj('#membership_type_id_1').val( ) );
556 }
557
558 //we don't have both required values.
559 if (!processorId || !membershipTypeID) {
560 cj("#auto_renew").prop('checked', false);
561 cj("#autoRenew").hide();
562 showEmailOptions();
563 return;
564 }
565
566 var recurProcessors = {/literal}{$recurProcessor}{literal};
567 var autoRenewOptions = {/literal}{$autoRenewOptions}{literal};
568 var currentOption = autoRenewOptions[membershipTypeID];
569
570 if (!currentOption || !recurProcessors[processorId]) {
571 cj("#auto_renew").prop('checked', false );
572 cj("#autoRenew").hide();
573 return;
574 }
575
576 if (currentOption == 1) {
577 cj("#autoRenew").show();
578 if (cj("#auto_renew").attr('readonly')) {
579 cj("#auto_renew").prop('checked', false).removeAttr('readonly');
580 }
581 }
582 else if ( currentOption == 2 ) {
583 cj("#autoRenew").show();
584 cj("#auto_renew").prop('checked', true).attr('readonly', true);
585 }
586 else {
587 cj("#auto_renew").prop('checked', false);
588 cj("#autoRenew").hide( );
589 }
590 showEmailOptions();
591 }
592
593 var customDataType = {/literal}{$customDataType|@json_encode}{literal};
594
595 // load form during form rule.
596 {/literal}{if $buildPriceSet}{literal}
597 cj("#totalAmountORPriceSet, #mem_type_id, #num_terms_row, .crm-membership-form-block-financial_type_id-mode").hide();
598 cj('#total_amount').attr("readonly", true);
599 {/literal}{/if}{literal}
600
601 function buildAmount( priceSetId ) {
602 if (!priceSetId) {
603 priceSetId = cj("#price_set_id").val();
604 }
605 var fname = '#priceset';
606 if ( !priceSetId ) {
607 cj('#membership_type_id_1').val(0);
608 CRM.buildCustomData(customDataType, null);
609
610 // hide price set fields.
611 cj( fname ).hide( );
612
613 // show/hide price set amount and total amount.
614 cj( "#mem_type_id").show( );
615 var choose = "{/literal}{ts escape='js'}Choose price set{/ts}{literal}";
616 cj("#price_set_id option[value='']").html( choose );
617 cj( "#totalAmountORPriceSet" ).show( );
618 cj('#total_amount').removeAttr("readonly");
619 cj( "#num_terms_row").show( );
620 cj(".crm-membership-form-block-financial_type_id-mode").show();
621
622 {/literal}{if $allowAutoRenew}{literal}
623 cj('#autoRenew').hide();
624 cj("#auto_renew").removeAttr('readOnly').prop('checked', false );
625 {/literal}{/if}{literal}
626 return;
627 }
628
629 cj( "#total_amount" ).val('').attr("readonly", true);
630
631 var dataUrl = {/literal}"{crmURL h=0 q='snippet=4'}"{literal} + '&priceSetId=' + priceSetId;
632
633 var response = cj.ajax({
634 url: dataUrl,
635 async: false
636 }).responseText;
637
638 cj( fname ).show( ).html( response );
639 // freeze total amount text field.
640
641 cj( "#totalAmountORPriceSet" ).hide( );
642 cj( "#mem_type_id" ).hide( );
643 var manual = "{/literal}{ts escape='js'}Manual membership and price{/ts}{literal}";
644 cj("#price_set_id option[value='']").html( manual );
645 cj( "#num_terms_row" ).hide( );
646 cj(".crm-membership-form-block-financial_type_id-mode").hide();
647 }
648
649 buildMaxRelated(cj('#membership_type_id_1', false).val());
650
651 function buildMaxRelated( memType, setDefault ) {
652 var allMemberships = {/literal}{$allMembershipInfo}{literal};
653
654 if ((memType > 0) && (allMemberships[memType]['has_related'])) {
655 if (setDefault) cj('#max_related').val(allMemberships[memType]['max_related']);
656 cj('#maxRelated').show();
657 var cid = {/literal}{if $contactID}{$contactID}{else}null{/if}{literal};
658 if (cid) {
659 CRM.api('relationship', 'getcount', {contact_id: cid, membership_type_id: memType}, {
660 success: function(result) {
661 var relatable;
662 if (result.result === 0) {
663 relatable = '{/literal}{ts escape='js'}No contacts are currently eligible to inherit this relationship.{/ts}{literal}';
664 }
665 else if (result.result === 1) {
666 relatable = '{/literal}{ts escape='js'}One contact is currently eligible to inherit this relationship.{/ts}{literal}';
667 }
668 else {
669 relatable = '{/literal}{ts escape='js' 1='%1'}%1 contacts are currently eligible to inherit this relationship.{/ts}{literal}';
670 relatable = ts(relatable, {1: result});
671 }
672 cj('#eligibleRelated').text(relatable);
673 }
674 });
675 }
676 } else {
677 cj('#max_related').val('');
678 cj('#maxRelated').hide();
679 }
680 }
681
682 var lastMembershipTypes = [];
683 var optionsMembershipTypes = [];
684
685 // function to load custom data for selected membership types through priceset
686 function processMembershipPriceset( membershipValues, autoRenewOption, reload ) {
687 var currentMembershipType = [];
688 var count = 0;
689 var loadCustomData = 0;
690 if ( membershipValues ) {
691 optionsMembershipTypes = membershipValues;
692 }
693
694 if ( reload ) {
695 lastMembershipTypes = [];
696 {/literal}{if $allowAutoRenew}{literal}
697 cj('#autoRenew').hide();
698 var autoRenew = cj("#auto_renew");
699 autoRenew.removeAttr('readOnly').prop('checked', false );
700 if (autoRenewOption == 1) {
701 cj('#autoRenew').show();
702 }
703 else if (autoRenewOption == 2) {
704 autoRenew.attr('readOnly', true).prop('checked', true );
705 cj('#autoRenew').show();
706 }
707 {/literal}{/if}{literal}
708 }
709
710 cj("input,#priceset select,#priceset").each(function () {
711 if ( cj(this).attr('price') ) {
712 switch( cj(this).attr('type') ) {
713 case 'checkbox':
714 if ( cj(this).prop('checked') ) {
715 eval( 'var option = ' + cj(this).attr('price') ) ;
716 var ele = option[0];
717 var memTypeId = optionsMembershipTypes[ele];
718 if ( memTypeId && cj.inArray(optionsMembershipTypes[ele], currentMembershipType) == -1 ) {
719 currentMembershipType[count] = memTypeId;
720 count++;
721 }
722 }
723 if ( reload ) {
724 cj(this).click( function( ) {
725 processMembershipPriceset();
726 });
727 }
728 break;
729
730 case 'radio':
731 if ( cj(this).prop('checked') && cj(this).val() ) {
732 var memTypeId = optionsMembershipTypes[cj(this).val()];
733 if ( memTypeId && cj.inArray(memTypeId, currentMembershipType) == -1 ) {
734 currentMembershipType[count] = memTypeId;
735 count++;
736 }
737 }
738 if ( reload ) {
739 cj(this).click( function( ) {
740 processMembershipPriceset();
741 });
742 }
743 break;
744
745 case 'select-one':
746 if ( cj(this).val( ) ) {
747 var memTypeId = optionsMembershipTypes[cj(this).val()];
748 if ( memTypeId && cj.inArray(memTypeId, currentMembershipType) == -1 ) {
749 currentMembershipType[count] = memTypeId;
750 count++;
751 }
752 }
753 if ( reload ) {
754 cj(this).change( function( ) {
755 processMembershipPriceset();
756 });
757 }
758 break;
759 }
760 }
761 });
762
763 for( i in currentMembershipType ) {
764 if ( cj.inArray(currentMembershipType[i], lastMembershipTypes) == -1 ) {
765 loadCustomData = 1;
766 break;
767 }
768 }
769
770 if ( !loadCustomData ) {
771 for( i in lastMembershipTypes) {
772 if ( cj.inArray(lastMembershipTypes[i], currentMembershipType) == -1 ) {
773 loadCustomData = 1;
774 break;
775 }
776 }
777 }
778
779 lastMembershipTypes = currentMembershipType;
780
781 // load custom data only if change in membership type selection
782 if ( !loadCustomData ) {
783 return;
784 }
785
786 subTypeNames = currentMembershipType.join(',');
787 if ( subTypeNames.length < 1 ) {
788 subTypeNames = null;
789 }
790
791 CRM.buildCustomData(customDataType, subTypeNames);
792 }
793
794 function enableAmountSection( setContributionType ) {
795 if ( !cj('#record_contribution').prop('checked') ) {
796 cj('#record_contribution').click( );
797 cj('#recordContribution').show( );
798 }
799 if ( setContributionType ) {
800 cj('#financial_type_id').val(setContributionType);
801 }
802 }
803 </script>
804 {/literal}
805 {/if} {* closing of delete check if *}
806 {/if}{* closing of custom data if *}