<?php
// $Id: i18nstrings.install,v 1.2.2.20 2010/09/20 07:51:17 jareyero Exp $

/**
 * @file
 * Installation file for i18nstrings module.
 */

/**
 * Implementation of hook_install().
 */
function i18nstrings_install() {
  // Create database tables.
  drupal_install_schema('i18nstrings');

  // Set module weight for it to run after core modules.
  db_query("UPDATE {system} SET weight = 10 WHERE name = 'i18nstrings' AND type = 'module'");

  $ret = array();

  // Add a field to track whether a translation needs updating.
  db_add_field($ret, 'locales_target', 'i18n_status', array('type' => 'int', 'not null' => TRUE, 'default' => 0));

  // Add custom index to locales_source table.
  db_add_index($ret, 'locales_source', 'textgroup_location', array(array('textgroup', 30), 'location'));
}

/**
 * Implementation of hook_uninstall().
 */
function i18nstrings_uninstall() {
  $ret = array();

  // Create database tables.
  drupal_uninstall_schema('i18nstrings');
  // @TODO locale table cleanup, think about it.
  // Should we drop all strings for groups other than 'default' ?

  // Drop custom field.
  db_drop_field($ret, 'locales_target', 'i18n_status');
  // Drop custom index.
  db_drop_index($ret, 'locales_source', 'textgroup_location');
  // May be a left over variable
  variable_del('i18nstrings_update_skip');
}

/**
 * Implementation of hook_schema().
 */
function i18nstrings_schema() {
  $schema['i18n_strings'] = array(
    'description' => 'Metadata for source strings.',
    'fields' => array(
      'lid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Source string ID. References {locales_source}.lid.',
      ),
      'objectid' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Object ID.',
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Object type for this string.',
      ),
      'property' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Object property for this string.',
      ),
      'objectindex' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Integer value of Object ID.',
      ),
      'format' => array(
        'description' => "The input format used by this string.",
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0
      ),

    ),
    'primary key' => array('lid'),
  );
  return $schema;
}

/**
 * Implementation of hook_schema_alter().
 */
function i18nstrings_schema_alter(&$schema) {
  // Add index for textgroup and location to {locales_source}.
  $schema['locales_source']['indexes']['textgroup_location'] = array(array('textgroup', 30), 'location');
  // Add field for tracking whether translations need updating.
  $schema['locales_target']['fields']['i18n_status'] = array(
    'description' => 'A boolean indicating whether this translation needs to be updated.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  );
}

/**
 * Update from 5.x version
 */
function i18nstrings_update_5900() {
  i18nstrings_install();
  // Mark next update to be skipped
  variable_set('i18nstrings_update_skip', 1);
  return array();
}

/**
 * Add new 'oid' column to i18n_strings table.
 */
/*
function i18nstrings_update_6000() {
  $ret = array();
  db_add_field($ret, 'i18n_strings', 'oid', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  return $ret;
}
*/

/**
 * Drupal 6 update. Change field name, 'oid' was psql reserved name.
 *
 * ALTER TABLE `drupal6_i18n`.`i18n_strings` CHANGE COLUMN `oid` `objectid` INTEGER  NOT NULL DEFAULT 0;
 */
function i18nstrings_update_6001() {
  $ret = array();
  if (!variable_get('i18nstrings_update_skip', 0)) {
    db_change_field($ret, 'i18n_strings', 'oid', 'objectid', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  }
  return $ret;
}

/**
 * Add index to {locales_source}.
 */
function i18nstrings_update_6002() {
  $ret = array();
  if (!variable_get('i18nstrings_update_skip', 0)) {
    db_add_index($ret, 'locales_source', 'textgroup_location', array(array('textgroup', 30), 'location'));
  }
  return $ret;
}

/**
 * Create i18n_strings_status schema.
 * Add a field to track whether a translation needs updating.
 */
function i18nstrings_update_6003() {
  $ret = array();
  if (!variable_get('i18nstrings_update_skip', 0)) {
    db_add_field($ret, 'locales_target', 'status', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  }
  return $ret;
}

/**
 * TODO: Add new D6 columns to i18n_strings table.
 */
/*
function i18nstrings_update_6004() {
  $ret = array();

  // Remove D5 primary keys (`strid`,`locale`).
  db_drop_primary_key($ret, 'i18n_strings');

  // Add new lid field and add primary key back.
  db_add_field($ret, 'i18n_strings', 'lid', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  db_add_primary_key($ret, 'i18n_strings', array('lid'));

  db_add_field($ret, 'i18n_strings', 'type', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  db_add_field($ret, 'i18n_strings', 'property', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => 'default'));

  return $ret;
}
*/
 
/**
 * Add objectindex, format fields, change objectid to varchar
 */
function i18nstrings_update_6005() {
  $ret = array();
  if (!variable_get('i18nstrings_update_skip', 0)) {
    db_add_field($ret, 'i18n_strings', 'objectindex', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
    // Populate new field from objectid before changing objectid to varchar
    $ret[] = update_sql('UPDATE {i18n_strings} SET objectindex = objectid');
    db_change_field($ret, 'i18n_strings', 'objectid', 'objectid', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
    // Add format field
    db_add_field($ret, 'i18n_strings', 'format', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
    // Change wrong default value for property (was 'default')
    db_change_field($ret, 'i18n_strings', 'property', 'property', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  }
  return $ret;
}

/**
 * Rename status field so it doesn't clash with other l10n modules
 */
function i18nstrings_update_6006() {
  $ret = array();
  if (!variable_get('i18nstrings_update_skip', 0)) {
    db_change_field($ret, 'locales_target', 'status', 'i18n_status', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  }
  return $ret;
}