From eff45dcef39893440007366f2162a40478b61cd7 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 7 Mar 2015 07:56:16 +1100 Subject: [PATCH] CRM-16055 initial fix --- CRM/Contact/BAO/Relationship.php | 49 +++++++++++++++++++++---------- CRM/Contact/Form/Relationship.php | 17 ++++++++--- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index f093f93f52..721cdcde46 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -141,8 +141,9 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { $duplicate++; continue; } - self::setContactABFromIDs($params, $ids, $key); - $relationship = self::add($params); + $contactFields = self::setContactABFromIDs($params, $ids, $key); + $singleInstanceParams = array_merge($params,$contactFields); + $relationship = self::add($singleInstanceParams); $relationshipIds[] = $relationship->id; $relationships[$relationship->id] = $relationship; $valid++; @@ -215,7 +216,9 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { //@todo hook are called from create and add - remove one CRM_Utils_Hook::pre($hook, 'Relationship', $relationshipId, $params); - self::setContactABFromIDs($params, $ids); + // Requirement to set fields in this function is historical & hopefully can go at some point. + $contactFields = self::setContactABFromIDs($params, $ids); + $params = array_merge($params, $contactFields); $relationshipTypes = CRM_Utils_Array::value('relationship_type_id', $params); // explode the string with _ to get the relationship type id @@ -231,6 +234,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { $relationship = new CRM_Contact_BAO_Relationship(); //@todo this code needs to be updated for the possibility that not all fields are set + // by using $relationship->copyValues($params); // (update) $relationship->contact_id_b = $params['contact_id_b']; $relationship->contact_id_a = $params['contact_id_a']; @@ -310,14 +314,19 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { /** * Resolve passed in contact IDs to contact_id_a & contact_id_b + * * @param array $params * @param array $ids * @param null $contactID + * + * @return array * @throws \CRM_Core_Exception */ - public static function setContactABFromIDs(&$params, $ids = array(), $contactID = NULL) { - if (!empty($params['contact_id_a']) && !empty($params['contact_id_b'])) { - return; + public static function setContactABFromIDs($params, $ids = array(), $contactID = NULL) { + $returnFields = array(); + if (!empty($params['contact_id_a']) && !empty($params['contact_id_b']) && is_numeric + ($params['relationship_type_id'])) { + return $returnFields; } if (empty($ids['contact'])) { if (!empty($params['id'])) { @@ -331,28 +340,35 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { )); while ($result->fetch()) { foreach ($fieldsToFill as $field) { - $params[$field] = !empty($params[$field]) ? $params[$field] : $result->$field; + $returnFields[$field] = !empty($params[$field]) ? $params[$field] : $result->$field; } } - return; + return $returnFields; } throw new CRM_Core_Exception('Cannot create relationship, insufficient contact IDs provided'); } - $relationshipTypes = CRM_Utils_Array::value('relationship_type_id', $params); - list($relationshipTypeID, $first, $second) = explode('_', $relationshipTypes); - if (empty($params['relationship_type_id'])) { - $params['relationship_type_id'] = $relationshipTypeID; + if (!is_numeric($params['relationship_type_id'])) { + $relationshipTypes = CRM_Utils_Array::value('relationship_type_id', $params); + list($relationshipTypeID, $first) = explode('_', $relationshipTypes); + if (empty($params['relationship_type_id'])) { + $returnFields['relationship_type_id'] = $relationshipTypeID; + } + } + else { + // if we haven't been given a relationship type to dis-entangle we assume that 'first' is a + $first = 'a'; } foreach (array('a', 'b') as $contactLetter) { if (empty($params['contact_' . $contactLetter])) { if ($first == $contactLetter) { - $params['contact_id_' . $contactLetter] = CRM_Utils_Array::value('contact', $ids); + $returnFields['contact_id_' . $contactLetter] = CRM_Utils_Array::value('contact', $ids); } else { - $params['contact_id_' . $contactLetter] = $contactID; + $returnFields['contact_id_' . $contactLetter] = $contactID; } } } + return $returnFields; } /** @@ -654,7 +670,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { * * @return \CRM_Contact_DAO_Relationship */ - public static function getContactIds($id) { + public static function getRelationshipByID($id) { $relationship = new CRM_Contact_DAO_Relationship(); $relationship->id = $id; @@ -722,7 +738,8 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { */ public static function checkValidRelationship($params, $ids, $contactId) { $errors = ''; - self::setContactABFromIDs($params, $ids, $contactId); + $contactParams = self::setContactABFromIDs($params, $ids, $contactId); + $params = array_merge($params, $contactParams); // get the string of relationship type $relationshipTypes = CRM_Utils_Array::value('relationship_type_id', $params); list($type) = explode('_', $relationshipTypes); diff --git a/CRM/Contact/Form/Relationship.php b/CRM/Contact/Form/Relationship.php index 9d8722ce65..27bb2b039b 100644 --- a/CRM/Contact/Form/Relationship.php +++ b/CRM/Contact/Form/Relationship.php @@ -414,6 +414,15 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { return; } + $relationshipTypeParts = explode('_', $params['relationship_type_id']); + $params['relationship_type_id'] = $relationshipTypeParts[0]; + if (!$this->_rtype) { + // Do we need to wrap this in an if - when is rtype used & is relationship_type_id always set then? + $this->_rtype = $params['relationship_type_id']; + } + $params['contact_id_' . $relationshipTypeParts[1]] = $this->_contactId; + + $ids = array('contact' => $this->_contactId); $relationshipTypeId = str_replace(array('_', 'a', 'b'), '', $params['relationship_type_id']); @@ -425,13 +434,13 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { // Update mode (always single) if ($this->_action & CRM_Core_Action::UPDATE) { $ids['relationship'] = $this->_relationshipId; - $relation = CRM_Contact_BAO_Relationship::getContactIds($this->_relationshipId); + $relation = CRM_Contact_BAO_Relationship::getRelationshipByID($this->_relationshipId); $ids['contactTarget'] = ($relation->contact_id_a == $this->_contactId) ? $relation->contact_id_b : $relation->contact_id_a; if ($this->_isCurrentEmployer) { // if relationship type changes, relationship is disabled, or "current employer" is unchecked, // clear the current employer. CRM-3235. - $relChanged = $relationshipTypeId != $this->_values['relationship_type_id']; + $relChanged = $params['relationship_type_id'] != $this->_values['relationship_type_id']; if (!$params['is_active'] || !$params['is_current_employer'] || $relChanged) { CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['contact_id_a']); // Refresh contact summary if in ajax mode @@ -451,7 +460,7 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], NULL, TRUE); // Process custom data - $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', FALSE, FALSE, $relationshipTypeId); + $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', FALSE, FALSE, $params['relationship_type_id']); $params['custom'] = CRM_Core_BAO_CustomField::postProcess( $params, $customFields, @@ -544,7 +553,7 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { $employerParams = array(); foreach ($relationshipIds as $id) { // Fixme this is dumb why do we have to look this up again? - $rel = CRM_Contact_BAO_Relationship::getContactIds($id); + $rel = CRM_Contact_BAO_Relationship::getRelationshipByID($id); $employerParams[$rel->contact_id_a] = $rel->contact_id_b; } CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($employerParams); -- 2.25.1