CRM-14974 , worked on coding standard
[civicrm-core.git] / CRM / Core / BAO / Setting.php
index f42e66c6f98d669ea54e30e687285f4204205970..c8cfb25d2960284b88408e814f5235e18cdeee80 100644 (file)
@@ -44,7 +44,7 @@
 class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
 
   /**
-   * Various predefined settings that have been migrated to the setting table
+   * Various predefined settings that have been migrated to the setting table.
    */
   const
     ADDRESS_STANDARDIZATION_PREFERENCES_NAME = 'Address Standardization Preferences',
@@ -115,7 +115,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
   }
 
   /**
-   * Allow key o be cleared
+   * Allow key o be cleared.
    * @param string $cacheKey
    */
   public static function flushCache($cacheKey) {
@@ -201,7 +201,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
   }
 
   /**
-   * Retrieve the value of a setting from the DB table
+   * Retrieve the value of a setting from the DB table.
    *
    * @param string $group
    *   (required) The group name of the item.
@@ -228,14 +228,29 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
     $domainID = NULL
   ) {
 
+    $override_group = array();
     if (NULL !== ($override = self::getOverride($group, $name, NULL))) {
-      return $override;
+      if (isset($name)) {
+        return $override;
+      }
+      else {
+        $override_group = $override;
+      }
     }
 
     if (empty($domainID)) {
       $domainID = CRM_Core_Config::domainID();
     }
     $cacheKey = self::inCache($group, $name, $componentID, $contactID, TRUE, $domainID);
+
+    if ($group && !isset($name) && $cacheKey) {
+      // check value against the cache, and unset key if values are different
+      $valueDifference = array_diff($override_group, self::$_cache[$cacheKey]);
+      if (!empty($valueDifference)) {
+        $cacheKey = '';
+      }
+    }
+
     if (!$cacheKey) {
       $dao = self::dao($group, NULL, $componentID, $contactID, $domainID);
       $dao->find();
@@ -254,21 +269,30 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
       }
       $dao->free();
 
+      if (!isset($name)) {
+        // merge db and override group values
+        // When no $name is present, the getItem() function should return an array
+        // consisting of the sum of all override settings + all settings present in
+        // the database for the given $group (with the overrides taking precedence,
+        // and applying even if the setting is not defined in the database).
+        //
+        $values = array_merge($values, $override_group);
+      }
+
       $cacheKey = self::setCache($values, $group, $componentID, $contactID, $domainID);
     }
     return $name ? CRM_Utils_Array::value($name, self::$_cache[$cacheKey], $defaultValue) : self::$_cache[$cacheKey];
   }
 
   /**
-   * Store multiple items in the setting table
+   * Store multiple items in the setting table.
    *
    * @param array $params
    *   (required) An api formatted array of keys and values.
-   * @param null $domains
+   * @param array $domains Array of domains to get settings for. Default is the current domain
    * @param $settingsToReturn
    *
-   * @domains array an array of domains to get settings for. Default is the current domain
-   * @return void
+   * @return array
    */
   public static function getItems(&$params, $domains = NULL, $settingsToReturn) {
     $originalDomain = CRM_Core_Config::domainID();
@@ -325,7 +349,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
   }
 
   /**
-   * Store an item in the setting table
+   * Store an item in the setting table.
    *
    * _setItem() is the common logic shared by setItem() and setItems().
    *
@@ -365,7 +389,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
   }
 
   /**
-   * Store an item in a setting table
+   * Store an item in a setting table.
    *
    * _setItem() is the common logic shared by setItem() and setItems().
    *
@@ -690,7 +714,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
   }
 
   /**
-   * Load the settings files defined in a series of folders
+   * Load the settings files defined in a series of folders.
    * @param array $metaDataFolders
    *   List of folder paths.
    * @return array
@@ -709,7 +733,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
   }
 
   /**
-   * Load up settings metadata from files
+   * Load up settings metadata from files.
    */
   public static function loadSettingsMetadata($metaDataFolder) {
     $settingMetaData = array();
@@ -750,7 +774,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting {
   }
 
   /**
-   * Look for any missing settings and convert them from config or load default as appropriate
+   * Look for any missing settings and convert them from config or load default as appropriate.
    * This should be run from GenCode & also from upgrades to add any new defaults.
    *
    * Multisites have often been overlooked in upgrade scripts so can be expected to be missing
@@ -1102,6 +1126,9 @@ AND domain_id = %3
     if ($group && $name && isset($civicrm_setting[$group][$name])) {
       return $civicrm_setting[$group][$name];
     }
+    elseif ($group && !isset($name) && isset($civicrm_setting[$group])) {
+      return $civicrm_setting[$group];
+    }
     else {
       return $default;
     }