Merge pull request #6535 from LevityNL/upstream-relationships
[civicrm-core.git] / CRM / Admin / Form / Setting.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 */
34
35 /**
36 * This class generates form components generic to CiviCRM settings
37 */
38 class CRM_Admin_Form_Setting extends CRM_Core_Form {
39
40 protected $_settings = array();
41
42 /**
43 * Set default values for the form.
44 *
45 * Default values are retrieved from the database.
46 */
47 public function setDefaultValues() {
48 if (!$this->_defaults) {
49 $this->_defaults = array();
50 $formArray = array('Component', 'Localization');
51 $formMode = FALSE;
52 if (in_array($this->_name, $formArray)) {
53 $formMode = TRUE;
54 }
55
56 CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults);
57
58 CRM_Core_Config_Defaults::setValues($this->_defaults, $formMode);
59
60 $list = array_flip(CRM_Core_OptionGroup::values('contact_autocomplete_options',
61 FALSE, FALSE, TRUE, NULL, 'name'
62 ));
63
64 $cRlist = array_flip(CRM_Core_OptionGroup::values('contact_reference_options',
65 FALSE, FALSE, TRUE, NULL, 'name'
66 ));
67
68 $listEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
69 'contact_autocomplete_options'
70 );
71 $cRlistEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
72 'contact_reference_options'
73 );
74
75 $autoSearchFields = array();
76 if (!empty($list) && !empty($listEnabled)) {
77 $autoSearchFields = array_combine($list, $listEnabled);
78 }
79
80 $cRSearchFields = array();
81 if (!empty($cRlist) && !empty($cRlistEnabled)) {
82 $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
83 }
84
85 //Set defaults for autocomplete and contact reference options
86 $this->_defaults['autocompleteContactSearch'] = array(
87 '1' => 1,
88 ) + $autoSearchFields;
89 $this->_defaults['autocompleteContactReference'] = array(
90 '1' => 1,
91 ) + $cRSearchFields;
92
93 // we can handle all the ones defined in the metadata here. Others to be converted
94 foreach ($this->_settings as $setting => $group) {
95 $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
96 $this->_defaults[$setting] = civicrm_api('setting', 'getvalue', array(
97 'version' => 3,
98 'name' => $setting,
99 'group' => $group,
100 'default_value' => CRM_Utils_Array::value('default', $settingMetaData['values'][$setting]),
101 )
102 );
103 }
104
105 $this->_defaults['enableSSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enableSSL', NULL, 0);
106 $this->_defaults['verifySSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL', NULL, 1);
107 }
108
109 return $this->_defaults;
110 }
111
112 /**
113 * Build the form object.
114 */
115 public function buildQuickForm() {
116 $session = CRM_Core_Session::singleton();
117 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
118 $args = func_get_args();
119 $check = reset($args);
120 $this->addButtons(array(
121 array(
122 'type' => 'next',
123 'name' => ts('Save'),
124 'isDefault' => TRUE,
125 ),
126 array(
127 'type' => 'cancel',
128 'name' => ts('Cancel'),
129 ),
130 )
131 );
132
133 foreach ($this->_settings as $setting => $group) {
134 $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
135 $props = $settingMetaData['values'][$setting];
136 if (isset($props['quick_form_type'])) {
137 $add = 'add' . $props['quick_form_type'];
138 if ($add == 'addElement') {
139 $this->$add(
140 $props['html_type'],
141 $setting,
142 ts($props['title']),
143 CRM_Utils_Array::value($props['html_type'] == 'select' ? 'option_values' : 'html_attributes', $props, array()),
144 $props['html_type'] == 'select' ? CRM_Utils_Array::value('html_attributes', $props) : NULL
145 );
146 }
147 elseif ($add == 'addSelect') {
148 $options = civicrm_api3('Setting', 'getoptions', array(
149 'field' => $setting,
150 ));
151 $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
152 }
153 else {
154 $this->$add($setting, ts($props['title']));
155 }
156 $this->assign("{$setting}_description", ts($props['description']));
157 if ($setting == 'max_attachments') {
158 //temp hack @todo fix to get from metadata
159 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
160 }
161 if ($setting == 'maxFileSize') {
162 //temp hack
163 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
164 }
165
166 }
167 }
168 }
169
170 public function getDefaultEntity() {
171 return 'Setting';
172 }
173
174 /**
175 * Process the form submission.
176 */
177 public function postProcess() {
178 // store the submitted values in an array
179 $params = $this->controller->exportValues($this->_name);
180
181 self::commonProcess($params);
182 }
183
184 /**
185 * Common Process.
186 *
187 * @todo Document what I do.
188 *
189 * @param array $params
190 */
191 public function commonProcess(&$params) {
192
193 // save autocomplete search options
194 if (!empty($params['autocompleteContactSearch'])) {
195 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
196 array_keys($params['autocompleteContactSearch'])
197 ) . CRM_Core_DAO::VALUE_SEPARATOR;
198
199 CRM_Core_BAO_Setting::setItem($value,
200 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
201 'contact_autocomplete_options'
202 );
203
204 unset($params['autocompleteContactSearch']);
205 }
206
207 // save autocomplete contact reference options
208 if (!empty($params['autocompleteContactReference'])) {
209 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
210 array_keys($params['autocompleteContactReference'])
211 ) . CRM_Core_DAO::VALUE_SEPARATOR;
212
213 CRM_Core_BAO_Setting::setItem($value,
214 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
215 'contact_reference_options'
216 );
217
218 unset($params['autocompleteContactReference']);
219 }
220
221 // save components to be enabled
222 if (array_key_exists('enableComponents', $params)) {
223 civicrm_api3('setting', 'create', array(
224 'enable_components' => $params['enableComponents'],
225 ));
226 unset($params['enableComponents']);
227 }
228
229 // save checksum timeout
230 if (!empty($params['checksumTimeout'])) {
231 CRM_Core_BAO_Setting::setItem($params['checksumTimeout'],
232 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
233 'checksum_timeout'
234 );
235 }
236
237 // verify ssl peer option
238 if (isset($params['verifySSL'])) {
239 CRM_Core_BAO_Setting::setItem($params['verifySSL'],
240 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
241 'verifySSL'
242 );
243 unset($params['verifySSL']);
244 }
245
246 // force secure URLs
247 if (isset($params['enableSSL'])) {
248 CRM_Core_BAO_Setting::setItem($params['enableSSL'],
249 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
250 'enableSSL'
251 );
252 unset($params['enableSSL']);
253 }
254 $settings = array_intersect_key($params, $this->_settings);
255 $result = civicrm_api('setting', 'create', $settings + array('version' => 3));
256 foreach ($settings as $setting => $settingGroup) {
257 //@todo array_diff this
258 unset($params[$setting]);
259 }
260 if (!empty($result['error_message'])) {
261 CRM_Core_Session::setStatus($result['error_message'], ts('Save Failed'), 'error');
262 }
263
264 CRM_Core_BAO_ConfigSetting::create($params);
265
266 CRM_Core_Config::clearDBCache();
267 CRM_Utils_System::flushCache();
268 CRM_Core_Resources::singleton()->resetCacheCode();
269
270 CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
271 }
272
273 public function rebuildMenu() {
274 // ensure config is set with new values
275 $config = CRM_Core_Config::singleton(TRUE, TRUE);
276
277 // rebuild menu items
278 CRM_Core_Menu::store();
279
280 // also delete the IDS file so we can write a new correct one on next load
281 $configFile = $config->uploadDir . 'Config.IDS.ini';
282 @unlink($configFile);
283 }
284
285 }