Merge pull request #17540 from colemanw/getActionsPerm
[civicrm-core.git] / CRM / Admin / Form / Setting.php
CommitLineData
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/**
ce064e4f 19 * This class generates form components generic to CiviCRM settings.
6a488035
TO
20 */
21class CRM_Admin_Form_Setting extends CRM_Core_Form {
22
946389fb 23 use CRM_Admin_Form_SettingTrait;
24
be2fb01f 25 protected $_settings = [];
6a488035 26
4dd431ca 27 protected $includesReadOnlyFields;
28
6a488035 29 /**
c490a46a 30 * Set default values for the form.
6a488035 31 *
ee0ce2ef 32 * Default values are retrieved from the database.
6a488035 33 */
00be9182 34 public function setDefaultValues() {
6a488035 35 if (!$this->_defaults) {
be2fb01f 36 $this->_defaults = [];
601361a3 37 $this->setDefaultsForMetadataDefinedFields();
6a488035 38
ec3cc27f 39 // @todo these should be retrievable from the above function.
aaffa79f 40 $this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL');
41 $this->_defaults['verifySSL'] = Civi::settings()->get('verifySSL');
f008885c 42 $this->_defaults['environment'] = CRM_Core_Config::environment();
f4fb2d79 43 $this->_defaults['enableComponents'] = Civi::settings()->get('enable_components');
6a488035
TO
44 }
45
46 return $this->_defaults;
47 }
48
49 /**
567b2076 50 * Build the form object.
4252f46f 51 *
52 * @throws \CRM_Core_Exception
6a488035
TO
53 */
54 public function buildQuickForm() {
089360bb 55 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
be2fb01f 56 $this->addButtons([
0d48f1cc
TO
57 [
58 'type' => 'next',
59 'name' => ts('Save'),
60 'isDefault' => TRUE,
61 ],
62 [
63 'type' => 'cancel',
64 'name' => ts('Cancel'),
65 ],
66 ]);
6a488035 67
e894ae15 68 $this->addFieldsDefinedInSettingsMetadata();
4dd431ca 69
70 if ($this->includesReadOnlyFields) {
be2fb01f 71 CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', ['expires' => 0]);
4dd431ca 72 }
6a488035
TO
73 }
74
75 /**
ee0ce2ef 76 * Process the form submission.
6a488035
TO
77 */
78 public function postProcess() {
79 // store the submitted values in an array
80 $params = $this->controller->exportValues($this->_name);
81
82 self::commonProcess($params);
83 }
84
e0ef6999 85 /**
5c9ff055
EM
86 * Common Process.
87 *
88 * @todo Document what I do.
89 *
c490a46a 90 * @param array $params
946389fb 91 * @throws \CRM_Core_Exception
e0ef6999 92 */
6a488035
TO
93 public function commonProcess(&$params) {
94
be2fb01f 95 foreach (['verifySSL', 'enableSSL'] as $name) {
5e7f101a 96 if (isset($params[$name])) {
97 Civi::settings()->set($name, $params[$name]);
98 unset($params[$name]);
99 }
6a488035 100 }
946389fb 101 try {
6821aa1d 102 $this->saveMetadataDefinedSettings($params);
6a488035 103 }
946389fb 104 catch (CiviCRM_API3_Exception $e) {
105 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
af962699
TO
106 }
107
946389fb 108 $this->filterParamsSetByMetadata($params);
109
7e0c769c
TO
110 $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
111 if (!empty($params)) {
946389fb 112 throw new CRM_Core_Exception('Unrecognized setting. This may be a config field which has not been properly migrated to a setting. (' . implode(', ', array_keys($params)) . ')');
7e0c769c 113 }
b08fb110
CW
114
115 CRM_Core_Config::clearDBCache();
0d48f1cc
TO
116 // This doesn't make a lot of sense to me, but it maintains pre-existing behavior.
117 Civi::cache('session')->clear();
b08fb110
CW
118 CRM_Utils_System::flushCache();
119 CRM_Core_Resources::singleton()->resetCacheCode();
120
4481526f 121 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
6a488035
TO
122 }
123
124 public function rebuildMenu() {
125 // ensure config is set with new values
126 $config = CRM_Core_Config::singleton(TRUE, TRUE);
127
128 // rebuild menu items
129 CRM_Core_Menu::store();
6a488035 130 }
96025800 131
6a488035 132}