From: Alice Frumin Date: Wed, 1 May 2019 19:47:55 +0000 (-0400) Subject: dev/core#530 CiviCase: translate case role direction coming from xml X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d0a94888435a0bdc1f451cd3d74c25748fe189a3;p=civicrm-core.git dev/core#530 CiviCase: translate case role direction coming from xml --- diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index dabd71f63f..f25f917749 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -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; diff --git a/CRM/Case/Info.php b/CRM/Case/Info.php index f7f0e0cd95..ce5421fd1b 100644 --- a/CRM/Case/Info.php +++ b/CRM/Case/Info.php @@ -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', diff --git a/CRM/Case/XMLProcessor.php b/CRM/Case/XMLProcessor.php index f118d4c203..245e9ace1c 100644 --- a/CRM/Case/XMLProcessor.php +++ b/CRM/Case/XMLProcessor.php @@ -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']); } } diff --git a/CRM/Case/XMLProcessor/Process.php b/CRM/Case/XMLProcessor/Process.php index 03501e993e..4cfb5c8d7e 100644 --- a/CRM/Case/XMLProcessor/Process.php +++ b/CRM/Case/XMLProcessor/Process.php @@ -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 symbolic relationship-type names diff --git a/CRM/Case/XMLRepository.php b/CRM/Case/XMLRepository.php index 54011e77e3..243c74c5cc 100644 --- a/CRM/Case/XMLRepository.php +++ b/CRM/Case/XMLRepository.php @@ -258,6 +258,8 @@ class CRM_Case_XMLRepository { } /** + * Relationships are straight from XML, described from perspective of non-client + * * @return array symbolic-names of relationship-types */ public function getAllDeclaredRelationshipTypes() {