Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-04-22-25-32
[civicrm-core.git] / templates / CRM / Contact / Form / ShareAddress.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.5 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2014 |
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 <div id="shared-address-{$blockId}" class="form-layout-compressed hiddenElement">
45 {$form.address.$blockId.master_contact_id.label}
46 {$form.address.$blockId.master_contact_id.html}
47 </div>
48 </td>
49 </tr>
50
51
52 {literal}
53 <script type="text/javascript">
54 function showHideSharedAddress( blockNo, showSelect ) {
55 // based on checkbox, show or hide
56 if ( cj( '#address\\[' + blockNo + '\\]\\[use_shared_address\\]' ).prop('checked') ) {
57 if ( showSelect && cj( '#shared-address-display-' + blockNo ).length == 0 ) {
58 cj( '#shared-address-' + blockNo ).show( );
59 }
60 cj( 'table#address_table_' + blockNo ).hide( );
61 cj( '#shared-address-display-' + blockNo ).show( );
62 cj( '#shared-address-display-name-' + blockNo ).show( );
63 cj( '#shared-address-display-cancel-' + blockNo ).hide( );
64 cj( '.crm-address-custom-set-block-' + blockNo).hide( );
65 } else {
66 cj( '#shared-address-' + blockNo ).hide( );
67 cj( 'table#address_table_' + blockNo ).show( );
68 cj( '#shared-address-display-' + blockNo ).hide( );
69 cj( '#shared-address-display-name-' + blockNo ).hide( );
70 cj( '#shared-address-display-cancel-' + blockNo ).hide( );
71 cj( '.crm-address-custom-set-block-' + blockNo).show( );
72 }
73 }
74
75 CRM.$(function($) {
76 var blockNo = {/literal}{$blockId}{literal};
77
78 // call this when form loads
79 showHideSharedAddress( blockNo, true );
80
81 // handle check / uncheck of checkbox
82 cj( '#address\\[' + blockNo + '\\]\\[use_shared_address\\]' ).click( function( ) {
83 showHideSharedAddress( blockNo, true );
84 });
85
86 // start of code to add onchange event for hidden element
87 var contactHiddenElement = 'input[name="address[' + blockNo +'][master_contact_id]"]';
88
89 // observe changes
90 cj( contactHiddenElement ).change(function( ) {
91 var sharedContactId = cj( this ).val( );
92 if ( !sharedContactId || isNaN( sharedContactId ) ) {
93 return;
94 }
95
96 var addressHTML = '';
97 var postUrl = {/literal}"{crmURL p='civicrm/ajax/inline' h=0}"{literal};
98
99 cj.post( postUrl, {
100 'contact_id': sharedContactId,
101 'type': 'method',
102 'async': false,
103 'class_name': 'CRM_Contact_Page_AJAX',
104 'fn_name': 'getAddressDisplay'
105 },
106 function( response ) {
107 if ( response ) {
108 var selected = 'checked';
109 var addressExists = false;
110
111 cj.each( response, function( i, val ) {
112 if ( i > 1 ) {
113 selected = '';
114 } else {
115 cj( 'input[name="address[' + blockNo + '][master_id]"]' ).val( val.id );
116 }
117
118 addressHTML = addressHTML + '<input type="radio" name="selected_shared_address-'+ blockNo +'" value=' + val.id + ' ' + selected +'>' + val.display_text + '<br/>';
119
120 addressExists = true;
121 });
122
123 if ( addressExists ) {
124 cj( '#shared-address-' + blockNo + ' .shared-address-list' ).remove( );
125 cj( '#shared-address-' + blockNo ).append( '<tr class="shared-address-list"><td></td><td>' + addressHTML + '</td></tr>');
126 cj( 'input[name^=selected_shared_address-]' ).click( function( ) {
127
128 // get the block id
129 var elemId = cj(this).attr( 'name' ).split('-');
130 cj( 'input[name="address[' + elemId[1] + '][master_id]"]' ).val( cj(this).val( ) );
131 });
132 } else {
133 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};
134 cj( '#shared-address-' + blockNo + ' .shared-address-list' ).remove( );
135 cj( '#shared-address-' + blockNo ).append( '<tr class="shared-address-list"><td></td><td>' + helpText + '</td></tr>');
136 }
137 }
138 },'json');
139 });
140 });
141 </script>
142 {/literal}
143
144