Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-21008 - Checkbox to explicitly change employer when sharing address #13700

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CRM/Contact/BAO/Contact/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ public static function processSharedAddress(&$address) {
// Normal update process will automatically create new address with submitted values

// 1. loop through entire submitted address array
$skipFields = array('is_primary', 'location_type_id', 'is_billing', 'master_id');
$skipFields = array('is_primary', 'location_type_id', 'is_billing', 'master_id', 'update_current_employer');
foreach ($address as & $values) {
// 2. check if "Use another contact's address" is checked, if not continue
// Additionally, if master_id is set (address was shared), set master_id to empty value.
Expand All @@ -836,6 +836,9 @@ public static function processSharedAddress(&$address) {
continue;
}

// Set update_current_employer checkbox value
$values['update_current_employer'] = !empty($values['update_current_employer']);

// 3. get the address details for master_id
$masterAddress = new CRM_Core_BAO_Address();
$masterAddress->id = CRM_Utils_Array::value('master_id', $values);
Expand Down
5 changes: 4 additions & 1 deletion CRM/Contact/Form/Edit/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharin

// Override the default profile links to add address form
$profileLinks = CRM_Contact_BAO_Contact::getEntityRefCreateLinks('shared_address');
$form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks));
$form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), ['create' => $profileLinks, 'api' => ['extra' => ['contact_type']]]);
Copy link
Member Author

@colemanw colemanw Feb 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samuelsov by sending back the contact type here as extra data, I was able to avoid the added ajax callback from your original PR.


// do we want to update employer for shared address
$form->addElement('checkbox', "address[$blockId][update_current_employer]", NULL, ts('Set this organization as current employer'));
}
}

Expand Down
11 changes: 8 additions & 3 deletions CRM/Core/BAO/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,11 +1047,16 @@ public static function processSharedAddress($addressId, $params) {
$query = 'SELECT id, contact_id FROM civicrm_address WHERE master_id = %1';
$dao = CRM_Core_DAO::executeQuery($query, array(1 => array($addressId, 'Integer')));

// Default to TRUE if not set to maintain api backward compatibility.
$createRelationship = isset($params['update_current_employer']) ? $params['update_current_employer'] : TRUE;

// unset contact id
$skipFields = array('is_primary', 'location_type_id', 'is_billing', 'contact_id');
if (isset($params['master_id']) && !CRM_Utils_System::isNull($params['master_id'])) {
// call the function to create a relationship for the new shared address
self::processSharedAddressRelationship($params['master_id'], $params['contact_id']);
if ($createRelationship) {
// call the function to create a relationship for the new shared address
self::processSharedAddressRelationship($params['master_id'], $params['contact_id']);
}
}
else {
// else no new shares will be created, only update shared addresses
Expand All @@ -1064,7 +1069,7 @@ public static function processSharedAddress($addressId, $params) {
$addressDAO = new CRM_Core_DAO_Address();
while ($dao->fetch()) {
// call the function to update the relationship
if (isset($params['master_id']) && !CRM_Utils_System::isNull($params['master_id'])) {
if ($createRelationship && isset($params['master_id']) && !CRM_Utils_System::isNull($params['master_id'])) {
self::processSharedAddressRelationship($params['master_id'], $dao->contact_id);
}
$addressDAO->copyValues($params);
Expand Down
7 changes: 7 additions & 0 deletions templates/CRM/Contact/Form/Contact.hlp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@
<p>{ts}You can use an address belonging to an existing contact or create a new contact by selecting the desired contact type from the select field. If you link an individual's address to an organization, an employee-employer relationship will be automatically created. If you link an individual's address to a household, a household member relationship is created.{/ts}</p>
{/htxt}

{htxt id="id-sharedAddress-updateRelationships-title"}
{ts}Update Employer{/ts}
{/htxt}
{htxt id="id-sharedAddress-updateRelationships"}
<p>{ts}Set this organization as the current employer for this individual. Note: will disable any existing employer relationship for this individual.{/ts}</p>
{/htxt}

{htxt id="id-communication_style-title"}
{ts}Communication Style{/ts}
{/htxt}
Expand Down
11 changes: 11 additions & 0 deletions templates/CRM/Contact/Form/ShareAddress.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<div id="shared-address-{$blockId}" class="form-layout-compressed">
{$form.address.$blockId.master_contact_id.label}
{$form.address.$blockId.master_contact_id.html}
<div class="shared-address-update-employer" style="display: none;">
{$form.address.$blockId.update_current_employer.html}
{$form.address.$blockId.update_current_employer.label}
{help id="id-sharedAddress-updateRelationships" file="CRM/Contact/Form/Contact"}
</div>
<div class="shared-address-list">
{if !empty($sharedAddresses.$blockId.shared_address_display)}
{foreach item='sa' from=$sharedAddresses.$blockId.shared_address_display.options}
Expand All @@ -49,6 +54,8 @@
<script type="text/javascript">
CRM.$(function($) {
var blockNo = {/literal}{$blockId}{literal},
contactType = {/literal}{$contactType|@json_encode}{literal},
$employerSection = $('#shared-address-' + blockNo + ' .shared-address-update-employer'),
$contentArea = $('#shared-address-' + blockNo + ' .shared-address-list'),
$masterElement = $('input[name="address[' + blockNo + '][master_id]"]');

Expand Down Expand Up @@ -76,9 +83,13 @@
$masterElement.val('');

if (!sharedContactId || isNaN(sharedContactId)) {
$employerSection.hide();
return;
}

var otherContactType = $el.select2('data').extra.contact_type;
$employerSection.toggle(contactType === 'Individual' && otherContactType === 'Organization');

$.post(CRM.url('civicrm/ajax/inline'), {
'contact_id': sharedContactId,
'type': 'method',
Expand Down
20 changes: 19 additions & 1 deletion tests/phpunit/api/v3/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,25 @@ public function testCreateAddressWithMasterRelationshipOrganization() {
$this->callAPISuccess('relationship', 'getcount', array(
'contact_id_a' => $individualID,
'contact_id_b' => $this->_contactID,
));
), 1);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing unit test of this functionality was actually not testing anything because no expected value was set here.

}

/**
* Create an address with a master ID and relationship creation disabled.
*/
public function testCreateAddressWithoutMasterRelationshipOrganization() {
$address = $this->callAPISuccess('address', 'create', $this->_params);
$individualID = $this->individualCreate();
$individualParams = array(
'contact_id' => $individualID,
'master_id' => $address['id'],
'update_current_employer' => 0,
);
$this->callAPISuccess('address', 'create', array_merge($this->_params, $individualParams));
$this->callAPISuccess('relationship', 'getcount', array(
'contact_id_a' => $individualID,
'contact_id_b' => $this->_contactID,
), 0);
}

/**
Expand Down