Make checkboxes save
[civicrm-core.git] / CRM / Admin / Form / Setting.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * This class generates form components generic to CiviCRM settings.
36 */
37 class CRM_Admin_Form_Setting extends CRM_Core_Form {
38
39 use CRM_Admin_Form_SettingTrait;
40
41 protected $_settings = array();
42
43 protected $includesReadOnlyFields;
44
45 /**
46 * Set default values for the form.
47 *
48 * Default values are retrieved from the database.
49 */
50 public function setDefaultValues() {
51 if (!$this->_defaults) {
52 $this->_defaults = array();
53 $formArray = array('Component', 'Localization');
54 $formMode = FALSE;
55 if (in_array($this->_name, $formArray)) {
56 $formMode = TRUE;
57 }
58
59 $this->setDefaultsForMetadataDefinedFields();
60
61 // @todo thise should be retrievable from the above function.
62 $this->_defaults['contact_autocomplete_options'] = self::getAutocompleteContactSearch();
63 $this->_defaults['contact_reference_options'] = self::getAutocompleteContactReference();
64 $this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL');
65 $this->_defaults['verifySSL'] = Civi::settings()->get('verifySSL');
66 $this->_defaults['environment'] = CRM_Core_Config::environment();
67 $this->_defaults['enableComponents'] = Civi::settings()->get('enable_components');
68 }
69
70 return $this->_defaults;
71 }
72
73 /**
74 * Build the form object.
75 */
76 public function buildQuickForm() {
77 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
78 $this->addButtons(array(
79 array(
80 'type' => 'next',
81 'name' => ts('Save'),
82 'isDefault' => TRUE,
83 ),
84 array(
85 'type' => 'cancel',
86 'name' => ts('Cancel'),
87 ),
88 )
89 );
90
91 $this->addFieldsDefinedInSettingsMetadata();
92
93 if ($this->includesReadOnlyFields) {
94 CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', array('expires' => 0));
95 }
96 }
97
98 /**
99 * Process the form submission.
100 */
101 public function postProcess() {
102 // store the submitted values in an array
103 $params = $this->controller->exportValues($this->_name);
104
105 self::commonProcess($params);
106 }
107
108 /**
109 * Common Process.
110 *
111 * @todo Document what I do.
112 *
113 * @param array $params
114 * @throws \CRM_Core_Exception
115 */
116 public function commonProcess(&$params) {
117
118 // save autocomplete search options
119 if (!empty($params['contact_autocomplete_options'])) {
120 Civi::settings()->set('contact_autocomplete_options',
121 CRM_Utils_Array::implodePadded(array_keys($params['contact_autocomplete_options'])));
122 unset($params['contact_autocomplete_options']);
123 }
124
125 // save autocomplete contact reference options
126 if (!empty($params['contact_reference_options'])) {
127 Civi::settings()->set('contact_reference_options',
128 CRM_Utils_Array::implodePadded(array_keys($params['contact_reference_options'])));
129 unset($params['contact_reference_options']);
130 }
131
132 // save components to be enabled
133 if (array_key_exists('enableComponents', $params)) {
134 civicrm_api3('setting', 'create', array(
135 'enable_components' => $params['enableComponents'],
136 ));
137 unset($params['enableComponents']);
138 }
139
140 foreach (array('verifySSL', 'enableSSL') as $name) {
141 if (isset($params[$name])) {
142 Civi::settings()->set($name, $params[$name]);
143 unset($params[$name]);
144 }
145 }
146 try {
147 $settings = $this->getSettingsToSetByMetadata($params);
148 $this->saveMetadataDefinedSettings($params);
149 }
150 catch (CiviCRM_API3_Exception $e) {
151 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
152 }
153
154 $this->filterParamsSetByMetadata($params);
155
156 $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
157 if (!empty($params)) {
158 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)) . ')');
159 }
160
161 CRM_Core_Config::clearDBCache();
162 Civi::cache('session')->clear(); // This doesn't make a lot of sense to me, but it maintains pre-existing behavior.
163 CRM_Utils_System::flushCache();
164 CRM_Core_Resources::singleton()->resetCacheCode();
165
166 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
167 }
168
169 public function rebuildMenu() {
170 // ensure config is set with new values
171 $config = CRM_Core_Config::singleton(TRUE, TRUE);
172
173 // rebuild menu items
174 CRM_Core_Menu::store();
175
176 // also delete the IDS file so we can write a new correct one on next load
177 $configFile = $config->uploadDir . 'Config.IDS.ini';
178 @unlink($configFile);
179 }
180
181 /**
182 * Ugh, this shouldn't exist.
183 *
184 * Get the selected values of "contact_reference_options" formatted for checkboxes.
185 *
186 * @return array
187 */
188 public static function getAutocompleteContactReference() {
189 $cRlist = array_flip(CRM_Core_OptionGroup::values('contact_reference_options',
190 FALSE, FALSE, TRUE, NULL, 'name'
191 ));
192 $cRlistEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
193 'contact_reference_options'
194 );
195 $cRSearchFields = array();
196 if (!empty($cRlist) && !empty($cRlistEnabled)) {
197 $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
198 }
199 return array(
200 '1' => 1,
201 ) + $cRSearchFields;
202 }
203
204 /**
205 * Ugh, this shouldn't exist.
206 *
207 * Get the selected values of "contact_autocomplete_options" formatted for checkboxes.
208 *
209 * @return array
210 */
211 public static function getAutocompleteContactSearch() {
212 $list = array_flip(CRM_Core_OptionGroup::values('contact_autocomplete_options',
213 FALSE, FALSE, TRUE, NULL, 'name'
214 ));
215 $listEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
216 'contact_autocomplete_options'
217 );
218 $autoSearchFields = array();
219 if (!empty($list) && !empty($listEnabled)) {
220 $autoSearchFields = array_combine($list, $listEnabled);
221 }
222 //Set defaults for autocomplete and contact reference options
223 return array(
224 '1' => 1,
225 ) + $autoSearchFields;
226 }
227
228 }