From 3b9a69463536dd1e04e183100f446a7a977f6234 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 16 Jan 2022 08:44:14 +1300 Subject: [PATCH] Move deprecated function back to the only class that calls it 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 --- CRM/Contact/BAO/Relationship.php | 1 + CRM/Contact/Import/Parser/Contact.php | 69 ++++++++++++++++- .../BAO/ContactType/RelationshipTest.php | 75 ------------------- 3 files changed, 68 insertions(+), 77 deletions(-) diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index ace3d020aaaa..306cabb988b0 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -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) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 5fd6af3b7264..c46a60338f9a 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -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' => [ @@ -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; @@ -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. * diff --git a/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php b/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php index e4ff5266d9e1..4419de4dcd50 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php @@ -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 = [