From ef618a14e0a06b1a6cf034b45f25bdd703d0e439 Mon Sep 17 00:00:00 2001 From: DemeritCowboy Date: Sat, 23 Nov 2019 15:30:02 -0500 Subject: [PATCH] status check for external case xml files --- CRM/Utils/Check/Component/Case.php | 110 +++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/CRM/Utils/Check/Component/Case.php b/CRM/Utils/Check/Component/Case.php index b9108b6654..cf5074e3e0 100644 --- a/CRM/Utils/Check/Component/Case.php +++ b/CRM/Utils/Check/Component/Case.php @@ -397,4 +397,114 @@ class CRM_Utils_Check_Component_Case extends CRM_Utils_Check_Component { return $messages; } + /** + * Check any xml definitions stored as external files to see if they + * have label as the role and where the label is different from the name. + * We don't have to think about edge cases because there are already + * status checks above for those. + * + * @return array + * An empty array, or a list of warnings + */ + public function checkExternalXmlFileRoleNames() { + $messages = []; + + // Get config for relationship types + $relationship_types = civicrm_api3('RelationshipType', 'get', [ + 'options' => ['limit' => 0], + ])['values']; + // keyed on name, with id as the value, e.g. 'Case Coordinator is' => 10 + $names_a_b = array_column($relationship_types, 'id', 'name_a_b'); + $names_b_a = array_column($relationship_types, 'id', 'name_b_a'); + $labels_a_b = array_column($relationship_types, 'id', 'label_a_b'); + $labels_b_a = array_column($relationship_types, 'id', 'label_b_a'); + + $dao = CRM_Core_DAO::executeQuery("SELECT id FROM civicrm_case_type WHERE definition IS NULL OR definition=''"); + while ($dao->fetch()) { + $case_type = civicrm_api3('CaseType', 'get', [ + 'id' => $dao->id, + ])['values'][$dao->id]; + if (empty($case_type['definition'])) { + $messages[] = new CRM_Utils_Check_Message( + __FUNCTION__ . "missingcasetypedefinition", + '

' . ts('Unable to locate xml file for Case Type "%1".', + [ + 1 => htmlspecialchars(empty($case_type['title']) ? $dao->id : $case_type['title']), + ]) . '

', + ts('Missing Case Type Definition'), + \Psr\Log\LogLevel::ERROR, + 'fa-exclamation' + ); + continue; + } + + if (empty($case_type['definition']['caseRoles'])) { + $messages[] = new CRM_Utils_Check_Message( + __FUNCTION__ . "missingcaseroles", + '

' . ts('CaseRoles seems to be missing in the xml file for Case Type "%1".', + [ + 1 => htmlspecialchars(empty($case_type['title']) ? $dao->id : $case_type['title']), + ]) . '

', + ts('Missing Case Roles'), + \Psr\Log\LogLevel::ERROR, + 'fa-exclamation' + ); + continue; + } + + // Loop thru each role in the xml. + foreach ($case_type['definition']['caseRoles'] as $role) { + $name_to_suggest = NULL; + $xml_name = $role['name']; + if (isset($names_a_b[$xml_name]) || isset($names_b_a[$xml_name])) { + // It matches a name, so either name and label are the same or it's + // an edge case already dealt with by core status checks, so do + // nothing. + continue; + } + elseif (isset($labels_b_a[$xml_name])) { + // $labels_b_a[$xml_name] gives us the id, so then look up name_b_a + // from the original relationship_types array which is keyed on id. + // We do b_a first because it's the more standard one, although it + // will only make a difference in edge cases which we leave to the + // other checks. + $name_to_suggest = $relationship_types[$labels_b_a[$xml_name]]['name_b_a']; + } + elseif (isset($labels_a_b[$xml_name])) { + $name_to_suggest = $relationship_types[$labels_a_b[$xml_name]]['name_a_b']; + } + + // If it didn't match any name or label then that's weird. + if (empty($name_to_suggest)) { + $messages[] = new CRM_Utils_Check_Message( + __FUNCTION__ . "invalidcaserole", + '

' . ts('CaseRole "%1" in the xml file for Case Type "%2" doesn\'t seem to match any existing relationship type.', + [ + 1 => htmlspecialchars($xml_name), + 2 => htmlspecialchars(empty($case_type['title']) ? $dao->id : $case_type['title']), + ]) . '

', + ts('Invalid Case Role'), + \Psr\Log\LogLevel::ERROR, + 'fa-exclamation' + ); + } + else { + $messages[] = new CRM_Utils_Check_Message( + __FUNCTION__ . "suggestedchange", + '

' . ts('Please edit the XML file for case type "%2" so that the case role label "%1" is changed to its corresponding name "%3". Using label is deprecated as of version 5.20.', + [ + 1 => htmlspecialchars($xml_name), + 2 => htmlspecialchars(empty($case_type['title']) ? $dao->id : $case_type['title']), + 3 => htmlspecialchars($name_to_suggest), + ]) . '

', + ts('Case Role using display label instead of internal machine name'), + \Psr\Log\LogLevel::WARNING, + 'fa-code' + ); + } + } + } + return $messages; + } + } -- 2.25.1