dev/core#530 CiviCase: translate case role direction coming from xml
authorAlice Frumin <alice@aghstrategies.com>
Wed, 1 May 2019 19:47:55 +0000 (15:47 -0400)
committerAlice Frumin <alice@aghtrategies.com>
Mon, 10 Jun 2019 15:26:09 +0000 (11:26 -0400)
CRM/Case/BAO/Case.php
CRM/Case/Info.php
CRM/Case/XMLProcessor.php
CRM/Case/XMLProcessor/Process.php
CRM/Case/XMLRepository.php

index dabd71f63f62ebe5c643e27d35afeebdd270fb8a..f25f917749eebc7264bc6fac47866a9df52f198b 100644 (file)
@@ -3295,7 +3295,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
           }
           if (!empty($relType['id'])) {
             $roleDetails['id'] = $relType['id'];
-            $roleDetails['direction'] = 'a_b';
+            $roleDetails['direction'] = 'b_a';
           }
           // Check if its a b_a label
           try {
@@ -3309,7 +3309,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
             }
             else {
               $roleDetails['id'] = $relTypeBa['id'];
-              $roleDetails['direction'] = 'b_a';
+              $roleDetails['direction'] = 'a_b';
             }
           }
           $caseRoles[$roleDetails['id']] = $roleDetails;
