Merge pull request #20059 from samuelsov/dev/core#2479
[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
TO
26
27 /**
c490a46a 28 * Set default values for the form.
6a488035 29 *
ee0ce2ef 30 * Default values are retrieved from the database.
6a488035 31 */
00be9182 32 public function setDefaultValues() {
6a488035 33 if (!$this->_defaults) {
be2fb01f 34 $this->_defaults = [];
601361a3 35 $this->setDefaultsForMetadataDefinedFields();
6a488035 36
ec3cc27f 37 // @todo these should be retrievable from the above function.
aaffa79f 38 $this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL');
39 $this->_defaults['verifySSL'] = Civi::settings()->get('verifySSL');
f008885c 40 $this->_defaults['environment'] = CRM_Core_Config::environment();
f4fb2d79 41 $this->_defaults['enableComponents'] = Civi::settings()->get('enable_components');
6a488035
TO
42 }
43
44 return $this->_defaults;
45 }
46
47 /**
567b2076 48 * Build the form object.
4252f46f 49 *
50 * @throws \CRM_Core_Exception
6a488035
TO
51 */
52 public function buildQuickForm() {
089360bb 53 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
be2fb01f 54 $this->addButtons([
0d48f1cc
TO
55 [
56 'type' => 'next',
57 'name' => ts('Save'),
58 'isDefault' => TRUE,
59 ],
60 [
61 'type' => 'cancel',
62 'name' => ts('Cancel'),
63 ],
64 ]);
6a488035 65
e894ae15 66 $this->addFieldsDefinedInSettingsMetadata();
6a488035
TO
67 }
68
69 /**
ee0ce2ef 70 * Process the form submission.
6a488035
TO
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
e0ef6999 79 /**
5c9ff055
EM
80 * Common Process.
81 *
82 * @todo Document what I do.
83 *
c490a46a 84 * @param array $params
946389fb 85 * @throws \CRM_Core_Exception
e0ef6999 86 */
6a488035
TO
87 public function commonProcess(&$params) {
88
be2fb01f 89 foreach (['verifySSL', 'enableSSL'] as $name) {
5e7f101a 90 if (isset($params[$name])) {
91 Civi::settings()->set($name, $params[$name]);
92 unset($params[$name]);
93 }
6a488035 94 }
946389fb 95 try {
6821aa1d 96 $this->saveMetadataDefinedSettings($params);
6a488035 97 }
946389fb 98 catch (CiviCRM_API3_Exception $e) {
99 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
af962699
TO
100 }
101
946389fb 102 $this->filterParamsSetByMetadata($params);
103
7e0c769c
TO
104 $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
105 if (!empty($params)) {
946389fb 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)) . ')');
7e0c769c 107 }
b08fb110
CW
108
109 CRM_Core_Config::clearDBCache();
0d48f1cc
TO
110 // This doesn't make a lot of sense to me, but it maintains pre-existing behavior.
111 Civi::cache('session')->clear();
b08fb110
CW
112 CRM_Utils_System::flushCache();
113 CRM_Core_Resources::singleton()->resetCacheCode();
0bbf1069 114 $this->rebuildMenu();
b08fb110 115
4481526f 116 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
6a488035
TO
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();
6a488035 125 }
96025800 126
6a488035 127}