<?php
// $Id: i18nstrings.install,v 1.2.2.11 2009/06/12 11:33:16 jareyero Exp $

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

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', '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'));
}

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', 'status');
  // Drop custom index.
  db_drop_index($ret, 'locales_source', 'textgroup_location');
}

/**
 * Implementation of hook_schema().
 */
function i18nstrings_schema() {
  $schema['i18n_strings'] = array(
    'description' => t('Metadata for source strings.'),
    'fields' => array(
      'lid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => t('Source string ID. References {locales_source}.lid.'),
      ),
      'objectid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => t('Object ID.'),
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => t('Object type for this string.'),
      ),
      'property' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'default',
        'description' => t('Object property for this string.'),
      ),
    ),
    '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']['status'] = array(
    'description' => t('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();
}
*/

/**
 * 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));
  } else {
    variable_del('i18nstrings_update_skip');
  }
  return $ret;
}

/**
 * Add index to {locales_source}.
 */
function i18nstrings_update_6002() {
  $ret = array();
  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();
  db_add_field($ret, 'locales_target', 'status', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  return $ret;
}

