CRM-21008 - Checkbox to explicitly change employer when sharing address
[civicrm-core.git] / templates / CRM / Contact / Form / ShareAddress.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 5 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2019 |
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"}
30 <div id="shared-address-{$blockId}" class="form-layout-compressed">
31 {$form.address.$blockId.master_contact_id.label}
32 {$form.address.$blockId.master_contact_id.html}
33 <div class="shared-address-update-employer" style="display: none;">
34 {$form.address.$blockId.update_current_employer.html}
35 {$form.address.$blockId.update_current_employer.label}
36 {help id="id-sharedAddress-updateRelationships" file="CRM/Contact/Form/Contact"}
37 </div>
38 <div class="shared-address-list">
39 {if !empty($sharedAddresses.$blockId.shared_address_display)}
40 {foreach item='sa' from=$sharedAddresses.$blockId.shared_address_display.options}
41 {assign var="sa_name" value="selected_shared_address-`$blockId`"}
42 {assign var="sa_id" value="`$sa_name`-`$sa.id`"}
43 <input type="radio" name="{$sa_name}" id="{$sa_id}" value="{$sa.id}" {if $sa.id eq $sharedAddresses.$blockId.shared_address_display.master_id}checked="checked"{/if}>
44 <label for="{$sa_id}">{$sa.display_text}</label>{if $sa.location_type}({$sa.location_type}){/if}<br/>
45 {/foreach}
46 {/if}
47 </div>
48 </div>
49 </td>
50 </tr>
51
52
53 {literal}
54 <script type="text/javascript">
55 CRM.$(function($) {
56 var blockNo = {/literal}{$blockId}{literal},
57 contactType = {/literal}{$contactType|@json_encode}{literal},
58 $employerSection = $('#shared-address-' + blockNo + ' .shared-address-update-employer'),
59 $contentArea = $('#shared-address-' + blockNo + ' .shared-address-list'),
60 $masterElement = $('input[name="address[' + blockNo + '][master_id]"]');
61
62 function showHideSharedAddress() {
63 // based on checkbox, show or hide
64 var share = $(this).prop('checked');
65 $('#shared-address-' + blockNo).toggle(!!share);
66 $('table#address_table_' + blockNo +', .crm-address-custom-set-block-' + blockNo).toggle(!share);
67 }
68
69 // "Use another contact's address" checkbox
70 $('#address\\[' + blockNo + '\\]\\[use_shared_address\\]').each(showHideSharedAddress).click(showHideSharedAddress);
71
72 // When an address is selected
73 $contentArea.off().on('click', 'input', function() {
74 $masterElement.val($(this).val());
75 });
76
77 // When shared contact is selected/unselected
78 $('input[name="address[' + blockNo +'][master_contact_id]"]').change(function() {
79 var $el = $(this),
80 sharedContactId = $el.val();
81
82 $contentArea.html('');
83 $masterElement.val('');
84
85 if (!sharedContactId || isNaN(sharedContactId)) {
86 $employerSection.hide();
87 return;
88 }
89
90 var otherContactType = $el.select2('data').extra.contact_type;
91 $employerSection.toggle(contactType === 'Individual' && otherContactType === 'Organization');
92
93 $.post(CRM.url('civicrm/ajax/inline'), {
94 'contact_id': sharedContactId,
95 'type': 'method',
96 'class_name': 'CRM_Contact_Page_AJAX',
97 'fn_name': 'getAddressDisplay'
98 },
99 function(response) {
100 // Avoid race conditions - check that value hasn't been changed by the user while we were waiting for response
101 if (response && $el.val() === sharedContactId) {
102 var selected = ' checked="checked"',
103 addressHTML = '';
104
105 $.each(response, function(i, val) {
106 if (addressHTML) {
107 selected = '';
108 } else {
109 $('input[name="address[' + blockNo + '][master_id]"]').val(val.id);
110 }
111 var name = 'selected_shared_address-'+ blockNo,
112 id = name + '-' + val.id;
113 addressHTML += '<input type="radio" name="' + name + '" id="' + id + '" value="' + val.id + '"' + selected +'><label for="' + id + '">' + val.display_text + '</label>('+val.location_type+')<br/>';
114 });
115
116 if (!addressHTML) {
117 addressHTML = {/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};
118 }
119
120 $contentArea.html(addressHTML);
121 }
122 },'json');
123 });
124 });
125 </script>
126 {/literal}
127
128