Use markdown in php docblocks & display in APIv4 Explorer
[civicrm-core.git] / Civi / Api4 / Action / Setting / AbstractSettingAction.php
CommitLineData
19b53e5b 1<?php
380f3545
TO
2
3/*
4 +--------------------------------------------------------------------+
41498ac5 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
41498ac5
TO
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 |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545
TO
17 * $Id$
18 *
19 */
20
19b53e5b
C
21namespace Civi\Api4\Action\Setting;
22
23use Civi\Api4\Domain;
24use Civi\Api4\Generic\Result;
25
26/**
27 * Base class for setting actions.
28 *
29 * @method int getDomainId
30 * @method $this setDomainId(int $domainId)
31 */
32abstract class AbstractSettingAction extends \Civi\Api4\Generic\AbstractAction {
33
34 /**
35 * Domain id of setting. Leave NULL for default domain.
36 *
37 * @var int|string|array
38 */
39 protected $domainId;
40
41 /**
42 * Contact - if this is a contact-related setting.
43 *
44 * @var int
45 */
46 protected $contactId;
47
48 public function _run(Result $result) {
49 $this->findDomains();
50 $meta = [];
51 foreach ($this->domainId as $domain) {
52 $meta[$domain] = $this->validateSettings($domain);
53 }
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);
57 }
58 }
59
60 /**
136ca5bb 61 * Checks that really ought to be taken care of by `Civi::settings`.
19b53e5b
C
62 *
63 * @param int $domain
64 * @return array
65 * @throws \API_Exception
66 */
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));
71 if ($invalid) {
72 throw new \API_Exception("Unknown settings for domain $domain: " . implode(', ', $invalid));
73 }
74 if (isset($this->values)) {
75 foreach ($this->values as $name => &$value) {
76 \CRM_Core_BAO_Setting::validateSetting($value, $meta[$name]);
77 }
78 }
79 return $meta;
80 }
81
82 protected function findDomains() {
83 if ($this->domainId == 'all') {
84 $this->domainId = Domain::get()->setCheckPermissions(FALSE)->addSelect('id')->execute()->column('id');
85 }
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);
90 if ($invalid) {
91 throw new \API_Exception('Invalid domain id: ' . implode(', ', $invalid));
92 }
93 }
94 else {
95 $this->domainId = [\CRM_Core_Config::domainID()];
96 }
97 }
98
99}