REF - Deprecate RelationshipType add function in favor of writeRecord
authorColeman Watts <coleman@civicrm.org>
Wed, 5 Apr 2023 12:59:52 +0000 (08:59 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 5 Apr 2023 12:59:52 +0000 (08:59 -0400)
CRM/Case/BAO/Case.php
CRM/Contact/BAO/RelationshipType.php
tests/phpunit/CRM/Contact/BAO/ContactType/RelationshipTest.php

index 43e6435dc60e3f527f7c9c228a057e4140a8039f..cad24a4b9d88149f6bb280a1e22d15b5b515bb0b 100644 (file)
@@ -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();
+    }
   }
 
   /**
index e74b475c3d112f70137aa7b78d7332dc7d8f33b8..576f6e1ee98e82806756285242096a2a3de36b11 100644 (file)
@@ -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
index 525ea7a8fa8760d7d265b3aa1817719b3d103707..54277d7ff4fc17b4f34de3f8db15b40c914c9ea8 100644 (file)
@@ -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');