<?php

/*
 * $Id: icl_core.install,v 1.6 2010/08/30 09:38:05 jozik Exp $
 * 
 * @file Installation and uninstallation functions
 */


module_load_include ( 'module', 'icl_core' );

/**
 * The ICL Core schema definition
 *
 * @return array
 */
function icl_core_schema() {
  $schema ['icl_core_status'] = array (
      'description' => t ( 'The ICanLocalize core API statuses' ), 
      'fields' => array (
          'rid' => array (
              'description' => t ( 'The request ID' ), 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => FALSE ), 
          'module' => array (
              'description' => t ( 'The module which requested the translation' ), 
              'type' => 'varchar', 
              'length' => 255, 
              'not null' => TRUE ), 
          'origin' => array (
              'description' => t ( 'Th origin language of the requested translation' ), 
              'type' => 'varchar', 
              'length' => 12, 
              'not null' => TRUE ), 
          'target' => array (
              'description' => t ( 'The target language of the requested translation' ), 
              'type' => 'varchar', 
              'length' => 12, 
              'not null' => TRUE ), 
          'status' => array (
              'description' => t ( 'The status of the request' ), 
              'type' => 'int', 
              'unsigned' => TRUE, 
              'not null' => TRUE, 
              'length' => 1 ), 
          'translation_service' => array(
              'description' => t ( 'indicates if translation is done locally or another translation service' ), 
              'type' => 'varchar', 
              'length' => 32, 
              'default' => 'ICL',   
              'not null' => TRUE ), 
          'service_rid' => array(
              'description' => t ( 'stores the request id from the translation service' ), 
              'type' => 'varchar', 
              'length' => 32, 
              'default' => 'ICL',   
              'not null' => TRUE ), 
          ) );
  

  $schema ['icl_languages'] = array (
      'description' => t ( 'The ICanLocalize languages and their mapping to Drupal languages' ), 
      'fields' => array (
          'code' => array (
              'description' => t ( 'Drupal language code' ), 
              'type' => 'varchar',
              'length' => 12,
              'not null' => TRUE,
              'default' => ''),
          'icl_name' => array (
              'description' => t ( 'The ICanLocalize language name' ), 
              'type' => 'varchar', 
              'length' => 255, 
              'not null' => TRUE ), 
          'icl_id' => array (
              'description' => t ( 'The ICanLocalize language id' ), 
              'type' => 'int', 
              'not null' => TRUE ), 
           ) );

    $schema ['icl_reminders'] = array (
        'description' => t ( 'The reminders from ICanLocalize' ), 
        'fields' => array (
            'id' => array (
                'description' => 'ICanLocalize id',
                'type' => 'int',
                'not null' => TRUE,
                'lenght' => 1),
            'message' => array (
                'description' => 'ICanLocalize reminder text',
                'type' => 'text',
                'not null' => TRUE,
                'size' => 'big'),
            'url' => array (
                'description' => 'url on ICanLocalize',
                'type' => 'text',
                'not null' => TRUE,
                'size' => 'big'),
            'can_delete' => array (
                'description' => 'indicate if the reminder can be deleted',
                'type' => 'int',
                'not null' => TRUE,
                'lenght' => 1),
            'show' => array (
                'description' => 'indicate if the reminder should be shown',
                'type' => 'int',
                'not null' => TRUE,
                'lenght' => 1),
                
            ) );
    
  return $schema;
}

/**
 * Implementation of hook_install()
 * @see http://api.drupal.org/api/function/hook_install/6
 *
 */
function icl_core_install() {
  drupal_install_schema ( 'icl_core' );
  
  // set all the languages defined.
  
  icl_core_initialize_languages();
  
  
}


/**
 * Implementation of hook_uninstall()
 * @see http://api.drupal.org/api/function/hook_uninstall/6
 *
 */
function icl_core_uninstall() {
  variable_del ( 'icl_core_languages' );
  variable_del ( 'icl_core_website_id' );
  variable_del ( 'icl_core_accesskey' );
  variable_del ( 'icl_core_receive_options' );
  drupal_uninstall_schema ( 'icl_core' );
}

function icl_core_update_6002() {
    $ret = array();
    
    db_change_field($ret, 'icl_core_status', 'origin', 'origin', array (
              'description' => t ( 'Th origin language of the requested translation' ), 
              'type' => 'varchar', 
              'length' => 12, 
              'not null' => TRUE ));
    
    db_change_field($ret, 'icl_core_status', 'target', 'target', array (
              'description' => t ( 'The target language of the requested translation' ), 
              'type' => 'varchar', 
              'length' => 12, 
              'not null' => TRUE ));
    
    return $ret;
}


function icl_core_update_6003() {
  $ret = array ();
  
  $schema = icl_core_schema();
  
  db_create_table ( $ret, 'icl_languages', $schema['icl_languages'] );

  // set all the languages defined.
  
  icl_core_initialize_languages();
  

  return $ret;
}

function icl_core_update_6004() {
  $ret = array();
  
  $schema = icl_core_schema();
  
  db_create_table ( $ret, 'icl_reminders', $schema['icl_reminders'] );

  return $ret;    
}


function icl_core_update_6100() {
  // This is specifically to handle migrating from the icanlocalize module to
  // the translation management module.

  $ret = array();
  
  if (!db_column_exists('icl_core_status', 'service_rid')) {
    db_add_field ($ret, 'icl_core_status', 'service_rid', array(
                'description' => 'stores the request id from the translation service', 
                'type' => 'varchar', 
                'length' => 32, 
                'default' => '',
                'not null' => TRUE));

    db_query('UPDATE {icl_core_status} SET service_rid = rid WHERE 1');    
  }

  if (!db_column_exists('icl_core_status', 'translation_service')) {
    db_add_field ($ret, 'icl_core_status', 'translation_service', array(
              'description' => 'indicates if translation is done locally or another translation service', 
              'type' => 'varchar', 
              'length' => 32, 
              'default' => 'icanlocalize',
              'not null' => TRUE));
  }
  if (db_column_exists('icl_core_status', 'review_required')) {
    db_drop_field($ret, 'icl_core_status', 'review_required');
  }
  
  // Delete any rids that are duplicates. This only occurred on early
  // icanlocalize module versions where the same rid was used for several
  // languages. We no long support this.
  
  $query = db_query("SELECT rid FROM {icl_core_status} GROUP BY rid HAVING ( count(rid) > 1 )");
  while ($result = db_fetch_object($query)) {
    db_query("DELETE FROM {icl_core_status} WHERE rid = %d", $result->rid);
  }
  
  return $ret;
}

function icl_core_update_6101() {
  $ret = array();

  // Delete any rids from the icl_content_status table that are not in the icl_core_status table.
  
  $query = db_query("SELECT rid FROM {icl_content_status} ");
  while ($result = db_fetch_object($query)) {

    // see if it's in the icl_core_status table.
    if (db_result(db_query("SELECT rid FROM {icl_core_status} WHERE rid=%d", $result->rid)) != $result->rid) {
      
      // not found so delete it.
      $ret[] = update_sql("DELETE FROM {icl_content_status} WHERE rid =" . $result->rid);
    }
    
  }
  return $ret;
}
