Merge pull request #4627 from colemanw/docblocks
[civicrm-core.git] / CRM / Core / BAO / ConfigSetting.php
index 0e0df8f304bc78911757aeedcc6d8b5ba101d5ec..279a2fb8607eef750adce83154c2b5802d763027 100644 (file)
 class CRM_Core_BAO_ConfigSetting {
 
   /**
-   * Function to create civicrm settings. This is the same as add but it clears the cache and
+   * create civicrm settings. This is the same as add but it clears the cache and
    * reloads the config object
    *
-   * @params array $params associated array of civicrm variables
+   * @param array $params associated array of civicrm variables
    *
    * @return null
    * @static
@@ -58,9 +58,9 @@ class CRM_Core_BAO_ConfigSetting {
   }
 
   /**
-   * Function to add civicrm settings
+   * add civicrm settings
    *
-   * @params array $params associated array of civicrm variables
+   * @param array $params associated array of civicrm variables
    *
    * @return null
    * @static
@@ -75,8 +75,8 @@ class CRM_Core_BAO_ConfigSetting {
       str_replace(
         'CIVI_BASE_TEMPLATE',
         '',
-      $params['civiRelativeURL']
-    );
+        $params['civiRelativeURL']
+      );
 
     // also add the version number for use by template / js etc
     $params['civiVersion'] = CRM_Utils_System::version();
@@ -139,9 +139,9 @@ class CRM_Core_BAO_ConfigSetting {
   }
 
   /**
-   * Function to fix civicrm setting variables
+   * fix civicrm setting variables
    *
-   * @params array $params associated array of civicrm variables
+   * @param array $params associated array of civicrm variables
    *
    * @return null
    * @static
@@ -198,6 +198,8 @@ class CRM_Core_BAO_ConfigSetting {
   /**
    * Function to retrieve the settings values from db
    *
+   * @param $defaults
+   *
    * @return array $defaults
    * @static
    */
@@ -309,8 +311,9 @@ class CRM_Core_BAO_ConfigSetting {
         $dbLocale = $multiLang ? "_{$defaults['lcMessages']}" : '';
         $lcMessages = CRM_Utils_System::getUFLocale();
         if ($domain->locales and !in_array($lcMessages, explode(CRM_Core_DAO::VALUE_SEPARATOR,
-              $domain->locales
-            ))) {
+            $domain->locales
+          ))
+        ) {
           $lcMessages = NULL;
         }
       }
@@ -321,7 +324,7 @@ class CRM_Core_BAO_ConfigSetting {
       }
       else {
         // if a single-lang site or the above didn't yield a result, use default
-        $lcMessages = CRM_Utils_Array::value( 'lcMessages', $defaults );
+        $lcMessages = CRM_Utils_Array::value('lcMessages', $defaults);
       }
 
       // set suffix for table names - use views if more than one language
@@ -358,6 +361,9 @@ class CRM_Core_BAO_ConfigSetting {
     }
   }
 
