From 6552bd202cf3087a933e27501960e7249996f620 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 2 Dec 2021 15:59:40 -0500 Subject: [PATCH] RelationshipType - Use standard delete function which calls hooks --- CRM/Contact/BAO/RelationshipType.php | 48 +++++++++------------------- CRM/Core/BAO/Mapping.php | 16 +++++++++- CRM/Member/BAO/MembershipType.php | 26 ++++++++++++++- 3 files changed, 55 insertions(+), 35 deletions(-) diff --git a/CRM/Contact/BAO/RelationshipType.php b/CRM/Contact/BAO/RelationshipType.php index 7515f4f14d..8a12e55bb4 100644 --- a/CRM/Contact/BAO/RelationshipType.php +++ b/CRM/Contact/BAO/RelationshipType.php @@ -14,7 +14,7 @@ * @package CRM * @copyright CiviCRM LLC https://civicrm.org/licensing */ -class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType { +class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType implements \Civi\Test\HookInterface { /** * Class constructor. @@ -99,6 +99,7 @@ class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType * * @param int $relationshipTypeId * + * @deprecated * @throws CRM_Core_Exception * @return mixed */ @@ -109,40 +110,21 @@ class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) { throw new CRM_Core_Exception(ts('Invalid relationship type')); } + return static::deleteRecord(['id' => $relationshipTypeId]); + } - //check dependencies - - // delete all relationships - $relationship = new CRM_Contact_DAO_Relationship(); - $relationship->relationship_type_id = $relationshipTypeId; - $relationship->delete(); - - // remove this relationship type from membership types - $mems = civicrm_api3('MembershipType', 'get', [ - 'relationship_type_id' => ['LIKE' => "%{$relationshipTypeId}%"], - 'return' => ['id', 'relationship_type_id', 'relationship_direction'], - ]); - foreach ($mems['values'] as $membershipTypeId => $membershipType) { - $pos = array_search($relationshipTypeId, $membershipType['relationship_type_id']); - // Api call may have returned false positives but currently the relationship_type_id uses - // nonstandard serialization which makes anything more accurate impossible. - if ($pos !== FALSE) { - unset($membershipType['relationship_type_id'][$pos], $membershipType['relationship_direction'][$pos]); - civicrm_api3('MembershipType', 'create', $membershipType); - } - } - - //fixed for CRM-3323 - $mappingField = new CRM_Core_DAO_MappingField(); - $mappingField->relationship_type_id = $relationshipTypeId; - $mappingField->find(); - while ($mappingField->fetch()) { - $mappingField->delete(); + /** + * Callback for hook_civicrm_pre(). + * @param \Civi\Core\Event\PreEvent $event + * @throws CRM_Core_Exception + */ + public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) { + if ($event->action === 'delete') { + // need to delete all option value field before deleting group + \Civi\Api4\Relationship::delete(FALSE) + ->addWhere('relationship_type_id', '=', $event->id) + ->execute(); } - - $relationshipType = new CRM_Contact_DAO_RelationshipType(); - $relationshipType->id = $relationshipTypeId; - return $relationshipType->delete(); } } diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index d8358649bb..8d7a139a98 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -14,7 +14,7 @@ * @package CRM * @copyright CiviCRM LLC https://civicrm.org/licensing */ -class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { +class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping implements \Civi\Test\HookInterface { /** * Class constructor. @@ -1211,4 +1211,18 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { $mappingField->delete(); } + /** + * Callback for hook_civicrm_pre(). + * @param \Civi\Core\Event\PreEvent $event + * @throws CRM_Core_Exception + */ + public static function on_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) { + if ($event->action === 'delete' && $event->entity === 'RelationshipType') { + // CRM-3323 - Delete mappingField records when deleting relationship type + \Civi\Api4\MappingField::delete(FALSE) + ->addWhere('relationship_type_id', '=', $event->id) + ->execute(); + } + } + } diff --git a/CRM/Member/BAO/MembershipType.php b/CRM/Member/BAO/MembershipType.php index 1ec67d2e16..6d0efebc33 100644 --- a/CRM/Member/BAO/MembershipType.php +++ b/CRM/Member/BAO/MembershipType.php @@ -14,7 +14,7 @@ * @package CRM * @copyright CiviCRM LLC https://civicrm.org/licensing */ -class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType { +class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType implements \Civi\Test\HookInterface { /** * Static holder for the default Membership Type. @@ -205,6 +205,30 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType { return $result; } + /** + * Callback for hook_civicrm_pre(). + * @param \Civi\Core\Event\PreEvent $event + * @throws CRM_Core_Exception + */ + public static function on_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) { + if ($event->action === 'delete' && $event->entity === 'RelationshipType') { + // When deleting relationship type, remove from membership types + $mems = civicrm_api3('MembershipType', 'get', [ + 'relationship_type_id' => ['LIKE' => "%{$event->id}%"], + 'return' => ['id', 'relationship_type_id', 'relationship_direction'], + ]); + foreach ($mems['values'] as $membershipType) { + $pos = array_search($event->id, $membershipType['relationship_type_id']); + // Api call may have returned false positives but currently the relationship_type_id uses + // nonstandard serialization which makes anything more accurate impossible. + if ($pos !== FALSE) { + unset($membershipType['relationship_type_id'][$pos], $membershipType['relationship_direction'][$pos]); + civicrm_api3('MembershipType', 'create', $membershipType); + } + } + } + } + /** * Convert membership type's 'start day' & 'rollover day' to human readable formats. * -- 2.25.1