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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 namespace Civi\Api4\Action\Setting
;
24 use Civi\Api4\Generic\Result
;
27 * Base class for setting actions.
29 * @method int getDomainId
30 * @method $this setDomainId(int $domainId)
32 abstract class AbstractSettingAction
extends \Civi\Api4\Generic\AbstractAction
{
35 * Domain id of setting. Leave NULL for default domain.
37 * @var int|string|array
42 * Contact - if this is a contact-related setting.
48 public function _run(Result
$result) {
51 foreach ($this->domainId
as $domain) {
52 $meta[$domain] = $this->validateSettings($domain);
54 foreach ($this->domainId
as $domain) {
55 $settingsBag = $this->contactId ? \Civi
::contactSettings($this->contactId
, $domain) : \Civi
::settings($domain);
56 $this->processSettings($result, $settingsBag, $meta[$domain], $domain);
61 * Checks that really ought to be taken care of by `Civi::settings`.
65 * @throws \API_Exception
67 protected function validateSettings($domain) {
68 $meta = \Civi\Core\SettingsMetadata
::getMetadata([], $domain);
69 $names = isset($this->values
) ?
array_keys($this->values
) : $this->select
;
70 $invalid = array_diff($names, array_keys($meta));
72 throw new \
API_Exception("Unknown settings for domain $domain: " . implode(', ', $invalid));
74 if (isset($this->values
)) {
75 foreach ($this->values
as $name => &$value) {
76 \CRM_Core_BAO_Setting
::validateSetting($value, $meta[$name]);
82 protected function findDomains() {
83 if ($this->domainId
== 'all') {
84 $this->domainId
= Domain
::get()->setCheckPermissions(FALSE)->addSelect('id')->execute()->column('id');
86 elseif ($this->domainId
) {
87 $this->domainId
= (array) $this->domainId
;
88 $domains = Domain
::get()->setCheckPermissions(FALSE)->addSelect('id')->execute()->column('id');
89 $invalid = array_diff($this->domainId
, $domains);
91 throw new \
API_Exception('Invalid domain id: ' . implode(', ', $invalid));
95 $this->domainId
= [\CRM_Core_Config
::domainID()];