Commit | Line | Data |
---|---|---|
3a84c0ab TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
41498ac5 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
3a84c0ab | 5 | | | |
41498ac5 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
3a84c0ab TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | namespace Civi\Core; | |
13 | ||
14 | /** | |
15 | * Class SettingsBag | |
16 | * @package Civi\Core | |
17 | * | |
18 | * Read and write settings for a given domain (or contact). | |
19 | * | |
20 | * If the target entity does not already have a value for the setting, then | |
21 | * the defaults will be used. If mandatory values are provided, they will | |
22 | * override any defaults or custom settings. | |
23 | * | |
24 | * It's expected that the SettingsBag will have O(50-250) settings -- and that | |
25 | * we'll load the full bag on many page requests. Consequently, we don't | |
26 | * want the full metadata (help text and version history and HTML widgets) | |
27 | * for all 250 settings, but we do need the default values. | |
28 | * | |
29 | * This class is not usually instantiated directly. Instead, use SettingsManager | |
30 | * or Civi::settings(). | |
31 | * | |
32 | * @see \Civi::settings() | |
33 | * @see SettingsManagerTest | |
34 | */ | |
35 | class SettingsBag { | |
36 | ||
37 | protected $domainId; | |
38 | ||
39 | protected $contactId; | |
40 | ||
41 | /** | |
42 | * @var array | |
43 | * Array(string $settingName => mixed $value). | |
44 | */ | |
45 | protected $defaults; | |
46 | ||
47 | /** | |
48 | * @var array | |
49 | * Array(string $settingName => mixed $value). | |
50 | */ | |
51 | protected $mandatory; | |
52 | ||
53 | /** | |
54 | * The result of combining default values, mandatory | |
55 | * values, and user values. | |
56 | * | |
cc101011 | 57 | * @var array|null |
3a84c0ab TO |
58 | * Array(string $settingName => mixed $value). |
59 | */ | |
60 | protected $combined; | |
61 | ||
62 | /** | |
63 | * @var array | |
64 | */ | |
65 | protected $values; | |
66 | ||
67 | /** | |
68 | * @param int $domainId | |
69 | * The domain for which we want settings. | |
e97c66ff | 70 | * @param int|null $contactId |
3a84c0ab | 71 | * The contact for which we want settings. Use NULL for domain settings. |
5dbaf8de TO |
72 | */ |
73 | public function __construct($domainId, $contactId) { | |
74 | $this->domainId = $domainId; | |
75 | $this->contactId = $contactId; | |
c64f69d9 | 76 | $this->values = []; |
5dbaf8de TO |
77 | $this->combined = NULL; |
78 | } | |
79 | ||
80 | /** | |
81 | * Set/replace the default values. | |
82 | * | |
3a84c0ab TO |
83 | * @param array $defaults |
84 | * Array(string $settingName => mixed $value). | |
4b350175 | 85 | * @return SettingsBag |
5dbaf8de TO |
86 | */ |
87 | public function loadDefaults($defaults) { | |
88 | $this->defaults = $defaults; | |
5dbaf8de TO |
89 | $this->combined = NULL; |
90 | return $this; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Set/replace the mandatory values. | |
95 | * | |
3a84c0ab TO |
96 | * @param array $mandatory |
97 | * Array(string $settingName => mixed $value). | |
4b350175 | 98 | * @return SettingsBag |
3a84c0ab | 99 | */ |
5dbaf8de | 100 | public function loadMandatory($mandatory) { |
3a84c0ab TO |
101 | $this->mandatory = $mandatory; |
102 | $this->combined = NULL; | |
5dbaf8de | 103 | return $this; |
3a84c0ab TO |
104 | } |
105 | ||
106 | /** | |
5dbaf8de | 107 | * Load all explicit settings that apply to this domain or contact. |
3a84c0ab | 108 | * |
4b350175 | 109 | * @return SettingsBag |
3a84c0ab | 110 | */ |
5dbaf8de | 111 | public function loadValues() { |
f806379b | 112 | // Note: Don't use DAO child classes. They require fields() which require |
5dbaf8de | 113 | // translations -- which are keyed off settings! |
f806379b | 114 | |
c64f69d9 | 115 | $this->values = []; |
3a84c0ab | 116 | $this->combined = NULL; |
f806379b TO |
117 | |
118 | // Ordinarily, we just load values from `civicrm_setting`. But upgrades require care. | |
119 | // In v4.0 and earlier, all values were stored in `civicrm_domain.config_backend`. | |
120 | // In v4.1-v4.6, values were split between `civicrm_domain` and `civicrm_setting`. | |
121 | // In v4.7+, all values are stored in `civicrm_setting`. | |
122 | // Whenever a value is available in civicrm_setting, it will take precedence. | |
123 | ||
124 | $isUpgradeMode = \CRM_Core_Config::isUpgradeMode(); | |
125 | ||
eed7e803 | 126 | if ($isUpgradeMode && empty($this->contactId) && \CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_domain', 'config_backend', FALSE)) { |
f806379b | 127 | $config_backend = \CRM_Core_DAO::singleValueQuery('SELECT config_backend FROM civicrm_domain WHERE id = %1', |
c64f69d9 | 128 | [1 => [$this->domainId, 'Positive']]); |
f806379b TO |
129 | $oldSettings = \CRM_Upgrade_Incremental_php_FourSeven::convertBackendToSettings($this->domainId, $config_backend); |
130 | \CRM_Utils_Array::extend($this->values, $oldSettings); | |
131 | } | |
132 | ||
133 | // Normal case. Aside: Short-circuit prevents unnecessary query. | |
134 | if (!$isUpgradeMode || \CRM_Core_DAO::checkTableExists('civicrm_setting')) { | |
135 | $dao = \CRM_Core_DAO::executeQuery($this->createQuery()->toSQL()); | |
136 | while ($dao->fetch()) { | |
f24846d5 | 137 | $this->values[$dao->name] = ($dao->value !== NULL) ? \CRM_Utils_String::unserialize($dao->value) : NULL; |
f806379b | 138 | } |
ff6f993e | 139 | $dao->values['contribution_invoice_settings'] = $this->getContributionSettings(); |
f806379b TO |
140 | } |
141 | ||
3a84c0ab TO |
142 | return $this; |
143 | } | |
144 | ||
145 | /** | |
146 | * Add a batch of settings. Save them. | |
147 | * | |
148 | * @param array $settings | |
149 | * Array(string $settingName => mixed $settingValue). | |
4b350175 | 150 | * @return SettingsBag |
3a84c0ab TO |
151 | */ |
152 | public function add(array $settings) { | |
153 | foreach ($settings as $key => $value) { | |
154 | $this->set($key, $value); | |
155 | } | |
156 | return $this; | |
157 | } | |
158 | ||
159 | /** | |
160 | * Get a list of all effective settings. | |
161 | * | |
162 | * @return array | |
163 | * Array(string $settingName => mixed $settingValue). | |
164 | */ | |
165 | public function all() { | |
166 | if ($this->combined === NULL) { | |
167 | $this->combined = $this->combine( | |
c64f69d9 | 168 | [$this->defaults, $this->values, $this->mandatory] |
3a84c0ab TO |
169 | ); |
170 | } | |
1807fca4 | 171 | $this->combined['contribution_invoice_settings'] = $this->getContributionSettings(); |
3a84c0ab TO |
172 | return $this->combined; |
173 | } | |
174 | ||
175 | /** | |
176 | * Determine the effective value. | |
177 | * | |
178 | * @param string $key | |
179 | * @return mixed | |
180 | */ | |
181 | public function get($key) { | |
182 | $all = $this->all(); | |
2e1f50d6 | 183 | return $all[$key] ?? NULL; |
3a84c0ab TO |
184 | } |
185 | ||
1a6ba7d4 TO |
186 | /** |
187 | * Determine the default value of a setting. | |
188 | * | |
189 | * @param string $key | |
190 | * The simple name of the setting. | |
191 | * @return mixed|NULL | |
192 | */ | |
193 | public function getDefault($key) { | |
2e1f50d6 | 194 | return $this->defaults[$key] ?? NULL; |
1a6ba7d4 TO |
195 | } |
196 | ||
3a84c0ab TO |
197 | /** |
198 | * Determine the explicitly designated value, regardless of | |
199 | * any default or mandatory values. | |
200 | * | |
201 | * @param string $key | |
1a6ba7d4 TO |
202 | * The simple name of the setting. |
203 | * @return mixed|NULL | |
3a84c0ab TO |
204 | */ |
205 | public function getExplicit($key) { | |
2e1f50d6 | 206 | return ($this->values[$key] ?? NULL); |
3a84c0ab TO |
207 | } |
208 | ||
1a6ba7d4 TO |
209 | /** |
210 | * Determine the mandatory value of a setting. | |
211 | * | |
212 | * @param string $key | |
213 | * The simple name of the setting. | |
214 | * @return mixed|NULL | |
215 | */ | |
216 | public function getMandatory($key) { | |
2e1f50d6 | 217 | return $this->mandatory[$key] ?? NULL; |
1a6ba7d4 TO |
218 | } |
219 | ||
3a84c0ab TO |
220 | /** |
221 | * Determine if the entity has explicitly designated a value. | |
222 | * | |
223 | * Note that get() may still return other values based on | |
224 | * mandatory values or defaults. | |
225 | * | |
226 | * @param string $key | |
1a6ba7d4 | 227 | * The simple name of the setting. |
3a84c0ab TO |
228 | * @return bool |
229 | */ | |
230 | public function hasExplict($key) { | |
231 | // NULL means no designated value. | |
232 | return isset($this->values[$key]); | |
233 | } | |
234 | ||
235 | /** | |
236 | * Removes any explicit settings. This restores the default. | |
237 | * | |
238 | * @param string $key | |
1a6ba7d4 | 239 | * The simple name of the setting. |
4b350175 | 240 | * @return SettingsBag |
3a84c0ab TO |
241 | */ |
242 | public function revert($key) { | |
243 | // It might be better to DELETE (to avoid long-term leaks), | |
244 | // but setting NULL is simpler for now. | |
245 | return $this->set($key, NULL); | |
246 | } | |
247 | ||
248 | /** | |
249 | * Add a single setting. Save it. | |
250 | * | |
251 | * @param string $key | |
1a6ba7d4 | 252 | * The simple name of the setting. |
3a84c0ab | 253 | * @param mixed $value |
1a6ba7d4 | 254 | * The new, explicit value of the setting. |
4b350175 | 255 | * @return SettingsBag |
3a84c0ab TO |
256 | */ |
257 | public function set($key, $value) { | |
ff6f993e | 258 | if ($key === 'contribution_invoice_settings') { |
259 | $this->setContributionSettings($value); | |
260 | return $this; | |
261 | } | |
3a84c0ab TO |
262 | $this->setDb($key, $value); |
263 | $this->values[$key] = $value; | |
264 | $this->combined = NULL; | |
265 | return $this; | |
266 | } | |
267 | ||
ff6f993e | 268 | /** |
269 | * Temporary handling for phasing out contribution_invoice_settings. | |
270 | * | |
271 | * Until we have transitioned we need to handle setting & retrieving | |
272 | * contribution_invoice_settings. | |
273 | * | |
274 | * Once removed from core we will add deprecation notices & then remove this. | |
275 | * | |
276 | * https://lab.civicrm.org/dev/core/issues/1558 | |
277 | * | |
278 | * @param array $value | |
279 | */ | |
280 | public function setContributionSettings($value) { | |
281 | foreach (SettingsBag::getContributionInvoiceSettingKeys() as $possibleKeyName => $settingName) { | |
282 | $keyValue = $value[$possibleKeyName] ?? ''; | |
283 | $this->set($settingName, $keyValue); | |
284 | } | |
285 | $this->values['contribution_invoice_settings'] = $this->getContributionSettings(); | |
286 | } | |
287 | ||
288 | /** | |
289 | * Temporary function to handle returning the contribution_settings key despite it being deprecated. | |
290 | * | |
291 | * See more in comment block on previous function. | |
292 | * | |
293 | * @return array | |
294 | */ | |
295 | public function getContributionSettings() { | |
296 | $contributionSettings = []; | |
297 | foreach (SettingsBag::getContributionInvoiceSettingKeys() as $keyName => $settingName) { | |
298 | $contributionSettings[$keyName] = $this->values[$settingName] ?? ''; | |
299 | } | |
300 | return $contributionSettings; | |
301 | } | |
302 | ||
3a84c0ab | 303 | /** |
5dbaf8de | 304 | * @return \CRM_Utils_SQL_Select |
3a84c0ab | 305 | */ |
5dbaf8de TO |
306 | protected function createQuery() { |
307 | $select = \CRM_Utils_SQL_Select::from('civicrm_setting') | |
f806379b | 308 | ->select('id, name, value, domain_id, contact_id, is_domain, component_id, created_date, created_id') |
c64f69d9 | 309 | ->where('domain_id = #id', [ |
5dbaf8de | 310 | 'id' => $this->domainId, |
c64f69d9 | 311 | ]); |
3a84c0ab | 312 | if ($this->contactId === NULL) { |
5dbaf8de | 313 | $select->where('is_domain = 1'); |
3a84c0ab TO |
314 | } |
315 | else { | |
c64f69d9 | 316 | $select->where('contact_id = #id', [ |
5dbaf8de | 317 | 'id' => $this->contactId, |
c64f69d9 | 318 | ]); |
5dbaf8de | 319 | $select->where('is_domain = 0'); |
3a84c0ab | 320 | } |
5dbaf8de | 321 | return $select; |
3a84c0ab TO |
322 | } |
323 | ||
324 | /** | |
325 | * Combine a series of arrays, excluding any | |
326 | * null values. Later values override earlier | |
327 | * values. | |
328 | * | |
1a6ba7d4 TO |
329 | * @param array $arrays |
330 | * List of arrays to combine. | |
3a84c0ab TO |
331 | * @return array |
332 | */ | |
333 | protected function combine($arrays) { | |
c64f69d9 | 334 | $combined = []; |
3a84c0ab TO |
335 | foreach ($arrays as $array) { |
336 | foreach ($array as $k => $v) { | |
337 | if ($v !== NULL) { | |
338 | $combined[$k] = $v; | |
339 | } | |
340 | } | |
341 | } | |
342 | return $combined; | |
343 | } | |
344 | ||
345 | /** | |
055f6c27 TO |
346 | * Update the DB record for this setting. |
347 | * | |
1a6ba7d4 TO |
348 | * @param string $name |
349 | * The simple name of the setting. | |
350 | * @param mixed $value | |
351 | * The new value of the setting. | |
3a84c0ab TO |
352 | */ |
353 | protected function setDb($name, $value) { | |
c64f69d9 CW |
354 | $fields = []; |
355 | $fieldsToSet = \CRM_Core_BAO_Setting::validateSettingsInput([$name => $value], $fields); | |
3a84c0ab TO |
356 | //We haven't traditionally validated inputs to setItem, so this breaks things. |
357 | //foreach ($fieldsToSet as $settingField => &$settingValue) { | |
358 | // self::validateSetting($settingValue, $fields['values'][$settingField]); | |
359 | //} | |
055f6c27 TO |
360 | |
361 | $metadata = $fields['values'][$name]; | |
362 | ||
363 | $dao = new \CRM_Core_DAO_Setting(); | |
364 | $dao->name = $name; | |
365 | $dao->domain_id = $this->domainId; | |
366 | if ($this->contactId) { | |
367 | $dao->contact_id = $this->contactId; | |
368 | $dao->is_domain = 0; | |
369 | } | |
370 | else { | |
371 | $dao->is_domain = 1; | |
372 | } | |
373 | $dao->find(TRUE); | |
055f6c27 | 374 | |
38e5457e TO |
375 | // Call 'on_change' listeners. It would be nice to only fire when there's |
376 | // a genuine change in the data. However, PHP developers have mixed | |
377 | // expectations about whether 0, '0', '', NULL, and FALSE represent the same | |
378 | // value, so there's no universal way to determine if a change is genuine. | |
379 | if (isset($metadata['on_change'])) { | |
055f6c27 TO |
380 | foreach ($metadata['on_change'] as $callback) { |
381 | call_user_func( | |
382 | \Civi\Core\Resolver::singleton()->get($callback), | |
f24846d5 | 383 | \CRM_Utils_String::unserialize($dao->value), |
055f6c27 TO |
384 | $value, |
385 | $metadata, | |
386 | $this->domainId | |
387 | ); | |
388 | } | |
389 | } | |
390 | ||
bf322c68 | 391 | if (!is_array($value) && \CRM_Utils_System::isNull($value)) { |
055f6c27 TO |
392 | $dao->value = 'null'; |
393 | } | |
394 | else { | |
395 | $dao->value = serialize($value); | |
396 | } | |
397 | ||
8ff759c4 C |
398 | if (!isset(\Civi::$statics[__CLASS__]['upgradeMode'])) { |
399 | \Civi::$statics[__CLASS__]['upgradeMode'] = \CRM_Core_Config::isUpgradeMode(); | |
400 | } | |
eed7e803 | 401 | if (\Civi::$statics[__CLASS__]['upgradeMode'] && \CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_setting', 'group_name')) { |
8ff759c4 C |
402 | $dao->group_name = 'placeholder'; |
403 | } | |
404 | ||
9ece35cc | 405 | $dao->created_date = \CRM_Utils_Time::getTime('YmdHis'); |
055f6c27 TO |
406 | |
407 | $session = \CRM_Core_Session::singleton(); | |
408 | if (\CRM_Contact_BAO_Contact_Utils::isContactId($session->get('userID'))) { | |
409 | $dao->created_id = $session->get('userID'); | |
410 | } | |
411 | ||
867a532b TO |
412 | if ($dao->id) { |
413 | $dao->save(); | |
414 | } | |
415 | else { | |
416 | // Cannot use $dao->save(); in upgrade mode (eg WP + Civi 4.4=>4.7), the DAO will refuse | |
417 | // to save the field `group_name`, which is required in older schema. | |
418 | \CRM_Core_DAO::executeQuery(\CRM_Utils_SQL_Insert::dao($dao)->toSQL()); | |
419 | } | |
3a84c0ab TO |
420 | } |
421 | ||
ff6f993e | 422 | /** |
423 | * @return array | |
424 | */ | |
425 | public static function getContributionInvoiceSettingKeys(): array { | |
426 | $convertedKeys = [ | |
427 | 'credit_notes_prefix' => 'credit_notes_prefix', | |
428 | 'invoice_prefix' => 'invoice_prefix', | |
429 | 'due_date' => 'invoice_due_date', | |
430 | 'due_date_period' => 'invoice_due_date_period', | |
431 | 'notes' => 'invoice_notes', | |
432 | 'is_email_pdf' => 'invoice_is_email_pdf', | |
433 | 'tax_term' => 'tax_term', | |
434 | 'tax_display_settings' => 'tax_display_settings', | |
435 | 'invoicing' => 'invoicing', | |
436 | ]; | |
437 | return $convertedKeys; | |
438 | } | |
439 | ||
3a84c0ab | 440 | } |