From 2ee1ee310645519f86aae7237374f6d8912e3c24 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Dani=C3=ABl?= Date: Fri, 22 Sep 2017 13:18:29 +0200 Subject: [PATCH] CRM-21214 - Fix address sharing 2 --- CRM/Core/BAO/Address.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index df274fd788..27a237f0f6 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -87,6 +87,11 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address { } } + if ($value['id'] == $value['master_id']) { + $value['master_id'] = 'null'; + CRM_Core_Session::setStatus(ts("You can't connect an address to itself"), '', 'warning'); + } + // Note there could be cases when address info already exist ($value[id] is set) for a contact/entity // BUT info is not present at this time, and therefore we should be really careful when deleting the block. // $updateBlankLocInfo will help take appropriate decision. CRM-5969 @@ -174,6 +179,22 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address { //call the function to sync shared address self::processSharedAddress($address->id, $params); + //if address is already shared, share the master address with all children (prevent chaining) + if ($address->master_id > 0 && $value['id'] != $value['master_id']) { + $result = civicrm_api3('Address', 'get', [ + 'return' => array("contact_id"), + 'master_id' => $address->id, + ]); + if ($result['count'] > 0) { + $params_copy['address'][1] = $params; + foreach ($result['values'] as $key => $value) { + $params_copy['contact_id'] = $value['contact_id']; + $params_copy['address'][1]['id'] = $key; + self::create($params_copy); + } + } + } + // call the function to create shared relationships // we only create create relationship if address is shared by Individual if (!CRM_Utils_System::isNull($address->master_id)) { -- 2.25.1