Commit | Line | Data |
---|---|---|
19b53e5b | 1 | <?php |
380f3545 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 5 | | |
6 | +--------------------------------------------------------------------+ | |
7 | | Copyright CiviCRM LLC (c) 2004-2019 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
32 | * @copyright CiviCRM LLC (c) 2004-2019 | |
33 | * $Id$ | |
34 | * | |
35 | */ | |
36 | ||
19b53e5b C |
37 | namespace Civi\Api4\Action\Setting; |
38 | ||
39 | use Civi\Api4\Domain; | |
40 | use Civi\Api4\Generic\Result; | |
41 | ||
42 | /** | |
43 | * Base class for setting actions. | |
44 | * | |
45 | * @method int getDomainId | |
46 | * @method $this setDomainId(int $domainId) | |
47 | */ | |
48 | abstract class AbstractSettingAction extends \Civi\Api4\Generic\AbstractAction { | |
49 | ||
50 | /** | |
51 | * Domain id of setting. Leave NULL for default domain. | |
52 | * | |
53 | * @var int|string|array | |
54 | */ | |
55 | protected $domainId; | |
56 | ||
57 | /** | |
58 | * Contact - if this is a contact-related setting. | |
59 | * | |
60 | * @var int | |
61 | */ | |
62 | protected $contactId; | |
63 | ||
64 | public function _run(Result $result) { | |
65 | $this->findDomains(); | |
66 | $meta = []; | |
67 | foreach ($this->domainId as $domain) { | |
68 | $meta[$domain] = $this->validateSettings($domain); | |
69 | } | |
70 | foreach ($this->domainId as $domain) { | |
71 | $settingsBag = $this->contactId ? \Civi::contactSettings($this->contactId, $domain) : \Civi::settings($domain); | |
72 | $this->processSettings($result, $settingsBag, $meta[$domain], $domain); | |
73 | } | |
74 | } | |
75 | ||
76 | /** | |
77 | * Checks that really ought to be taken care of by Civi::settings | |
78 | * | |
79 | * @param int $domain | |
80 | * @return array | |
81 | * @throws \API_Exception | |
82 | */ | |
83 | protected function validateSettings($domain) { | |
84 | $meta = \Civi\Core\SettingsMetadata::getMetadata([], $domain); | |
85 | $names = isset($this->values) ? array_keys($this->values) : $this->select; | |
86 | $invalid = array_diff($names, array_keys($meta)); | |
87 | if ($invalid) { | |
88 | throw new \API_Exception("Unknown settings for domain $domain: " . implode(', ', $invalid)); | |
89 | } | |
90 | if (isset($this->values)) { | |
91 | foreach ($this->values as $name => &$value) { | |
92 | \CRM_Core_BAO_Setting::validateSetting($value, $meta[$name]); | |
93 | } | |
94 | } | |
95 | return $meta; | |
96 | } | |
97 | ||
98 | protected function findDomains() { | |
99 | if ($this->domainId == 'all') { | |
100 | $this->domainId = Domain::get()->setCheckPermissions(FALSE)->addSelect('id')->execute()->column('id'); | |
101 | } | |
102 | elseif ($this->domainId) { | |
103 | $this->domainId = (array) $this->domainId; | |
104 | $domains = Domain::get()->setCheckPermissions(FALSE)->addSelect('id')->execute()->column('id'); | |
105 | $invalid = array_diff($this->domainId, $domains); | |
106 | if ($invalid) { | |
107 | throw new \API_Exception('Invalid domain id: ' . implode(', ', $invalid)); | |
108 | } | |
109 | } | |
110 | else { | |
111 | $this->domainId = [\CRM_Core_Config::domainID()]; | |
112 | } | |
113 | } | |
114 | ||
115 | } |