+  /**
+   * @return array
+   */
   static function getConfigSettings() {
     $config = CRM_Core_Config::singleton();
 
@@ -372,11 +378,23 @@ class CRM_Core_BAO_ConfigSetting {
       // lets use imageUploadDir since we dont mess around with its values
       // in the config object, lets kep it a bit generic since folks
       // might have different values etc
-      $dir = preg_replace(
-        '|civicrm/templates_c/.*$|',
-        '',
-        $config->templateCompileDir
-      );
+
+      //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
+      if (DIRECTORY_SEPARATOR == '\\') {
+        $dir = preg_replace(
+          '|civicrm[/\\\\]templates_c[/\\\\].*$|',
+          '',
+          $config->templateCompileDir
+        );
+      }
+      else {
+        $dir = preg_replace(
+          '|civicrm/templates_c/.*$|',
+          '',
+          $config->templateCompileDir
+        );
+      }
+
       $siteRoot = preg_replace(
         '|/media/civicrm/.*$|',
         '',
@@ -393,11 +411,23 @@ class CRM_Core_BAO_ConfigSetting {
       // lets use imageUploadDir since we dont mess around with its values
       // in the config object, lets kep it a bit generic since folks
       // might have different values etc
-      $dir = preg_replace(
-        '|civicrm/templates_c/.*$|',
-        '',
-        $config->templateCompileDir
-      );
+
+      //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
+      if (DIRECTORY_SEPARATOR == '\\') {
+        $dir = preg_replace(
+          '|civicrm[/\\\\]templates_c[/\\\\].*$|',
+          '',
+          $config->templateCompileDir
+        );
+      }
+      else {
+        $dir = preg_replace(
+          '|civicrm/templates_c/.*$|',
+          '',
+          $config->templateCompileDir
+        );
+      }
+
       $siteRoot = preg_replace(
         '|/wp-content/plugins/files/civicrm/.*$|',
         '',
@@ -414,18 +444,29 @@ class CRM_Core_BAO_ConfigSetting {
       // lets use imageUploadDir since we dont mess around with its values
       // in the config object, lets kep it a bit generic since folks
       // might have different values etc
-      $dir = preg_replace(
-        '|/files/civicrm/.*$|',
-        '/files/',
-        $config->imageUploadDir
-      );
+
+      //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
+      if (DIRECTORY_SEPARATOR == '\\') {
+        $dir = preg_replace(
+          '|[/\\\\]files[/\\\\]civicrm[/\\\\].*$|',
+          '\\\\files\\\\',
+          $config->imageUploadDir
+        );
+      }
+      else {
+        $dir = preg_replace(
+          '|/files/civicrm/.*$|',
+          '/files/',
+          $config->imageUploadDir
+        );
+      }
 
       $matches = array();
       if (preg_match(
-          '|/sites/([\w\.\-\_]+)/|',
-          $config->imageUploadDir,
-          $matches
-        )) {
+        '|/sites/([\w\.\-\_]+)/|',
+        $config->imageUploadDir,
+        $matches
+      )) {
         $siteName = $matches[1];
         if ($siteName) {
           $siteName = "/sites/$siteName/";
@@ -437,22 +478,30 @@ class CRM_Core_BAO_ConfigSetting {
       }
     }
 
-
     return array($url, $dir, $siteName, $siteRoot);
   }
 
-/**
- * Return likely default settings
- * @return array site settings
- *  -$url,
- * - $dir Base Directory
- * - $siteName
- * - $siteRoot
- */
+  /**
  * Return likely default settings
  * @return array site settings
  *  -$url,
  * - $dir Base Directory
  * - $siteName
  * - $siteRoot
  */
   static function getBestGuessSettings() {
     $config = CRM_Core_Config::singleton();
+
+    //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths
+    if (DIRECTORY_SEPARATOR == '\\') {
+      $needle = 'civicrm[/\\\\]templates_c[/\\\\].*$';
+    }
+    else {
+      $needle = 'civicrm/templates_c/.*$';
+    }
+
     $dir = preg_replace(
-      '|civicrm/templates_c/.*$|',
+      "|$needle|",
       '',
       $config->templateCompileDir
     );
@@ -461,7 +510,13 @@ class CRM_Core_BAO_ConfigSetting {
     return array($url, $dir, $siteName, $siteRoot);
   }
 
-  static function doSiteMove($defaultValues = array() ) {
+  /**
+   * @param array $defaultValues
+   *
+   * @return string
+   * @throws Exception
+   */
+  static function doSiteMove($defaultValues = array()) {
     $moveStatus = ts('Beginning site move process...') . '<br />';
     // get the current and guessed values
     list($oldURL, $oldDir, $oldSiteName, $oldSiteRoot) = self::getConfigSettings();
@@ -551,7 +606,8 @@ WHERE  option_group_id = (
   FROM   civicrm_option_group
   WHERE  name = %3 )
 ';
-          $params = array(1 => array($from, 'String'),
+          $params = array(
+            1 => array($from, 'String'),
             2 => array($to, 'String'),
             3 => array($option, 'String'),
           );
@@ -560,7 +616,9 @@ WHERE  option_group_id = (
       }
     }
 
-    $moveStatus .= ts('Directory and Resource URLs have been updated in the moved database to reflect current site location.') . '<br />';
+    $moveStatus .=
+      ts('Directory and Resource URLs have been updated in the moved database to reflect current site location.') .
+      '<br />';
 
     $config = CRM_Core_Config::singleton();
 
@@ -609,10 +667,9 @@ WHERE  option_group_id = (
       // component is already enabled
       return TRUE;
     }
-    $components = CRM_Core_Component::getComponents();
 
     // return if component does not exist
-    if (!array_key_exists($componentName, $components)) {
+    if (!array_key_exists($componentName, CRM_Core_Component::getComponents())) {
       return FALSE;
     }
 
@@ -621,6 +678,34 @@ WHERE  option_group_id = (
       CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components', NULL, array());
     $enabledComponents[] = $componentName;
 
+    self::setEnabledComponents($enabledComponents);
+
+    return TRUE;
+  }
+
+  static function disableComponent($componentName) {
+    $config = CRM_Core_Config::singleton();
+    if (!in_array($componentName, $config->enableComponents) ||
+      !array_key_exists($componentName, CRM_Core_Component::getComponents())
+    ) {
+      // post-condition satisified
+      return TRUE;
+    }
+
+    // get enabled-components from DB and add to the list
+    $enabledComponents =
+      CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components', NULL, array());
+    $enabledComponents = array_diff($enabledComponents, array($componentName));
+
+    self::setEnabledComponents($enabledComponents);
+
+    return TRUE;
+  }
+
+  public static function setEnabledComponents($enabledComponents) {
+    $config = CRM_Core_Config::singleton();
+    $components = CRM_Core_Component::getComponents();
+
     $enabledComponentIDs = array();
     foreach ($enabledComponents as $name) {
       $enabledComponentIDs[] = $components[$name]->componentID;
@@ -635,21 +720,33 @@ WHERE  option_group_id = (
 
     // update DB
     CRM_Core_BAO_Setting::setItem($enabledComponents,
-      CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,'enable_components');
-
-    return TRUE;
+      CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components');
   }
 
+  /**
+   * @return array
+   */
   static function skipVars() {
     return array(
-      'dsn', 'templateCompileDir',
+      'dsn',
+      'templateCompileDir',
       'userFrameworkDSN',
       'userFramework',
-      'userFrameworkBaseURL', 'userFrameworkClass', 'userHookClass',
-      'userPermissionClass', 'userFrameworkURLVar', 'userFrameworkVersion',
-      'newBaseURL', 'newBaseDir', 'newSiteName', 'configAndLogDir',
-      'qfKey', 'gettextResourceDir', 'cleanURL',
-      'locale_custom_strings', 'localeCustomStrings',
+      'userFrameworkBaseURL',
+      'userFrameworkClass',
+      'userHookClass',
+      'userPermissionClass',
+      'userFrameworkURLVar',
+      'userFrameworkVersion',
+      'newBaseURL',
+      'newBaseDir',
+      'newSiteName',
+      'configAndLogDir',
+      'qfKey',
+      'gettextResourceDir',
+      'cleanURL',
+      'locale_custom_strings',
+      'localeCustomStrings',
       'autocompleteContactSearch',
       'autocompleteContactReference',
       'checksumTimeout',