Skip to content

Commit

Permalink
Move deprecated function back to the only class that calls it
Browse files Browse the repository at this point in the history
This function is only called from the importer (in core) - copy back there & noisily
deprecate (one known extension usage).

Note that I removed 2 parts of it in the copy back that are unreachable
as only specific params are passed in we know they don't include
case & always include skipRecent

https://github.com/civicrm/civicrm-core/blob/5484728757de4c85d44305a92bfa2aa35351fbce/CRM/Contact/Import/Parser/Contact.php#L880-L887
  • Loading branch information
eileenmcnaughton committed Jan 16, 2022
1 parent 2d5dd21 commit 3b9a694
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 77 deletions.
1 change: 1 addition & 0 deletions CRM/Contact/BAO/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public static function createMultiple($params, $primaryContactLetter) {
* @throws \CRM_Core_Exception
*/
public static function legacyCreateMultiple($params, $ids = []) {
CRM_Core_Error::deprecatedFunctionWarning('api v4');
// clarify that the only key ever pass in the ids array is 'contact'
// There is legacy handling for other keys but a universe search on
// calls to this function (not supported to be called from outside core)
Expand Down
69 changes: 67 additions & 2 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,6 @@ public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
//if more than one duplicate contact
//found, create relationship with first contact
// now create the relationship record
$relationParams = [];
$relationParams = [
'relationship_type_id' => $key,
'contact_check' => [
Expand All @@ -893,7 +892,7 @@ public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
'contact' => $primaryContactId,
];

[$valid, $duplicate] = CRM_Contact_BAO_Relationship::legacyCreateMultiple($relationParams, $relationIds);
[$valid, $duplicate] = self::legacyCreateMultiple($relationParams, $relationIds);

if ($valid || $duplicate) {
$relationIds['contactTarget'] = $relContactId;
Expand Down Expand Up @@ -957,6 +956,72 @@ public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
return $this->processMessage($values, $statusFieldName, CRM_Import_Parser::VALID);
}

/**
* Only called from import now... plus one place outside of core & tests.
*
* @todo - deprecate more aggressively - will involve copying to the import
* class, adding a deprecation notice here & removing from tests.
*
* Takes an associative array and creates a relationship object.
*
* @deprecated For single creates use the api instead (it's tested).
* For multiple a new variant of this function needs to be written and migrated to as this is a bit
* nasty
*
* @param array $params
* (reference ) an assoc array of name/value pairs.
* @param array $ids
* The array that holds all the db ids.
* per http://wiki.civicrm.org/confluence/display/CRM/Database+layer
* "we are moving away from the $ids param "
*
* @return array
* @throws \CRM_Core_Exception
*/
private static function legacyCreateMultiple($params, $ids = []) {
// clarify that the only key ever pass in the ids array is 'contact'
// There is legacy handling for other keys but a universe search on
// calls to this function (not supported to be called from outside core)
// only returns 2 calls - one in CRM_Contact_Import_Parser_Contact
// and the other in jma grant applications (CRM_Grant_Form_Grant_Confirm)
// both only pass in contact as a key here.
$contactID = $ids['contact'];
unset($ids);
// There is only ever one value passed in from the 2 places above that call
// this - by clarifying here like this we can cleanup within this
// function without having to do more universe searches.
$relatedContactID = key($params['contact_check']);

// check if the relationship is valid between contacts.
// step 1: check if the relationship is valid if not valid skip and keep the count
// step 2: check the if two contacts already have a relationship if yes skip and keep the count
// step 3: if valid relationship then add the relation and keep the count

// step 1
[$contactFields['relationship_type_id'], $firstLetter, $secondLetter] = explode('_', $params['relationship_type_id']);
$contactFields['contact_id_' . $firstLetter] = $contactID;
$contactFields['contact_id_' . $secondLetter] = $relatedContactID;
if (!CRM_Contact_BAO_Relationship::checkRelationshipType($contactFields['contact_id_a'], $contactFields['contact_id_b'],
$contactFields['relationship_type_id'])) {
return [0, 0];
}

if (
CRM_Contact_BAO_Relationship::checkDuplicateRelationship(
$contactFields,
$contactID,
// step 2
$relatedContactID
)
) {
return [0, 1];
}

$singleInstanceParams = array_merge($params, $contactFields);
CRM_Contact_BAO_Relationship::add($singleInstanceParams);
return [1, 0];
}

/**
* Format common params data to proper format to store.
*
Expand Down
75 changes: 0 additions & 75 deletions tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,81 +146,6 @@ public function testRelationshipTypeAddStudentSponcor() {
$this->relationshipTypeDelete($result->id);
}

/**
* Methods create relationships within same contact type with valid data.
*
* Success expected
*/
public function testRelationshipCreateWithinSameType(): void {
//check for Individual to Parent
$relTypeParams = [
'name_a_b' => 'individualToParent',
'name_b_a' => 'parentToIndividual',
'contact_type_a' => 'Individual',
'contact_type_b' => 'Individual',
'contact_sub_type_b' => $this->parent,
];
$relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams);
$params = [
'relationship_type_id' => $relType->id . '_a_b',
'is_active' => 1,
'contact_check' => [$this->indivi_parent => $this->indivi_parent],
];
$ids = ['contact' => $this->individual];
[$valid] = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);

$this->assertEquals($valid, 1);
}

/**
* Methods create relationshipe within different contact type with valid data.
* success expected
*/
public function testRelCreateWithinDiffTypeSponsorIndivi() {
//check for Sponcer to Individual
$relTypeParams = [
'name_a_b' => 'SponsorToIndiv',
'name_b_a' => 'IndivToSponsor',
'contact_type_a' => 'Organization',
'contact_sub_type_a' => $this->sponsor,
'contact_type_b' => 'Individual',
];
$relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams);
$params = [
'relationship_type_id' => $relType->id . '_a_b',
'is_active' => 1,
'contact_check' => [$this->indivi_student => 1],
];
$ids = ['contact' => $this->organization_sponsor];
list($valid) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);

$this->assertEquals($valid, 1);
$this->relationshipTypeDelete($relType->id);
}

public function testRelCreateWithinDiffTypeStudentSponsor() {
//check for Student to Sponcer
$relTypeParams = [
'name_a_b' => 'StudentToSponsor',
'name_b_a' => 'SponsorToStudent',
'contact_type_a' => 'Individual',
'contact_sub_type_a' => $this->student,
'contact_type_b' => 'Organization',
'contact_sub_type_b' => $this->sponsor,
];
$relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams);
$params = [
'relationship_type_id' => $relType->id . '_a_b',
'is_active' => 1,
'contact_check' => [$this->organization_sponsor => 1],
];
$ids = ['contact' => $this->indivi_student];
list($valid) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);

$this->assertEquals($valid, 1);
$this->relationshipTypeDelete($relType->id);
}

public function testGetAnyToAnyRelTypes() {
// Create an any to any relationship.
$relTypeParams = [
Expand Down

0 comments on commit 3b9a694

Please sign in to comment.