Merge pull request #3793 from yashodha/version-fix
[civicrm-core.git] / CRM / Admin / Form / Setting / UF.php
index 3f7d0a8eb141b0bb3065ab2e277ce104da6d1e5c..11705be524eb1369fe1f6613487e6dd8a17b0039 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -41,12 +41,12 @@ class CRM_Admin_Form_Setting_UF extends CRM_Admin_Form_Setting {
 
   protected $_settings = array();
 
-  protected $_uf = null;
+  protected $_uf = NULL;
 
   /**
    * Function to build the form
    *
-   * @return None
+   * @return void
    * @access public
    */
   public function buildQuickForm() {
@@ -62,16 +62,35 @@ class CRM_Admin_Form_Setting_UF extends CRM_Admin_Form_Setting {
     );
 
     $this->addElement('text', 'userFrameworkUsersTableName', ts('%1 Users Table Name', array(1 => $this->_uf)));
+    // find out if drupal has its database prefixed
+    global $databases;
+    $drupal_prefix = '';
+    if (isset($databases['default']['default']['prefix'])) {
+      if (is_array($databases['default']['default']['prefix'])) {
+        $drupal_prefix = $databases['default']['default']['prefix']['default'];
+      }
+      else {
+        $drupal_prefix = $databases['default']['default']['prefix'];
+      }
+    }
+
     if (
       function_exists('module_exists') &&
       module_exists('views') &&
-      $config->dsn != $config->userFrameworkDSN
+      (
+        $config->dsn != $config->userFrameworkDSN || !empty($drupal_prefix)
+      )
     ) {
-      $dsnArray      = DB::parseDSN($config->dsn);
-      $tableNames    = CRM_Core_DAO::GetStorageValues(NULL, 0, 'Name');
+      $dsnArray = DB::parseDSN($config->dsn);
+      $tableNames = CRM_Core_DAO::GetStorageValues(NULL, 0, 'Name');
       $tablePrefixes = '$databases[\'default\'][\'default\'][\'prefix\']= array(';
+      $tablePrefixes .= "\n  'default' => '$drupal_prefix',"; // add default prefix: the drupal database prefix
+      $prefix = "";
+      if ($config->dsn != $config->userFrameworkDSN) {
+        $prefix = "`{$dsnArray['database']}`.";
+      }
       foreach ($tableNames as $tableName => $value) {
-        $tablePrefixes .= "\n  '" . str_pad($tableName . "'", 41) . " => '`{$dsnArray['database']}`.',";
+        $tablePrefixes .= "\n  '" . str_pad($tableName . "'", 41) . " => '{$prefix}',";
       }
       $tablePrefixes .= "\n);";
       $this->assign('tablePrefixes', $tablePrefixes);