From 1d1331a95332f6b1893fd1e0becdba753475fd23 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 5 Apr 2023 08:59:52 -0400 Subject: [PATCH] REF - Deprecate RelationshipType add function in favor of writeRecord --- CRM/Case/BAO/Case.php | 3 + CRM/Contact/BAO/RelationshipType.php | 69 +++++++++---------- .../BAO/ContactType/RelationshipTest.php | 8 +-- 3 files changed, 39 insertions(+), 41 deletions(-) diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index 43e6435dc6..cad24a4b9d 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -96,6 +96,9 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case implements \Civi\Core\HookInte } } } + if ($e->entity === 'RelationshipType') { + CRM_Case_XMLProcessor::flushStaticCaches(); + } } /** diff --git a/CRM/Contact/BAO/RelationshipType.php b/CRM/Contact/BAO/RelationshipType.php index e74b475c3d..576f6e1ee9 100644 --- a/CRM/Contact/BAO/RelationshipType.php +++ b/CRM/Contact/BAO/RelationshipType.php @@ -49,53 +49,23 @@ class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType } /** - * Add the relationship type in the db. - * + * @deprecated * @param array $params - * * @return CRM_Contact_DAO_RelationshipType */ public static function add($params) { - if (empty($params['id'])) { - // Set name to label if not set - if (empty($params['label_a_b']) && !empty($params['name_a_b'])) { - $params['label_a_b'] = $params['name_a_b']; - } - if (empty($params['label_b_a']) && !empty($params['name_b_a'])) { - $params['label_b_a'] = $params['name_b_a']; - } - - // set label to name if it's not set - if (empty($params['name_a_b']) && !empty($params['label_a_b'])) { - $params['name_a_b'] = $params['label_a_b']; - } - if (empty($params['name_b_a']) && !empty($params['label_b_a'])) { - $params['name_b_a'] = $params['label_b_a']; - } - } - - // action is taken depending upon the mode - $relationshipType = self::writeRecord($params); - - CRM_Core_PseudoConstant::relationshipType('label', TRUE); - CRM_Core_PseudoConstant::relationshipType('name', TRUE); - CRM_Core_PseudoConstant::flush(); - CRM_Case_XMLProcessor::flushStaticCaches(); - return $relationshipType; + CRM_Core_Error::deprecatedFunctionWarning('writeRecord'); + return self::writeRecord($params); } /** - * Delete Relationship Types. - * - * @param int $relationshipTypeId - * * @deprecated + * @param int $relationshipTypeId * @throws CRM_Core_Exception * @return mixed */ public static function del($relationshipTypeId) { CRM_Core_Error::deprecatedFunctionWarning('deleteRecord'); - return static::deleteRecord(['id' => $relationshipTypeId]); } @@ -103,19 +73,44 @@ class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType * Callback for hook_civicrm_pre(). * * @param \Civi\Core\Event\PreEvent $event - * * @throws \CRM_Core_Exception - * @throws \Civi\API\Exception\UnauthorizedException */ public static function self_hook_civicrm_pre(PreEvent $event): void { + if ($event->action === 'create') { + // Set name to label if not set + if (empty($event->params['label_a_b']) && !empty($event->params['name_a_b'])) { + $event->params['label_a_b'] = $event->params['name_a_b']; + } + if (empty($event->params['label_b_a']) && !empty($event->params['name_b_a'])) { + $event->params['label_b_a'] = $event->params['name_b_a']; + } + + // set label to name if it's not set + if (empty($event->params['name_a_b']) && !empty($event->params['label_a_b'])) { + $event->params['name_a_b'] = $event->params['label_a_b']; + } + if (empty($event->params['name_b_a']) && !empty($event->params['label_b_a'])) { + $event->params['name_b_a'] = $event->params['label_b_a']; + } + } if ($event->action === 'delete') { - // need to delete all option value field before deleting group + // Delete all existing relationships with this type Relationship::delete(FALSE) ->addWhere('relationship_type_id', '=', $event->id) ->execute(); } } + /** + * Callback for hook_civicrm_post(). + * @param \Civi\Core\Event\PostEvent $event + */ + public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) { + CRM_Core_PseudoConstant::relationshipType('label', TRUE); + CRM_Core_PseudoConstant::relationshipType('name', TRUE); + CRM_Core_PseudoConstant::flush(); + } + /** * Get the id of the employee relationship, checking it is valid. * We check that contact_type_a is Individual, but not contact_type_b because there's diff --git a/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php b/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php index 525ea7a8fa..54277d7ff4 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php @@ -120,7 +120,7 @@ DELETE FROM civicrm_contact_type 'contact_type_b' => 'Individual', 'contact_sub_type_b' => $this->parent, ]; - $result = CRM_Contact_BAO_RelationshipType::add($params); + $result = CRM_Contact_BAO_RelationshipType::writeRecord($params); $this->assertEquals($result->name_a_b, 'indivToparent'); $this->assertEquals($result->contact_type_a, 'Individual'); $this->assertEquals($result->contact_type_b, 'Individual'); @@ -137,7 +137,7 @@ DELETE FROM civicrm_contact_type 'contact_sub_type_a' => $this->sponsor, 'contact_type_b' => 'Individual', ]; - $result = CRM_Contact_BAO_RelationshipType::add($params); + $result = CRM_Contact_BAO_RelationshipType::writeRecord($params); $this->assertEquals($result->name_a_b, 'SponsorToIndiv'); $this->assertEquals($result->contact_type_a, 'Organization'); $this->assertEquals($result->contact_sub_type_a, $this->sponsor); @@ -155,7 +155,7 @@ DELETE FROM civicrm_contact_type 'contact_type_b' => 'Organization', 'contact_sub_type_b' => $this->sponsor, ]; - $result = CRM_Contact_BAO_RelationshipType::add($params); + $result = CRM_Contact_BAO_RelationshipType::writeRecord($params); $this->assertEquals($result->name_a_b, 'StudentToSponser'); $this->assertEquals($result->contact_type_a, 'Individual'); $this->assertEquals($result->contact_sub_type_a, $this->student); @@ -172,7 +172,7 @@ DELETE FROM civicrm_contact_type 'contact_type_a' => '', 'contact_type_b' => '', ]; - $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams); + $relType = CRM_Contact_BAO_RelationshipType::writeRecord($relTypeParams); $indTypes = CRM_Contact_BAO_Relationship::getRelationType('Individual'); $orgTypes = CRM_Contact_BAO_Relationship::getRelationType('Organization'); -- 2.25.1