Add Civi-Member is enabled check
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 3 May 2023 00:39:14 +0000 (12:39 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 3 May 2023 00:39:14 +0000 (12:39 +1200)
CRM/Contact/BAO/Relationship.php

index 0a3c41081740cd984e910a316e27c81a108b1694..29066d14ab301a3b89eaefa37e745c6234f5bb2e 100644 (file)
@@ -9,6 +9,7 @@
  +--------------------------------------------------------------------+
  */
 
+use Civi\Api4\MembershipType;
 use Civi\Api4\Relationship;
 
 /**
@@ -608,54 +609,8 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship implemen
   public static function disableEnableRelationship($id, $action, $params = [], $ids = [], $active = FALSE) {
     $relationship = self::clearCurrentEmployer($id, $action);
 
-    if ($id) {
-      // create $params array which is required to delete memberships
-      // of the related contacts.
-      if (empty($params)) {
-        $params = [
-          'relationship_type_id' => "{$relationship->relationship_type_id}_a_b",
-          'contact_check' => [$relationship->contact_id_b => 1],
-        ];
-      }
-      $contact_id_a = empty($params['contact_id_a']) ? $relationship->contact_id_a : $params['contact_id_a'];
-
-      // Check if relationship can be used for related memberships
-      $membershipTypes = \Civi\Api4\MembershipType::get(FALSE)
-        ->addSelect('relationship_type_id')
-        ->addGroupBy('relationship_type_id')
-        ->addWhere('relationship_type_id', 'IS NOT EMPTY')
-        ->execute();
-      foreach ($membershipTypes as $membershipType) {
-        // We have to loop through them because relationship_type_id is an array and we can't filter by a single
-        // relationship id using API.
-        if (in_array($relationship->relationship_type_id, $membershipType['relationship_type_id'])) {
-          $relationshipIsUsedForRelatedMemberships = TRUE;
-        }
-      }
-      if (empty($relationshipIsUsedForRelatedMemberships)) {
-        // This relationship is not configured for any related membership types
-        return;
-      }
-      // Call relatedMemberships to delete/add the memberships of related contacts.
-      if ($action & CRM_Core_Action::DISABLE) {
-        // @todo this could call a subset of the function that just relates to
-        // cleaning up no-longer-inherited relationships
-        CRM_Contact_BAO_Relationship::relatedMemberships($contact_id_a,
-          $params,
-          $ids,
-          CRM_Core_Action::DELETE,
-          $active
-        );
-      }
-      elseif ($action & CRM_Core_Action::ENABLE) {
-        $ids['contact'] = empty($ids['contact']) ? $contact_id_a : $ids['contact'];
-        CRM_Contact_BAO_Relationship::relatedMemberships($contact_id_a,
-          $params,
-          $ids,
-          empty($params['id']) ? CRM_Core_Action::ADD : CRM_Core_Action::UPDATE,
-          $active
-        );
-      }
+    if ($id && CRM_Core_Component::isEnabled('CiviMember')) {
+      self::updateMembershipsByRelationship($params, $relationship, $action, $ids, $active);
     }
   }
 
@@ -2258,4 +2213,64 @@ SELECT count(*)
     return $membershipValues;
   }
 
+  /**
+   * @param array $params
+   * @param \CRM_Contact_DAO_Relationship $relationship
+   * @param int $action
+   * @param array $ids
+   * @param bool $active
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
+   */
+  protected static function updateMembershipsByRelationship(array $params, CRM_Contact_DAO_Relationship $relationship, int $action, array $ids, bool $active): void {
+    // create $params array which is required to delete memberships
+    // of the related contacts.
+    if (empty($params)) {
+      $params = [
+        'relationship_type_id' => "{$relationship->relationship_type_id}_a_b",
+        'contact_check' => [$relationship->contact_id_b => 1],
+      ];
+    }
+    $contact_id_a = empty($params['contact_id_a']) ? $relationship->contact_id_a : $params['contact_id_a'];
+
+    // Check if relationship can be used for related memberships
+    $membershipTypes = MembershipType::get(FALSE)
+      ->addSelect('relationship_type_id')
+      ->addGroupBy('relationship_type_id')
+      ->addWhere('relationship_type_id', 'IS NOT EMPTY')
+      ->execute();
+    foreach ($membershipTypes as $membershipType) {
+      // We have to loop through them because relationship_type_id is an array and we can't filter by a single
+      // relationship id using API.
+      if (in_array($relationship->relationship_type_id, $membershipType['relationship_type_id'])) {
+        $relationshipIsUsedForRelatedMemberships = TRUE;
+      }
+    }
+    if (empty($relationshipIsUsedForRelatedMemberships)) {
+      // This relationship is not configured for any related membership types
+      return;
+    }
+    // Call relatedMemberships to delete/add the memberships of related contacts.
+    if ($action & CRM_Core_Action::DISABLE) {
+      // @todo this could call a subset of the function that just relates to
+      // cleaning up no-longer-inherited relationships
+      CRM_Contact_BAO_Relationship::relatedMemberships($contact_id_a,
+        $params,
+        $ids,
+        CRM_Core_Action::DELETE,
+        $active
+      );
+    }
+    elseif ($action & CRM_Core_Action::ENABLE) {
+      $ids['contact'] = empty($ids['contact']) ? $contact_id_a : $ids['contact'];
+      CRM_Contact_BAO_Relationship::relatedMemberships($contact_id_a,
+        $params,
+        $ids,
+        empty($params['id']) ? CRM_Core_Action::ADD : CRM_Core_Action::UPDATE,
+        $active
+      );
+    }
+  }
+
 }