From a18a4870c42b071eeb11f408b7a3b8258888fba3 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 25 May 2020 18:51:33 +1200 Subject: [PATCH] Use apiv4, cache infra for basicTypes I came across this as using an old weird pattern accessing the DAO. On looking it made most sense just to switch to an apiv4 call. The caching was not convoluted but effectively used an array cache, if loaded, and the array or Redis or sql cache if not. This is the same as using the 'metadata' or 'contactTypes' cache so I switched to the latter. I was inclined towards the former as I lean towards thinking this metadata all belongs in one - but there is already another function in the same class going to the latter. --- CRM/Contact/BAO/ContactType.php | 54 ++++++++++----------------------- CRM/Core/SelectValues.php | 6 +--- 2 files changed, 17 insertions(+), 43 deletions(-) diff --git a/CRM/Contact/BAO/ContactType.php b/CRM/Contact/BAO/ContactType.php index 947f8dc430..49ac32ea6c 100644 --- a/CRM/Contact/BAO/ContactType.php +++ b/CRM/Contact/BAO/ContactType.php @@ -9,6 +9,8 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\ContactType; + /** * * @package CRM @@ -53,48 +55,24 @@ class CRM_Contact_BAO_ContactType extends CRM_Contact_DAO_ContactType { /** * Retrieve basic contact type information. * - * @param bool $all + * @param bool $includeInactive * * @return array * Array of basic contact types information. + * + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException */ - public static function basicTypeInfo($all = FALSE) { - static $_cache = NULL; - - if ($_cache === NULL) { - $_cache = []; - } - - $argString = $all ? 'CRM_CT_BTI_1' : 'CRM_CT_BTI_0'; - if (!array_key_exists($argString, $_cache)) { - $cache = CRM_Utils_Cache::singleton(); - $_cache[$argString] = $cache->get($argString); - if (!$_cache[$argString]) { - $sql = " -SELECT * -FROM civicrm_contact_type -WHERE parent_id IS NULL -"; - if ($all === FALSE) { - $sql .= " AND is_active = 1"; - } - - $params = []; - $dao = CRM_Core_DAO::executeQuery($sql, - $params, - FALSE, - 'CRM_Contact_DAO_ContactType' - ); - while ($dao->fetch()) { - $value = []; - CRM_Core_DAO::storeValues($dao, $value); - $_cache[$argString][$dao->name] = $value; - } - - $cache->set($argString, $_cache[$argString]); + public static function basicTypeInfo($includeInactive = FALSE) { + $cacheKey = 'CRM_CT_BTI_' . (int) $includeInactive; + if (!Civi::cache('contactTypes')->has($cacheKey)) { + $contactType = ContactType::get()->setCheckPermissions(FALSE)->setSelect(['*'])->addWhere('parent_id', 'IS NULL'); + if ($includeInactive === FALSE) { + $contactType->addWhere('is_active', '=', 1); } + Civi::cache('contactTypes')->set($cacheKey, (array) $contactType->execute()->indexBy('name')); } - return $_cache[$argString]; + return Civi::cache('contactTypes')->get($cacheKey); } /** @@ -578,10 +556,10 @@ WHERE contact_sub_type = '$name'"; // remove navigation entry if any if ($name) { - $sql = " + $sql = ' DELETE FROM civicrm_navigation -WHERE name = %1"; +WHERE name = %1'; $params = [1 => ["New $name", 'String']]; CRM_Core_DAO::executeQuery($sql, $params); CRM_Core_BAO_Navigation::resetNavigation(); diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index 3455d14849..b2c515867e 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -68,11 +68,7 @@ class CRM_Core_SelectValues { * @return array */ public static function contactType() { - static $contactType = NULL; - if (!$contactType) { - $contactType = CRM_Contact_BAO_ContactType::basicTypePairs(); - } - return $contactType; + return CRM_Contact_BAO_ContactType::basicTypePairs(); } /** -- 2.25.1