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

Respect pre hook for relationship to alter id in $params #12834

Merged
merged 2 commits into from
Feb 4, 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
14 changes: 7 additions & 7 deletions CRM/Contact/BAO/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,15 @@ public static function legacyCreateMultiple(&$params, $ids = array()) {
*
* @return CRM_Contact_BAO_Relationship
*/
public static function add(&$params, $ids = array(), $contactId = NULL) {
$relationshipId = CRM_Utils_Array::value('relationship', $ids, CRM_Utils_Array::value('id', $params));
public static function add($params, $ids = array(), $contactId = NULL) {
$params['id'] = CRM_Utils_Array::value('relationship', $ids, CRM_Utils_Array::value('id', $params));

$hook = 'create';
if ($relationshipId) {
if ($params['id']) {
$hook = 'edit';
}
//@todo hook are called from create and add - remove one
CRM_Utils_Hook::pre($hook, 'Relationship', $relationshipId, $params);
CRM_Utils_Hook::pre($hook, 'Relationship', $params['id'], $params);

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

Expand All @@ -306,7 +306,7 @@ public static function add(&$params, $ids = array(), $contactId = NULL) {
if ($type == 6) {
CRM_Contact_BAO_Household::updatePrimaryContact($params['contact_id_b'], $params['contact_id_a']);
}
if (!empty($relationshipId) && self::isCurrentEmployerNeedingToBeCleared($params, $relationshipId, $type)) {
if (!empty($params['id']) && self::isCurrentEmployerNeedingToBeCleared($params, $params['id'], $type)) {
CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($params['contact_id_a']);
}
$relationship = new CRM_Contact_BAO_Relationship();
Expand All @@ -316,7 +316,7 @@ public static function add(&$params, $ids = array(), $contactId = NULL) {
$relationship->contact_id_b = $params['contact_id_b'];
$relationship->contact_id_a = $params['contact_id_a'];
$relationship->relationship_type_id = $type;
$relationship->id = $relationshipId;
$relationship->id = $params['id'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pradpnayak I worry that this isn't robust & could be changed at any point, breaking any extensions that rely on it, since there is no specific contract / commitment to permitting id to be altered. 2 ways to protect it are

  1. by using code practices as standardised as possible - in this case perhaps merge in the defaults first & then do per code comment
 //@todo this code needs to be updated for the possibility that not all fields are set
    // by using $relationship->copyValues($params);


  1. unit tests


$dateFields = array('end_date', 'start_date');

Expand All @@ -332,7 +332,7 @@ public static function add(&$params, $ids = array(), $contactId = NULL) {
$relationship->$defaultField = $params[$defaultField];
}
}
elseif (!$relationshipId) {
elseif (empty($params['id'])) {
$relationship->$defaultField = $defaultValue;
}
}
Expand Down