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

Refactor CRM_Contact_BAO_Relationship::add to autoload missing params from existing record #15103

Merged
merged 1 commit into from
Aug 23, 2019
Merged
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
46 changes: 30 additions & 16 deletions CRM/Contact/BAO/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public static function legacyCreateMultiple(&$params, $ids = []) {
$hook = 'edit';
}

// @todo pre hook is called from add - remove it from here
CRM_Utils_Hook::pre($hook, 'Relationship', $relationshipId, $params);

if (!$relationshipId) {
Expand Down Expand Up @@ -295,33 +296,46 @@ public static function add($params, $ids = [], $contactId = NULL) {
if ($params['id']) {
$hook = 'edit';
}
//@todo hook are called from create and add - remove one
mattwire marked this conversation as resolved.
Show resolved Hide resolved
CRM_Utils_Hook::pre($hook, 'Relationship', $params['id'], $params);

$relationshipTypes = CRM_Utils_Array::value('relationship_type_id', $params);

// explode the string with _ to get the relationship type id
// and to know which contact has to be inserted in
// contact_id_a and which one in contact_id_b
list($type) = explode('_', $relationshipTypes);
list($relationshipTypeID) = explode('_', $relationshipTypes);

$relationship = new CRM_Contact_BAO_Relationship();
if (!empty($params['id'])) {
$relationship->id = $params['id'];
// Only load the relationship if we're missing required params
$requiredParams = ['contact_id_a', 'contact_id_b', 'relationship_type_id'];
foreach ($requiredParams as $requiredKey) {
if (!isset($params[$requiredKey])) {
$relationship->find(TRUE);
break;
}
}

}
$relationship->copyValues($params);
// @todo we could probably set $params['relationship_type_id'] above but it's unclear
// what that would do with the code below this. So for now be conservative and set it manually.
if (!empty($relationshipTypeID)) {
$relationship->relationship_type_id = $relationshipTypeID;
}

$params['contact_id_a'] = $relationship->contact_id_a;
$params['contact_id_b'] = $relationship->contact_id_b;

// check if the relationship type is Head of Household then update the
// household's primary contact with this contact.
if ($type == 6) {
CRM_Contact_BAO_Household::updatePrimaryContact($params['contact_id_b'], $params['contact_id_a']);
}
if (!empty($params['id']) && self::isCurrentEmployerNeedingToBeCleared($params, $params['id'], $type)) {
CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($params['contact_id_a']);
if ($relationshipTypeID == 6) {
CRM_Contact_BAO_Household::updatePrimaryContact($relationship->contact_id_b, $relationship->contact_id_a);
}

$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'];
$relationship->relationship_type_id = $type;
$relationship->id = $params['id'];
if (!empty($params['id']) && self::isCurrentEmployerNeedingToBeCleared($relationship->toArray(), $params['id'], $relationshipTypeID)) {
CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($relationship->contact_id_a);
}

$dateFields = ['end_date', 'start_date'];

Expand Down