Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | {* |
2 | +--------------------------------------------------------------------+ | |
3 | | CiviCRM version 4.3 | | |
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 handling share address functionality*} | |
27 | <tr> | |
28 | <td> | |
29 | {$form.address.$blockId.use_shared_address.html}{$form.address.$blockId.use_shared_address.label}{help id="id-sharedAddress" file="CRM/Contact/Form/Contact.hlp"}<br /> | |
30 | {if !empty($sharedAddresses.$blockId.shared_address_display)} | |
31 | <span class="shared-address-display" id="shared-address-display-name-{$blockId}"> | |
32 | {$sharedAddresses.$blockId.shared_address_display.name} | |
33 | </span> | |
34 | ||
35 | <span class="shared-address-display" id="shared-address-display-{$blockId}" onclick="cj(this).hide( );cj('#shared-address-display-name-{$blockId}').hide( );cj('#shared-address-display-cancel-{$blockId}').show( );cj('#shared-address-{$blockId}').show( );"> | |
36 | {$sharedAddresses.$blockId.shared_address_display.address} <a href='#' onclick='return false;'>( {ts}Change current shared address{/ts} )</a> | |
37 | </span> | |
38 | ||
39 | <span id="shared-address-display-cancel-{$blockId}" class="hiddenElement" onclick="cj(this).hide( );cj('#shared-address-display-name-{$blockId}').show( );cj('#shared-address-display-{$blockId}').show( );cj('#shared-address-{$blockId}').hide( );"> | |
40 | <a href='#' onclick='return false;'>( {ts}Cancel{/ts} )</a> | |
41 | </span> | |
42 | {/if} | |
43 | ||
44 | <table id="shared-address-{$blockId}" class="form-layout-compressed hiddenElement"> | |
45 | {include file="CRM/Contact/Form/NewContact.tpl" blockNo="$blockId"} | |
46 | </table> | |
47 | </td> | |
48 | </tr> | |
49 | ||
50 | ||
51 | {literal} | |
52 | <script type="text/javascript"> | |
53 | function showHideSharedAddress( blockNo, showSelect ) { | |
54 | // based on checkbox, show or hide | |
55 | if ( cj( '#address\\[' + blockNo + '\\]\\[use_shared_address\\]' ).attr( 'checked') ) { | |
56 | if ( showSelect && cj( '#shared-address-display-' + blockNo ).length == 0 ) { | |
57 | cj( '#shared-address-' + blockNo ).show( ); | |
58 | } | |
59 | cj( 'table#address_table_' + blockNo ).hide( ); | |
60 | cj( '#shared-address-display-' + blockNo ).show( ); | |
61 | cj( '#shared-address-display-name-' + blockNo ).show( ); | |
62 | cj( '#shared-address-display-cancel-' + blockNo ).hide( ); | |
63 | cj( '.crm-address-custom-set-block-' + blockNo).hide( ); | |
64 | } else { | |
65 | cj( '#shared-address-' + blockNo ).hide( ); | |
66 | cj( 'table#address_table_' + blockNo ).show( ); | |
67 | cj( '#shared-address-display-' + blockNo ).hide( ); | |
68 | cj( '#shared-address-display-name-' + blockNo ).hide( ); | |
69 | cj( '#shared-address-display-cancel-' + blockNo ).hide( ); | |
70 | cj( '.crm-address-custom-set-block-' + blockNo).show( ); | |
71 | } | |
72 | } | |
73 | ||
74 | cj( function( ) { | |
75 | var blockNo = {/literal}{$blockId}{literal}; | |
76 | ||
77 | // call this when form loads | |
78 | showHideSharedAddress( blockNo, true ); | |
79 | ||
80 | // handle check / uncheck of checkbox | |
81 | cj( '#address\\[' + blockNo + '\\]\\[use_shared_address\\]' ).click( function( ) { | |
82 | showHideSharedAddress( blockNo, true ); | |
83 | }); | |
84 | ||
85 | // start of code to add onchange event for hidden element | |
86 | var contactHiddenElement = 'input[name="contact_select_id[' + blockNo +']"]'; | |
87 | ||
88 | // store initial value | |
89 | var _default = cj( contactHiddenElement ).val(); | |
90 | ||
91 | // observe changes | |
92 | cj( contactHiddenElement ).change(function( ) { | |
93 | var sharedContactId = cj( this ).val( ); | |
94 | if ( !sharedContactId || isNaN( sharedContactId ) ) { | |
95 | return; | |
96 | } | |
97 | ||
98 | var addressHTML = ''; | |
99 | var postUrl = {/literal}"{crmURL p='civicrm/ajax/inline' h=0}"{literal}; | |
100 | ||
101 | addCiviOverlay('div.crm-address_' + blockNo); | |
102 | ||
103 | cj.post( postUrl, { | |
104 | 'contact_id': sharedContactId, | |
105 | 'type': 'method', | |
106 | 'class_name': 'CRM_Contact_Page_AJAX', | |
107 | 'fn_name': 'getAddressDisplay' | |
108 | }, | |
109 | function( response ) { | |
110 | if ( response ) { | |
111 | var selected = 'checked'; | |
112 | var addressExists = false; | |
113 | ||
114 | cj.each( response, function( i, val ) { | |
115 | if ( i > 1 ) { | |
116 | selected = ''; | |
117 | } else { | |
118 | cj( 'input[name="address[' + blockNo + '][master_id]"]' ).val( val.id ); | |
119 | } | |
120 | ||
121 | addressHTML = addressHTML + '<input type="radio" name="selected_shared_address-'+ blockNo +'" value=' + val.id + ' ' + selected +'>' + val.display_text + '<br/>'; | |
122 | ||
123 | addressExists = true; | |
124 | }); | |
125 | ||
126 | if ( addressExists ) { | |
127 | cj( '#shared-address-' + blockNo + ' .shared-address-list' ).remove( ); | |
128 | cj( '#shared-address-' + blockNo ).append( '<tr class="shared-address-list"><td></td><td>' + addressHTML + '</td></tr>'); | |
129 | cj( 'input[name^=selected_shared_address-]' ).click( function( ) { | |
130 | ||
131 | // get the block id | |
132 | var elemId = cj(this).attr( 'name' ).split('-'); | |
133 | cj( 'input[name="address[' + elemId[1] + '][master_id]"]' ).val( cj(this).val( ) ); | |
134 | }); | |
135 | } else { | |
136 | var helpText = {/literal}"{ts escape='js'}Selected contact does not have an address. Please edit that contact to add an address, or select a different contact.{/ts}"{literal}; | |
137 | cj( '#shared-address-' + blockNo + ' .shared-address-list' ).remove( ); | |
138 | cj( '#shared-address-' + blockNo ).append( '<tr class="shared-address-list"><td></td><td>' + helpText + '</td></tr>'); | |
139 | } | |
140 | ||
141 | removeCiviOverlay('div.crm-address_' + blockNo); | |
142 | } | |
143 | },'json'); | |
144 | }); | |
145 | ||
146 | ||
147 | // continuous check for changed value | |
148 | setInterval(function( ) { | |
149 | if ( cj( contactHiddenElement ).val( ) != _default ) { | |
150 | // trigger native | |
151 | cj( contactHiddenElement ).change( ); | |
152 | ||
153 | // update stored value | |
154 | _default = cj( contactHiddenElement ).val( ); | |
155 | } | |
156 | ||
157 | }, 500); | |
158 | // end of code to add onchange event for hidden element | |
159 | }); | |
160 | </script> | |
161 | {/literal} | |
162 | ||
163 |