Merge pull request #16077 from civicrm/5.21
[civicrm-core.git] / CRM / Admin / Form / Setting.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components generic to CiviCRM settings.
20 */
21 class CRM_Admin_Form_Setting extends CRM_Core_Form {
22
23 use CRM_Admin_Form_SettingTrait;
24
25 protected $_settings = [];
26
27 protected $includesReadOnlyFields;
28
29 /**
30 * Set default values for the form.
31 *
32 * Default values are retrieved from the database.
33 */
34 public function setDefaultValues() {
35 if (!$this->_defaults) {
36 $this->_defaults = [];
37 $formArray = ['Component', 'Localization'];
38 $formMode = FALSE;
39 if (in_array($this->_name, $formArray)) {
40 $formMode = TRUE;
41 }
42
43 $this->setDefaultsForMetadataDefinedFields();
44
45 // @todo these should be retrievable from the above function.
46 $this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL');
47 $this->_defaults['verifySSL'] = Civi::settings()->get('verifySSL');
48 $this->_defaults['environment'] = CRM_Core_Config::environment();
49 $this->_defaults['enableComponents'] = Civi::settings()->get('enable_components');
50 }
51
52 return $this->_defaults;
53 }
54
55 /**
56 * Build the form object.
57 */
58 public function buildQuickForm() {
59 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
60 $this->addButtons([
61 [
62 'type' => 'next',
63 'name' => ts('Save'),
64 'isDefault' => TRUE,
65 ],
66 [
67 'type' => 'cancel',
68 'name' => ts('Cancel'),
69 ],
70 ]);
71
72 $this->addFieldsDefinedInSettingsMetadata();
73
74 if ($this->includesReadOnlyFields) {
75 CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', ['expires' => 0]);
76 }
77 }
78
79 /**
80 * Process the form submission.
81 */
82 public function postProcess() {
83 // store the submitted values in an array
84 $params = $this->controller->exportValues($this->_name);
85
86 self::commonProcess($params);
87 }
88
89 /**
90 * Common Process.
91 *
92 * @todo Document what I do.
93 *
94 * @param array $params
95 * @throws \CRM_Core_Exception
96 */
97 public function commonProcess(&$params) {
98
99 foreach (['verifySSL', 'enableSSL'] as $name) {
100 if (isset($params[$name])) {
101 Civi::settings()->set($name, $params[$name]);
102 unset($params[$name]);
103 }
104 }
105 try {
106 $this->saveMetadataDefinedSettings($params);
107 }
108 catch (CiviCRM_API3_Exception $e) {
109 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
110 }
111
112 $this->filterParamsSetByMetadata($params);
113
114 $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
115 if (!empty($params)) {
116 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)) . ')');
117 }
118
119 CRM_Core_Config::clearDBCache();
120 // This doesn't make a lot of sense to me, but it maintains pre-existing behavior.
121 Civi::cache('session')->clear();
122 CRM_Utils_System::flushCache();
123 CRM_Core_Resources::singleton()->resetCacheCode();
124
125 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
126 }
127
128 public function rebuildMenu() {
129 // ensure config is set with new values
130 $config = CRM_Core_Config::singleton(TRUE, TRUE);
131
132 // rebuild menu items
133 CRM_Core_Menu::store();
134 }
135
136 }