3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * Class SettingsManager
34 * @see SettingsManagerTest
36 class SettingsManager
{
39 * @var \CRM_Utils_Cache_Interface
45 * Array (int $id => SettingsBag $bag).
47 protected $bagsByDomain = array(), $bagsByContact = array();
52 protected $mandatory = NULL;
55 * @param \CRM_Utils_Cache_Interface $cache
56 * @param NULL|array $mandatory
58 public function __construct($cache, $mandatory = NULL) {
59 $this->cache
= $cache;
60 $this->mandatory
= $mandatory;
64 * @param int $domainId
67 public function getBagByDomain($domainId) {
68 if ($domainId === NULL) {
69 $domainId = \CRM_Core_Config
::domainID();
72 if (!isset($this->bagsByDomain
[$domainId])) {
73 $defaults = $this->getDefaults('domain');
74 // Filter $mandatory to only include domain-settings.
75 $mandatory = \CRM_Utils_Array
::subset($this->getMandatory(), array_keys($defaults));
76 $this->bagsByDomain
[$domainId] = new SettingsBag($domainId, NULL, $defaults, $mandatory);
77 $this->bagsByDomain
[$domainId]->load();
79 return $this->bagsByDomain
[$domainId];
83 * @param int $domainId
84 * @param int $contactId
87 public function getBagByContact($domainId, $contactId) {
88 if ($domainId === NULL) {
89 $domainId = \CRM_Core_Config
::domainID();
92 $key = "$domainId:$contactId";
93 if (!isset($this->bagsByContact
[$key])) {
94 $defaults = $this->getDefaults('contact');
95 // Filter $mandatory to only include domain-settings.
96 $mandatory = \CRM_Utils_Array
::subset($this->getMandatory(), array_keys($defaults));
97 $this->bagsByContact
[$key] = new SettingsBag($domainId, $contactId, $defaults, $mandatory);
98 $this->bagsByContact
[$key]->load();
100 return $this->bagsByContact
[$key];
104 * Determine the default settings.
106 * @param string $entity
107 * Ex: 'domain' or 'contact'.
109 * Array(string $settingName => mixed $value).
111 public function getDefaults($entity) {
112 $cacheKey = 'defaults:' . $entity;
113 $defaults = $this->cache
->get($cacheKey);
114 if (!is_array($defaults)) {
115 $specs = \CRM_Core_BAO_Setting
::getSettingSpecification(NULL, array(
116 'is_contact' => ($entity === 'contact' ?
1 : 0),
119 foreach ($specs as $key => $spec) {
120 $defaults[$key] = \CRM_Utils_Array
::value('default', $spec);
122 $this->cache
->set($cacheKey, $defaults);
128 * Get a list of mandatory/overriden settings.
131 * Array(string $settingName => mixed $value).
133 public function getMandatory() {
134 if ($this->mandatory
=== NULL) {
135 if (isset($GLOBALS['civicrm_setting'])) {
136 $this->mandatory
= self
::parseMandatorySettings($GLOBALS['civicrm_setting']);
139 $this->mandatory
= array();
142 return $this->mandatory
;
148 * @param array $civicrm_setting
149 * Ex: $civicrm_setting['Group Name']['field'] = 'value'.
150 * Group names are an historical quirk; ignore them.
153 public static function parseMandatorySettings($civicrm_setting) {
155 if (is_array($civicrm_setting)) {
156 foreach ($civicrm_setting as $group => $settings) {
157 foreach ($settings as $k => $v) {