Merge pull request #18377 from seamuslee001/eway_further
[civicrm-core.git] / CRM / Core / I18n / Schema.php
index 13318083bbd0667edceda50045fa9d71e048f2a8..3a6e763d233c168afe427a5fc20006157339f482 100644 (file)
@@ -20,13 +20,11 @@ class CRM_Core_I18n_Schema {
    * Drop all views (for use by CRM_Core_DAO::dropAllTables() mostly).
    */
   public static function dropAllViews() {
-    $domain = new CRM_Core_DAO_Domain();
-    $domain->find(TRUE);
-    if (!$domain->locales) {
+    $locales = CRM_Core_I18n::getMultilingual();
+    if (!$locales) {
       return;
     }
 
-    $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
     $tables = CRM_Core_I18n_SchemaStructure::tables();
 
     foreach ($locales as $locale) {
@@ -44,56 +42,32 @@ class CRM_Core_I18n_Schema {
    *   the first locale to create (migrate to).
    */
   public static function makeMultilingual($locale) {
-    $domain = new CRM_Core_DAO_Domain();
-    $domain->find(TRUE);
-
-    // break early if the db is already multi-lang
-    if ($domain->locales) {
-      return;
-    }
-
-    $dao = new CRM_Core_DAO();
-
-    // build the column-adding SQL queries
-    $columns = CRM_Core_I18n_SchemaStructure::columns();
-    $indices = CRM_Core_I18n_SchemaStructure::indices();
-    $queries = [];
-    foreach ($columns as $table => $hash) {
-      // drop old indices
-      if (isset($indices[$table])) {
-        foreach ($indices[$table] as $index) {
-          if (CRM_Core_BAO_SchemaHandler::checkIfIndexExists($table, $index['name'])) {
-            $queries[] = "DROP INDEX {$index['name']} ON {$table}";
-          }
-        }
+    $isUpdateDone = FALSE;
+    $domain = new CRM_Core_BAO_Domain();
+    $domain->find();
+    $domains = [];
+    while ($domain->fetch()) {
+      // We need to build an array to iterate through here as something later down clears
+      // the cache on the fetch results & causes only the first to be retrieved.
+      $domains[] = clone $domain;
+    }
+    foreach ($domains as $domain) {
+      // skip if the domain is already multi-lang.
+      if ($domain->locales) {
+        continue;
       }
-      // deal with columns
-      foreach ($hash as $column => $type) {
-        $queries[] = "ALTER TABLE {$table} ADD {$column}_{$locale} {$type}";
-        if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column)) {
-          $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}";
-          $queries[] = "ALTER TABLE {$table} DROP {$column}";
-        }
+
+      if (!$isUpdateDone) {
+        $isUpdateDone = self::alterTablesToSupportMultilingual($locale);
       }
 
-      // add view
-      $queries[] = self::createViewQuery($locale, $table, $dao);
+      // update civicrm_domain.locales
+      $domain->locales = $locale;
+      $domain->save();
 
-      // add new indices
-      $queries = array_merge($queries, array_values(self::createIndexQueries($locale, $table)));
-    }
-
-    // execute the queries without i18n rewriting
-    foreach ($queries as $query) {
-      $dao->query($query, FALSE);
+      // CRM-21627 Updates the $dbLocale
+      CRM_Core_BAO_ConfigSetting::applyLocale(Civi::settings($domain->id), $domain->locales);
     }
-
-    // update civicrm_domain.locales
-    $domain->locales = $locale;
-    $domain->save();
-
-    // CRM-21627 Updates the $dbLocale
-    CRM_Core_BAO_ConfigSetting::applyLocale(Civi::settings($domain->id), $domain->locales);
   }
 
   /**
@@ -105,6 +79,7 @@ class CRM_Core_I18n_Schema {
    */
   public static function makeSinglelingual($retain) {
     $domain = new CRM_Core_DAO_Domain();
+    $domain->id = CRM_Core_Config::domainID();
     $domain->find(TRUE);
     $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
 
@@ -154,6 +129,7 @@ class CRM_Core_I18n_Schema {
     $triggers = []
   ) {
     $domain = new CRM_Core_DAO_Domain();
+    $domain->id = CRM_Core_Config::domainID();
     $domain->find(TRUE);
     $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
 
@@ -231,6 +207,7 @@ class CRM_Core_I18n_Schema {
   public static function addLocale($locale, $source) {
     // get the current supported locales
     $domain = new CRM_Core_DAO_Domain();
+    $domain->id = CRM_Core_Config::domainID();
     $domain->find(TRUE);
     $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
 
@@ -509,13 +486,11 @@ class CRM_Core_I18n_Schema {
    */
   public static function triggerInfo(&$info, $tableName = NULL) {
     // get the current supported locales
-    $domain = new CRM_Core_DAO_Domain();
-    $domain->find(TRUE);
-    if (empty($domain->locales)) {
+    $locales = CRM_Core_I18n::getMultilingual();
+    if (!$locales) {
       return;
     }
 
-    $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
     $locale = array_pop($locales);
 
     // CRM-10027
@@ -606,4 +581,53 @@ class CRM_Core_I18n_Schema {
     }
   }
 
+  /**
+   * Alter tables to the structure to support multilingual.
+   *
+   * This alters the db structure to use language specific field names for
+   * localised fields and adds the relevant views.
+   *
+   * @param string $locale
+   *
+   * @return bool
+   */
+  protected static function alterTablesToSupportMultilingual($locale): bool {
+    $dao = new CRM_Core_DAO();
+
+    // build the column-adding SQL queries
+    $columns = CRM_Core_I18n_SchemaStructure::columns();
+    $indices = CRM_Core_I18n_SchemaStructure::indices();
+    $queries = [];
+    foreach ($columns as $table => $hash) {
+      // drop old indices
+      if (isset($indices[$table])) {
+        foreach ($indices[$table] as $index) {
+          if (CRM_Core_BAO_SchemaHandler::checkIfIndexExists($table, $index['name'])) {
+            $queries[] = "DROP INDEX {$index['name']} ON {$table}";
+          }
+        }
+      }
+      // deal with columns
+      foreach ($hash as $column => $type) {
+        $queries[] = "ALTER TABLE {$table} ADD {$column}_{$locale} {$type}";
+        if (CRM_Core_BAO_SchemaHandler::checkIfFieldExists($table, $column)) {
+          $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}";
+          $queries[] = "ALTER TABLE {$table} DROP {$column}";
+        }
+      }
+
+      // add view
+      $queries[] = self::createViewQuery($locale, $table, $dao);
+
+      // add new indices
+      $queries = array_merge($queries, array_values(self::createIndexQueries($locale, $table)));
+    }
+
+    // execute the queries without i18n rewriting
+    foreach ($queries as $query) {
+      $dao->query($query, FALSE);
+    }
+    return TRUE;
+  }
+
 }