<?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();
  }
}

/**
 * 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() {
  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');
  }
}