Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * BAO object for civicrm_setting table. This table is used to store civicrm settings that are not used | |
20 | * very frequently (i.e. not on every page load) | |
21 | * | |
22 | * The group column is used for grouping together all settings that logically belong to the same set. | |
23 | * Thus all settings in the same group are retrieved with one DB call and then cached for future needs. | |
6a488035 TO |
24 | */ |
25 | class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { | |
26 | ||
27 | /** | |
fe482240 | 28 | * Various predefined settings that have been migrated to the setting table. |
6a488035 | 29 | */ |
7da04cde | 30 | const |
6a488035 TO |
31 | ADDRESS_STANDARDIZATION_PREFERENCES_NAME = 'Address Standardization Preferences', |
32 | CAMPAIGN_PREFERENCES_NAME = 'Campaign Preferences', | |
5d0140d9 | 33 | DEVELOPER_PREFERENCES_NAME = 'Developer Preferences', |
6a488035 TO |
34 | DIRECTORY_PREFERENCES_NAME = 'Directory Preferences', |
35 | EVENT_PREFERENCES_NAME = 'Event Preferences', | |
36 | MAILING_PREFERENCES_NAME = 'Mailing Preferences', | |
1ec598f3 | 37 | MAP_PREFERENCES_NAME = 'Map Preferences', |
6a488035 TO |
38 | CONTRIBUTE_PREFERENCES_NAME = 'Contribute Preferences', |
39 | MEMBER_PREFERENCES_NAME = 'Member Preferences', | |
40 | MULTISITE_PREFERENCES_NAME = 'Multi Site Preferences', | |
41 | PERSONAL_PREFERENCES_NAME = 'Personal Preferences', | |
42 | SYSTEM_PREFERENCES_NAME = 'CiviCRM Preferences', | |
43 | URL_PREFERENCES_NAME = 'URL Preferences', | |
44 | LOCALIZATION_PREFERENCES_NAME = 'Localization Preferences', | |
45 | SEARCH_PREFERENCES_NAME = 'Search Preferences'; | |
6a488035 | 46 | |
6a488035 | 47 | /** |
fe482240 | 48 | * Retrieve the value of a setting from the DB table. |
6a488035 | 49 | * |
3fd42bb5 | 50 | * @param string|null $group |
73152f09 | 51 | * The group name of the item (deprecated). |
6a0b768e TO |
52 | * @param string $name |
53 | * (required) The name under which this item is stored. | |
54 | * @param int $componentID | |
55 | * The optional component ID (so componenets can share the same name space). | |
56 | * @param string $defaultValue | |
57 | * The default value to return for this setting if not present in DB. | |
58 | * @param int $contactID | |
59 | * If set, this is a contactID specific setting, else its a global setting. | |
da6b46f4 | 60 | * |
100fef9d | 61 | * @param int $domainID |
6a488035 | 62 | * |
72b3a70c CW |
63 | * @return mixed |
64 | * The data if present in the setting table, else null | |
6a488035 | 65 | */ |
2da40d21 | 66 | public static function getItem( |
56cb3188 | 67 | $group, |
242bd179 TO |
68 | $name = NULL, |
69 | $componentID = NULL, | |
6a488035 | 70 | $defaultValue = NULL, |
242bd179 TO |
71 | $contactID = NULL, |
72 | $domainID = NULL | |
6a488035 | 73 | ) { |
3a84c0ab TO |
74 | /** @var \Civi\Core\SettingsManager $manager */ |
75 | $manager = \Civi::service('settings_manager'); | |
76 | $settings = ($contactID === NULL) ? $manager->getBagByDomain($domainID) : $manager->getBagByContact($domainID, $contactID); | |
73152f09 | 77 | if ($name === NULL) { |
3bafd4a9 | 78 | CRM_Core_Error::deprecatedWarning("Deprecated: Group='$group'. Name should be provided.\n"); |
73152f09 JK |
79 | } |
80 | if ($componentID !== NULL) { | |
3bafd4a9 | 81 | CRM_Core_Error::deprecatedWarning("Deprecated: Group='$group'. Name='$name'. Component should be omitted\n"); |
73152f09 JK |
82 | } |
83 | if ($defaultValue !== NULL) { | |
3bafd4a9 | 84 | CRM_Core_Error::deprecatedWarning("Deprecated: Group='$group'. Name='$name'. Defaults should come from metadata\n"); |
6a488035 | 85 | } |
3a84c0ab | 86 | return $name ? $settings->get($name) : $settings->all(); |
6a488035 TO |
87 | } |
88 | ||
89 | /** | |
299cd62a | 90 | * Get multiple items from the setting table. |
6a488035 | 91 | * |
6a0b768e TO |
92 | * @param array $params |
93 | * (required) An api formatted array of keys and values. | |
35823763 | 94 | * @param array $domains Array of domains to get settings for. Default is the current domain |
dd244018 EM |
95 | * @param $settingsToReturn |
96 | * | |
35823763 | 97 | * @return array |
6a488035 | 98 | */ |
cf348a5e | 99 | public static function getItems(&$params, $domains, $settingsToReturn) { |
0e04f44e | 100 | $originalDomain = CRM_Core_Config::domainID(); |
6a488035 | 101 | if (empty($domains)) { |
0e04f44e | 102 | $domains[] = $originalDomain; |
6a488035 TO |
103 | } |
104 | if (!empty($settingsToReturn) && !is_array($settingsToReturn)) { | |
be2fb01f | 105 | $settingsToReturn = [$settingsToReturn]; |
6a488035 | 106 | } |
0e04f44e | 107 | |
be2fb01f | 108 | $fields = $result = []; |
6a488035 | 109 | $fieldsToGet = self::validateSettingsInput(array_flip($settingsToReturn), $fields, FALSE); |
7583c3f3 | 110 | foreach ($domains as $domainID) { |
be2fb01f | 111 | $result[$domainID] = []; |
6a488035 | 112 | foreach ($fieldsToGet as $name => $value) { |
9c1bc317 | 113 | $contactID = $params['contact_id'] ?? NULL; |
76bd16ab | 114 | $setting = CRM_Core_BAO_Setting::getItem(NULL, $name, NULL, NULL, $contactID, $domainID); |
6a488035 TO |
115 | if (!is_null($setting)) { |
116 | // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones | |
117 | // e.g for revert of fill actions | |
7583c3f3 | 118 | $result[$domainID][$name] = $setting; |
6a488035 TO |
119 | } |
120 | } | |
121 | } | |
122 | return $result; | |
123 | } | |
124 | ||
125 | /** | |
fe482240 | 126 | * Store an item in the setting table. |
6a488035 | 127 | * |
385d8c8e | 128 | * @param mixed $value |
6a0b768e TO |
129 | * (required) The value that will be serialized and stored. |
130 | * @param string $group | |
73152f09 | 131 | * The group name of the item (deprecated). |
6a0b768e TO |
132 | * @param string $name |
133 | * (required) The name of the setting. | |
134 | * @param int $componentID | |
135 | * The optional component ID (so componenets can share the same name space). | |
100fef9d | 136 | * @param int $contactID |
6a0b768e TO |
137 | * @param int $createdID |
138 | * An optional ID to assign the creator to. If not set, retrieved from session. | |
fd31fa4c | 139 | * |
100fef9d | 140 | * @param int $domainID |
c0e04a3c | 141 | * |
142 | * @throws \CRM_Core_Exception | |
143 | * | |
144 | * @deprecated - refer docs https://docs.civicrm.org/dev/en/latest/framework/setting/ | |
6a488035 | 145 | */ |
2da40d21 | 146 | public static function setItem( |
6a488035 TO |
147 | $value, |
148 | $group, | |
149 | $name, | |
150 | $componentID = NULL, | |
242bd179 TO |
151 | $contactID = NULL, |
152 | $createdID = NULL, | |
153 | $domainID = NULL | |
a57707d3 | 154 | ) { |
c0e04a3c | 155 | CRM_Core_Error::deprecatedFunctionWarning('refer docs for correct methods https://docs.civicrm.org/dev/en/latest/framework/setting/'); |
156 | ||
3a84c0ab TO |
157 | /** @var \Civi\Core\SettingsManager $manager */ |
158 | $manager = \Civi::service('settings_manager'); | |
159 | $settings = ($contactID === NULL) ? $manager->getBagByDomain($domainID) : $manager->getBagByContact($domainID, $contactID); | |
160 | $settings->set($name, $value); | |
a57707d3 TO |
161 | } |
162 | ||
6a488035 TO |
163 | /** |
164 | * Store multiple items in the setting table. Note that this will also store config keys | |
165 | * the storage is determined by the metdata and is affected by | |
166 | * 'name' setting's name | |
6a488035 TO |
167 | * 'config_key' = the config key is different to the settings key - e.g. debug where there was a conflict |
168 | * 'legacy_key' = rename from config or setting with this name | |
169 | * | |
6a0b768e TO |
170 | * @param array $params |
171 | * (required) An api formatted array of keys and values. | |
3fd42bb5 | 172 | * @param array|null $domains |
2a6da8d7 | 173 | * |
c8ee2f3f | 174 | * @throws API_Exception |
6a488035 | 175 | * @domains array an array of domains to get settings for. Default is the current domain |
3d0d359e | 176 | * @return array |
6a488035 | 177 | */ |
00be9182 | 178 | public static function setItems(&$params, $domains = NULL) { |
be2fb01f | 179 | $domains = empty($domains) ? [CRM_Core_Config::domainID()] : $domains; |
3a84c0ab TO |
180 | |
181 | // FIXME: redundant validation | |
182 | // FIXME: this whole thing should just be a loop to call $settings->add() on each domain. | |
183 | ||
be2fb01f | 184 | $fields = []; |
6a488035 TO |
185 | $fieldsToSet = self::validateSettingsInput($params, $fields); |
186 | ||
187 | foreach ($fieldsToSet as $settingField => &$settingValue) { | |
4cf5500c | 188 | if (empty($fields['values'][$settingField])) { |
31177cf4 | 189 | CRM_Core_Error::deprecatedWarning('Deprecated Path: There is a setting (' . $settingField . ') not correctly defined. You may see unpredictability due to this. CRM_Core_Setting::setItems'); |
be2fb01f | 190 | $fields['values'][$settingField] = []; |
4cf5500c | 191 | } |
6a488035 TO |
192 | self::validateSetting($settingValue, $fields['values'][$settingField]); |
193 | } | |
194 | ||
7583c3f3 | 195 | foreach ($domains as $domainID) { |
23bb9c85 TO |
196 | Civi::settings($domainID)->add($fieldsToSet); |
197 | $result[$domainID] = $fieldsToSet; | |
6a488035 TO |
198 | } |
199 | ||
200 | return $result; | |
201 | } | |
202 | ||
203 | /** | |
100fef9d | 204 | * Gets metadata about the settings fields (from getfields) based on the fields being passed in |
6a488035 TO |
205 | * |
206 | * This function filters on the fields like 'version' & 'debug' that are not settings | |
77b97be7 | 207 | * |
6a0b768e TO |
208 | * @param array $params |
209 | * Parameters as passed into API. | |
210 | * @param array $fields | |
211 | * Empty array to be populated with fields metadata. | |
6a488035 TO |
212 | * @param bool $createMode |
213 | * | |
c8ee2f3f | 214 | * @throws API_Exception |
a6c01b45 CW |
215 | * @return array |
216 | * name => value array of the fields to be set (with extraneous removed) | |
6a488035 | 217 | */ |
00be9182 | 218 | public static function validateSettingsInput($params, &$fields, $createMode = TRUE) { |
be2fb01f | 219 | $ignoredParams = [ |
6a488035 TO |
220 | 'version', |
221 | 'id', | |
222 | 'domain_id', | |
223 | 'debug', | |
224 | 'created_id', | |
225 | 'component_id', | |
226 | 'contact_id', | |
227 | 'filters', | |
228 | 'entity_id', | |
229 | 'entity_table', | |
230 | 'sequential', | |
231 | 'api.has_parent', | |
f704dce7 | 232 | 'IDS_request_uri', |
233 | 'IDS_user_agent', | |
234 | 'check_permissions', | |
80fbde47 | 235 | 'options', |
e56fd67f | 236 | 'prettyprint', |
2ed80443 FG |
237 | // CRM-18347: ignore params unintentionally passed by API explorer on WP |
238 | 'page', | |
239 | 'noheader', | |
240 | // CRM-18347: ignore params unintentionally passed by wp CLI tool | |
241 | '', | |
a3396545 FG |
242 | // CRM-19877: ignore params extraneously passed by Joomla |
243 | 'option', | |
244 | 'task', | |
be2fb01f | 245 | ]; |
6a488035 | 246 | $settingParams = array_diff_key($params, array_fill_keys($ignoredParams, TRUE)); |
be2fb01f | 247 | $getFieldsParams = ['version' => 3]; |
d3e86119 | 248 | if (count($settingParams) == 1) { |
6a488035 | 249 | // ie we are only setting one field - we'll pass it into getfields for efficiency |
385d8c8e | 250 | [$name] = array_keys($settingParams); |
6a488035 TO |
251 | $getFieldsParams['name'] = $name; |
252 | } | |
353ffa53 | 253 | $fields = civicrm_api3('setting', 'getfields', $getFieldsParams); |
6a488035 TO |
254 | $invalidParams = (array_diff_key($settingParams, $fields['values'])); |
255 | if (!empty($invalidParams)) { | |
c8ee2f3f | 256 | throw new API_Exception(implode(',', array_keys($invalidParams)) . " not valid settings"); |
6a488035 TO |
257 | } |
258 | if (!empty($settingParams)) { | |
259 | $filteredFields = array_intersect_key($settingParams, $fields['values']); | |
260 | } | |
261 | else { | |
262 | // no filters so we are interested in all for get mode. In create mode this means nothing to set | |
be2fb01f | 263 | $filteredFields = $createMode ? [] : $fields['values']; |
6a488035 TO |
264 | } |
265 | return $filteredFields; | |
266 | } | |
267 | ||
268 | /** | |
3bdf1f3a | 269 | * Validate & convert settings input. |
6a488035 | 270 | * |
3bdf1f3a | 271 | * @param mixed $value |
272 | * value of the setting to be set | |
273 | * @param array $fieldSpec | |
274 | * Metadata for given field (drawn from the xml) | |
67fea9b4 CW |
275 | * @param bool $convertToSerializedString |
276 | * Deprecated mode | |
3bdf1f3a | 277 | * |
278 | * @return bool | |
c8ee2f3f | 279 | * @throws \API_Exception |
6a488035 | 280 | */ |
67fea9b4 | 281 | public static function validateSetting(&$value, array $fieldSpec, $convertToSerializedString = TRUE) { |
c8ee2f3f | 282 | // Deprecated guesswork - should use $fieldSpec['serialize'] |
67fea9b4 | 283 | if ($convertToSerializedString && $fieldSpec['type'] == 'String' && is_array($value)) { |
353ffa53 | 284 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR; |
6a488035 TO |
285 | } |
286 | if (empty($fieldSpec['validate_callback'])) { | |
607cb45e | 287 | return TRUE; |
6a488035 TO |
288 | } |
289 | else { | |
c8074a93 TO |
290 | $cb = Civi\Core\Resolver::singleton()->get($fieldSpec['validate_callback']); |
291 | if (!call_user_func_array($cb, array(&$value, $fieldSpec))) { | |
c8ee2f3f | 292 | throw new API_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}"); |
6a488035 TO |
293 | } |
294 | } | |
295 | } | |
296 | ||
297 | /** | |
ea3ddccf | 298 | * Validate & convert settings input - translate True False to 0 or 1. |
6a488035 | 299 | * |
ea3ddccf | 300 | * @param mixed $value value of the setting to be set |
301 | * @param array $fieldSpec Metadata for given field (drawn from the xml) | |
302 | * | |
303 | * @return bool | |
c8ee2f3f | 304 | * @throws \API_Exception |
6a488035 | 305 | */ |
00be9182 | 306 | public static function validateBoolSetting(&$value, $fieldSpec) { |
6a488035 | 307 | if (!CRM_Utils_Rule::boolean($value)) { |
c8ee2f3f | 308 | throw new API_Exception("Boolean value required for {$fieldSpec['name']}"); |
6a488035 TO |
309 | } |
310 | if (!$value) { | |
311 | $value = 0; | |
312 | } | |
313 | else { | |
314 | $value = 1; | |
315 | } | |
316 | return TRUE; | |
317 | } | |
318 | ||
6a488035 TO |
319 | /** |
320 | * This provides information about the setting - similar to the fields concept for DAO information. | |
321 | * As the setting is serialized code creating validation setting input needs to know the data type | |
322 | * This also helps move information out of the form layer into the data layer where people can interact with | |
323 | * it via the API or other mechanisms. In order to keep this consistent it is important the form layer | |
324 | * also leverages it. | |
325 | * | |
326 | * Note that this function should never be called when using the runtime getvalue function. Caching works | |
327 | * around the expectation it will be called during setting administration | |
328 | * | |
329 | * Function is intended for configuration rather than runtime access to settings | |
330 | * | |
331 | * The following params will filter the result. If none are passed all settings will be returns | |
332 | * | |
6a0b768e TO |
333 | * @param int $componentID |
334 | * Id of relevant component. | |
2a6da8d7 | 335 | * @param array $filters |
c490a46a | 336 | * @param int $domainID |
2a6da8d7 EM |
337 | * @param null $profile |
338 | * | |
a6c01b45 CW |
339 | * @return array |
340 | * the following information as appropriate for each setting | |
5c766a0b TO |
341 | * - name |
342 | * - type | |
343 | * - default | |
344 | * - add (CiviCRM version added) | |
345 | * - is_domain | |
346 | * - is_contact | |
347 | * - description | |
348 | * - help_text | |
6a488035 | 349 | */ |
2da40d21 | 350 | public static function getSettingSpecification( |
607cb45e | 351 | $componentID = NULL, |
be2fb01f | 352 | $filters = [], |
607cb45e TO |
353 | $domainID = NULL, |
354 | $profile = NULL | |
b597d0b1 | 355 | ) { |
e1d39824 | 356 | return \Civi\Core\SettingsMetadata::getMetadata($filters, $domainID); |
6a488035 TO |
357 | } |
358 | ||
b5c2afd0 EM |
359 | /** |
360 | * @param $group | |
100fef9d | 361 | * @param string $name |
b5c2afd0 | 362 | * @param bool $system |
100fef9d | 363 | * @param int $userID |
b5c2afd0 EM |
364 | * @param bool $localize |
365 | * @param string $returnField | |
366 | * @param bool $returnNameANDLabels | |
367 | * @param null $condition | |
368 | * | |
369 | * @return array | |
370 | */ | |
2da40d21 | 371 | public static function valueOptions( |
f9f40af3 | 372 | $group, |
6a488035 | 373 | $name, |
242bd179 TO |
374 | $system = TRUE, |
375 | $userID = NULL, | |
376 | $localize = FALSE, | |
377 | $returnField = 'name', | |
6a488035 | 378 | $returnNameANDLabels = FALSE, |
242bd179 | 379 | $condition = NULL |
6a488035 TO |
380 | ) { |
381 | $optionValue = self::getItem($group, $name); | |
382 | ||
383 | $groupValues = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, $returnField); | |
384 | ||
385 | //enabled name => label require for new contact edit form, CRM-4605 | |
386 | if ($returnNameANDLabels) { | |
be2fb01f | 387 | $names = $labels = $nameAndLabels = []; |
6a488035 TO |
388 | if ($returnField == 'name') { |
389 | $names = $groupValues; | |
390 | $labels = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, 'label'); | |
391 | } | |
392 | else { | |
393 | $labels = $groupValues; | |
394 | $names = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, 'name'); | |
395 | } | |
396 | } | |
397 | ||
be2fb01f | 398 | $returnValues = []; |
6a488035 TO |
399 | foreach ($groupValues as $gn => $gv) { |
400 | $returnValues[$gv] = 0; | |
401 | } | |
402 | ||
403 | if ($optionValue && !empty($groupValues)) { | |
404 | $dbValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
405 | substr($optionValue, 1, -1) | |
406 | ); | |
407 | ||
408 | if (!empty($dbValues)) { | |
409 | foreach ($groupValues as $key => $val) { | |
410 | if (in_array($key, $dbValues)) { | |
411 | $returnValues[$val] = 1; | |
412 | if ($returnNameANDLabels) { | |
413 | $nameAndLabels[$names[$key]] = $labels[$key]; | |
414 | } | |
415 | } | |
416 | } | |
417 | } | |
418 | } | |
419 | return ($returnNameANDLabels) ? $nameAndLabels : $returnValues; | |
420 | } | |
421 | ||
b5c2afd0 | 422 | /** |
385d8c8e EM |
423 | * @param string $group |
424 | * Deprecated parameter | |
100fef9d | 425 | * @param string $name |
385d8c8e | 426 | * @param mixed $value |
b5c2afd0 | 427 | * @param bool $system |
100fef9d | 428 | * @param int $userID |
b5c2afd0 | 429 | * @param string $keyField |
c0e04a3c | 430 | * |
431 | * @throws \CRM_Core_Exception | |
432 | * | |
433 | * @deprecated | |
b5c2afd0 | 434 | */ |
2da40d21 | 435 | public static function setValueOption( |
f9f40af3 | 436 | $group, |
6a488035 TO |
437 | $name, |
438 | $value, | |
242bd179 TO |
439 | $system = TRUE, |
440 | $userID = NULL, | |
6a488035 TO |
441 | $keyField = 'name' |
442 | ) { | |
c0e04a3c | 443 | CRM_Core_Error::deprecatedFunctionWarning('refer docs for correct methods https://docs.civicrm.org/dev/en/latest/framework/setting/'); |
6a488035 TO |
444 | if (empty($value)) { |
445 | $optionValue = NULL; | |
446 | } | |
447 | elseif (is_array($value)) { | |
448 | $groupValues = CRM_Core_OptionGroup::values($name, FALSE, FALSE, FALSE, NULL, $keyField); | |
449 | ||
be2fb01f | 450 | $cbValues = []; |
6a488035 | 451 | foreach ($groupValues as $key => $val) { |
a7488080 | 452 | if (!empty($value[$val])) { |
6a488035 TO |
453 | $cbValues[$key] = 1; |
454 | } | |
455 | } | |
456 | ||
457 | if (!empty($cbValues)) { | |
458 | $optionValue = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
459 | array_keys($cbValues) |
460 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
461 | } |
462 | else { | |
463 | $optionValue = NULL; | |
464 | } | |
465 | } | |
466 | else { | |
467 | $optionValue = $value; | |
468 | } | |
469 | ||
470 | self::setItem($optionValue, $group, $name); | |
471 | } | |
472 | ||
f008885c E |
473 | /** |
474 | * Check if environment is explicitly set. | |
475 | * | |
385d8c8e EM |
476 | * @param $setting |
477 | * | |
f008885c E |
478 | * @return bool |
479 | */ | |
385d8c8e EM |
480 | public static function isEnvironmentSet($setting): bool { |
481 | CRM_Core_Error::deprecatedFunctionWarning('no alternative'); | |
f008885c | 482 | $environment = CRM_Core_Config::environment(); |
385d8c8e | 483 | return $setting === 'environment' && $environment; |
f008885c E |
484 | } |
485 | ||
486 | /** | |
487 | * Check if job is able to be executed by API. | |
488 | * | |
385d8c8e EM |
489 | * @param $params |
490 | * | |
491 | * @throws \CRM_Core_Exception | |
f008885c | 492 | */ |
385d8c8e | 493 | public static function isAPIJobAllowedToRun($params): void { |
03c5ceba | 494 | $environment = CRM_Core_Config::environment(NULL, TRUE); |
385d8c8e | 495 | if ($environment !== 'Production') { |
de6c59ca | 496 | if (!empty($params['runInNonProductionEnvironment'])) { |
03c5ceba | 497 | $mailing = Civi::settings()->get('mailing_backend_store'); |
498 | if ($mailing) { | |
499 | Civi::settings()->set('mailing_backend', $mailing); | |
500 | } | |
501 | } | |
502 | else { | |
385d8c8e | 503 | throw new CRM_Core_Exception(ts('Job has not been executed as it is a %1 (non-production) environment.', [1 => $environment])); |
03c5ceba | 504 | } |
505 | } | |
506 | } | |
507 | ||
508 | /** | |
509 | * Setting Callback - On Change. | |
510 | * | |
511 | * Respond to changes in the "environment" setting. | |
512 | * | |
513 | * @param array $oldValue | |
514 | * Value of old environment mode. | |
515 | * @param array $newValue | |
516 | * Value of new environment mode. | |
517 | * @param array $metadata | |
518 | * Specification of the setting (per *.settings.php). | |
519 | */ | |
520 | public static function onChangeEnvironmentSetting($oldValue, $newValue, $metadata) { | |
385d8c8e | 521 | if ($newValue !== 'Production') { |
03c5ceba | 522 | $mailing = Civi::settings()->get('mailing_backend'); |
dec5fe86 | 523 | if ($mailing['outBound_option'] != CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) { |
03c5ceba | 524 | Civi::settings()->set('mailing_backend_store', $mailing); |
525 | } | |
be2fb01f | 526 | Civi::settings()->set('mailing_backend', ['outBound_option' => CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED]); |
03c5ceba | 527 | CRM_Core_Session::setStatus(ts('Outbound emails have been disabled. Scheduled jobs will not run unless runInNonProductionEnvironment=TRUE is added as a parameter for a specific job'), ts("Non-production environment set"), "success"); |
528 | } | |
529 | else { | |
530 | $mailing = Civi::settings()->get('mailing_backend_store'); | |
531 | if ($mailing) { | |
532 | Civi::settings()->set('mailing_backend', $mailing); | |
533 | } | |
f008885c E |
534 | } |
535 | } | |
536 | ||
2c7039ef | 537 | } |