CRM-16373 - CRM_Core_BAO_ConfigSetting - Fix enableComponent
[civicrm-core.git] / CRM / Core / BAO / ConfigSetting.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
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 *
31 * @package CRM
e7112fa7 32 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
33 */
34
35/**
192d36c5 36 * File contains functions used in civicrm configuration.
6a488035
TO
37 */
38class CRM_Core_BAO_ConfigSetting {
39
40 /**
100fef9d 41 * Create civicrm settings. This is the same as add but it clears the cache and
b597d0b1 42 * reloads the config object
6a488035 43 *
6a0b768e
TO
44 * @param array $params
45 * Associated array of civicrm variables.
6a488035 46 */
00be9182 47 public static function create($params) {
6a488035
TO
48 self::add($params);
49 $cache = CRM_Utils_Cache::singleton();
50 $cache->delete('CRM_Core_Config');
0e04f44e 51 $cache->delete('CRM_Core_Config' . CRM_Core_Config::domainID());
6a488035
TO
52 $config = CRM_Core_Config::singleton(TRUE, TRUE);
53 }
54
55 /**
fe482240 56 * Add civicrm settings.
6a488035 57 *
6a0b768e
TO
58 * @param array $params
59 * Associated array of civicrm variables.
6a488035 60 */
00be9182 61 public static function add(&$params) {
6a488035
TO
62 $domain = new CRM_Core_DAO_Domain();
63 $domain->id = CRM_Core_Config::domainID();
64 $domain->find(TRUE);
65 if ($domain->config_backend) {
6e5b5e59 66 $params = array_merge(unserialize($domain->config_backend), $params);
6a488035
TO
67 }
68
7e0c769c 69 $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
6a488035 70
6a488035
TO
71 // also skip all Dir Params, we dont need to store those in the DB!
72 foreach ($params as $name => $val) {
73 if (substr($name, -3) == 'Dir') {
74 unset($params[$name]);
75 }
76 }
77
6a488035
TO
78 $domain->config_backend = serialize($params);
79 $domain->save();
80 }
81
6a488035 82 /**
fe482240 83 * Retrieve the settings values from db.
6a488035 84 *
da6b46f4
EM
85 * @param $defaults
86 *
a6c01b45 87 * @return array
6a488035 88 */
00be9182 89 public static function retrieve(&$defaults) {
6a488035
TO
90 $domain = new CRM_Core_DAO_Domain();
91
92 //we are initializing config, really can't use, CRM-7863
93 $urlVar = 'q';
94 if (defined('CIVICRM_UF') && CIVICRM_UF == 'Joomla') {
95 $urlVar = 'task';
96 }
97
98 if (CRM_Core_Config::isUpgradeMode()) {
99 $domain->selectAdd('config_backend');
100 }
6a488035 101 else {
234d8f09 102 $domain->selectAdd('config_backend, locales');
6a488035
TO
103 }
104
105 $domain->id = CRM_Core_Config::domainID();
106 $domain->find(TRUE);
107 if ($domain->config_backend) {
108 $defaults = unserialize($domain->config_backend);
109 if ($defaults === FALSE || !is_array($defaults)) {
110 $defaults = array();
608e6658 111 return FALSE;
6a488035
TO
112 }
113
114 $skipVars = self::skipVars();
115 foreach ($skipVars as $skip) {
116 if (array_key_exists($skip, $defaults)) {
117 unset($defaults[$skip]);
118 }
119 }
120
b72b5fc0
TO
121 CRM_Core_BAO_ConfigSetting::applyLocale(Civi::settings($domain->id), $domain->locales);
122 }
123 }
6a488035 124
b72b5fc0
TO
125 /**
126 * Evaluate locale preferences and activate a chosen locale by
127 * updating session+global variables.
128 *
129 * @param \Civi\Core\SettingsBag $settings
130 * @param string $activatedLocales
131 * Imploded list of locales which are supported in the DB.
b72b5fc0
TO
132 */
133 public static function applyLocale($settings, $activatedLocales) {
134 // are we in a multi-language setup?
135 $multiLang = $activatedLocales ? TRUE : FALSE;
6a488035 136
b72b5fc0
TO
137 // set the current language
138 $chosenLocale = NULL;
6a488035 139
b72b5fc0 140 $session = CRM_Core_Session::singleton();
6a488035 141
b72b5fc0
TO
142 // on multi-lang sites based on request and civicrm_uf_match
143 if ($multiLang) {
144 $languageLimit = array();
145 if (is_array($settings->get('languageLimit'))) {
146 $languageLimit = $settings->get('languageLimit');
147 }
6a488035 148
b72b5fc0
TO
149 $requestLocale = CRM_Utils_Request::retrieve('lcMessages', 'String');
150 if (in_array($requestLocale, array_keys($languageLimit))) {
151 $chosenLocale = $requestLocale;
6a488035 152
b72b5fc0
TO
153 //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache.
154 // Ed: This doesn't sound good.
155 CRM_Core_BAO_Cache::deleteGroup('navigation');
156 }
157 else {
158 $requestLocale = NULL;
159 }
6a488035 160
b72b5fc0
TO
161 if (!$requestLocale) {
162 $sessionLocale = $session->get('lcMessages');
163 if (in_array($sessionLocale, array_keys($languageLimit))) {
164 $chosenLocale = $sessionLocale;
165 }
166 else {
167 $sessionLocale = NULL;
6a488035 168 }
b72b5fc0 169 }
6a488035 170
b72b5fc0
TO
171 if ($requestLocale) {
172 $ufm = new CRM_Core_DAO_UFMatch();
173 $ufm->contact_id = $session->get('userID');
174 if ($ufm->find(TRUE)) {
175 $ufm->language = $chosenLocale;
176 $ufm->save();
6a488035 177 }
b72b5fc0 178 $session->set('lcMessages', $chosenLocale);
6a488035 179 }
b72b5fc0
TO
180
181 if (!$chosenLocale and $session->get('userID')) {
182 $ufm = new CRM_Core_DAO_UFMatch();
183 $ufm->contact_id = $session->get('userID');
184 if ($ufm->find(TRUE) &&
185 in_array($ufm->language, array_keys($languageLimit))
4c235182 186 ) {
b72b5fc0 187 $chosenLocale = $ufm->language;
6a488035 188 }
b72b5fc0 189 $session->set('lcMessages', $chosenLocale);
6a488035 190 }
b72b5fc0
TO
191 }
192 global $dbLocale;
193
194 // try to inherit the language from the hosting CMS
195 if ($settings->get('inheritLocale')) {
196 // FIXME: On multilanguage installs, CRM_Utils_System::getUFLocale() in many cases returns nothing if $dbLocale is not set
197 $dbLocale = $multiLang ? ("_" . $settings->get('lcMessages')) : '';
198 $chosenLocale = CRM_Utils_System::getUFLocale();
199 if ($activatedLocales and !in_array($chosenLocale, explode(CRM_Core_DAO::VALUE_SEPARATOR, $activatedLocales))) {
200 $chosenLocale = NULL;
5b6ed484 201 }
b72b5fc0 202 }
6a488035 203
b72b5fc0
TO
204 if (empty($chosenLocale)) {
205 //CRM-11993 - if a single-lang site, use default
206 $chosenLocale = $settings->get('lcMessages');
207 }
6a488035 208
b72b5fc0
TO
209 // set suffix for table names - use views if more than one language
210 $dbLocale = $multiLang ? "_{$chosenLocale}" : '';
6a488035 211
b72b5fc0
TO
212 // FIXME: an ugly hack to fix CRM-4041
213 global $tsLocale;
214 $tsLocale = $chosenLocale;
215
216 // FIXME: as bad aplace as any to fix CRM-5428
217 // (to be moved to a sane location along with the above)
218 if (function_exists('mb_internal_encoding')) {
219 mb_internal_encoding('UTF-8');
6a488035 220 }
6a488035
TO
221 }
222
b5c2afd0
EM
223 /**
224 * @param array $defaultValues
225 *
226 * @return string
227 * @throws Exception
228 */
00be9182 229 public static function doSiteMove($defaultValues = array()) {
6a488035 230 $moveStatus = ts('Beginning site move process...') . '<br />';
4f240ac1
TO
231 $settings = Civi::settings();
232
233 foreach (array_merge(self::getPathSettings(), self::getUrlSettings()) as $key) {
234 $value = $settings->get($key);
235 if ($value && $value != $settings->getDefault($key)) {
236 if ($settings->getMandatory($key) === NULL) {
237 $settings->revert($key);
238 $moveStatus .= ts("WARNING: The setting (%1) has been reverted.", array(
239 1 => $key,
240 ));
241 $moveStatus .= '<br />';
6a488035 242 }
4f240ac1
TO
243 else {
244 $moveStatus .= ts("WARNING: The setting (%1) is overridden and could not be reverted.", array(
245 1 => $key,
246 ));
247 $moveStatus .= '<br />';
6a488035
TO
248 }
249 }
250 }
251
6a488035
TO
252 $config = CRM_Core_Config::singleton();
253
254 // clear the template_c and upload directory also
255 $config->cleanup(3, TRUE);
256 $moveStatus .= ts('Template cache and upload directory have been cleared.') . '<br />';
257
258 // clear all caches
259 CRM_Core_Config::clearDBCache();
260 $moveStatus .= ts('Database cache tables cleared.') . '<br />';
261
262 $resetSessionTable = CRM_Utils_Request::retrieve('resetSessionTable',
263 'Boolean',
264 CRM_Core_DAO::$_nullArray,
265 FALSE,
266 FALSE,
267 'REQUEST'
268 );
269 if ($config->userSystem->is_drupal &&
270 $resetSessionTable
271 ) {
272 db_query("DELETE FROM {sessions} WHERE 1");
273 $moveStatus .= ts('Drupal session table cleared.') . '<br />';
274 }
275 else {
276 $session = CRM_Core_Session::singleton();
277 $session->reset(2);
278 $moveStatus .= ts('Session has been reset.') . '<br />';
279 }
280
281 return $moveStatus;
282 }
283
284 /**
fe482240 285 * Takes a componentName and enables it in the config.
6a488035
TO
286 * Primarily used during unit testing
287 *
6a0b768e
TO
288 * @param string $componentName
289 * Name of the component to be enabled, needs to be valid.
6a488035 290 *
608e6658 291 * @return bool
a6c01b45 292 * true if valid component name and enabling succeeds, else false
6a488035 293 */
00be9182 294 public static function enableComponent($componentName) {
6a488035
TO
295 $config = CRM_Core_Config::singleton();
296 if (in_array($componentName, $config->enableComponents)) {
297 // component is already enabled
298 return TRUE;
299 }
6a488035
TO
300
301 // return if component does not exist
b086633a 302 if (!array_key_exists($componentName, CRM_Core_Component::getComponents())) {
6a488035
TO
303 return FALSE;
304 }
305
3124edb3 306 // get enabled-components from DB and add to the list
84fb7424 307 $enabledComponents = Civi::settings()->get('enable_components');
3124edb3 308 $enabledComponents[] = $componentName;
6a488035 309
b086633a
TO
310 self::setEnabledComponents($enabledComponents);
311
312 return TRUE;
313 }
314
b896fa44
EM
315 /**
316 * Disable specified component.
317 *
318 * @param string $componentName
319 *
320 * @return bool
321 */
00be9182 322 public static function disableComponent($componentName) {
b086633a 323 $config = CRM_Core_Config::singleton();
4c235182
EM
324 if (!in_array($componentName, $config->enableComponents) ||
325 !array_key_exists($componentName, CRM_Core_Component::getComponents())
326 ) {
b896fa44 327 // Post-condition is satisfied.
b086633a
TO
328 return TRUE;
329 }
330
331 // get enabled-components from DB and add to the list
84fb7424 332 $enabledComponents = Civi::settings()->get('enable_components');
b086633a
TO
333 $enabledComponents = array_diff($enabledComponents, array($componentName));
334
335 self::setEnabledComponents($enabledComponents);
336
337 return TRUE;
338 }
339
b896fa44
EM
340 /**
341 * Set enabled components.
342 *
343 * @param array $enabledComponents
344 */
b086633a 345 public static function setEnabledComponents($enabledComponents) {
edbcbd96
TO
346 // fix the config object. update db.
347 Civi::settings()->set('enable_components', $enabledComponents);
6a488035
TO
348
349 // also force reset of component array
350 CRM_Core_Component::getEnabledComponents(TRUE);
6a488035
TO
351 }
352
b5c2afd0
EM
353 /**
354 * @return array
355 */
00be9182 356 public static function skipVars() {
6a488035 357 return array(
4c235182
EM
358 'dsn',
359 'templateCompileDir',
6a488035
TO
360 'userFrameworkDSN',
361 'userFramework',
4c235182
EM
362 'userFrameworkBaseURL',
363 'userFrameworkClass',
364 'userHookClass',
365 'userPermissionClass',
59735506 366 'userPermissionTemp',
4c235182
EM
367 'userFrameworkURLVar',
368 'userFrameworkVersion',
369 'newBaseURL',
370 'newBaseDir',
371 'newSiteName',
372 'configAndLogDir',
373 'qfKey',
374 'gettextResourceDir',
375 'cleanURL',
7e0c769c 376 'entryURL',
4c235182
EM
377 'locale_custom_strings',
378 'localeCustomStrings',
6a488035
TO
379 'autocompleteContactSearch',
380 'autocompleteContactReference',
381 'checksumTimeout',
92a8de72 382 'checksum_timeout',
6a488035
TO
383 );
384 }
96025800 385
7e0c769c
TO
386 /**
387 * @param array $params
388 * @return array
389 */
390 public static function filterSkipVars($params) {
391 $skipVars = self::skipVars();
392 foreach ($skipVars as $var) {
393 unset($params[$var]);
394 }
395 foreach (array_keys($params) as $key) {
396 if (preg_match('/^_qf_/', $key)) {
397 unset($params[$key]);
398 }
399 }
400 return $params;
401 }
402
4f240ac1
TO
403 /**
404 * @return array
405 */
406 private static function getUrlSettings() {
407 return array(
408 'userFrameworkResourceURL',
409 'imageUploadURL',
410 'customCSSURL',
411 'extensionsURL',
412 );
413 }
414
415 /**
416 * @return array
417 */
418 private static function getPathSettings() {
419 return array(
420 'uploadDir',
421 'imageUploadDir',
422 'customFileUploadDir',
423 'customTemplateDir',
424 'customPHPPathDir',
425 'extensionsDir',
426 );
427 }
428
6a488035 429}