index f7f0e0cd95221c242d268dadaec5d8a9b21b606d..ce5421fd1b0d754869efd47c00da6e5f7e1844ac 100644 (file)
@@ -144,8 +144,14 @@ class CRM_Case_Info extends CRM_Core_Component_Info {
     }
     elseif ($dao instanceof CRM_Contact_DAO_RelationshipType) {
       /** @var $dao CRM_Contact_DAO_RelationshipType */
-      $count = CRM_Case_XMLRepository::singleton()
-        ->getRelationshipReferenceCount($dao->label_a_b);
+
+      // Need to look both directions, but no need to translate case role
+      // direction from XML perspective to client-based perspective
+      $xmlRepo = CRM_Case_XMLRepository::singleton();
+      $count = $xmlRepo->getRelationshipReferenceCount($dao->label_a_b);
+      if ($dao->label_a_b != $dao->label_b_a) {
+        $count += $xmlRepo->getRelationshipReferenceCount($dao->label_b_a);
+      }
       if ($count > 0) {
         $result[] = [
           'name' => 'casetypexml:relationships',
index f118d4c203beb375f6903da41b7cb4307312c99e..245e9ace1c268058e45222cd0903df05d3c2083b 100644 (file)
@@ -42,16 +42,6 @@ class CRM_Case_XMLProcessor {
    */
   public static $activityTypes = NULL;
 
-  /**
-   * FIXME: This does *NOT* belong in a static property, but we're too late in
-   * the 4.5-cycle to do the necessary cleanup.
-   *
-   * Format is array(int $id => string $relTypeCname).
-   *
-   * @var array|null
-   */
-  public static $relationshipTypes = NULL;
-
   /**
    * @param $caseType
    *
@@ -101,22 +91,33 @@ class CRM_Case_XMLProcessor {
   }
 
   /**
+   * Get all relationship type labels
+   *
+   * TODO: These should probably be names, but under legacy behavior this has
+   * been labels.
+   *
+   * @param bool $fromXML
+   *   Is this to be used for lookup of values from XML?
+   *   Relationships are recorded in XML from the perspective of the non-client
+   *   while relationships in the UI and everywhere else are from the
+   *   perspective of the client.  Since the XML can't be expected to be
+   *   switched, the direction needs to be translated.
    * @return array
    */
-  public function &allRelationshipTypes() {
-    if (self::$relationshipTypes === NULL) {
+  public function &allRelationshipTypes($fromXML = FALSE) {
+    if (!isset(Civi::$statics[__CLASS__]['reltypes'][$fromXML])) {
       $relationshipInfo = CRM_Core_PseudoConstant::relationshipType('label', TRUE);
 
-      self::$relationshipTypes = [];
+      Civi::$statics[__CLASS__]['reltypes'][$fromXML] = [];
       foreach ($relationshipInfo as $id => $info) {
-        self::$relationshipTypes[$id . '_b_a'] = $info['label_b_a'];
+        Civi::$statics[__CLASS__]['reltypes'][$fromXML][$id . '_b_a'] = ($fromXML) ? $info['label_a_b'] : $info['label_b_a'];
         if ($info['label_b_a'] !== $info['label_a_b']) {
-          self::$relationshipTypes[$id . '_a_b'] = $info['label_a_b'];
+          Civi::$statics[__CLASS__]['reltypes'][$fromXML][$id . '_a_b'] = ($fromXML) ? $info['label_b_a'] : $info['label_a_b'];
         }
       }
     }
 
-    return self::$relationshipTypes;
+    return Civi::$statics[__CLASS__]['reltypes'][$fromXML];
   }
 
   /**
@@ -124,7 +125,7 @@ class CRM_Case_XMLProcessor {
    */
   public static function flushStaticCaches() {
     self::$activityTypes = NULL;
-    self::$relationshipTypes = NULL;
+    unset(Civi::$statics[__CLASS__]['reltypes']);
   }
 
 }
index 03501e993eff137a8c6a7288edcd2e8fe90998a4..4cfb5c8d7e1abcf6142a5e97ab2acc580d1663c3 100644 (file)
@@ -181,7 +181,11 @@ class CRM_Case_XMLProcessor_Process extends CRM_Case_XMLProcessor {
    * @return array|mixed
    */
   public function &caseRoles($caseRolesXML, $isCaseManager = FALSE) {
-    $relationshipTypes = &$this->allRelationshipTypes();
+    // Look up relationship types according to the XML convention (described
+    // from perspective of non-client) but return the labels according to the UI
+    // convention (described from perspective of client)
+    $relationshipTypes = &$this->allRelationshipTypes(TRUE);
+    $relationshipTypesToReturn = &$this->allRelationshipTypes(FALSE);
 
     $result = [];
     foreach ($caseRolesXML as $caseRoleXML) {
@@ -195,7 +199,7 @@ class CRM_Case_XMLProcessor_Process extends CRM_Case_XMLProcessor {
         }
 
         if (!$isCaseManager) {
-          $result[$relationshipTypeID] = $relationshipTypeName;
+          $result[$relationshipTypeID] = $relationshipTypesToReturn[$relationshipTypeID];
         }
         elseif ($relationshipTypeXML->manager == 1) {
           return $relationshipTypeID;
@@ -213,7 +217,9 @@ class CRM_Case_XMLProcessor_Process extends CRM_Case_XMLProcessor {
    * @throws Exception
    */
   public function createRelationships($relationshipTypeName, &$params) {
-    $relationshipTypes = &$this->allRelationshipTypes();
+    // The relationshipTypeName is coming from XML, so the argument should be
+    // `TRUE`
+    $relationshipTypes = &$this->allRelationshipTypes(TRUE);
     // get the relationship
     $relationshipType = array_search($relationshipTypeName, $relationshipTypes);
 
@@ -350,6 +356,8 @@ class CRM_Case_XMLProcessor_Process extends CRM_Case_XMLProcessor {
   }
 
   /**
+   * Relationships are straight from XML, described from perspective of non-client
+   *
    * @param SimpleXMLElement $caseTypeXML
    *
    * @return array<string> symbolic relationship-type names
index 54011e77e3b91aa0da88799ea7d85d06520febf7..243c74c5cc7325a38b74cb97806c61b9acc960d0 100644 (file)
@@ -258,6 +258,8 @@ class CRM_Case_XMLRepository {
   }
 
   /**
+   * Relationships are straight from XML, described from perspective of non-client
+   *
    * @return array<string> symbolic-names of relationship-types
    */
   public function getAllDeclaredRelationshipTypes() {