CRM-21008 - Checkbox to explicitly change employer when sharing address
authorColeman Watts <coleman@civicrm.org>
Mon, 25 Feb 2019 15:50:54 +0000 (10:50 -0500)
committerColeman Watts <coleman@civicrm.org>
Mon, 25 Feb 2019 16:14:04 +0000 (11:14 -0500)
CRM/Contact/BAO/Contact/Utils.php
CRM/Contact/Form/Edit/Address.php
CRM/Core/BAO/Address.php
templates/CRM/Contact/Form/Contact.hlp
templates/CRM/Contact/Form/ShareAddress.tpl
tests/phpunit/api/v3/AddressTest.php

index db4114aa0d9630b928a5cb73d1592c6056a96879..a4aff68fa82c6ceb357bd16a45951982e1fdb50f 100644 (file)
@@ -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);
index 9dad5533f51bed7e78718929920f22141cb91982..2e91480071694539a1d8adc6e6fa5c68a6a3fabe 100644 (file)
@@ -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'));
     }
   }
 
index 2ff74bbb3740a2ed0726826795635103670f18b9..090bde61b3464a8fbc739b8f1b00d64900cf67d7 100644 (file)
@@ -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);
index 3aae0d213217e69ed3e91ec874582181c4859dca..5132abe68a64e49638a9315fd5ac2a3ac254c23d 100644 (file)
 <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}
index 7c854be0e90ed07e5f01f2c9f2cbcda1c47a91ea..93c1c99a6bc97e0eb2b7313319a28a49d58afb57 100644 (file)
     <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}
@@ -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]"]');
 
       $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',
index a496071e778dc91a8697304503eb14cff945411b..dcb5cf8451f4addea0b3e283f9c399a853c20731 100644 (file)
@@ -146,7 +146,25 @@ class api_v3_AddressTest extends CiviUnitTestCase {
     $this->callAPISuccess('relationship', 'getcount', array(
       'contact_id_a' => $individualID,
       'contact_id_b' => $this->_contactID,
-    ));
+    ), 1);
+  }
+
+  /**
+   * 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);
   }
 
   /**