PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Core/BAO/Setting.php

https://github.com/michaelmcandrew/citylink
PHP | 251 lines | 137 code | 31 blank | 83 comment | 29 complexity | e9dd1d2921847d1a1b18bedb5bf8256b MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 2.2 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  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. |
  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 along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. /**
  27. *
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2009
  31. * $Id$
  32. *
  33. */
  34. /**
  35. * file contains functions used in civicrm configuration
  36. *
  37. */
  38. class CRM_Core_BAO_Setting
  39. {
  40. /**
  41. * Function to add civicrm settings
  42. *
  43. * @params array $params associated array of civicrm variables
  44. *
  45. * @return null
  46. * @static
  47. */
  48. static function add(&$params)
  49. {
  50. CRM_Core_BAO_Setting::fixParams($params);
  51. require_once "CRM/Core/DAO/Domain.php";
  52. $domain =& new CRM_Core_DAO_Domain();
  53. $domain->find(true);
  54. if ($domain->config_backend) {
  55. $values = unserialize($domain->config_backend);
  56. CRM_Core_BAO_Setting::formatParams($params, $values);
  57. }
  58. // unset any of the variables we read from file that should not be stored in the database
  59. // the username and certpath are stored flat with _test and _live
  60. // check CRM-1470
  61. $skipVars = array( 'dsn', 'templateCompileDir',
  62. 'userFrameworkDSN',
  63. 'userFrameworkBaseURL', 'userFrameworkClass', 'userHookClass',
  64. 'userPermissionClass', 'userFrameworkURLVar',
  65. 'qfKey', 'gettextResourceDir', 'cleanURL' );
  66. foreach ( $skipVars as $var ) {
  67. unset( $params[$var] );
  68. }
  69. $domain->config_backend = serialize($params);
  70. $domain->save();
  71. }
  72. /**
  73. * Function to fix civicrm setting variables
  74. *
  75. * @params array $params associated array of civicrm variables
  76. *
  77. * @return null
  78. * @static
  79. */
  80. static function fixParams(&$params)
  81. {
  82. // in our old civicrm.settings.php we were using ISO code for country and
  83. // province limit, now we have changed it to use ids
  84. $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode( );
  85. $specialArray = array('countryLimit', 'provinceLimit');
  86. foreach($params as $key => $value) {
  87. if ( in_array($key, $specialArray) && is_array($value) ) {
  88. foreach( $value as $k => $val ) {
  89. if ( !is_numeric($val) ) {
  90. $params[$key][$k] = array_search($val, $countryIsoCodes);
  91. }
  92. }
  93. } else if ( $key == 'defaultContactCountry' ) {
  94. if ( !is_numeric($value) ) {
  95. $params[$key] = array_search($value, $countryIsoCodes);
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * Function to format the array containing before inserting in db
  102. *
  103. * @param array $params associated array of civicrm variables(submitted)
  104. * @param array $values associated array of civicrm variables stored in db
  105. *
  106. * @return null
  107. * @static
  108. */
  109. static function formatParams(&$params, &$values)
  110. {
  111. if ( empty( $params ) ||
  112. ! is_array( $params ) ) {
  113. $params = $values;
  114. } else {
  115. foreach ($params as $key => $val) {
  116. if ( array_key_exists($key, $values)) {
  117. unset($values[$key]);
  118. }
  119. }
  120. $params = array_merge($params, $values);
  121. }
  122. }
  123. /**
  124. * Function to retrieve the settings values from db
  125. *
  126. * @return array $defaults
  127. * @static
  128. */
  129. static function retrieve(&$defaults)
  130. {
  131. require_once "CRM/Core/DAO/Domain.php";
  132. $domain =& new CRM_Core_DAO_Domain();
  133. $domain->selectAdd( );
  134. if ( CRM_Utils_Array::value( 'q', $_GET ) == 'civicrm/upgrade' ) {
  135. $domain->selectAdd( 'config_backend' );
  136. } else {
  137. $domain->selectAdd( 'config_backend, locales' );
  138. }
  139. $domain->find(true);
  140. if ($domain->config_backend) {
  141. $defaults = unserialize($domain->config_backend);
  142. // calculate month var
  143. $defaults['dateformatMonthVar'] =
  144. strstr($defaults['dateformatQfDate'], '%m') ? 'm' : (strstr($defaults['dateformatQfDate'], '%b') ? 'M' : (strstr($defaults['dateformatQfDate'], '%B') ? 'F' : null));
  145. //calculate month var for Date Time
  146. $defaults['datetimeformatMonthVar'] =
  147. strstr($defaults['dateformatQfDatetime'], '%m') ? 'm' : (strstr($defaults['dateformatQfDatetime'], '%b') ? 'M' : (strstr($defaults['dateformatQfDatetime'], '%B') ? 'F' : null));
  148. //calculate hour var for Date Time
  149. $defaults['datetimeformatHourVar'] = strstr($defaults['dateformatQfDatetime'], '%I') ?'h' : (strstr($defaults['dateformatQfDatetime'], '%l') ? 'g' : null);
  150. // set proper monetary formatting, falling back to en_US and C (CRM-2782)
  151. setlocale(LC_MONETARY, $defaults['lcMonetary'].'.utf8', $defaults['lcMonetary'], 'en_US.utf8', 'en_US', 'C');
  152. $skipVars = array( 'dsn', 'templateCompileDir',
  153. 'userFrameworkDSN',
  154. 'userFrameworkBaseURL', 'userFrameworkClass', 'userHookClass',
  155. 'userPermissionClass', 'userFrameworkURLVar',
  156. 'qfKey', 'gettextResourceDir', 'cleanURL' );
  157. foreach ( $skipVars as $skip ) {
  158. if ( array_key_exists( $skip, $defaults ) ) {
  159. unset( $defaults[$skip] );
  160. }
  161. }
  162. // since language field won't be present before upgrade.
  163. if ( CRM_Utils_Array::value( 'q', $_GET ) == 'civicrm/upgrade' ) {
  164. return;
  165. }
  166. // are we in a multi-language setup?
  167. $multiLang = $domain->locales ? true : false;
  168. // set the current language
  169. $lcMessages = null;
  170. $session =& CRM_Core_Session::singleton();
  171. // on multi-lang sites based on request and civicrm_uf_match
  172. if ($multiLang) {
  173. require_once 'CRM/Utils/Request.php';
  174. $lcMessagesRequest = CRM_Utils_Request::retrieve('lcMessages', 'String', $this);
  175. if (in_array($lcMessagesRequest, array_keys($defaults['languageLimit']))) {
  176. $lcMessages = $lcMessagesRequest;
  177. } else {
  178. $lcMessagesRequest = null;
  179. }
  180. if (!$lcMessagesRequest) {
  181. $lcMessagesSession = $session->get('lcMessages');
  182. if (in_array($lcMessagesSession, array_keys($defaults['languageLimit']))) {
  183. $lcMessages = $lcMessagesSession;
  184. } else {
  185. $lcMessagesSession = null;
  186. }
  187. }
  188. if ($lcMessagesRequest) {
  189. require_once 'CRM/Core/DAO/UFMatch.php';
  190. $ufm =& new CRM_Core_DAO_UFMatch();
  191. $ufm->contact_id = $session->get('userID');
  192. if ($ufm->find(true)) {
  193. $ufm->language = $lcMessages;
  194. $ufm->save();
  195. }
  196. $session->set('lcMessages', $lcMessages);
  197. }
  198. if (!$lcMessages and $session->get('userID')) {
  199. require_once 'CRM/Core/DAO/UFMatch.php';
  200. $ufm =& new CRM_Core_DAO_UFMatch();
  201. $ufm->contact_id = $session->get('userID');
  202. if ($ufm->find(true) and in_array($ufm->language, array_keys($defaults['languageLimit']))) {
  203. $lcMessages = $ufm->language;
  204. }
  205. $session->set('lcMessages', $lcMessages);
  206. }
  207. }
  208. // if a single-lang site or the above didn't yield a result, use default
  209. if ($lcMessages === null) {
  210. $lcMessages = $defaults['lcMessages'];
  211. }
  212. // set suffix for table names - use views if more than one language
  213. global $dbLocale;
  214. $dbLocale = $multiLang ? "_{$lcMessages}" : '';
  215. // FIXME: an ugly hack to fix CRM-4041
  216. global $tsLocale;
  217. $tsLocale = $lcMessages;
  218. }
  219. }
  220. }