Merge pull request #11304 from JMAConsulting/CRM-21461
[civicrm-core.git] / CRM / Core / DAO / AllCoreTables.php
index 2fced2a763d293c401be265e3cf876250bd8063b..f70d2a5bedf091e67a22e2c05fba705d3af063f2 100644 (file)
@@ -4,7 +4,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2016                                |
+ | Copyright CiviCRM LLC (c) 2004-2017                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -29,9 +29,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2016
- * $Id$
- *
+ * @copyright CiviCRM LLC (c) 2004-2017
  */
 class CRM_Core_DAO_AllCoreTables {
 
@@ -39,6 +37,11 @@ class CRM_Core_DAO_AllCoreTables {
   private static $daoToClass = NULL;
   private static $entityTypes = NULL;
 
+  /**
+   * Initialise.
+   *
+   * @param bool $fresh
+   */
   public static function init($fresh = FALSE) {
     static $init = FALSE;
     if ($init && !$fresh) {
@@ -68,6 +71,12 @@ class CRM_Core_DAO_AllCoreTables {
 
   /**
    * (Quasi-Private) Do not call externally (except for unit-testing)
+   *
+   * @param string $daoName
+   * @param string $className
+   * @param string $tableName
+   * @param string $fields_callback
+   * @param string $links_callback
    */
   public static function registerEntityType($daoName, $className, $tableName, $fields_callback = NULL, $links_callback = NULL) {
     self::$daoToClass[$daoName] = $className;
@@ -99,6 +108,70 @@ class CRM_Core_DAO_AllCoreTables {
     return self::$tables;
   }
 
+  /**
+   * @return array
+   *   List of indices.
+   */
+  public static function indices($localize = TRUE) {
+    $indices = array();
+    self::init();
+    foreach (self::$daoToClass as $class) {
+      if (is_callable(array($class, 'indices'))) {
+        $indices[$class::getTableName()] = $class::indices($localize);
+      }
+    }
+    return $indices;
+  }
+
+  /**
+   * Modify indices to account for localization options.
+   *
+   * @param CRM_Core_DAO $class DAO class
+   * @param array $originalIndices index definitions before localization
+   *
+   * @return array
+   *   index definitions after localization
+   */
+  public static function multilingualize($class, $originalIndices) {
+    $domain = new CRM_Core_DAO_Domain();
+    $domain->find(TRUE);
+    $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
+    if (CRM_Utils_System::isNull($locales)) {
+      return $originalIndices;
+    }
+    $classFields = $class::fields();
+
+    $finalIndices = array();
+    foreach ($originalIndices as $index) {
+      if ($index['localizable']) {
+        foreach ($locales as $locale) {
+          $localIndex = $index;
+          $localIndex['name'] .= "_" . $locale;
+          $fields = array();
+          foreach ($localIndex['field'] as $field) {
+            $baseField = explode('(', $field);
+            if ($classFields[$baseField[0]]['localizable']) {
+              // field name may have eg (3) at end for prefix length
+              // last_name => last_name_fr_FR
+              // last_name(3) => last_name_fr_FR(3)
+              $fields[] = preg_replace('/^([^(]+)(\(\d+\)|)$/', '${1}_' . $locale . '${2}', $field);
+            }
+            else {
+              $fields[] = $field;
+            }
+          }
+          $localIndex['field'] = $fields;
+          $finalIndices[$localIndex['name']] = $localIndex;
+        }
+      }
+      else {
+        $finalIndices[$index['name']] = $index;
+      }
+    }
+    CRM_Core_BAO_SchemaHandler::addIndexSignature(self::getTableForClass($class), $finalIndices);
+    return $finalIndices;
+  }
+
   /**
    * @return array
    *   Mapping from brief-names to class-names.
@@ -128,11 +201,20 @@ class CRM_Core_DAO_AllCoreTables {
     return FALSE !== array_search($tableName, self::tables());
   }
 
+  /**
+   * Get the DAO for the class.
+   *
+   * @param string $className
+   *
+   * @return string
+   */
   public static function getCanonicalClassName($className) {
     return str_replace('_BAO_', '_DAO_', $className);
   }
 
   /**
+   * Get a list of all DAO classes.
+   *
    * @return array
    *   List of class names.
    */
@@ -140,7 +222,18 @@ class CRM_Core_DAO_AllCoreTables {
     return array_values(self::daoToClass());
   }
 
+  /**
+   * Get the classname for the table.
+   *
+   * @param string $tableName
+   * @return string
+   */
   public static function getClassForTable($tableName) {
+    //CRM-19677: on multilingual setup, trim locale from $tableName to fetch class name
+    if (CRM_Core_I18n::isMultilingual()) {
+      global $dbLocale;
+      $tableName = str_replace($dbLocale, '', $tableName);
+    }
     return CRM_Utils_Array::value($tableName, self::tables());
   }
 
@@ -177,6 +270,11 @@ class CRM_Core_DAO_AllCoreTables {
       self::tables());
   }
 
+  /**
+   * Reinitialise cache.
+   *
+   * @param bool $fresh
+   */
   public static function reinitializeCache($fresh = FALSE) {
     self::init($fresh);
   }
@@ -201,11 +299,12 @@ class CRM_Core_DAO_AllCoreTables {
       $exports = array();
       $fields = $dao::fields();
 
-      foreach($fields as $name => $field) {
+      foreach ($fields as $name => $field) {
         if (CRM_Utils_Array::value('export', $field)) {
           if ($prefix) {
             $exports[$labelName] = & $fields[$name];
-          } else {
+          }
+          else {
             $exports[$name] = & $fields[$name];
           }
         }
@@ -240,11 +339,12 @@ class CRM_Core_DAO_AllCoreTables {
       $imports = array();
       $fields = $dao::fields();
 
-      foreach($fields as $name => $field) {
+      foreach ($fields as $name => $field) {
         if (CRM_Utils_Array::value('import', $field)) {
           if ($prefix) {
             $imports[$labelName] = & $fields[$name];
-          } else {
+          }
+          else {
             $imports[$name] = & $fields[$name];
           }
         }