From 23196798ae432847c87cba87946125605f22fde4 Mon Sep 17 00:00:00 2001 From: colemanw Date: Mon, 16 Oct 2023 14:33:52 -0400 Subject: [PATCH] CRM_Core_Pseudoconstant - Cleanup static arrays & functions --- CRM/Core/PseudoConstant.php | 173 +++++++++--------------------------- 1 file changed, 40 insertions(+), 133 deletions(-) diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 34c9161568..73275f3eb5 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -68,12 +68,6 @@ class CRM_Core_PseudoConstant { */ private static $country; - /** - * CountryIsoCode. - * @var array - */ - private static $countryIsoCode; - /** * group * @var array @@ -99,43 +93,6 @@ class CRM_Core_PseudoConstant { */ private static $currencyCode; - /** - * Payment processor - * @var array - */ - private static $paymentProcessor; - - /** - * Payment processor types - * @var array - */ - private static $paymentProcessorType; - - /** - * World Region - * @var array - */ - private static $worldRegions; - - /** - * activity status - * @var array - * @deprecated Please use the buildOptions() method in the appropriate BAO object. - */ - private static $activityStatus; - - /** - * Visibility - * @var array - */ - private static $visibility; - - /** - * Greetings - * @var array - */ - private static $greeting; - /** * Extensions of type module * @var array @@ -466,8 +423,7 @@ class CRM_Core_PseudoConstant { } /** - * Flush given pseudoconstant so it can be reread from db. - * nex time it's requested. + * Flush static array cache. * * @param bool|string $name pseudoconstant to be flushed */ @@ -668,6 +624,7 @@ WHERE id = %1"; /** * Get all the State/Province abbreviations from the database for the specified country. + * @deprecated * * @param int $countryID * @@ -675,21 +632,24 @@ WHERE id = %1"; * array of all State/Province abbreviations for the given country. */ public static function stateProvinceAbbreviationForCountry($countryID) { - if (!isset(\Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID])) { - \Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID] = []; - } - self::populate(\Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID], 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', "country_id = " . (int) $countryID, 'abbreviation'); - return \Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID]; + CRM_Core_Error::deprecatedFunctionWarning('API'); + $abbrs = []; + self::populate($abbrs, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', "country_id = " . (int) $countryID, 'abbreviation'); + return $abbrs; } /** * Get all the State/Province abbreviations from the database for the default country. + * @deprecated * * @return array * array of all State/Province abbreviations for the given country. */ public static function stateProvinceAbbreviationForDefaultCountry() { - return CRM_Core_PseudoConstant::stateProvinceAbbreviationForCountry(Civi::settings()->get('defaultContactCountry')); + $countryID = Civi::settings()->get('defaultContactCountry'); + $abbrs = []; + self::populate($abbrs, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', "country_id = " . (int) $countryID, 'abbreviation'); + return $abbrs; } /** @@ -754,31 +714,18 @@ WHERE id = %1"; /** * Get all the country ISO Code abbreviations from the database. * - * The static array countryIsoCode is returned, and if it's - * called the first time, the Country DAO is used - * to get all the countries' ISO codes. - * - * Note: any database errors will be trapped by the DAO. - * - * * @param bool $id * * @return array - * array reference of all country ISO codes. */ - public static function &countryIsoCode($id = FALSE) { - if (!self::$countryIsoCode) { - self::populate(self::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code'); - } + public static function countryIsoCode($id = FALSE) { + $values = []; + self::populate($values, 'CRM_Core_DAO_Country', TRUE, 'iso_code'); + if ($id) { - if (array_key_exists($id, self::$countryIsoCode)) { - return self::$countryIsoCode[$id]; - } - else { - return CRM_Core_DAO::$_nullObject; - } + return $values[$id] ?? NULL; } - return self::$countryIsoCode; + return $values; } /** @@ -803,12 +750,9 @@ WHERE id = %1"; */ public static function allGroup($groupType = NULL, $excludeHidden = TRUE) { $condition = CRM_Contact_BAO_Group::groupTypeCondition($groupType, $excludeHidden); - $groupKey = ($groupType ?: 'null') . !empty($excludeHidden); - - if (!isset(Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey])) { - self::populate(Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition); - } - return Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey]; + $values = []; + self::populate($values, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition); + return $values; } /** @@ -955,9 +899,6 @@ WHERE id = %1"; /** * Get all the ISO 4217 currency codes * - * so far, we use this for validation only, so there's no point of putting this into the database - * - * * @return array * array reference of all currency codes */ @@ -966,7 +907,6 @@ WHERE id = %1"; $query = "SELECT name FROM civicrm_currency"; $dao = CRM_Core_DAO::executeQuery($query); - $currencyCode = []; while ($dao->fetch()) { self::$currencyCode[] = $dao->name; } @@ -1025,8 +965,7 @@ WHERE id = %1"; * array of all payment processors */ public static function paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) { - $condition = "is_test = "; - $condition .= ($test) ? '1' : '0'; + $condition = 'is_test = ' . ($test ? '1' : '0'); if ($additionalCond) { $condition .= " AND ( $additionalCond ) "; @@ -1035,13 +974,10 @@ WHERE id = %1"; // CRM-7178. Make sure we only include payment processors valid in this // domain $condition .= " AND domain_id = " . CRM_Core_Config::domainID(); + $values = []; + self::populate($values, 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name'); - $cacheKey = $condition . '_' . (int) $all; - if (!isset(self::$paymentProcessor[$cacheKey])) { - self::populate(self::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name'); - } - - return self::$paymentProcessor[$cacheKey]; + return $values; } /** @@ -1060,40 +996,32 @@ WHERE id = %1"; * array of all payment processor types */ public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') { - $cacheKey = $id . '_' . $return; - if (empty(self::$paymentProcessorType[$cacheKey])) { - self::populate(self::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id'); - } - if ($id && !empty(self::$paymentProcessorType[$cacheKey][$id])) { - return self::$paymentProcessorType[$cacheKey][$id]; - } - return self::$paymentProcessorType[$cacheKey]; + CRM_Core_Error::deprecatedFunctionWarning('API'); + $values = []; + self::populate($values, 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id'); + // This is incredibly stupid, but the whole function is deprecated anyway... + if ($id && !empty($values[$id])) { + return $values[$id]; + } + return $values; } /** * Get all the World Regions from Database. * - * * @param bool $id * * @return array * array reference of all World Regions */ - public static function &worldRegion($id = FALSE) { - if (!self::$worldRegions) { - self::populate(self::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id'); - } + public static function worldRegion($id = FALSE) { + $values = []; + self::populate($values, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id'); if ($id) { - if (array_key_exists($id, self::$worldRegions)) { - return self::$worldRegions[$id]; - } - else { - return CRM_Core_DAO::$_nullObject; - } + return $values[$id] ?? NULL; } - - return self::$worldRegions; + return $values; } /** @@ -1101,25 +1029,12 @@ WHERE id = %1"; * * Get all Activity Statuses. * - * The static array activityStatus is returned - * - * * @param string $column * * @return array - * array reference of all activity statuses */ public static function &activityStatus($column = 'label') { - if (NULL === self::$activityStatus) { - self::$activityStatus = []; - } - if (!array_key_exists($column, self::$activityStatus)) { - self::$activityStatus[$column] = []; - - self::$activityStatus[$column] = CRM_Core_OptionGroup::values('activity_status', FALSE, FALSE, FALSE, NULL, $column); - } - - return self::$activityStatus[$column]; + return CRM_Core_OptionGroup::values('activity_status', FALSE, FALSE, FALSE, NULL, $column); } /** @@ -1135,16 +1050,8 @@ WHERE id = %1"; * @return array * array reference of all Visibility levels. */ - public static function &visibility($column = 'label') { - if (!isset(self::$visibility)) { - self::$visibility = []; - } - - if (!isset(self::$visibility[$column])) { - self::$visibility[$column] = CRM_Core_OptionGroup::values('visibility', FALSE, FALSE, FALSE, NULL, $column); - } - - return self::$visibility[$column]; + public static function visibility($column = 'label') { + return CRM_Core_OptionGroup::values('visibility', FALSE, FALSE, FALSE, NULL, $column); } /** -- 2.25.1