checksum_timeout - Use consistent naming
[civicrm-core.git] / Civi / Core / SettingsManager.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 namespace Civi\Core;
29
30 /**
31 * Class SettingsManager
32 * @package Civi\Core
33 *
34 * @see SettingsManagerTest
35 */
36 class SettingsManager {
37
38 /**
39 * @var \CRM_Utils_Cache_Interface
40 */
41 protected $cache;
42
43 /**
44 * @var
45 * Array (int $id => SettingsBag $bag).
46 */
47 protected $bagsByDomain = array(), $bagsByContact = array();
48
49 /**
50 * @var array
51 */
52 protected $mandatory = NULL;
53
54 /**
55 * @param \CRM_Utils_Cache_Interface $cache
56 * @param NULL|array $mandatory
57 */
58 public function __construct($cache, $mandatory = NULL) {
59 $this->cache = $cache;
60 $this->mandatory = $mandatory;
61 }
62
63 /**
64 * @param int $domainId
65 * @return SettingsBag
66 */
67 public function getBagByDomain($domainId) {
68 if ($domainId === NULL) {
69 $domainId = \CRM_Core_Config::domainID();
70 }
71
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();
78 }
79 return $this->bagsByDomain[$domainId];
80 }
81
82 /**
83 * @param int $domainId
84 * @param int $contactId
85 * @return SettingsBag
86 */
87 public function getBagByContact($domainId, $contactId) {
88 if ($domainId === NULL) {
89 $domainId = \CRM_Core_Config::domainID();
90 }
91
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();
99 }
100 return $this->bagsByContact[$key];
101 }
102
103 /**
104 * Determine the default settings.
105 *
106 * @param string $entity
107 * Ex: 'domain' or 'contact'.
108 * @return array
109 * Array(string $settingName => mixed $value).
110 */
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),
117 ));
118 $defaults = array();
119 foreach ($specs as $key => $spec) {
120 $defaults[$key] = \CRM_Utils_Array::value('default', $spec);
121 }
122 $this->cache->set($cacheKey, $defaults);
123 }
124 return $defaults;
125 }
126
127 /**
128 * Get a list of mandatory/overriden settings.
129 *
130 * @return array
131 * Array(string $settingName => mixed $value).
132 */
133 public function getMandatory() {
134 if ($this->mandatory === NULL) {
135 if (isset($GLOBALS['civicrm_setting'])) {
136 $this->mandatory = self::parseMandatorySettings($GLOBALS['civicrm_setting']);
137 }
138 else {
139 $this->mandatory = array();
140 }
141 }
142 return $this->mandatory;
143 }
144
145 /**
146 * Parse
147 *
148 * @param array $civicrm_setting
149 * Ex: $civicrm_setting['Group Name']['field'] = 'value'.
150 * Group names are an historical quirk; ignore them.
151 * @return array
152 */
153 public static function parseMandatorySettings($civicrm_setting) {
154 $tmp = array();
155 if (is_array($civicrm_setting)) {
156 foreach ($civicrm_setting as $group => $settings) {
157 foreach ($settings as $k => $v) {
158 if ($v !== NULL) {
159 $tmp[$k] = $v;
160 }
161 }
162 }
163 return $tmp;
164 }
165 return $tmp;
166 }
167
168 }