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