From e0fb9216d8bc79248af4f66623f8654f1d2e27a1 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 25 Feb 2019 10:50:54 -0500 Subject: [PATCH] CRM-21008 - Checkbox to explicitly change employer when sharing address --- CRM/Contact/BAO/Contact/Utils.php | 5 ++++- CRM/Contact/Form/Edit/Address.php | 5 ++++- CRM/Core/BAO/Address.php | 11 ++++++++--- templates/CRM/Contact/Form/Contact.hlp | 7 +++++++ templates/CRM/Contact/Form/ShareAddress.tpl | 11 +++++++++++ tests/phpunit/api/v3/AddressTest.php | 20 +++++++++++++++++++- 6 files changed, 53 insertions(+), 6 deletions(-) diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index db4114aa0d..a4aff68fa8 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -825,7 +825,7 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i // 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. @@ -836,6 +836,9 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i 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); diff --git a/CRM/Contact/Form/Edit/Address.php b/CRM/Contact/Form/Edit/Address.php index 9dad5533f5..2e91480071 100644 --- a/CRM/Contact/Form/Edit/Address.php +++ b/CRM/Contact/Form/Edit/Address.php @@ -166,7 +166,10 @@ class CRM_Contact_Form_Edit_Address { // 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']]]); + + // 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')); } } diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index 2ff74bbb37..090bde61b3 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -1047,11 +1047,16 @@ SELECT is_primary, $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 @@ -1064,7 +1069,7 @@ SELECT is_primary, $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); diff --git a/templates/CRM/Contact/Form/Contact.hlp b/templates/CRM/Contact/Form/Contact.hlp index 3aae0d2132..5132abe68a 100644 --- a/templates/CRM/Contact/Form/Contact.hlp +++ b/templates/CRM/Contact/Form/Contact.hlp @@ -76,6 +76,13 @@

{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}

{/htxt} +{htxt id="id-sharedAddress-updateRelationships-title"} + {ts}Update Employer{/ts} +{/htxt} +{htxt id="id-sharedAddress-updateRelationships"} +

{ts}Set this organization as the current employer for this individual. Note: will disable any existing employer relationship for this individual.{/ts}

+{/htxt} + {htxt id="id-communication_style-title"} {ts}Communication Style{/ts} {/htxt} diff --git a/templates/CRM/Contact/Form/ShareAddress.tpl b/templates/CRM/Contact/Form/ShareAddress.tpl index 7c854be0e9..93c1c99a6b 100644 --- a/templates/CRM/Contact/Form/ShareAddress.tpl +++ b/templates/CRM/Contact/Form/ShareAddress.tpl @@ -30,6 +30,11 @@
{$form.address.$blockId.master_contact_id.label} {$form.address.$blockId.master_contact_id.html} +
{if !empty($sharedAddresses.$blockId.shared_address_display)} {foreach item='sa' from=$sharedAddresses.$blockId.shared_address_display.options} @@ -49,6 +54,8 @@