Skip to content

Commit

Permalink
Merge pull request #22491 from eileenmcnaughton/legacy2
Browse files Browse the repository at this point in the history
Rationalise relationship validation
  • Loading branch information
colemanw authored Jan 14, 2022
2 parents 39d3df2 + dead2d6 commit be0c852
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
20 changes: 7 additions & 13 deletions CRM/Contact/BAO/Contact/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,13 @@ public static function createCurrentEmployerRelationship($contactID, $organizati
])->execute()->first()['id'];
}

// get the relationship type id of "Employee of"
$relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
if (!$relTypeId) {
throw new CRM_Core_Exception(ts("You seem to have deleted the relationship type 'Employee of'"));
$relTypeId = CRM_Contact_BAO_RelationshipType::getEmployeeRelationshipTypeID();
if (!CRM_Contact_BAO_Contact::getContactType($contactID) || !CRM_Contact_BAO_Contact::getContactType($organization)) {
// There doesn't seem to be any reason to think this would ever be true but there
// was a previous more complicated check.
CRM_Core_Error::deprecatedWarning('attempting to create an employer with invalid contact types is deprecated');
return;
}

// create employee of relationship
[$duplicate, $relationshipIds]
= self::legacyCreateMultiple($relTypeId, $organization, $contactID);
Expand Down Expand Up @@ -340,10 +341,6 @@ private static function legacyCreateMultiple(int $relationshipTypeID, int $organ
'contact_id_b' => $organizationID,
'relationship_type_id' => $relationshipTypeID,
];
if (!CRM_Contact_BAO_Relationship::checkRelationshipType($contactFields['contact_id_a'], $contactFields['contact_id_b'],
$contactFields['relationship_type_id'])) {
return [0, []];
}

if (
CRM_Contact_BAO_Relationship::checkDuplicateRelationship(
Expand Down Expand Up @@ -460,10 +457,7 @@ public static function clearCurrentEmployer($contactId, $employerId = NULL) {
//2. delete related membership.

//get the relationship type id of "Employee of"
$relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
if (!$relTypeId) {
throw new CRM_Core_Exception(ts("You seem to have deleted the relationship type 'Employee of'"));
}
$relTypeId = CRM_Contact_BAO_RelationshipType::getEmployeeRelationshipTypeID();
$relMembershipParams['relationship_type_id'] = $relTypeId . '_a_b';
$relMembershipParams['contact_check'][$employerId] = 1;

Expand Down
7 changes: 1 addition & 6 deletions CRM/Contact/BAO/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,7 @@ public static function clearCurrentEmployer($id, $action) {
$sharedContact->id = $relationship->contact_id_a;
$sharedContact->find(TRUE);

// CRM-15881 UPDATES
// changed FROM "...relationship->relationship_type_id == 4..." TO "...relationship->relationship_type_id == 5..."
// As the system should be looking for type "employer of" (id 5) and not "sibling of" (id 4)
// As suggested by @davecivicrm, the employee relationship type id is fetched using the CRM_Core_DAO::getFieldValue() class and method, since these ids differ from system to system.
$employerRelTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');

$employerRelTypeId = CRM_Contact_BAO_RelationshipType::getEmployeeRelationshipTypeID();
if ($relationship->relationship_type_id == $employerRelTypeId && $relationship->contact_id_b == $sharedContact->employer_id) {
CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($relationship->contact_id_a);
}
Expand Down
40 changes: 37 additions & 3 deletions CRM/Contact/BAO/RelationshipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\Relationship;
use Civi\Api4\RelationshipType;
use Civi\Core\Event\PreEvent;

/**
*
* @package CRM
Expand Down Expand Up @@ -108,16 +112,46 @@ public static function del($relationshipTypeId) {

/**
* Callback for hook_civicrm_pre().
*
* @param \Civi\Core\Event\PreEvent $event
* @throws CRM_Core_Exception
*
* @throws \API_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) {
public static function self_hook_civicrm_pre(PreEvent $event): void {
if ($event->action === 'delete') {
// need to delete all option value field before deleting group
\Civi\Api4\Relationship::delete(FALSE)
Relationship::delete(FALSE)
->addWhere('relationship_type_id', '=', $event->id)
->execute();
}
}

/**
* Get the id of the employee relationship, checking it is valid.
*
* @return int|string
*
* @throws \CRM_Core_Exception
*/
public static function getEmployeeRelationshipTypeID(): int {
try {
if (!Civi::cache('metadata')->has(__CLASS__ . __FUNCTION__)) {
$relationship = RelationshipType::get(FALSE)
->addWhere('name_a_b', '=', 'Employee of')
->addWhere('contact_type_a', '=', 'Individual')
->addWhere('contact_type_b', '=', 'Organization')
->addSelect('id')->execute()->first();
if (empty($relationship)) {
throw new API_Exception('no valid relationship');
}
Civi::cache('metadata')->set(__CLASS__ . __FUNCTION__, $relationship['id']);
}
}
catch (API_Exception $e) {
throw new CRM_Core_Exception(ts("You seem to have deleted the relationship type 'Employee of'"));
}
return Civi::cache('metadata')->get(__CLASS__ . __FUNCTION__);
}

}
2 changes: 1 addition & 1 deletion CRM/Contribute/Form/Contribution/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ public static function processOnBehalfOrganization(&$behalfOrganization, &$conta
\Civi\Api4\Relationship::create(FALSE)
->addValue('contact_id_a', $contactID)
->addValue('contact_id_b', $orgID)
->addValue('relationship_type_id', CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b'))
->addValue('relationship_type_id', $relTypeId = CRM_Contact_BAO_RelationshipType::getEmployeeRelationshipTypeID())
->addValue('is_permission_a_b:name', 'View and update')
->execute();
}
Expand Down
2 changes: 0 additions & 2 deletions tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,8 +909,6 @@ public function testContactCreateCurrentEmployer(): void {
* Test creating a current employer through API.
*
* Check it will re-activate a de-activated employer
*
* @throws \CRM_Core_Exception
*/
public function testContactCreateDuplicateCurrentEmployerEnables(): void {
// Set up - create employer relationship.
Expand Down

0 comments on commit be0c852

Please sign in to comment.