CRM-16373 - CRM_Core_Config_Variables should be strictly about data
authorTim Otten <totten@civicrm.org>
Thu, 10 Sep 2015 21:38:55 +0000 (14:38 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 17 Sep 2015 22:49:29 +0000 (15:49 -0700)
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
CRM/Core/BAO/Country.php [new file with mode: 0644]
CRM/Core/Block.php
CRM/Core/Config.php
CRM/Core/Config/Variables.php
CRM/Core/PseudoConstant.php
settings/Localization.setting.php

index fcfd43b3311729e0469555c5e59ac9bf2e13bcd1..c823bcd0a33a7fb458ade84c298e4b278e2efcd6 100644 (file)
@@ -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 (file)
index 0000000..56fc6cd
--- /dev/null
@@ -0,0 +1,153 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2015
+ * $Id$
+ *
+ */
+
+/**
+ * This class contains functions for managing Action Logs
+ */
+class CRM_Core_BAO_Country extends CRM_Core_DAO_Country {
+
+  /**
+   * Get the list of countries for which we offer provinces.
+   *
+   * @return mixed
+   */
+  public static function provinceLimit() {
+    if (!isset(Civi::$statics[__CLASS__]['provinceLimit'])) {
+      $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode();
+      $provinceLimit = Civi::settings()->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;
+  }
+
+}
index db6ad2e00de376c2739b9e3d9870a99d766327d5..c5f168ae071a202d595dc39a4f7be416df8a7fe2 100644 (file)
@@ -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;
     }
 
index 688c7dabd28fde1979170d02d3ee2daddc7efba9..ec10378a2900a50e2eb21b6bf6a127bb371ebd27 100644 (file)
@@ -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.
index 7cb80b9ea93e7c41549ae6573c3fbfc7934434c3..56d4e282c82018006f5d87373e3ba5bacdd47272 100644 (file)
  */
 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
index a0b2ecc6260b53f65915dbe69601d91730edc9f3..3c126366df080a7ca1708c3918496324e62625df 100644 (file)
@@ -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,
index 4e57ffb6a45d7ca55f42e78c7452755d5bfe0753..326969b7649132b81049fa6796f4934989c4cffc 100644 (file)
@@ -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,