4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Action\Setting
;
16 use Civi\Api4\Generic\Result
;
19 * Base class for setting actions.
21 * @method int getDomainId
22 * @method $this setDomainId(int $domainId)
24 abstract class AbstractSettingAction
extends \Civi\Api4\Generic\AbstractAction
{
27 * Domain id of setting. Leave NULL for default domain.
29 * @var int|string|array
34 * Contact - if this is a contact-related setting.
40 public function _run(Result
$result) {
43 foreach ($this->domainId
as $domain) {
44 $meta[$domain] = $this->validateSettings($domain);
46 foreach ($this->domainId
as $domain) {
47 $settingsBag = $this->contactId ? \Civi
::contactSettings($this->contactId
, $domain) : \Civi
::settings($domain);
48 $this->processSettings($result, $settingsBag, $meta[$domain], $domain);
53 * Checks that really ought to be taken care of by `Civi::settings`.
57 * @throws \API_Exception
59 protected function validateSettings($domain) {
60 $meta = \Civi\Core\SettingsMetadata
::getMetadata([], $domain);
61 $names = isset($this->values
) ?
array_keys($this->values
) : $this->select
;
62 $invalid = array_diff($names, array_keys($meta));
64 throw new \
API_Exception("Unknown settings for domain $domain: " . implode(', ', $invalid));
66 if (isset($this->values
)) {
67 foreach ($this->values
as $name => &$value) {
68 \CRM_Core_BAO_Setting
::validateSetting($value, $meta[$name]);
74 protected function findDomains() {
75 if ($this->domainId
== 'all') {
76 $this->domainId
= Domain
::get(FALSE)->addSelect('id')->execute()->column('id');
78 elseif ($this->domainId
) {
79 $this->domainId
= (array) $this->domainId
;
80 $domains = Domain
::get(FALSE)->addSelect('id')->execute()->column('id');
81 $invalid = array_diff($this->domainId
, $domains);
83 throw new \
API_Exception('Invalid domain id: ' . implode(', ', $invalid));
87 $this->domainId
= [\CRM_Core_Config
::domainID()];