From b5f3c53a4ec26aa368d070738e9333474e51b47f Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 24 Jul 2020 13:43:31 +1200 Subject: [PATCH] [REF] Update subtypeInfo function to leverage getAllContactTypes This is part of a cleanup I've been doing to remove some special handling in executeQuery that seems purely there to support some very old functions in this class --- CRM/Contact/BAO/ContactType.php | 55 ++++++------------- .../BAO/ContactType/ContactTypeTest.php | 5 ++ 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/CRM/Contact/BAO/ContactType.php b/CRM/Contact/BAO/ContactType.php index 4284efa467..18b62ab86e 100644 --- a/CRM/Contact/BAO/ContactType.php +++ b/CRM/Contact/BAO/ContactType.php @@ -45,9 +45,11 @@ class CRM_Contact_BAO_ContactType extends CRM_Contact_DAO_ContactType { * @param string $contactType * * @return bool + * + * @throws \API_Exception */ public static function isActive($contactType) { - $contact = self::contactTypeInfo(FALSE); + $contact = self::contactTypeInfo(); return array_key_exists($contactType, $contact); } @@ -113,51 +115,25 @@ class CRM_Contact_BAO_ContactType extends CRM_Contact_DAO_ContactType { /** * Retrieve all subtypes Information. * - * @todo - call getAllContactTypes & return filtered results. - * * @param array $contactType - * .. * @param bool $all - * @param bool $ignoreCache * * @return array - * Array of sub type information + * Array of sub type information, subset of getAllContactTypes. + * + * @throws \API_Exception */ - public static function subTypeInfo($contactType = NULL, $all = FALSE, $ignoreCache = FALSE) { - $argString = $all ? 'CRM_CT_STI_1_' : 'CRM_CT_STI_0_'; - if (!empty($contactType)) { - $contactType = (array) $contactType; - $argString .= implode('_', $contactType); - } - if (!Civi::cache('contactTypes')->has($argString) || $ignoreCache) { - $ctWHERE = ''; - if (!empty($contactType)) { - $ctWHERE = " AND parent.name IN ('" . implode("','", $contactType) . "')"; - } - - $sql = " -SELECT subtype.*, parent.name as parent, parent.label as parent_label -FROM civicrm_contact_type subtype -INNER JOIN civicrm_contact_type parent ON subtype.parent_id = parent.id -WHERE subtype.name IS NOT NULL AND subtype.parent_id IS NOT NULL {$ctWHERE} -"; - if ($all === FALSE) { - $sql .= " AND subtype.is_active = 1 AND parent.is_active = 1 ORDER BY parent.id"; - } - $dao = CRM_Core_DAO::executeQuery($sql, [], - FALSE, 'CRM_Contact_DAO_ContactType' - ); - $values = []; - while ($dao->fetch()) { - $value = []; - CRM_Core_DAO::storeValues($dao, $value); - $value['parent'] = $dao->parent; - $value['parent_label'] = $dao->parent_label; - $values[$dao->name] = $value; + public static function subTypeInfo($contactType = NULL, $all = FALSE) { + $contactTypes = self::getAllContactTypes(); + foreach ($contactTypes as $index => $type) { + if (empty($type['parent']) || + (!$all && !$type['is_active']) + || ($contactType && $type['parent'] !== $contactType) + ) { + unset($contactTypes[$index]); } - Civi::cache('contactTypes')->set($argString, $values); } - return Civi::cache('contactTypes')->get($argString); + return $contactTypes; } /** @@ -173,6 +149,7 @@ WHERE subtype.name IS NOT NULL AND subtype.parent_id IS NOT NULL {$ctWHERE} * @return array * all subtypes OR list of subtypes associated to * a given basic contact type + * @throws \API_Exception */ public static function subTypes($contactType = NULL, $all = FALSE, $columnName = 'name', $ignoreCache = FALSE) { if ($columnName === 'name') { diff --git a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php index 5f829c9a40..e726e82524 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php @@ -66,19 +66,24 @@ class CRM_Contact_BAO_ContactType_ContactTypeTest extends CiviUnitTestCase { // check for type:Individual $result = CRM_Contact_BAO_ContactType::subTypes('Individual'); $this->assertEquals(array_keys($this->getExpectedContactSubTypes('Individual')), $result); + $this->assertEquals($this->getExpectedContactSubTypes('Individual'), CRM_Contact_BAO_ContactType::subTypeInfo('Individual')); // check for type:Organization $result = CRM_Contact_BAO_ContactType::subTypes('Organization'); $this->assertEquals(array_keys($this->getExpectedContactSubTypes('Organization')), $result); + $this->assertEquals($this->getExpectedContactSubTypes('Organization'), CRM_Contact_BAO_ContactType::subTypeInfo('Organization')); // check for type:Household $result = CRM_Contact_BAO_ContactType::subTypes('Household'); $this->assertEquals(array_keys($this->getExpectedContactSubTypes('Household')), $result); + $this->assertEquals($this->getExpectedContactSubTypes('Household'), CRM_Contact_BAO_ContactType::subTypeInfo('Household')); // check for all contact types $result = CRM_Contact_BAO_ContactType::subTypes(); $subtypes = array_keys($this->getExpectedAllSubtypes()); $this->assertEquals(sort($subtypes), sort($result)); + $this->assertEquals($this->getExpectedAllSubtypes(), CRM_Contact_BAO_ContactType::subTypeInfo()); + } /** -- 2.25.1