From f00a0870bf369b1182b3310ee9d3ab80018c4c0c Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 8 Jan 2022 13:28:39 +1300 Subject: [PATCH] Function to get employer relationship type id --- CRM/Contact/BAO/RelationshipType.php | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/CRM/Contact/BAO/RelationshipType.php b/CRM/Contact/BAO/RelationshipType.php index eb8d41583c..b7c5c75580 100644 --- a/CRM/Contact/BAO/RelationshipType.php +++ b/CRM/Contact/BAO/RelationshipType.php @@ -9,6 +9,8 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\RelationshipType; + /** * * @package CRM @@ -120,4 +122,32 @@ class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType } } + + /** + * 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.25.1