diff --git a/CRM/Contact/BAO/ContactType.php b/CRM/Contact/BAO/ContactType.php index 685ff08f6fa6..b356a39616ed 100644 --- a/CRM/Contact/BAO/ContactType.php +++ b/CRM/Contact/BAO/ContactType.php @@ -54,6 +54,8 @@ public static function isActive($contactType) { /** * Retrieve basic contact type information. * + * @todo - call getAllContactTypes & return filtered results. + * * @param bool $includeInactive * * @return array @@ -111,6 +113,8 @@ public static function basicTypePairs($all = FALSE, $key = 'name') { /** * Retrieve all subtypes Information. * + * @todo - call getAllContactTypes & return filtered results. + * * @param array $contactType * .. * @param bool $all @@ -217,54 +221,26 @@ public static function contactTypes($all = FALSE) { /** * Retrieve info array about all types i.e basic + subtypes. * + * @todo deprecate calling this with $all = TRUE in favour of getAllContactTypes + * & ideally add getActiveContactTypes & call that from this fully + * deprecated function. + * * @param bool $all * * @return array * Array of basic types + all subtypes. + * @throws \API_Exception */ public static function contactTypeInfo($all = FALSE) { - - if (!isset(Civi::$statics[__CLASS__]['contactTypeInfo'])) { - Civi::$statics[__CLASS__]['contactTypeInfo'] = []; - } - $_cache = &Civi::$statics[__CLASS__]['contactTypeInfo']; - - $argString = $all ? 'CRM_CT_CTI_1' : 'CRM_CT_CTI_0'; - if (!array_key_exists($argString, $_cache)) { - $cache = CRM_Utils_Cache::singleton(); - $_cache[$argString] = $cache->get($argString); - if (!$_cache[$argString]) { - $_cache[$argString] = []; - - $sql = ' -SELECT type.*, parent.name as parent, parent.label as parent_label -FROM civicrm_contact_type type -LEFT JOIN civicrm_contact_type parent ON type.parent_id = parent.id'; - - if ($all === FALSE) { - $sql .= ' WHERE type.is_active = 1'; - } - - $dao = CRM_Core_DAO::executeQuery($sql, - [], - FALSE, - 'CRM_Contact_DAO_ContactType' - ); - while ($dao->fetch()) { - $value = []; - CRM_Core_DAO::storeValues($dao, $value); - if (array_key_exists('parent_id', $value)) { - $value['parent'] = $dao->parent; - $value['parent_label'] = $dao->parent_label; - } - $_cache[$argString][$dao->name] = $value; + $contactTypes = self::getAllContactTypes(); + if (!$all) { + foreach ($contactTypes as $index => $value) { + if (!$value['is_active']) { + unset($contactTypes[$index]); } - - $cache->set($argString, $_cache[$argString]); } } - - return $_cache[$argString]; + return $contactTypes; } /** @@ -276,6 +252,7 @@ public static function contactTypeInfo($all = FALSE) { * * @return array * Array of basictypes with name as 'built-in name' and 'label' as value + * @throws \API_Exception */ public static function contactTypePairs($all = FALSE, $typeName = NULL, $delimiter = NULL) { $types = self::contactTypeInfo($all); @@ -325,6 +302,7 @@ public static function getSelectElements( $_cache = []; } + // @todo - call getAllContactTypes & return filtered results. $argString = $all ? 'CRM_CT_GSE_1' : 'CRM_CT_GSE_0'; $argString .= $isSeparator ? '_1' : '_0'; $argString .= $separator; @@ -658,7 +636,8 @@ public static function setIsActive($id, $is_active) { /** * @param string $typeName * - * @return mixed + * @return string + * @throws \API_Exception */ public static function getLabel($typeName) { $types = self::contactTypeInfo(TRUE); @@ -804,14 +783,16 @@ public static function getSubtypeCustomPair($contactType, $subtypeSet = []) { /** * Function that does something. - * @todo what does this function do? * * @param int $contactID - * @param $contactType + * @param string $contactType * @param array $oldSubtypeSet * @param array $newSubtypeSet * * @return bool + * @throws \CRM_Core_Exception + * + * @todo what does this function do? */ public static function deleteCustomSetForSubtypeMigration( $contactID, @@ -841,6 +822,8 @@ public static function deleteCustomSetForSubtypeMigration( * @param array $subtypesToPreserve * * @return bool + * + * @throws \CRM_Core_Exception */ public static function deleteCustomRowsOfSubtype($gID, $subtypes = [], $subtypesToPreserve = []) { if (!$gID or empty($subtypes)) { @@ -886,6 +869,8 @@ public static function deleteCustomRowsOfSubtype($gID, $subtypes = [], $subtypes * Entity id. * * @return null|string + * + * @throws \CRM_Core_Exception */ public static function deleteCustomRowsForEntityID($customTable, $entityID) { $customTable = CRM_Utils_Type::escape($customTable, 'String'); @@ -893,4 +878,28 @@ public static function deleteCustomRowsForEntityID($customTable, $entityID) { return CRM_Core_DAO::singleValueQuery($query, [1 => [$entityID, 'Integer']]); } + /** + * Get all contact types, leveraging caching. + * + * @return array + * + * @throws \API_Exception + */ + protected static function getAllContactTypes() { + if (!Civi::cache('contactTypes')->has('all')) { + $contactTypes = (array) ContactType::get()->setCheckPermissions(FALSE) + ->setSelect(['id', 'name', 'label', 'description', 'is_active', 'is_reserved', 'image_URL', 'parent_id', 'parent_id:name', 'parent_id:label']) + ->execute()->indexBy('name'); + + foreach ($contactTypes as $id => $contactType) { + $contactTypes[$id]['parent'] = $contactType['parent_id:name']; + $contactTypes[$id]['parent_label'] = $contactType['parent_id:label']; + unset($contactTypes[$id]['parent_id:name'], $contactTypes[$id]['parent_id:label']); + } + Civi::cache('contactTypes')->set('all', $contactTypes); + } + $contactTypes = Civi::cache('contactTypes')->get('all'); + return $contactTypes; + } + } diff --git a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php index 6fd24f0289b8..ce3894441b05 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactType/ContactTypeTest.php @@ -133,6 +133,11 @@ public function getExpectedContactTypes() { 'label' => 'Individual', 'is_active' => '1', 'is_reserved' => '1', + 'description' => NULL, + 'parent_id' => NULL, + 'parent' => NULL, + 'parent_label' => NULL, + 'image_URL' => NULL, ], 'Household' => [ @@ -141,6 +146,11 @@ public function getExpectedContactTypes() { 'label' => 'Household', 'is_active' => '1', 'is_reserved' => '1', + 'description' => NULL, + 'parent_id' => NULL, + 'parent' => NULL, + 'parent_label' => NULL, + 'image_URL' => NULL, ], 'Organization' => [ @@ -149,6 +159,11 @@ public function getExpectedContactTypes() { 'label' => 'Organization', 'is_active' => '1', 'is_reserved' => '1', + 'description' => NULL, + 'parent_id' => NULL, + 'parent' => NULL, + 'parent_label' => NULL, + 'image_URL' => NULL, ], 'Student' => [ @@ -158,8 +173,10 @@ public function getExpectedContactTypes() { 'parent_id' => '1', 'is_active' => '1', 'is_reserved' => '0', + 'description' => NULL, 'parent' => 'Individual', 'parent_label' => 'Individual', + 'image_URL' => NULL, ], 'Parent' => [ @@ -169,8 +186,10 @@ public function getExpectedContactTypes() { 'parent_id' => '1', 'is_active' => '1', 'is_reserved' => '0', + 'description' => NULL, 'parent' => 'Individual', 'parent_label' => 'Individual', + 'image_URL' => NULL, ], 'Staff' => [ @@ -180,8 +199,10 @@ public function getExpectedContactTypes() { 'parent_id' => '1', 'is_active' => '1', 'is_reserved' => '0', + 'description' => NULL, 'parent' => 'Individual', 'parent_label' => 'Individual', + 'image_URL' => NULL, ], 'Team' => [ @@ -191,8 +212,10 @@ public function getExpectedContactTypes() { 'parent_id' => '3', 'is_active' => '1', 'is_reserved' => '0', + 'description' => NULL, 'parent' => 'Organization', 'parent_label' => 'Organization', + 'image_URL' => NULL, ], 'Sponsor' => [ @@ -202,8 +225,10 @@ public function getExpectedContactTypes() { 'parent_id' => '3', 'is_active' => '1', 'is_reserved' => '0', + 'description' => NULL, 'parent' => 'Organization', 'parent_label' => 'Organization', + 'image_URL' => NULL, ], 'sub1_individual' => [ @@ -213,8 +238,10 @@ public function getExpectedContactTypes() { 'parent_id' => '1', 'is_active' => '1', 'is_reserved' => '0', + 'description' => NULL, 'parent' => 'Individual', 'parent_label' => 'Individual', + 'image_URL' => NULL, ], 'sub2_individual' => [ @@ -224,8 +251,10 @@ public function getExpectedContactTypes() { 'parent_id' => '1', 'is_active' => '1', 'is_reserved' => '0', + 'description' => NULL, 'parent' => 'Individual', 'parent_label' => 'Individual', + 'image_URL' => NULL, ], 'sub_organization' => [ @@ -235,8 +264,10 @@ public function getExpectedContactTypes() { 'parent_id' => '3', 'is_active' => '1', 'is_reserved' => '0', + 'description' => NULL, 'parent' => 'Organization', 'parent_label' => 'Organization', + 'image_URL' => NULL, ], 'sub_household' => [ @@ -246,8 +277,10 @@ public function getExpectedContactTypes() { 'parent_id' => '2', 'is_active' => '1', 'is_reserved' => '0', + 'description' => NULL, 'parent' => 'Household', 'parent_label' => 'Household', + 'image_URL' => NULL, ], ]; }