<?php

/**
 * @file
 * Installation file for Internationalization (i18n) module.
 */

/**
 * Implements hook_install().
 */
function i18n_variable_install() {
  // Set module weight for it to run before most modules, but after variable_realm
  db_query("UPDATE {system} SET weight = -900 WHERE name = 'i18n_variable' AND type = 'module'");
  // Update from d6, module changed name
  if (variable_get('i18n_drupal6_update')) {
    i18n_variable_update_7000();
    i18n_variable_update_7001();
    i18n_variable_update_7002();
  }
  // Set some minimum variables to be translated.
  variable_set('variable_realm_list_language', array('site_name', 'site_slogan'));
}

/**
 * Implements hook_install().
 */
function i18n_variable_uninstall() {
  if (drupal_load('module', 'variable_store')) {
    // Delete all our variables from store.
    variable_store_delete_all('language', NULL);
  }
}

/**
 * Update multilingual variables variable name
 */
function i18n_variable_update_7000() {
  variable_set('i18n_variable_list', variable_get('i18n_variables', array()));
  variable_set('i18n_variable_conf', variable_get('i18n_variables', array()));
  variable_del('i18n_variables');
}

/**
 * Move variables from old storage to new table
 */
function i18n_variable_update_7001() {
  drupal_load('module', 'variable_store');
  foreach (db_select('i18n_variable', 'v')->fields('v')->execute()->fetchAll() as $variable) {
    variable_store_set('language', $variable->language, $variable->name, unserialize($variable->value));
  }
}

/**
 * Drop i18n_variable table if exists
 */
function i18n_variable_update_7002() {
  if (db_table_exists('i18n_variable')) {
    db_drop_table('i18n_variable');
  }
}

/**
 * Update list of realm variables.
 */
function i18n_variable_update_7003() {
  drupal_load('module', 'variable_store');
  $variable_list = variable_get('i18n_variable_conf', array());
  variable_set('variable_realm_list_language', $variable_list);
  // Delete old variables from store that are not in the list.
  $old_variables = array_diff(variable_store_list_all('language'), variable_children($variable_list));
  foreach ($old_variables as $name) {
    variable_store_delete_all('language', NULL, $name);
  }
}

/**
 * Delete obsoleted variable realm variables.
 */
function i18n_variable_update_7004() {
  variable_del('i18n_variable_conf');
  variable_del('i18n_variable_list');
}

