Merge pull request #20059 from samuelsov/dev/core#2479
[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 /**
28 * Set default values for the form.
29 *
30 * Default values are retrieved from the database.
31 */
32 public function setDefaultValues() {
33 if (!$this->_defaults) {
34 $this->_defaults = [];
35 $this->setDefaultsForMetadataDefinedFields();
36
37 // @todo these should be retrievable from the above function.
38 $this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL');
39 $this->_defaults['verifySSL'] = Civi::settings()->get('verifySSL');
40 $this->_defaults['environment'] = CRM_Core_Config::environment();
41 $this->_defaults['enableComponents'] = Civi::settings()->get('enable_components');
42 }
43
44 return $this->_defaults;
45 }
46
47 /**
48 * Build the form object.
49 *
50 * @throws \CRM_Core_Exception
51 */
52 public function buildQuickForm() {
53 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
54 $this->addButtons([
55 [
56 'type' => 'next',
57 'name' => ts('Save'),
58 'isDefault' => TRUE,
59 ],
60 [
61 'type' => 'cancel',
62 'name' => ts('Cancel'),
63 ],
64 ]);
65
66 $this->addFieldsDefinedInSettingsMetadata();
67 }
68
69 /**
70 * Process the form submission.
71 */
72 public function postProcess() {
73 // store the submitted values in an array
74 $params = $this->controller->exportValues($this->_name);
75
76 self::commonProcess($params);
77 }
78
79 /**
80 * Common Process.
81 *
82 * @todo Document what I do.
83 *
84 * @param array $params
85 * @throws \CRM_Core_Exception
86 */
87 public function commonProcess(&$params) {
88
89 foreach (['verifySSL', 'enableSSL'] as $name) {
90 if (isset($params[$name])) {
91 Civi::settings()->set($name, $params[$name]);
92 unset($params[$name]);
93 }
94 }
95 try {
96 $this->saveMetadataDefinedSettings($params);
97 }
98 catch (CiviCRM_API3_Exception $e) {
99 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
100 }
101
102 $this->filterParamsSetByMetadata($params);
103
104 $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
105 if (!empty($params)) {
106 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)) . ')');
107 }
108
109 CRM_Core_Config::clearDBCache();
110 // This doesn't make a lot of sense to me, but it maintains pre-existing behavior.
111 Civi::cache('session')->clear();
112 CRM_Utils_System::flushCache();
113 CRM_Core_Resources::singleton()->resetCacheCode();
114 $this->rebuildMenu();
115
116 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
117 }
118
119 public function rebuildMenu() {
120 // ensure config is set with new values
121 $config = CRM_Core_Config::singleton(TRUE, TRUE);
122
123 // rebuild menu items
124 CRM_Core_Menu::store();
125 }
126
127 }