From 0acb7f15552889bdba24b013da4dafebebf3a838 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 10 Sep 2015 14:38:55 -0700 Subject: [PATCH] CRM-16373 - CRM_Core_Config_Variables should be strictly about data The distribution of variables and functions between `CRM_Core_Config` and `CRM_Core_Config_Variables` seems pretty random. In anticipation of swapping out `CRM_Core_Config_Variables`, make a few changes: * Move various functions from `CRM_Core_Config_Variables` to BAOs (`provinceLimit, `countryLimit`, `defaultContactCountry`, `defaultContactCountryName`, `defaultCurrencySymbol`) * Put deprecated stubs in `CRM_Core_Config` * Move most variables from `CRM_Core_Config` to `CRM_Core_Config_Variables`. --- CRM/Contact/Import/Parser/Contact.php | 6 +- CRM/Core/BAO/Country.php | 153 +++++++++++++++++ CRM/Core/Block.php | 2 +- CRM/Core/Config.php | 129 ++++---------- CRM/Core/Config/Variables.php | 234 ++++++++++---------------- CRM/Core/PseudoConstant.php | 8 +- settings/Localization.setting.php | 4 +- 7 files changed, 281 insertions(+), 255 deletions(-) create mode 100644 CRM/Core/BAO/Country.php diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index fcfd43b331..c823bcd0a3 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -1224,8 +1224,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { if ($countryValue) { CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active'); CRM_Core_PseudoConstant::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code'); - $config = CRM_Core_Config::singleton(); - $limitCodes = $config->countryLimit(); + $limitCodes = CRM_Core_BAO_Country::countryLimit(); $error = TRUE; foreach (array( @@ -1414,8 +1413,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { if ($stateValue['country']) { CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active'); CRM_Core_PseudoConstant::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code'); - $config = CRM_Core_Config::singleton(); - $limitCodes = $config->countryLimit(); + $limitCodes = CRM_Core_BAO_Country::countryLimit(); //If no country is selected in //localization then take all countries if (empty($limitCodes)) { diff --git a/CRM/Core/BAO/Country.php b/CRM/Core/BAO/Country.php new file mode 100644 index 0000000000..56fc6cd7aa --- /dev/null +++ b/CRM/Core/BAO/Country.php @@ -0,0 +1,153 @@ +get('provinceLimit'); + $country = array(); + if (is_array($provinceLimit)) { + foreach ($provinceLimit as $val) { + // CRM-12007 + // some countries have disappeared and hence they might be in country limit + // but not in the country table + if (isset($countryIsoCodes[$val])) { + $country[] = $countryIsoCodes[$val]; + } + } + } + else { + $country[] = $countryIsoCodes[$provinceLimit]; + } + Civi::$statics[__CLASS__]['provinceLimit'] = $country; + } + return Civi::$statics[__CLASS__]['provinceLimit']; + } + + /** + * Get the list of countries (with names) which are available to user. + * + * @return mixed + */ + public static function countryLimit() { + if (!isset(Civi::$statics[__CLASS__]['countryLimit'])) { + $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode(); + $country = array(); + $countryLimit = Civi::settings()->get('countryLimit'); + if (is_array($countryLimit)) { + foreach ($countryLimit as $val) { + // CRM-12007 + // some countries have disappeared and hence they might be in country limit + // but not in the country table + if (isset($countryIsoCodes[$val])) { + $country[] = $countryIsoCodes[$val]; + } + } + } + else { + $country[] = $countryIsoCodes[$countryLimit]; + } + Civi::$statics[__CLASS__]['countryLimit'] = $country; + } + return Civi::$statics[__CLASS__]['countryLimit']; + } + + /** + * Provide cached default contact country. + * + * @return string + */ + public static function defaultContactCountry() { + static $cachedContactCountry = NULL; + $defaultContactCountry = Civi::settings()->get('defaultContactCountry'); + + if (!empty($defaultContactCountry) && !$cachedContactCountry) { + $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode(); + $cachedContactCountry = CRM_Utils_Array::value($defaultContactCountry, + $countryIsoCodes + ); + } + return $cachedContactCountry; + } + + /** + * Provide cached default country name. + * + * @return string + */ + public static function defaultContactCountryName() { + static $cachedContactCountryName = NULL; + $defaultContactCountry = Civi::settings()->get('defaultContactCountry'); + if (!$cachedContactCountryName && $defaultContactCountry) { + $countryCodes = CRM_Core_PseudoConstant::country(); + $cachedContactCountryName = $countryCodes[$defaultContactCountry]; + } + return $cachedContactCountryName; + } + + /** + * Provide cached default currency symbol. + * + * @return string + */ + public static function defaultCurrencySymbol($defaultCurrency = NULL) { + static $cachedSymbol = NULL; + if (!$cachedSymbol || $defaultCurrency) { + $currency = $defaultCurrency ? $defaultCurrency : Civi::settings()->get('defaultCurrency'); + if ($currency) { + $currencySymbols = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array( + 'labelColumn' => 'symbol', + 'orderColumn' => TRUE, + )); + $cachedSymbol = CRM_Utils_Array::value($currency, $currencySymbols, ''); + } + else { + $cachedSymbol = '$'; + } + } + return $cachedSymbol; + } + +} diff --git a/CRM/Core/Block.php b/CRM/Core/Block.php index db6ad2e00d..c5f168ae07 100644 --- a/CRM/Core/Block.php +++ b/CRM/Core/Block.php @@ -620,7 +620,7 @@ class CRM_Core_Block { // Suppress Language switcher if language is inherited from CMS - CRM-9971 $config = CRM_Core_Config::singleton(); - if ($id == self::LANGSWITCH && property_exists($config, "inheritLocale") && $config->inheritLocale) { + if ($id == self::LANGSWITCH && $config->inheritLocale) { return NULL; } diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index 688c7dabd2..ec10378a29 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -44,80 +44,6 @@ require_once 'api/api.php'; * Class CRM_Core_Config */ class CRM_Core_Config extends CRM_Core_Config_Variables { - /// - /// BASE SYSTEM PROPERTIES (CIVICRM.SETTINGS.PHP) - /// - - /** - * The dsn of the database connection - * - * @var string - */ - public $dsn; - - /** - * The name of user framework - * - * @var string - */ - public $userFramework = 'Drupal'; - - /** - * The name of user framework url variable name - * - * @var string - */ - public $userFrameworkURLVar = 'q'; - - /** - * The dsn of the database connection for user framework - * - * @var string - */ - public $userFrameworkDSN = NULL; - - /** - * The connector module for the CMS/UF - * @todo Introduce an interface. - * - * @var CRM_Utils_System_Base - */ - public $userSystem = NULL; - - /** - * @var CRM_Core_Permission_Base - */ - public $userPermissionClass; - - /** - * The root directory where Smarty should store compiled files. - * - * @var string - */ - public $templateCompileDir; - - /** - * @var string - */ - public $configAndLogDir = NULL; - - // END: BASE SYSTEM PROPERTIES (CIVICRM.SETTINGS.PHP) - - /// - /// BEGIN HELPER CLASS PROPERTIES - /// - - /** - * Are we initialized and in a proper state - * - * @var string - */ - public $initialized = 0; - - /** - * @var string - */ - public $customPHPPathDir; /** * The handle to the log that we are using @@ -133,35 +59,12 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { */ private static $_singleton = NULL; - /// - /// END HELPER CLASS PROPERTIES - /// - - /// - /// RUNTIME SET CLASS PROPERTIES - /// - - /** - * @var bool - * TRUE, if the call is CiviCRM. - * FALSE, if the call is from the CMS. - */ - public $inCiviCRM = FALSE; - - /// - /// END: RUNTIME SET CLASS PROPERTIES - /// - - /** - * @var string - */ - public $recaptchaPublicKey; - /** * The constructor. Sets domain id if defined, otherwise assumes * single instance installation. */ - private function __construct() { + public function __construct() { + //parent::__construct(); } /** @@ -622,6 +525,34 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { return CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(array('BackOffice')); } + /** + * @deprecated + */ + public function addressSequence() { + return CRM_Utils_Address::sequence(Civi::settings()->get('address_format')); + } + + /** + * @deprecated + */ + public function defaultContactCountry() { + return CRM_Core_BAO_Country::defaultContactCountry(); + } + + /** + * @deprecated + */ + public function defaultContactCountryName() { + return CRM_Core_BAO_Country::defaultContactCountryName(); + } + + /** + * @deprecated + */ + public function defaultCurrencySymbol($defaultCurrency = NULL) { + return CRM_Core_BAO_Country::defaultCurrencySymbol($defaultCurrency); + } + /** * Resets the singleton, so that the next call to CRM_Core_Config::singleton() * reloads completely. diff --git a/CRM/Core/Config/Variables.php b/CRM/Core/Config/Variables.php index 7cb80b9ea9..56d4e282c8 100644 --- a/CRM/Core/Config/Variables.php +++ b/CRM/Core/Config/Variables.php @@ -37,6 +37,95 @@ */ class CRM_Core_Config_Variables extends CRM_Core_Config_Defaults { + /// + /// BASE SYSTEM PROPERTIES (CIVICRM.SETTINGS.PHP) + /// + + /** + * The dsn of the database connection + * + * @var string + */ + public $dsn; + + /** + * The name of user framework + * + * @var string + */ + public $userFramework = 'Drupal'; + + /** + * The name of user framework url variable name + * + * @var string + */ + public $userFrameworkURLVar = 'q'; + + /** + * The dsn of the database connection for user framework + * + * @var string + */ + public $userFrameworkDSN = NULL; + + /** + * The connector module for the CMS/UF + * @todo Introduce an interface. + * + * @var CRM_Utils_System_Base + */ + public $userSystem = NULL; + + /** + * The root directory where Smarty should store compiled files. + * + * @var string + */ + public $templateCompileDir; + + /** + * @var string + */ + public $configAndLogDir = NULL; + + // END: BASE SYSTEM PROPERTIES (CIVICRM.SETTINGS.PHP) + + + /** + * determine whether the call is from cms or civicrm + * + * @var bool + * TRUE, if the call is CiviCRM. + * FALSE, if the call is from the CMS. + */ + public $inCiviCRM = FALSE; + + /// + /// END: RUNTIME SET CLASS PROPERTIES + /// + + /** + * @var string + */ + public $recaptchaPublicKey; + + /// + /// BEGIN HELPER CLASS PROPERTIES + /// + + /** + * Are we initialized and in a proper state + * + * @var string + */ + public $initialized = 0; + + /** + * @var string + */ + public $customPHPPathDir; + /** * The debug level for civicrm. * @var int @@ -224,7 +313,6 @@ class CRM_Core_Config_Variables extends CRM_Core_Config_Defaults { /** * Default user framework. This basically makes Drupal 7 the default */ - public $userFramework = 'Drupal'; public $userFrameworkClass = 'CRM_Utils_System_Drupal'; public $userHookClass = 'CRM_Utils_Hook_Drupal'; @@ -238,8 +326,6 @@ class CRM_Core_Config_Variables extends CRM_Core_Config_Defaults { */ public $userPermissionTemp = NULL; - public $userFrameworkURLVar = 'q'; - public $userFrameworkDSN = NULL; public $userFrameworkBaseURL = NULL; public $userFrameworkResourceURL = NULL; public $userFrameworkFrontend = FALSE; @@ -364,11 +450,6 @@ class CRM_Core_Config_Variables extends CRM_Core_Config_Defaults { public $includeAlphabeticalPager = 1; public $includeOrderByClause = 1; - /** - * determine whether the call is from cms or civicrm - */ - public $inCiviCRM = FALSE; - /** * PDF receipt as attachment is disabled by default (CRM-8350) */ @@ -392,142 +473,7 @@ class CRM_Core_Config_Variables extends CRM_Core_Config_Defaults { public $verpSeparator = '.', $mailThrottleTime = 0, $mailerJobsMax = 0, $mailerJobSize = 0, $mailerBatchLimit = 0; - /** - * Provide addressSequence. - * - * @param - * - * @return string - */ - public function addressSequence() { - $addressFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, - 'address_format' - ); - - return CRM_Utils_Address::sequence($addressFormat); - } - - /** - * Provide cached default currency symbol. - * - * @param - * - * @return string - */ - public function defaultCurrencySymbol($defaultCurrency = NULL) { - static $cachedSymbol = NULL; - if (!$cachedSymbol || $defaultCurrency) { - $currency = $defaultCurrency ? $defaultCurrency : $this->defaultCurrency; - if ($currency) { - $currencySymbols = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array( - 'labelColumn' => 'symbol', - 'orderColumn' => TRUE, - )); - $cachedSymbol = CRM_Utils_Array::value($currency, $currencySymbols, ''); - } - else { - $cachedSymbol = '$'; - } - } - return $cachedSymbol; - } - - /** - * Provide cached default currency symbol. - * - * @param - * - * @return string - */ - public function defaultContactCountry() { - static $cachedContactCountry = NULL; - - if (!empty($this->defaultContactCountry) && - !$cachedContactCountry - ) { - $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode(); - $cachedContactCountry = CRM_Utils_Array::value($this->defaultContactCountry, - $countryIsoCodes - ); - } - return $cachedContactCountry; - } - - /** - * Provide cached default country name. - * - * @param - * - * @return string - */ - public function defaultContactCountryName() { - static $cachedContactCountryName = NULL; - if (!$cachedContactCountryName && $this->defaultContactCountry) { - $countryCodes = CRM_Core_PseudoConstant::country(); - $cachedContactCountryName = $countryCodes[$this->defaultContactCountry]; - } - return $cachedContactCountryName; - } - - /** - * Provide cached country limit translated to names. - * - * @param - * - * @return array - */ - public function countryLimit() { - static $cachedCountryLimit = NULL; - if (!$cachedCountryLimit) { - $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode(); - $country = array(); - if (is_array($this->countryLimit)) { - foreach ($this->countryLimit as $val) { - // CRM-12007 - // some countries have disappeared and hence they might be in country limit - // but not in the country table - if (isset($countryIsoCodes[$val])) { - $country[] = $countryIsoCodes[$val]; - } - } - } - else { - $country[] = $countryIsoCodes[$this->countryLimit]; - } - $cachedCountryLimit = $country; - } - return $cachedCountryLimit; - } - - /** - * Provide cached province limit translated to names. - * - * @param - * - * @return array - */ - public function provinceLimit() { - static $cachedProvinceLimit = NULL; - if (!$cachedProvinceLimit) { - $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode(); - $country = array(); - if (is_array($this->provinceLimit)) { - foreach ($this->provinceLimit as $val) { - // CRM-12007 - // some countries have disappeared and hence they might be in country limit - // but not in the country table - if (isset($countryIsoCodes[$val])) { - $country[] = $countryIsoCodes[$val]; - } - } - } - else { - $country[] = $countryIsoCodes[$this->provinceLimit]; - } - $cachedProvinceLimit = $country; - } - return $cachedProvinceLimit; - } + public $inheritLocale = 0; } // end CRM_Core_Config diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index a0b2ecc626..3c126366df 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -707,10 +707,9 @@ class CRM_Core_PseudoConstant { public static function &stateProvince($id = FALSE, $limit = TRUE) { if (($id && !CRM_Utils_Array::value($id, self::$stateProvince)) || !self::$stateProvince || !$id) { $whereClause = FALSE; - $config = CRM_Core_Config::singleton(); if ($limit) { $countryIsoCodes = self::countryIsoCode(); - $limitCodes = $config->provinceLimit(); + $limitCodes = CRM_Core_BAO_Country::provinceLimit(); $limitIds = array(); foreach ($limitCodes as $code) { $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code)); @@ -779,9 +778,8 @@ WHERE id = %1"; $whereClause = FALSE; if ($limit) { - $config = CRM_Core_Config::singleton(); $countryIsoCodes = self::countryIsoCode(); - $limitCodes = $config->provinceLimit(); + $limitCodes = CRM_Core_BAO_Country::provinceLimit(); $limitIds = array(); foreach ($limitCodes as $code) { $tmpArray = array_keys($countryIsoCodes, $code); @@ -836,7 +834,7 @@ WHERE id = %1"; // limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT // (ensuring it's a subset of the legal values) // K/P: We need to fix this, i dont think it works with new setting files - $limitCodes = $config->countryLimit(); + $limitCodes = CRM_Core_BAO_Country::countryLimit(); if (!is_array($limitCodes)) { $limitCodes = array( $config->countryLimit => 1, diff --git a/settings/Localization.setting.php b/settings/Localization.setting.php index 4e57ffb6a4..326969b764 100644 --- a/settings/Localization.setting.php +++ b/settings/Localization.setting.php @@ -210,7 +210,7 @@ return array( 'multiple' => 1, 'class' => 'crm-select2', ), - 'default' => NULL, + 'default' => array('1228'), 'add' => '4.3', 'title' => 'Available Countries', 'is_domain' => 1, @@ -234,7 +234,7 @@ return array( 'multiple' => 1, 'class' => 'crm-select2', ), - 'default' => NULL, + 'default' => array('1228'), 'add' => '4.3', 'title' => 'Available States and Provinces', 'is_domain' => 1, -- 2.25.1